blob: f23cf9866a95991f281dc87d41a76c14c44e2aaa [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 Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * 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 Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar51485f02005-06-04 21:55:20 +000053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000054 * Use this to adjust the score after finding suggestions, based on the
55 * suggested word sounding like the bad word. This is much faster than doing
56 * it for every possible suggestion.
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
58 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000059 * Used when 'spellsuggest' is set to "best".
60 */
61#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
62
63/*
64 * The double scoring mechanism is based on the principle that there are two
65 * kinds of spelling mistakes:
66 * 1. You know how to spell the word, but mistype something. This results in
67 * a small editing distance (character swapped/omitted/inserted) and
68 * possibly a word that sounds completely different.
69 * 2. You don't know how to spell the word and type something that sounds
70 * right. The edit distance can be big but the word is similar after
71 * sound-folding.
72 * Since scores for these two mistakes will be very different we use a list
73 * for each.
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
75 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000076 */
77
78/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000079 * Vim spell file format: <HEADER>
80 * <SUGGEST>
81 * <LWORDTREE>
82 * <KWORDTREE>
83 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * <HEADER>: <fileID>
86 * <regioncnt> <regionname> ...
87 * <charflagslen> <charflags>
88 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000089 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000090 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +000091 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000092 * <fileID> 10 bytes "VIMspell08"
Bram Moolenaar51485f02005-06-04 21:55:20 +000093 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000095 * First <regionname> is region 1.
96 *
97 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
98 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000099 * 0x01 word character CF_WORD
100 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 * <fcharslen> 2 bytes Number of bytes in <fchars>.
102 * <fchars> N bytes Folded characters, first one is for character 128.
103 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000104 * <midwordlen> 2 bytes Number of bytes in <midword>.
105 * <midword> N bytes Characters that are word characters only when used
106 * in the middle of a word.
107 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000108 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
109 *
110 * <prefcond> : <condlen> <condstr>
111 *
112 * <condlen> 1 byte Length of <condstr>.
113 *
114 * <condstr> N bytes Condition for the prefix.
115 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000116 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000117 * <SUGGEST> : <repcount> <rep> ...
118 * <salflags> <salcount> <sal> ...
119 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000120 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 * <repcount> 2 bytes number of <rep> items, MSB first.
122 *
123 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
124 *
125 * <repfromlen> 1 byte length of <repfrom>
126 *
127 * <repfrom> N bytes "from" part of replacement
128 *
129 * <reptolen> 1 byte length of <repto>
130 *
131 * <repto> N bytes "to" part of replacement
132 *
133 * <salflags> 1 byte flags for soundsalike conversion:
134 * SAL_F0LLOWUP
135 * SAL_COLLAPSE
136 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000137 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
138 *
139 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000140 *
141 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
142 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000143 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000144 *
145 * <salfrom> N bytes "from" part of soundsalike
146 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000147 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000148 *
149 * <salto> N bytes "to" part of soundsalike
150 *
151 * <maplen> 2 bytes length of <mapstr>, MSB first
152 *
153 * <mapstr> N bytes String with sequences of similar characters,
154 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000155 *
156 *
157 * <LWORDTREE>: <wordtree>
158 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000159 * <KWORDTREE>: <wordtree>
160 *
161 * <PREFIXTREE>: <wordtree>
162 *
163 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000164 * <wordtree>: <nodecount> <nodedata> ...
165 *
166 * <nodecount> 4 bytes Number of nodes following. MSB first.
167 *
168 * <nodedata>: <siblingcount> <sibling> ...
169 *
170 * <siblingcount> 1 byte Number of siblings in this node. The siblings
171 * follow in sorted order.
172 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000173 * <sibling>: <byte> [ <nodeidx> <xbyte>
174 * | <flags> [<region>] [<prefixID>]
175 * | <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000176 *
177 * <byte> 1 byte Byte value of the sibling. Special cases:
178 * BY_NOFLAGS: End of word without flags and for all
179 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000180 * For PREFIXTREE <prefixID> and
181 * <prefcondnr> follow.
182 * BY_FLAGS: End of word, <flags> follow.
183 * For PREFIXTREE <prefixID> and
184 * <prefcondnr> follow for rare prefix.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000185 * BY_INDEX: Child of sibling is shared, <nodeidx>
186 * and <xbyte> follow.
187 *
188 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
189 *
190 * <xbyte> 1 byte byte value of the sibling.
191 *
192 * <flags> 1 byte bitmask of:
193 * WF_ALLCAP word must have only capitals
194 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000195 * WF_KEEPCAP keep-case word
196 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000197 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000198 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000200 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 *
202 * <region> 1 byte Bitmask for regions in which word is valid. When
203 * omitted it's valid in all regions.
204 * Lowest bit is for region 1.
205 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000206 * <prefixID> 1 byte ID of prefix that can be used with this word. For
207 * PREFIXTREE used for the required prefix ID.
208 *
209 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
210 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000211 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000213 */
214
Bram Moolenaare19defe2005-03-21 08:23:33 +0000215#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
216# include <io.h> /* for lseek(), must be before vim.h */
217#endif
218
219#include "vim.h"
220
221#if defined(FEAT_SYN_HL) || defined(PROTO)
222
223#ifdef HAVE_FCNTL_H
224# include <fcntl.h>
225#endif
226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000227#define MAXWLEN 250 /* Assume max. word len is this many bytes.
228 Some places assume a word length fits in a
229 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000230
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000231/* Type used for indexes in the word tree need to be at least 3 bytes. If int
232 * is 8 bytes we could use something smaller, but what? */
233#if SIZEOF_INT > 2
234typedef int idx_T;
235#else
236typedef long idx_T;
237#endif
238
239/* Flags used for a word. Only the lowest byte can be used, the region byte
240 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000241#define WF_REGION 0x01 /* region byte follows */
242#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
243#define WF_ALLCAP 0x04 /* word must be all capitals */
244#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000245#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000246#define WF_PFX 0x20 /* prefix ID list follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000248#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000252#define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed
253 prefix; must be above prefixID (one byte)
254 and prefcondnr (two bytes) */
255
Bram Moolenaar51485f02005-06-04 21:55:20 +0000256#define BY_NOFLAGS 0 /* end of word without flags or region */
257#define BY_FLAGS 1 /* end of word, flag byte follows */
258#define BY_INDEX 2 /* child is shared, index follows */
259#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000261/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000262 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000263 * One replacement: from "ft_from" to "ft_to". */
264typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000265{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000266 char_u *ft_from;
267 char_u *ft_to;
268} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000269
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000270/* Info from "SAL" entries in ".aff" file used in sl_sal.
271 * The info is split for quick processing by spell_soundfold().
272 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
273typedef struct salitem_S
274{
275 char_u *sm_lead; /* leading letters */
276 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000277 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000278 char_u *sm_rules; /* rules like ^, $, priority */
279 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000280#ifdef FEAT_MBYTE
281 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000282 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000283 int *sm_to_w; /* wide character copy of "sm_to" */
284#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000285} salitem_T;
286
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000287#ifdef FEAT_MBYTE
288typedef int salfirst_T;
289#else
290typedef short salfirst_T;
291#endif
292
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000293/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000294 * Structure used to store words and other info for one language, loaded from
295 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000296 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
297 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
298 *
299 * The "byts" array stores the possible bytes in each tree node, preceded by
300 * the number of possible bytes, sorted on byte value:
301 * <len> <byte1> <byte2> ...
302 * The "idxs" array stores the index of the child node corresponding to the
303 * byte in "byts".
304 * Exception: when the byte is zero, the word may end here and "idxs" holds
305 * the flags and region for the word. There may be several zeros in sequence
306 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000307 */
308typedef struct slang_S slang_T;
309struct slang_S
310{
311 slang_T *sl_next; /* next language */
312 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000313 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000314 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000315
Bram Moolenaar51485f02005-06-04 21:55:20 +0000316 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000317 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000318 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000319 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000320 char_u *sl_pbyts; /* prefix tree word bytes */
321 idx_T *sl_pidxs; /* prefix tree word indexes */
322
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000323 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000324
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000325 char_u *sl_midword; /* MIDWORD string or NULL */
326
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000327 int sl_prefixcnt; /* number of items in "sl_prefprog" */
328 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000330 garray_T sl_rep; /* list of fromto_T entries from REP lines */
331 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
332 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000333 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000334 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000335 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000336 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
337 * "sl_sal_first" maps chars, when has_mbyte
338 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000339 int sl_followup; /* SAL followup */
340 int sl_collapse; /* SAL collapse_result */
341 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000342 int sl_has_map; /* TRUE if there is a MAP line */
343#ifdef FEAT_MBYTE
344 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
345 int sl_map_array[256]; /* MAP for first 256 chars */
346#else
347 char_u sl_map_array[256]; /* MAP for first 256 chars */
348#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000349};
350
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000351/* First language that is loaded, start of the linked list of loaded
352 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000353static slang_T *first_lang = NULL;
354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000355/* Flags used in .spl file for soundsalike flags. */
356#define SAL_F0LLOWUP 1
357#define SAL_COLLAPSE 2
358#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000359#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000360
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361/*
362 * Structure used in "b_langp", filled from 'spelllang'.
363 */
364typedef struct langp_S
365{
366 slang_T *lp_slang; /* info for this language (NULL for last one) */
367 int lp_region; /* bitmask for region or REGION_ALL */
368} langp_T;
369
370#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
371
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000372#define REGION_ALL 0xff /* word valid in all regions */
373
374/* Result values. Lower number is accepted over higher one. */
375#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000376#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000377#define SP_RARE 1
378#define SP_LOCAL 2
379#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000380
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000381#define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000382#define VIMSPELLMAGICL 10
383
Bram Moolenaar7887d882005-07-01 22:33:52 +0000384/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000385static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000387/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000388 * Information used when looking for suggestions.
389 */
390typedef struct suginfo_S
391{
392 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000393 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000394 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000395 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000396 char_u *su_badptr; /* start of bad word in line */
397 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000398 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000399 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
400 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
401 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000402} suginfo_T;
403
404/* One word suggestion. Used in "si_ga". */
405typedef struct suggest_S
406{
407 char_u *st_word; /* suggested word, allocated string */
408 int st_orglen; /* length of replaced text */
409 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000410 int st_altscore; /* used when st_score compares equal */
411 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000412 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000413} suggest_T;
414
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000415#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000416
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000417/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
418 * called the score may change, thus we need to keep more than what is
419 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000420#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000421
422/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
423 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000424#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000425
426/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000427#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000428#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000429#define SCORE_REGION 70 /* word is for different region */
430#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000431#define SCORE_SWAP 90 /* swap two characters */
432#define SCORE_SWAP3 110 /* swap two characters in three */
433#define SCORE_REP 87 /* REP replacement */
434#define SCORE_SUBST 93 /* substitute a character */
435#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000436#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000437#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000438#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000439#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000440#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000441
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000442#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000443#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
444 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000445
446#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000447#define SCORE_MAXMAX 999999 /* accept any score */
448
449/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000450 * Structure to store info for word matching.
451 */
452typedef struct matchinf_S
453{
454 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000455
456 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000457 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000458 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000459 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000460 char_u *mi_cend; /* char after what was used for
461 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000462
463 /* case-folded text */
464 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000465 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000466
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000467 /* for when checking word after a prefix */
468 int mi_prefarridx; /* index in sl_pidxs with list of
469 prefixID/condition */
470 int mi_prefcnt; /* number of entries at mi_prefarridx */
471 int mi_prefixlen; /* byte length of prefix */
472
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000473 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000474 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000475 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000476 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000477} matchinf_T;
478
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000479/*
480 * The tables used for recognizing word characters according to spelling.
481 * These are only used for the first 256 characters of 'encoding'.
482 */
483typedef struct spelltab_S
484{
485 char_u st_isw[256]; /* flags: is word char */
486 char_u st_isu[256]; /* flags: is uppercase char */
487 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000488 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000489} spelltab_T;
490
491static spelltab_T spelltab;
492static int did_set_spelltab;
493
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000494#define CF_WORD 0x01
495#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000496
497static void clear_spell_chartab __ARGS((spelltab_T *sp));
498static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000499static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
500static int spell_iswordp_nmw __ARGS((char_u *p));
501#ifdef FEAT_MBYTE
502static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
503#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000504static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000505
506/*
Bram Moolenaarea408852005-06-25 22:49:46 +0000507 * Return TRUE if "p" points to a word character. Like spell_iswordp() but
508 * without the special handling of a single quote.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000509 * Checking for a word character is done very often, avoid the function call
510 * overhead.
511 */
512#ifdef FEAT_MBYTE
513# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
514 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
515#else
516# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
517#endif
518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000519/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000520 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000521 */
522typedef enum
523{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524 STATE_START = 0, /* At start of node check for NUL bytes (goodword
525 * ends); if badword ends there is a match, otherwise
526 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000527 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000528 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000529 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
530 STATE_PLAIN, /* Use each byte of the node. */
531 STATE_DEL, /* Delete a byte from the bad word. */
532 STATE_INS, /* Insert a byte in the bad word. */
533 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000534 STATE_UNSWAP, /* Undo swap two characters. */
535 STATE_SWAP3, /* Swap two characters over three. */
536 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000537 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000538 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000539 STATE_REP_INI, /* Prepare for using REP items. */
540 STATE_REP, /* Use matching REP items from the .aff file. */
541 STATE_REP_UNDO, /* Undo a REP item replacement. */
542 STATE_FINAL /* End of this node. */
543} state_T;
544
545/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000546 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000547 */
548typedef struct trystate_S
549{
Bram Moolenaarea424162005-06-16 21:51:00 +0000550 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000551 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000552 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000553 short ts_curi; /* index in list of child nodes */
554 char_u ts_fidx; /* index in fword[], case-folded bad word */
555 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
556 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000557 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
558 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000559#ifdef FEAT_MBYTE
560 char_u ts_tcharlen; /* number of bytes in tword character */
561 char_u ts_tcharidx; /* current byte index in tword character */
562 char_u ts_isdiff; /* DIFF_ values */
563 char_u ts_fcharstart; /* index in fword where badword char started */
564#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000565 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000566 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000567 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000568} trystate_T;
569
Bram Moolenaarea424162005-06-16 21:51:00 +0000570/* values for ts_isdiff */
571#define DIFF_NONE 0 /* no different byte (yet) */
572#define DIFF_YES 1 /* different byte found */
573#define DIFF_INSERT 2 /* inserting character */
574
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000575/* special values ts_prefixdepth */
576#define PREFIXTREE 0xfe /* walking through the prefix tree */
577#define NOPREFIX 0xff /* not using prefixes */
578
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000579/* mode values for find_word */
580#define FIND_FOLDWORD 0 /* find word case-folded */
581#define FIND_KEEPWORD 1 /* find keep-case word */
582#define FIND_PREFIX 2 /* find word after prefix */
583
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000584static slang_T *slang_alloc __ARGS((char_u *lang));
585static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000586static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000587static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000588static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000589static void find_prefix __ARGS((matchinf_T *mip));
590static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000591static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000592static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000593static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000594static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000595static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000596static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000597static 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 +0000598static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000599static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
600static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000601#ifdef FEAT_MBYTE
602static int *mb_str2wide __ARGS((char_u *s));
603#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000604static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000605static void clear_midword __ARGS((buf_T *buf));
606static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000607static int find_region __ARGS((char_u *rp, char_u *region));
608static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000610static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000611static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
612static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000613static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000614static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000615#ifdef FEAT_EVAL
616static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
617#endif
618static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
619static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000620static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000621static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000622static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000623static void suggest_try_special __ARGS((suginfo_T *su));
624static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
626static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627static void score_comp_sal __ARGS((suginfo_T *su));
628static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000629static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000630static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000631static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000632static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000633static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000634static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000635static void add_banned __ARGS((suginfo_T *su, char_u *word));
636static int was_banned __ARGS((suginfo_T *su, char_u *word));
637static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000638static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000639static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000640static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
641static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
642static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000643#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000644static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000645#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000646static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000648static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
649static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000651/*
652 * Use our own character-case definitions, because the current locale may
653 * differ from what the .spl file uses.
654 * These must not be called with negative number!
655 */
656#ifndef FEAT_MBYTE
657/* Non-multi-byte implementation. */
658# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
659# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
660# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
661#else
662/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
663 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
664 * the "w" library function for characters above 255 if available. */
665# ifdef HAVE_TOWLOWER
666# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
667 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
668# else
669# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
670 : (c) < 256 ? spelltab.st_fold[c] : (c))
671# endif
672
673# ifdef HAVE_TOWUPPER
674# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
675 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
676# else
677# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
678 : (c) < 256 ? spelltab.st_upper[c] : (c))
679# endif
680
681# ifdef HAVE_ISWUPPER
682# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
683 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
684# else
685# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
686 : (c) < 256 ? spelltab.st_isu[c] : (c))
687# endif
688#endif
689
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000690
691static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000692static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000693
694/*
695 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000696 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000697 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
698 * or when it's OK it remains unchanged.
699 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000700 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000701 * "capcol" is used to check for a Capitalised word after the end of a
702 * sentence. If it's zero then perform the check. Return the column where to
703 * check next, or -1 when no sentence end was found. If it's NULL then don't
704 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000705 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000706 * Returns the length of the word in bytes, also when it's OK, so that the
707 * caller can skip over the word.
708 */
709 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000710spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000711 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000712 char_u *ptr;
713 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000714 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000715{
716 matchinf_T mi; /* Most things are put in "mi" so that it can
717 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000718 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000719 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000720
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000721 /* A word never starts at a space or a control character. Return quickly
722 * then, skipping over the character. */
723 if (*ptr <= ' ')
724 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000725
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000727 * 0X99FF. But when a word character follows do check spelling to find
728 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000729 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000730 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000731 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
732 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000733 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000735 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736 nrlen = mi.mi_end - ptr;
737 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000738 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000739 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000740
741 /* Try including the digits in the word. */
742 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000743 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000744 else
745 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000746
Bram Moolenaar0c405862005-06-22 22:26:26 +0000747 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000748 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000749 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000750 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000751 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000752 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000753 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000754 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000755
756 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
757 {
758 /* Check word starting with capital letter. */
759#ifdef FEAT_MBYTE
760 c = mb_ptr2char(ptr);
761#else
762 c = *ptr;
763#endif
764 if (!SPELL_ISUPPER(c))
765 {
766 *attrp = highlight_attr[HLF_SPC];
767 return (int)(mi.mi_fend - ptr);
768 }
769 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000771 if (capcol != NULL)
772 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000773
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000774 /* We always use the characters up to the next non-word character,
775 * also for bad words. */
776 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000777
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000778 /* Check caps type later. */
779 mi.mi_capflags = 0;
780 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000781 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000782
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 /* Include one non-word character so that we can check for the
784 * word end. */
785 if (*mi.mi_fend != NUL)
786 mb_ptr_adv(mi.mi_fend);
787
788 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
789 MAXWLEN + 1);
790 mi.mi_fwordlen = STRLEN(mi.mi_fword);
791
792 /* The word is bad unless we recognize it. */
793 mi.mi_result = SP_BAD;
794
795 /*
796 * Loop over the languages specified in 'spelllang'.
797 * We check them all, because a matching word may be longer than an
798 * already found matching word.
799 */
800 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
801 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
802 {
803 /* Check for a matching word in case-folded words. */
804 find_word(&mi, FIND_FOLDWORD);
805
806 /* Check for a matching word in keep-case words. */
807 find_word(&mi, FIND_KEEPWORD);
808
809 /* Check for matching prefixes. */
810 find_prefix(&mi);
811 }
812
813 if (mi.mi_result != SP_OK)
814 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000815 /* If we found a number skip over it. Allows for "42nd". Do flag
816 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000817 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000818 {
819 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
820 return nrlen;
821 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000822
823 /* When we are at a non-word character there is no error, just
824 * skip over the character (try looking for a word after it). */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000825 else if (!SPELL_ISWORDP(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000826 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000827 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
828 {
829 regmatch_T regmatch;
830
831 /* Check for end of sentence. */
832 regmatch.regprog = wp->w_buffer->b_cap_prog;
833 regmatch.rm_ic = FALSE;
834 if (vim_regexec(&regmatch, ptr, 0))
835 *capcol = (int)(regmatch.endp[0] - ptr);
836 }
837
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000838#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000839 if (has_mbyte)
840 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000841#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000842 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000843 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000844
845 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
846 *attrp = highlight_attr[HLF_SPB];
847 else if (mi.mi_result == SP_RARE)
848 *attrp = highlight_attr[HLF_SPR];
849 else
850 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000851 }
852
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 return (int)(mi.mi_end - ptr);
854}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000855
Bram Moolenaar51485f02005-06-04 21:55:20 +0000856/*
857 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000858 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
859 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
860 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
861 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000862 *
863 * For a match mip->mi_result is updated.
864 */
865 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000866find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000867 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000868 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000870 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000871 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000872 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000873 int endidxcnt = 0;
874 int len;
875 int wlen = 0;
876 int flen;
877 int c;
878 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000879 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000880#ifdef FEAT_MBYTE
881 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000882 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000883#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000884 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000885 slang_T *slang = mip->mi_lp->lp_slang;
886 unsigned flags;
887 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000888 idx_T *idxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000889 int prefid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000890
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000891 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000892 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893 /* Check for word with matching case in keep-case tree. */
894 ptr = mip->mi_word;
895 flen = 9999; /* no case folding, always enough bytes */
896 byts = slang->sl_kbyts;
897 idxs = slang->sl_kidxs;
898 }
899 else
900 {
901 /* Check for case-folded in case-folded tree. */
902 ptr = mip->mi_fword;
903 flen = mip->mi_fwordlen; /* available case-folded bytes */
904 byts = slang->sl_fbyts;
905 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000906
907 if (mode == FIND_PREFIX)
908 {
909 /* Skip over the prefix. */
910 wlen = mip->mi_prefixlen;
911 flen -= mip->mi_prefixlen;
912 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000913 }
914
Bram Moolenaar51485f02005-06-04 21:55:20 +0000915 if (byts == NULL)
916 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000917
Bram Moolenaar51485f02005-06-04 21:55:20 +0000918 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000919 * Repeat advancing in the tree until:
920 * - there is a byte that doesn't match,
921 * - we reach the end of the tree,
922 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000923 */
924 for (;;)
925 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000926 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000927 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000928
929 len = byts[arridx++];
930
931 /* If the first possible byte is a zero the word could end here.
932 * Remember this index, we first check for the longest word. */
933 if (byts[arridx] == 0)
934 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000935 if (endidxcnt == MAXWLEN)
936 {
937 /* Must be a corrupted spell file. */
938 EMSG(_(e_format));
939 return;
940 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000941 endlen[endidxcnt] = wlen;
942 endidx[endidxcnt++] = arridx++;
943 --len;
944
945 /* Skip over the zeros, there can be several flag/region
946 * combinations. */
947 while (len > 0 && byts[arridx] == 0)
948 {
949 ++arridx;
950 --len;
951 }
952 if (len == 0)
953 break; /* no children, word must end here */
954 }
955
956 /* Stop looking at end of the line. */
957 if (ptr[wlen] == NUL)
958 break;
959
960 /* Perform a binary search in the list of accepted bytes. */
961 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000962 if (c == TAB) /* <Tab> is handled like <Space> */
963 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000964 lo = arridx;
965 hi = arridx + len - 1;
966 while (lo < hi)
967 {
968 m = (lo + hi) / 2;
969 if (byts[m] > c)
970 hi = m - 1;
971 else if (byts[m] < c)
972 lo = m + 1;
973 else
974 {
975 lo = hi = m;
976 break;
977 }
978 }
979
980 /* Stop if there is no matching byte. */
981 if (hi < lo || byts[lo] != c)
982 break;
983
984 /* Continue at the child (if there is one). */
985 arridx = idxs[lo];
986 ++wlen;
987 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000988
989 /* One space in the good word may stand for several spaces in the
990 * checked word. */
991 if (c == ' ')
992 {
993 for (;;)
994 {
995 if (flen <= 0 && *mip->mi_fend != NUL)
996 flen = fold_more(mip);
997 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
998 break;
999 ++wlen;
1000 --flen;
1001 }
1002 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001003 }
1004
1005 /*
1006 * Verify that one of the possible endings is valid. Try the longest
1007 * first.
1008 */
1009 while (endidxcnt > 0)
1010 {
1011 --endidxcnt;
1012 arridx = endidx[endidxcnt];
1013 wlen = endlen[endidxcnt];
1014
1015#ifdef FEAT_MBYTE
1016 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1017 continue; /* not at first byte of character */
1018#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001019 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 continue; /* next char is a word character */
1021
1022#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001023 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024 {
1025 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001026 * when folding case. This can be slow, take a shortcut when the
1027 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001029 if (STRNCMP(ptr, p, wlen) != 0)
1030 {
1031 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1032 mb_ptr_adv(p);
1033 wlen = p - mip->mi_word;
1034 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001035 }
1036#endif
1037
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001038 /* Check flags and region. For FIND_PREFIX check the condition and
1039 * prefix ID.
1040 * Repeat this if there are more flags/region alternatives until there
1041 * is a match. */
1042 res = SP_BAD;
1043 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1044 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045 {
1046 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001047
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001048 /* For the fold-case tree check that the case of the checked word
1049 * matches with what the word in the tree requires.
1050 * For keep-case tree the case is always right. For prefixes we
1051 * don't bother to check. */
1052 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001053 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 if (mip->mi_cend != mip->mi_word + wlen)
1055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001056 /* mi_capflags was set for a different word length, need
1057 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001058 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001059 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 }
1061
Bram Moolenaar0c405862005-06-22 22:26:26 +00001062 if (mip->mi_capflags == WF_KEEPCAP
1063 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001064 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001065 }
1066
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001067 /* When mode is FIND_PREFIX the word must support the prefix:
1068 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001069 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001070 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001071 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001072 /* The prefix ID is stored two bytes above the flags. */
1073 prefid = (unsigned)flags >> 16;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001074 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001075 prefid, mip->mi_fword + mip->mi_prefixlen,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001076 slang);
1077 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001078 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001079
1080 /* Use the WF_RARE flag for a rare prefix. */
1081 if (c & WF_RAREPFX)
1082 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001083 }
1084
1085 if (flags & WF_BANNED)
1086 res = SP_BANNED;
1087 else if (flags & WF_REGION)
1088 {
1089 /* Check region. */
1090 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
1091 res = SP_OK;
1092 else
1093 res = SP_LOCAL;
1094 }
1095 else if (flags & WF_RARE)
1096 res = SP_RARE;
1097 else
1098 res = SP_OK;
1099
1100 /* Always use the longest match and the best result. */
1101 if (mip->mi_result > res)
1102 {
1103 mip->mi_result = res;
1104 mip->mi_end = mip->mi_word + wlen;
1105 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001106 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 mip->mi_end = mip->mi_word + wlen;
1108
1109 if (res == SP_OK)
1110 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001111 }
1112
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001113 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001114 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001115 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001116}
1117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001118/*
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001119 * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches
1120 * with the prefix ID "prefid" for the word "word".
1121 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001122 */
1123 static int
1124valid_word_prefix(totprefcnt, arridx, prefid, word, slang)
1125 int totprefcnt; /* nr of prefix IDs */
1126 int arridx; /* idx in sl_pidxs[] */
1127 int prefid;
1128 char_u *word;
1129 slang_T *slang;
1130{
1131 int prefcnt;
1132 int pidx;
1133 regprog_T *rp;
1134 regmatch_T regmatch;
1135
1136 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1137 {
1138 pidx = slang->sl_pidxs[arridx + prefcnt];
1139
1140 /* Check the prefix ID. */
1141 if (prefid != (pidx & 0xff))
1142 continue;
1143
1144 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001145 * stored in the two bytes above the prefix ID byte. */
1146 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001147 if (rp != NULL)
1148 {
1149 regmatch.regprog = rp;
1150 regmatch.rm_ic = FALSE;
1151 if (!vim_regexec(&regmatch, word, 0))
1152 continue;
1153 }
1154
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001155 /* It's a match! Return the WF_RAREPFX flag. */
1156 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001157 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001158 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001159}
1160
1161/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001162 * Check if the word at "mip->mi_word" has a matching prefix.
1163 * If it does, then check the following word.
1164 *
1165 * For a match mip->mi_result is updated.
1166 */
1167 static void
1168find_prefix(mip)
1169 matchinf_T *mip;
1170{
1171 idx_T arridx = 0;
1172 int len;
1173 int wlen = 0;
1174 int flen;
1175 int c;
1176 char_u *ptr;
1177 idx_T lo, hi, m;
1178 slang_T *slang = mip->mi_lp->lp_slang;
1179 char_u *byts;
1180 idx_T *idxs;
1181
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001182 byts = slang->sl_pbyts;
1183 if (byts == NULL)
1184 return; /* array is empty */
1185
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001186 /* We use the case-folded word here, since prefixes are always
1187 * case-folded. */
1188 ptr = mip->mi_fword;
1189 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001190 idxs = slang->sl_pidxs;
1191
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001192 /*
1193 * Repeat advancing in the tree until:
1194 * - there is a byte that doesn't match,
1195 * - we reach the end of the tree,
1196 * - or we reach the end of the line.
1197 */
1198 for (;;)
1199 {
1200 if (flen == 0 && *mip->mi_fend != NUL)
1201 flen = fold_more(mip);
1202
1203 len = byts[arridx++];
1204
1205 /* If the first possible byte is a zero the prefix could end here.
1206 * Check if the following word matches and supports the prefix. */
1207 if (byts[arridx] == 0)
1208 {
1209 /* There can be several prefixes with different conditions. We
1210 * try them all, since we don't know which one will give the
1211 * longest match. The word is the same each time, pass the list
1212 * of possible prefixes to find_word(). */
1213 mip->mi_prefarridx = arridx;
1214 mip->mi_prefcnt = len;
1215 while (len > 0 && byts[arridx] == 0)
1216 {
1217 ++arridx;
1218 --len;
1219 }
1220 mip->mi_prefcnt -= len;
1221
1222 /* Find the word that comes after the prefix. */
1223 mip->mi_prefixlen = wlen;
1224 find_word(mip, FIND_PREFIX);
1225
1226
1227 if (len == 0)
1228 break; /* no children, word must end here */
1229 }
1230
1231 /* Stop looking at end of the line. */
1232 if (ptr[wlen] == NUL)
1233 break;
1234
1235 /* Perform a binary search in the list of accepted bytes. */
1236 c = ptr[wlen];
1237 lo = arridx;
1238 hi = arridx + len - 1;
1239 while (lo < hi)
1240 {
1241 m = (lo + hi) / 2;
1242 if (byts[m] > c)
1243 hi = m - 1;
1244 else if (byts[m] < c)
1245 lo = m + 1;
1246 else
1247 {
1248 lo = hi = m;
1249 break;
1250 }
1251 }
1252
1253 /* Stop if there is no matching byte. */
1254 if (hi < lo || byts[lo] != c)
1255 break;
1256
1257 /* Continue at the child (if there is one). */
1258 arridx = idxs[lo];
1259 ++wlen;
1260 --flen;
1261 }
1262}
1263
1264/*
1265 * Need to fold at least one more character. Do until next non-word character
1266 * for efficiency.
1267 * Return the length of the folded chars in bytes.
1268 */
1269 static int
1270fold_more(mip)
1271 matchinf_T *mip;
1272{
1273 int flen;
1274 char_u *p;
1275
1276 p = mip->mi_fend;
1277 do
1278 {
1279 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001280 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001281
1282 /* Include the non-word character so that we can check for the
1283 * word end. */
1284 if (*mip->mi_fend != NUL)
1285 mb_ptr_adv(mip->mi_fend);
1286
1287 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1288 mip->mi_fword + mip->mi_fwordlen,
1289 MAXWLEN - mip->mi_fwordlen);
1290 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1291 mip->mi_fwordlen += flen;
1292 return flen;
1293}
1294
1295/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 * Check case flags for a word. Return TRUE if the word has the requested
1297 * case.
1298 */
1299 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001300spell_valid_case(wordflags, treeflags)
1301 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001302 int treeflags; /* flags for the word in the spell tree */
1303{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001304 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001305 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001306 && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001307}
1308
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001309/*
1310 * Return TRUE if spell checking is not enabled.
1311 */
1312 static int
1313no_spell_checking()
1314{
1315 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1316 {
1317 EMSG(_("E756: Spell checking is not enabled"));
1318 return TRUE;
1319 }
1320 return FALSE;
1321}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001322
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001323/*
1324 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001325 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001326 * Return OK if found, FAIL otherwise.
1327 */
1328 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001329spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001330 int dir; /* FORWARD or BACKWARD */
1331 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001332 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001333{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001334 linenr_T lnum;
1335 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001336 char_u *line;
1337 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001338 char_u *endp;
1339 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001340 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001341 int has_syntax = syntax_present(curbuf);
1342 int col;
1343 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001344 char_u *buf = NULL;
1345 int buflen = 0;
1346 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001347 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001348
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001349 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001350 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001352 /*
1353 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001354 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001355 *
1356 * When searching backwards, we continue in the line to find the last
1357 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001358 *
1359 * We concatenate the start of the next line, so that wrapped words work
1360 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1361 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001362 */
1363 lnum = curwin->w_cursor.lnum;
1364 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001365
1366 while (!got_int)
1367 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001368 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001369
Bram Moolenaar0c405862005-06-22 22:26:26 +00001370 len = STRLEN(line);
1371 if (buflen < len + MAXWLEN + 2)
1372 {
1373 vim_free(buf);
1374 buflen = len + MAXWLEN + 2;
1375 buf = alloc(buflen);
1376 if (buf == NULL)
1377 break;
1378 }
1379
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001380 /* In first line check first word for Capital. */
1381 if (lnum == 1)
1382 capcol = 0;
1383
1384 /* For checking first word with a capital skip white space. */
1385 if (capcol == 0)
1386 capcol = skipwhite(line) - line;
1387
Bram Moolenaar0c405862005-06-22 22:26:26 +00001388 /* Copy the line into "buf" and append the start of the next line if
1389 * possible. */
1390 STRCPY(buf, line);
1391 if (lnum < curbuf->b_ml.ml_line_count)
1392 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1393
1394 p = buf + skip;
1395 endp = buf + len;
1396 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001397 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001398 /* When searching backward don't search after the cursor. */
1399 if (dir == BACKWARD
1400 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001401 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001402 break;
1403
1404 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001405 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001406 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407
1408 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001409 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001412 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001413 /* When searching forward only accept a bad word after
1414 * the cursor. */
1415 if (dir == BACKWARD
1416 || lnum > curwin->w_cursor.lnum
1417 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001418 && (colnr_T)(curline ? p - buf + len
1419 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001420 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001421 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001422 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001423 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001424 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425 (void)syn_get_id(lnum, (colnr_T)col,
1426 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001427 }
1428 else
1429 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001430
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 if (can_spell)
1432 {
1433 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001434 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001435#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001436 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001437#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001438 if (dir == FORWARD)
1439 {
1440 /* No need to search further. */
1441 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001442 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001444 }
1445 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001446 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001447 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001448 }
1449
Bram Moolenaar51485f02005-06-04 21:55:20 +00001450 /* advance to character after the word */
1451 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001452 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001453 }
1454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001455 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001456 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001457
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001458 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001459 if (dir == BACKWARD)
1460 {
1461 if (found_pos.lnum != 0)
1462 {
1463 /* Use the last match in the line. */
1464 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001465 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001466 return OK;
1467 }
1468 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001469 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001470 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001471 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001472 }
1473 else
1474 {
1475 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001476 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001477 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001478
1479 /* Skip the characters at the start of the next line that were
1480 * included in a match crossing line boundaries. */
1481 if (attr == 0)
1482 skip = p - endp;
1483 else
1484 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001485
1486 /* Capscol skips over the inserted space. */
1487 --capcol;
1488
1489 /* But after empty line check first word in next line */
1490 if (*skipwhite(line) == NUL)
1491 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001492 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001493
1494 line_breakcheck();
1495 }
1496
Bram Moolenaar0c405862005-06-22 22:26:26 +00001497 vim_free(buf);
1498 return FAIL;
1499}
1500
1501/*
1502 * For spell checking: concatenate the start of the following line "line" into
1503 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1504 */
1505 void
1506spell_cat_line(buf, line, maxlen)
1507 char_u *buf;
1508 char_u *line;
1509 int maxlen;
1510{
1511 char_u *p;
1512 int n;
1513
1514 p = skipwhite(line);
1515 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1516 p = skipwhite(p + 1);
1517
1518 if (*p != NUL)
1519 {
1520 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001521 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001522 n = p - line;
1523 if (n >= maxlen)
1524 n = maxlen - 1;
1525 vim_memset(buf + 1, ' ', n);
1526 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001527}
1528
1529/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001530 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001531 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001532 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001533 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001534spell_load_lang(lang)
1535 char_u *lang;
1536{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001537 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001538 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001539 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001540
Bram Moolenaarb765d632005-06-07 21:00:02 +00001541 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001542 * It's truncated when an error is detected. */
1543 STRCPY(langcp, lang);
1544
Bram Moolenaarb765d632005-06-07 21:00:02 +00001545 /*
1546 * Find the first spell file for "lang" in 'runtimepath' and load it.
1547 */
1548 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1549 "spell/%s.%s.spl", lang, spell_enc());
1550 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001551
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001552 if (r == FAIL && *langcp != NUL)
1553 {
1554 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001555 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001556 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001557 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001558 }
1559
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001560 if (r == FAIL)
1561 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1562 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001563 else if (*langcp != NUL)
1564 {
1565 /* Load all the additions. */
1566 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1567 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1568 }
1569}
1570
1571/*
1572 * Return the encoding used for spell checking: Use 'encoding', except that we
1573 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1574 */
1575 static char_u *
1576spell_enc()
1577{
1578
1579#ifdef FEAT_MBYTE
1580 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1581 return p_enc;
1582#endif
1583 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001584}
1585
1586/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001587 * Get the name of the .spl file for the internal wordlist into
1588 * "fname[MAXPATHL]".
1589 */
1590 static void
1591int_wordlist_spl(fname)
1592 char_u *fname;
1593{
1594 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1595 int_wordlist, spell_enc());
1596}
1597
1598/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001599 * Allocate a new slang_T.
1600 * Caller must fill "sl_next".
1601 */
1602 static slang_T *
1603slang_alloc(lang)
1604 char_u *lang;
1605{
1606 slang_T *lp;
1607
Bram Moolenaar51485f02005-06-04 21:55:20 +00001608 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001609 if (lp != NULL)
1610 {
1611 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001612 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001613 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001614 }
1615 return lp;
1616}
1617
1618/*
1619 * Free the contents of an slang_T and the structure itself.
1620 */
1621 static void
1622slang_free(lp)
1623 slang_T *lp;
1624{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001625 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001626 vim_free(lp->sl_fname);
1627 slang_clear(lp);
1628 vim_free(lp);
1629}
1630
1631/*
1632 * Clear an slang_T so that the file can be reloaded.
1633 */
1634 static void
1635slang_clear(lp)
1636 slang_T *lp;
1637{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001638 garray_T *gap;
1639 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001640 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001641 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001642
Bram Moolenaar51485f02005-06-04 21:55:20 +00001643 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001644 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001645 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001646 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001647 vim_free(lp->sl_pbyts);
1648 lp->sl_pbyts = NULL;
1649
Bram Moolenaar51485f02005-06-04 21:55:20 +00001650 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001651 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001652 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001653 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001654 vim_free(lp->sl_pidxs);
1655 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001656
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001657 gap = &lp->sl_rep;
1658 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001659 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001660 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1661 vim_free(ftp->ft_from);
1662 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001663 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001664 ga_clear(gap);
1665
1666 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001667 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001668 {
1669 /* "ga_len" is set to 1 without adding an item for latin1 */
1670 if (gap->ga_data != NULL)
1671 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1672 for (i = 0; i < gap->ga_len; ++i)
1673 vim_free(((int **)gap->ga_data)[i]);
1674 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001675 else
1676 /* SAL items: free salitem_T items */
1677 while (gap->ga_len > 0)
1678 {
1679 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1680 vim_free(smp->sm_lead);
1681 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1682 vim_free(smp->sm_to);
1683#ifdef FEAT_MBYTE
1684 vim_free(smp->sm_lead_w);
1685 vim_free(smp->sm_oneof_w);
1686 vim_free(smp->sm_to_w);
1687#endif
1688 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001689 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001690
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001691 for (i = 0; i < lp->sl_prefixcnt; ++i)
1692 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001693 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001694 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001695 lp->sl_prefprog = NULL;
1696
1697 vim_free(lp->sl_midword);
1698 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001699
Bram Moolenaarea424162005-06-16 21:51:00 +00001700#ifdef FEAT_MBYTE
1701 {
1702 int todo = lp->sl_map_hash.ht_used;
1703 hashitem_T *hi;
1704
1705 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1706 if (!HASHITEM_EMPTY(hi))
1707 {
1708 --todo;
1709 vim_free(hi->hi_key);
1710 }
1711 }
1712 hash_clear(&lp->sl_map_hash);
1713#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001714}
1715
1716/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001717 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001718 * Invoked through do_in_runtimepath().
1719 */
1720 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001721spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001722 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001723 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001724{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001725 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001726}
1727
1728/*
1729 * Load one spell file and store the info into a slang_T.
1730 *
1731 * This is invoked in two ways:
1732 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1733 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1734 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1735 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001736 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001737 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001738 static slang_T *
1739spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001740 char_u *fname;
1741 char_u *lang;
1742 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001743 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001744{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001745 FILE *fd;
1746 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001747 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001748 char_u *bp;
1749 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001750 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001751 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001752 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001753 int round;
1754 char_u *save_sourcing_name = sourcing_name;
1755 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001756 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001757 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001758 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001759 garray_T *gap;
1760 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001761 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001762 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001763 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001764 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001765
Bram Moolenaarb765d632005-06-07 21:00:02 +00001766 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767 if (fd == NULL)
1768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001769 if (!silent)
1770 EMSG2(_(e_notopen), fname);
1771 else if (p_verbose > 2)
1772 {
1773 verbose_enter();
1774 smsg((char_u *)e_notopen, fname);
1775 verbose_leave();
1776 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001777 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001778 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001779 if (p_verbose > 2)
1780 {
1781 verbose_enter();
1782 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1783 verbose_leave();
1784 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001785
Bram Moolenaarb765d632005-06-07 21:00:02 +00001786 if (old_lp == NULL)
1787 {
1788 lp = slang_alloc(lang);
1789 if (lp == NULL)
1790 goto endFAIL;
1791
1792 /* Remember the file name, used to reload the file when it's updated. */
1793 lp->sl_fname = vim_strsave(fname);
1794 if (lp->sl_fname == NULL)
1795 goto endFAIL;
1796
1797 /* Check for .add.spl. */
1798 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1799 }
1800 else
1801 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001802
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001803 /* Set sourcing_name, so that error messages mention the file name. */
1804 sourcing_name = fname;
1805 sourcing_lnum = 0;
1806
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001807 /* <HEADER>: <fileID>
1808 * <regioncnt> <regionname> ...
1809 * <charflagslen> <charflags>
1810 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001811 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001812 * <prefcondcnt> <prefcond> ...
1813 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001814 for (i = 0; i < VIMSPELLMAGICL; ++i)
1815 buf[i] = getc(fd); /* <fileID> */
1816 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1817 {
1818 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001819 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820 }
1821
1822 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001823 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001824 {
1825truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001826 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001827 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001828 }
1829 if (cnt > 8)
1830 {
1831formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001832 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001833 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001834 }
1835 for (i = 0; i < cnt; ++i)
1836 {
1837 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1838 lp->sl_regions[i * 2 + 1] = getc(fd);
1839 }
1840 lp->sl_regions[cnt * 2] = NUL;
1841
Bram Moolenaar7887d882005-07-01 22:33:52 +00001842 /* <charflagslen> <charflags> */
1843 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001844 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001845 goto endFAIL;
1846
1847 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001848 fol = read_cnt_string(fd, 2, &ccnt);
1849 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001850 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001851 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001852 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001853 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001854
1855 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1856 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001857 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001858
1859 vim_free(p);
1860 vim_free(fol);
1861
1862 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1863 if ((p == NULL) != (fol == NULL))
1864 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001865
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001866 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001867 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001868 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001869 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001870
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001871 /* <prefcondcnt> <prefcond> ... */
1872 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1873 if (cnt > 0)
1874 {
1875 lp->sl_prefprog = (regprog_T **)alloc_clear(
1876 (unsigned)sizeof(regprog_T *) * cnt);
1877 if (lp->sl_prefprog == NULL)
1878 goto endFAIL;
1879 lp->sl_prefixcnt = cnt;
1880
1881 for (i = 0; i < cnt; ++i)
1882 {
1883 /* <prefcond> : <condlen> <condstr> */
1884 n = getc(fd); /* <condlen> */
1885 if (n < 0)
1886 goto formerr;
1887 /* When <condlen> is zero we have an empty condition. Otherwise
1888 * compile the regexp program used to check for the condition. */
1889 if (n > 0)
1890 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001891 buf[0] = '^'; /* always match at one position only */
1892 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001893 while (n-- > 0)
1894 *p++ = getc(fd); /* <condstr> */
1895 *p = NUL;
1896 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1897 }
1898 }
1899 }
1900
1901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001902 /* <SUGGEST> : <repcount> <rep> ...
1903 * <salflags> <salcount> <sal> ...
1904 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001905
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001906 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1907 if (cnt < 0)
1908 goto formerr;
1909
1910 gap = &lp->sl_rep;
1911 if (ga_grow(gap, cnt) == FAIL)
1912 goto endFAIL;
1913
1914 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1915 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001917 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001918 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001919 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001920 goto endFAIL;
1921 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001922 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001923 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001924 vim_free(ftp->ft_from);
1925 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001926 }
1927 }
1928
1929 /* Fill the first-index table. */
1930 first = lp->sl_rep_first;
1931 for (i = 0; i < 256; ++i)
1932 first[i] = -1;
1933 for (i = 0; i < gap->ga_len; ++i)
1934 {
1935 ftp = &((fromto_T *)gap->ga_data)[i];
1936 if (first[*ftp->ft_from] == -1)
1937 first[*ftp->ft_from] = i;
1938 }
1939
1940 i = getc(fd); /* <salflags> */
1941 if (i & SAL_F0LLOWUP)
1942 lp->sl_followup = TRUE;
1943 if (i & SAL_COLLAPSE)
1944 lp->sl_collapse = TRUE;
1945 if (i & SAL_REM_ACCENTS)
1946 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001947 if (i & SAL_SOFO)
1948 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001949 else
1950 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001951
1952 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
1953 if (cnt < 0)
1954 goto formerr;
1955
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001956 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001957 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001958 /*
1959 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
1960 */
1961 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001962 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001963
Bram Moolenaar7887d882005-07-01 22:33:52 +00001964 /* <salfromlen> <salfrom> */
1965 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001966 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001967 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001968
Bram Moolenaar7887d882005-07-01 22:33:52 +00001969 /* <saltolen> <salto> */
1970 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001971 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001972 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001973 vim_free(bp);
1974 goto endFAIL;
1975 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001976
Bram Moolenaar7887d882005-07-01 22:33:52 +00001977 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001978 if (bp != NULL && fol != NULL)
1979 i = set_sofo(lp, bp, fol);
1980 else if (bp != NULL || fol != NULL)
1981 i = FAIL; /* only one of two strings is an error */
1982 else
1983 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001984
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001985 vim_free(bp);
1986 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001987 if (i == FAIL)
1988 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001989 }
1990 else
1991 {
1992 /*
1993 * SAL items
1994 */
1995 gap = &lp->sl_sal;
1996 if (ga_grow(gap, cnt) == FAIL)
1997 goto endFAIL;
1998
1999 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2000 for (; gap->ga_len < cnt; ++gap->ga_len)
2001 {
2002 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2003 ccnt = getc(fd); /* <salfromlen> */
2004 if (ccnt < 0)
2005 goto formerr;
2006 if ((p = alloc(ccnt + 2)) == NULL)
2007 goto endFAIL;
2008 smp->sm_lead = p;
2009
2010 /* Read up to the first special char into sm_lead. */
2011 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002012 {
2013 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002014 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002015 break;
2016 *p++ = c;
2017 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002018 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002019 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002020
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002021 /* Put (abc) chars in sm_oneof, if any. */
2022 if (c == '(')
2023 {
2024 smp->sm_oneof = p;
2025 for (++i; i < ccnt; ++i)
2026 {
2027 c = getc(fd); /* <salfrom> */
2028 if (c == ')')
2029 break;
2030 *p++ = c;
2031 }
2032 *p++ = NUL;
2033 if (++i < ccnt)
2034 c = getc(fd);
2035 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002036 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002037 smp->sm_oneof = NULL;
2038
2039 /* Any following chars go in sm_rules. */
2040 smp->sm_rules = p;
2041 if (i < ccnt)
2042 /* store the char we got while checking for end of sm_lead */
2043 *p++ = c;
2044 for (++i; i < ccnt; ++i)
2045 *p++ = getc(fd); /* <salfrom> */
2046 *p++ = NUL;
2047
Bram Moolenaar7887d882005-07-01 22:33:52 +00002048 /* <saltolen> <salto> */
2049 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002050 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002051 {
2052 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002053 goto formerr;
2054 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002055
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002056#ifdef FEAT_MBYTE
2057 if (has_mbyte)
2058 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002059 /* convert the multi-byte strings to wide char strings */
2060 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2061 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2062 if (smp->sm_oneof == NULL)
2063 smp->sm_oneof_w = NULL;
2064 else
2065 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002066 if (smp->sm_to == NULL)
2067 smp->sm_to_w = NULL;
2068 else
2069 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002070 if (smp->sm_lead_w == NULL
2071 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002072 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002073 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002074 vim_free(smp->sm_lead);
2075 vim_free(smp->sm_to);
2076 vim_free(smp->sm_lead_w);
2077 vim_free(smp->sm_oneof_w);
2078 vim_free(smp->sm_to_w);
2079 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002080 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002081 }
2082#endif
2083 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002084
2085 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002086 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002087 }
2088
Bram Moolenaar7887d882005-07-01 22:33:52 +00002089 /* <maplen> <mapstr> */
2090 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002091 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002092 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002093 if (p != NULL)
2094 {
2095 set_map_str(lp, p);
2096 vim_free(p);
2097 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098
Bram Moolenaar51485f02005-06-04 21:55:20 +00002099 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002100 * round 2: <KWORDTREE>
2101 * round 3: <PREFIXTREE> */
2102 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002104 /* The tree size was computed when writing the file, so that we can
2105 * allocate it as one long block. <nodecount> */
2106 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2107 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002108 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002109 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002111 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002112 bp = lalloc((long_u)len, TRUE);
2113 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002114 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002115 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002116 lp->sl_fbyts = bp;
2117 else if (round == 2)
2118 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002119 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002120 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002121
2122 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002123 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2124 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002125 goto endFAIL;
2126 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002127 lp->sl_fidxs = ip;
2128 else if (round == 2)
2129 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002130 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002131 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002132
2133 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002134 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002135 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002136 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002137 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002138 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002139 }
2140 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002141
Bram Moolenaarb765d632005-06-07 21:00:02 +00002142 /* For a new file link it in the list of spell files. */
2143 if (old_lp == NULL)
2144 {
2145 lp->sl_next = first_lang;
2146 first_lang = lp;
2147 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002148
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002149 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002150
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002151endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002152 if (lang != NULL)
2153 /* truncating the name signals the error to spell_load_lang() */
2154 *lang = NUL;
2155 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002157 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002158 lp = NULL;
2159 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002160
2161endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002162 if (fd != NULL)
2163 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002164 sourcing_name = save_sourcing_name;
2165 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002166
2167 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168}
2169
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002170/*
2171 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002172 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002173 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002174 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002175 */
2176 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002177read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002178 FILE *fd;
2179 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002180 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002181{
2182 int cnt = 0;
2183 int i;
2184 char_u *str;
2185
2186 /* read the length bytes, MSB first */
2187 for (i = 0; i < cnt_bytes; ++i)
2188 cnt = (cnt << 8) + getc(fd);
2189 if (cnt < 0)
2190 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002191 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002192 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002193 return NULL;
2194 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002195 *cntp = cnt;
2196 if (cnt == 0)
2197 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002198
2199 /* allocate memory */
2200 str = alloc((unsigned)cnt + 1);
2201 if (str == NULL)
2202 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002203 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002204 return NULL;
2205 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002206
2207 /* Read the string. Doesn't check for truncated file. */
2208 for (i = 0; i < cnt; ++i)
2209 str[i] = getc(fd);
2210 str[i] = NUL;
2211
2212 return str;
2213}
2214
Bram Moolenaar7887d882005-07-01 22:33:52 +00002215/*
2216 * Set the SOFOFROM and SOFOTO items in language "lp".
2217 * Returns FAIL when there is something wrong.
2218 */
2219 static int
2220set_sofo(lp, from, to)
2221 slang_T *lp;
2222 char_u *from;
2223 char_u *to;
2224{
2225 int i;
2226
2227#ifdef FEAT_MBYTE
2228 garray_T *gap;
2229 char_u *s;
2230 char_u *p;
2231 int c;
2232 int *inp;
2233
2234 if (has_mbyte)
2235 {
2236 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2237 * characters. The index is the low byte of the character.
2238 * The list contains from-to pairs with a terminating NUL.
2239 * sl_sal_first[] is used for latin1 "from" characters. */
2240 gap = &lp->sl_sal;
2241 ga_init2(gap, sizeof(int *), 1);
2242 if (ga_grow(gap, 256) == FAIL)
2243 return FAIL;
2244 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2245 gap->ga_len = 256;
2246
2247 /* First count the number of items for each list. Temporarily use
2248 * sl_sal_first[] for this. */
2249 for (p = from, s = to; *p != NUL && *s != NUL; )
2250 {
2251 c = mb_ptr2char_adv(&p);
2252 mb_ptr_adv(s);
2253 if (c >= 256)
2254 ++lp->sl_sal_first[c & 0xff];
2255 }
2256 if (*p != NUL || *s != NUL) /* lengths differ */
2257 return FAIL;
2258
2259 /* Allocate the lists. */
2260 for (i = 0; i < 256; ++i)
2261 if (lp->sl_sal_first[i] > 0)
2262 {
2263 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2264 if (p == NULL)
2265 return FAIL;
2266 ((int **)gap->ga_data)[i] = (int *)p;
2267 *(int *)p = 0;
2268 }
2269
2270 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2271 * list. */
2272 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2273 for (p = from, s = to; *p != NUL && *s != NUL; )
2274 {
2275 c = mb_ptr2char_adv(&p);
2276 i = mb_ptr2char_adv(&s);
2277 if (c >= 256)
2278 {
2279 /* Append the from-to chars at the end of the list with
2280 * the low byte. */
2281 inp = ((int **)gap->ga_data)[c & 0xff];
2282 while (*inp != 0)
2283 ++inp;
2284 *inp++ = c; /* from char */
2285 *inp++ = i; /* to char */
2286 *inp++ = NUL; /* NUL at the end */
2287 }
2288 else
2289 /* mapping byte to char is done in sl_sal_first[] */
2290 lp->sl_sal_first[c] = i;
2291 }
2292 }
2293 else
2294#endif
2295 {
2296 /* mapping bytes to bytes is done in sl_sal_first[] */
2297 if (STRLEN(from) != STRLEN(to))
2298 return FAIL;
2299
2300 for (i = 0; to[i] != NUL; ++i)
2301 lp->sl_sal_first[from[i]] = to[i];
2302 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2303 }
2304
2305 return OK;
2306}
2307
2308/*
2309 * Fill the first-index table for "lp".
2310 */
2311 static void
2312set_sal_first(lp)
2313 slang_T *lp;
2314{
2315 salfirst_T *sfirst;
2316 int i;
2317 salitem_T *smp;
2318 int c;
2319 garray_T *gap = &lp->sl_sal;
2320
2321 sfirst = lp->sl_sal_first;
2322 for (i = 0; i < 256; ++i)
2323 sfirst[i] = -1;
2324 smp = (salitem_T *)gap->ga_data;
2325 for (i = 0; i < gap->ga_len; ++i)
2326 {
2327#ifdef FEAT_MBYTE
2328 if (has_mbyte)
2329 /* Use the lowest byte of the first character. For latin1 it's
2330 * the character, for other encodings it should differ for most
2331 * characters. */
2332 c = *smp[i].sm_lead_w & 0xff;
2333 else
2334#endif
2335 c = *smp[i].sm_lead;
2336 if (sfirst[c] == -1)
2337 {
2338 sfirst[c] = i;
2339#ifdef FEAT_MBYTE
2340 if (has_mbyte)
2341 {
2342 int n;
2343
2344 /* Make sure all entries with this byte are following each
2345 * other. Move the ones that are in the wrong position. Do
2346 * keep the same ordering! */
2347 while (i + 1 < gap->ga_len
2348 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2349 /* Skip over entry with same index byte. */
2350 ++i;
2351
2352 for (n = 1; i + n < gap->ga_len; ++n)
2353 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2354 {
2355 salitem_T tsal;
2356
2357 /* Move entry with same index byte after the entries
2358 * we already found. */
2359 ++i;
2360 --n;
2361 tsal = smp[i + n];
2362 mch_memmove(smp + i + 1, smp + i,
2363 sizeof(salitem_T) * n);
2364 smp[i] = tsal;
2365 }
2366 }
2367#endif
2368 }
2369 }
2370}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002371
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002372#ifdef FEAT_MBYTE
2373/*
2374 * Turn a multi-byte string into a wide character string.
2375 * Return it in allocated memory (NULL for out-of-memory)
2376 */
2377 static int *
2378mb_str2wide(s)
2379 char_u *s;
2380{
2381 int *res;
2382 char_u *p;
2383 int i = 0;
2384
2385 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2386 if (res != NULL)
2387 {
2388 for (p = s; *p != NUL; )
2389 res[i++] = mb_ptr2char_adv(&p);
2390 res[i] = NUL;
2391 }
2392 return res;
2393}
2394#endif
2395
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002397 * Read one row of siblings from the spell file and store it in the byte array
2398 * "byts" and index array "idxs". Recursively read the children.
2399 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002400 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002401 *
2402 * Returns the index follosing the siblings.
2403 * Returns -1 if the file is shorter than expected.
2404 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002406 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002407read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002408 FILE *fd;
2409 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002410 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002411 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002412 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002413 int prefixtree; /* TRUE for reading PREFIXTREE */
2414 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002415{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002416 int len;
2417 int i;
2418 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002419 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002420 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002421 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002422#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002423
Bram Moolenaar51485f02005-06-04 21:55:20 +00002424 len = getc(fd); /* <siblingcount> */
2425 if (len <= 0)
2426 return -1;
2427
2428 if (startidx + len >= maxidx)
2429 return -2;
2430 byts[idx++] = len;
2431
2432 /* Read the byte values, flag/region bytes and shared indexes. */
2433 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002434 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002435 c = getc(fd); /* <byte> */
2436 if (c < 0)
2437 return -1;
2438 if (c <= BY_SPECIAL)
2439 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002440 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002441 {
2442 /* No flags, all regions. */
2443 idxs[idx] = 0;
2444 c = 0;
2445 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002446 else if (c == BY_FLAGS || c == BY_NOFLAGS)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002447 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002448 if (prefixtree)
2449 {
2450 /* Read the prefix ID and the condition nr. In idxs[]
2451 * store the prefix ID in the low byte, the condition
2452 * index shifted up 8 bits. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002453 c2 = getc(fd); /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002454 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2455 if (n >= maxprefcondnr)
2456 return -2;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002457 c2 += (n << 8);
2458 if (c == BY_NOFLAGS)
2459 c = c2;
2460 else
2461 c = c2 | WF_RAREPFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002462 }
2463 else
2464 {
2465 /* Read flags and optional region and prefix ID. In
2466 * idxs[] the flags go in the low byte, region above that
2467 * and prefix ID above the region. */
2468 c = getc(fd); /* <flags> */
2469 if (c & WF_REGION)
2470 c = (getc(fd) << 8) + c; /* <region> */
2471 if (c & WF_PFX)
2472 c = (getc(fd) << 16) + c; /* <prefixID> */
2473 }
2474
Bram Moolenaar51485f02005-06-04 21:55:20 +00002475 idxs[idx] = c;
2476 c = 0;
2477 }
2478 else /* c == BY_INDEX */
2479 {
2480 /* <nodeidx> */
2481 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2482 if (n < 0 || n >= maxidx)
2483 return -2;
2484 idxs[idx] = n + SHARED_MASK;
2485 c = getc(fd); /* <xbyte> */
2486 }
2487 }
2488 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002489 }
2490
Bram Moolenaar51485f02005-06-04 21:55:20 +00002491 /* Recursively read the children for non-shared siblings.
2492 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2493 * remove SHARED_MASK) */
2494 for (i = 1; i <= len; ++i)
2495 if (byts[startidx + i] != 0)
2496 {
2497 if (idxs[startidx + i] & SHARED_MASK)
2498 idxs[startidx + i] &= ~SHARED_MASK;
2499 else
2500 {
2501 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002502 idx = read_tree(fd, byts, idxs, maxidx, idx,
2503 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002504 if (idx < 0)
2505 break;
2506 }
2507 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002508
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510}
2511
2512/*
2513 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002514 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515 */
2516 char_u *
2517did_set_spelllang(buf)
2518 buf_T *buf;
2519{
2520 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002521 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002522 char_u *region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002523 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002524 int region_mask;
2525 slang_T *lp;
2526 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002527 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002529 int len;
2530 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002531 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002532 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002533 char_u *use_region = NULL;
2534 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002535
2536 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002537 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002538
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002539 /* loop over comma separated language names. */
2540 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002542 /* Get one language name. */
2543 copy_option_part(&splp, lang, MAXWLEN, ",");
2544
Bram Moolenaar5482f332005-04-17 20:18:43 +00002545 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002546 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002548 /* If the name ends in ".spl" use it as the name of the spell file.
2549 * If there is a region name let "region" point to it and remove it
2550 * from the name. */
2551 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2552 {
2553 filename = TRUE;
2554
2555 /* Check if we loaded this language before. */
2556 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2557 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2558 break;
2559 }
2560 else
2561 {
2562 filename = FALSE;
2563 if (len > 3 && lang[len - 3] == '_')
2564 {
2565 region = lang + len - 2;
2566 len -= 3;
2567 lang[len] = NUL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002568
2569 /* If the region differs from what was used before then don't
2570 * use it for 'spellfile'. */
2571 if (use_region != NULL && STRCMP(region, use_region) != 0)
2572 dont_use_region = TRUE;
2573 use_region = region;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002574 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002575 else
2576 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002577
2578 /* Check if we loaded this language before. */
2579 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2580 if (STRICMP(lang, lp->sl_name) == 0)
2581 break;
2582 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002583
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002584 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002585 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002586 {
2587 if (filename)
2588 (void)spell_load_file(lang, lang, NULL, FALSE);
2589 else
2590 spell_load_lang(lang);
2591 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002592
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002593 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002594 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002595 */
2596 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002597 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2598 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002599 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002600 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002601 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002602 {
2603 /* find region in sl_regions */
2604 c = find_region(lp->sl_regions, region);
2605 if (c == REGION_ALL)
2606 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002607 if (lp->sl_add)
2608 {
2609 if (*lp->sl_regions != NUL)
2610 /* This addition file is for other regions. */
2611 region_mask = 0;
2612 }
2613 else
2614 /* This is probably an error. Give a warning and
2615 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002616 smsg((char_u *)
2617 _("Warning: region %s not supported"),
2618 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002619 }
2620 else
2621 region_mask = 1 << c;
2622 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002623
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002624 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002625 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002626 if (ga_grow(&ga, 1) == FAIL)
2627 {
2628 ga_clear(&ga);
2629 return e_outofmem;
2630 }
2631 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2632 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2633 ++ga.ga_len;
2634 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002635 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002637 }
2638
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002639 /* round 0: load int_wordlist, if possible.
2640 * round 1: load first name in 'spellfile'.
2641 * round 2: load second name in 'spellfile.
2642 * etc. */
2643 spf = curbuf->b_p_spf;
2644 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002645 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002646 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002647 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002648 /* Internal wordlist, if there is one. */
2649 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002650 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002651 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002652 }
2653 else
2654 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002655 /* One entry in 'spellfile'. */
2656 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2657 STRCAT(spf_name, ".spl");
2658
2659 /* If it was already found above then skip it. */
2660 for (c = 0; c < ga.ga_len; ++c)
2661 if (fullpathcmp(spf_name,
2662 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2663 FALSE) == FPC_SAME)
2664 break;
2665 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002666 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002667 }
2668
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002669 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2671 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2672 break;
2673 if (lp == NULL)
2674 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002675 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002676 * region name, the region is ignored otherwise. for int_wordlist
2677 * use an arbitrary name. */
2678 if (round == 0)
2679 STRCPY(lang, "internal wordlist");
2680 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002681 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002682 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002683 p = vim_strchr(lang, '.');
2684 if (p != NULL)
2685 *p = NUL; /* truncate at ".encoding.add" */
2686 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002687 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002688 }
2689 if (lp != NULL && ga_grow(&ga, 1) == OK)
2690 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002691 region_mask = REGION_ALL;
2692 if (use_region != NULL && !dont_use_region)
2693 {
2694 /* find region in sl_regions */
2695 c = find_region(lp->sl_regions, use_region);
2696 if (c != REGION_ALL)
2697 region_mask = 1 << c;
2698 else if (*lp->sl_regions != NUL)
2699 /* This spell file is for other regions. */
2700 region_mask = 0;
2701 }
2702
2703 if (region_mask != 0)
2704 {
2705 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2706 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2707 ++ga.ga_len;
2708 use_midword(lp, buf);
2709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 }
2711 }
2712
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002713 /* Add a NULL entry to mark the end of the list. */
2714 if (ga_grow(&ga, 1) == FAIL)
2715 {
2716 ga_clear(&ga);
2717 return e_outofmem;
2718 }
2719 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2720 ++ga.ga_len;
2721
2722 /* Everything is fine, store the new b_langp value. */
2723 ga_clear(&buf->b_langp);
2724 buf->b_langp = ga;
2725
2726 return NULL;
2727}
2728
2729/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002730 * Clear the midword characters for buffer "buf".
2731 */
2732 static void
2733clear_midword(buf)
2734 buf_T *buf;
2735{
2736 vim_memset(buf->b_spell_ismw, 0, 256);
2737#ifdef FEAT_MBYTE
2738 vim_free(buf->b_spell_ismw_mb);
2739 buf->b_spell_ismw_mb = NULL;
2740#endif
2741}
2742
2743/*
2744 * Use the "sl_midword" field of language "lp" for buffer "buf".
2745 * They add up to any currently used midword characters.
2746 */
2747 static void
2748use_midword(lp, buf)
2749 slang_T *lp;
2750 buf_T *buf;
2751{
2752 char_u *p;
2753
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002754 if (lp->sl_midword == NULL) /* there aren't any */
2755 return;
2756
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002757 for (p = lp->sl_midword; *p != NUL; )
2758#ifdef FEAT_MBYTE
2759 if (has_mbyte)
2760 {
2761 int c, l, n;
2762 char_u *bp;
2763
2764 c = mb_ptr2char(p);
2765 l = mb_ptr2len_check(p);
2766 if (c < 256)
2767 buf->b_spell_ismw[c] = TRUE;
2768 else if (buf->b_spell_ismw_mb == NULL)
2769 /* First multi-byte char in "b_spell_ismw_mb". */
2770 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2771 else
2772 {
2773 /* Append multi-byte chars to "b_spell_ismw_mb". */
2774 n = STRLEN(buf->b_spell_ismw_mb);
2775 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2776 if (bp != NULL)
2777 {
2778 vim_free(buf->b_spell_ismw_mb);
2779 buf->b_spell_ismw_mb = bp;
2780 vim_strncpy(bp + n, p, l);
2781 }
2782 }
2783 p += l;
2784 }
2785 else
2786#endif
2787 buf->b_spell_ismw[*p++] = TRUE;
2788}
2789
2790/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002791 * Find the region "region[2]" in "rp" (points to "sl_regions").
2792 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002793 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 */
2795 static int
2796find_region(rp, region)
2797 char_u *rp;
2798 char_u *region;
2799{
2800 int i;
2801
2802 for (i = 0; ; i += 2)
2803 {
2804 if (rp[i] == NUL)
2805 return REGION_ALL;
2806 if (rp[i] == region[0] && rp[i + 1] == region[1])
2807 break;
2808 }
2809 return i / 2;
2810}
2811
2812/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002813 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002814 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002815 * Word WF_ONECAP
2816 * W WORD WF_ALLCAP
2817 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 */
2819 static int
2820captype(word, end)
2821 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002822 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002823{
2824 char_u *p;
2825 int c;
2826 int firstcap;
2827 int allcap;
2828 int past_second = FALSE; /* past second word char */
2829
2830 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002831 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002832 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002833 return 0; /* only non-word characters, illegal word */
2834#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002835 if (has_mbyte)
2836 c = mb_ptr2char_adv(&p);
2837 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002838#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002839 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002840 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002841
2842 /*
2843 * Need to check all letters to find a word with mixed upper/lower.
2844 * But a word with an upper char only at start is a ONECAP.
2845 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002847 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002848 {
2849#ifdef FEAT_MBYTE
2850 c = mb_ptr2char(p);
2851#else
2852 c = *p;
2853#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002854 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002855 {
2856 /* UUl -> KEEPCAP */
2857 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002858 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002859 allcap = FALSE;
2860 }
2861 else if (!allcap)
2862 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002863 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002864 past_second = TRUE;
2865 }
2866
2867 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002868 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002870 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002871 return 0;
2872}
2873
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002874# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2875/*
2876 * Free all languages.
2877 */
2878 void
2879spell_free_all()
2880{
2881 slang_T *lp;
2882 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002883 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002884
2885 /* Go through all buffers and handle 'spelllang'. */
2886 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2887 ga_clear(&buf->b_langp);
2888
2889 while (first_lang != NULL)
2890 {
2891 lp = first_lang;
2892 first_lang = lp->sl_next;
2893 slang_free(lp);
2894 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002895
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002896 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002897 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002898 /* Delete the internal wordlist and its .spl file */
2899 mch_remove(int_wordlist);
2900 int_wordlist_spl(fname);
2901 mch_remove(fname);
2902 vim_free(int_wordlist);
2903 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002904 }
2905
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002906 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002907}
2908# endif
2909
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002910# if defined(FEAT_MBYTE) || defined(PROTO)
2911/*
2912 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002913 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002914 */
2915 void
2916spell_reload()
2917{
2918 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002919 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002920
Bram Moolenaarea408852005-06-25 22:49:46 +00002921 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002922 init_spell_chartab();
2923
2924 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002925 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002926
2927 /* Go through all buffers and handle 'spelllang'. */
2928 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2929 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002930 /* Only load the wordlists when 'spelllang' is set and there is a
2931 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002932 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002933 {
2934 FOR_ALL_WINDOWS(wp)
2935 if (wp->w_buffer == buf && wp->w_p_spell)
2936 {
2937 (void)did_set_spelllang(buf);
2938# ifdef FEAT_WINDOWS
2939 break;
2940# endif
2941 }
2942 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002943 }
2944}
2945# endif
2946
Bram Moolenaarb765d632005-06-07 21:00:02 +00002947/*
2948 * Reload the spell file "fname" if it's loaded.
2949 */
2950 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002951spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002952 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002954{
2955 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002956 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002957
Bram Moolenaarb765d632005-06-07 21:00:02 +00002958 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2959 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
2960 {
2961 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002962 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002963 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002964 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002966
2967 /* When "zg" was used and the file wasn't loaded yet, should redo
2968 * 'spelllang' to get it loaded. */
2969 if (added_word && !didit)
2970 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002971}
2972
2973
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002974/*
2975 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002976 */
2977
Bram Moolenaar51485f02005-06-04 21:55:20 +00002978#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002979 and .dic file. */
2980/*
2981 * Main structure to store the contents of a ".aff" file.
2982 */
2983typedef struct afffile_S
2984{
2985 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002986 int af_rar; /* RAR ID for rare word */
2987 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00002988 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002989 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002990 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
2991 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002992} afffile_T;
2993
2994typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002995/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
2996struct affentry_S
2997{
2998 affentry_T *ae_next; /* next affix with same name/number */
2999 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3000 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003001 char_u *ae_cond; /* condition (NULL for ".") */
3002 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003003 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003004};
3005
3006/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3007typedef struct affheader_S
3008{
3009 char_u ah_key[2]; /* key for hashtable == name of affix entry */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003010 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003011 int ah_combine; /* suffix may combine with prefix */
3012 affentry_T *ah_first; /* first affix entry */
3013} affheader_T;
3014
3015#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3016
3017/*
3018 * Structure that is used to store the items in the word tree. This avoids
3019 * the need to keep track of each allocated thing, it's freed all at once
3020 * after ":mkspell" is done.
3021 */
3022#define SBLOCKSIZE 16000 /* size of sb_data */
3023typedef struct sblock_S sblock_T;
3024struct sblock_S
3025{
3026 sblock_T *sb_next; /* next block in list */
3027 int sb_used; /* nr of bytes already in use */
3028 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003029};
3030
3031/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003032 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003033 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003034typedef struct wordnode_S wordnode_T;
3035struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003036{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003037 union /* shared to save space */
3038 {
3039 char_u hashkey[6]; /* room for the hash key */
3040 int index; /* index in written nodes (valid after first
3041 round) */
3042 } wn_u1;
3043 union /* shared to save space */
3044 {
3045 wordnode_T *next; /* next node with same hash key */
3046 wordnode_T *wnode; /* parent node that will write this node */
3047 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003048 wordnode_T *wn_child; /* child (next byte in word) */
3049 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3050 always sorted) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003051 char_u wn_byte; /* Byte for this node. NUL for word end */
3052 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003053 short wn_region; /* when wn_byte is NUL: region mask; for
3054 PREFIXTREE it's the prefcondnr */
3055 char_u wn_prefixID; /* supported/required prefix ID or 0 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003056};
3057
Bram Moolenaar51485f02005-06-04 21:55:20 +00003058#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003059
Bram Moolenaar51485f02005-06-04 21:55:20 +00003060/*
3061 * Info used while reading the spell files.
3062 */
3063typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003064{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003065 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003066 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003067 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003068 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003069 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003070 sblock_T *si_blocks; /* memory blocks used */
3071 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003072 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003073 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003074 int si_region; /* region mask */
3075 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003076 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003077 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003078 int si_region_count; /* number of regions supported (1 when there
3079 are no regions) */
3080 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081
3082 garray_T si_rep; /* list of fromto_T entries from REP lines */
3083 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003084 char_u *si_sofofr; /* SOFOFROM text */
3085 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003086 int si_followup; /* soundsalike: ? */
3087 int si_collapse; /* soundsalike: ? */
3088 int si_rem_accents; /* soundsalike: remove accents */
3089 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003090 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003091 garray_T si_prefcond; /* table with conditions for postponed
3092 * prefixes, each stored as a string */
3093 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003094} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003095
Bram Moolenaar51485f02005-06-04 21:55:20 +00003096static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003097static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003098static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3099static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003100static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003101static void spell_free_aff __ARGS((afffile_T *aff));
3102static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003103static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3104static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003105static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
3106static void *getroom __ARGS((sblock_T **blp, size_t len));
3107static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3108static void free_blocks __ARGS((sblock_T *bl));
3109static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003110static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
3111static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, int prefixID, sblock_T **blp));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003112static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003113static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3114static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003115static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003116static void clear_node __ARGS((wordnode_T *node));
3117static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003118static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003119static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003120
3121/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003122 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003123 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003124 */
3125 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003126spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003127 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003128 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003129{
3130 FILE *fd;
3131 afffile_T *aff;
3132 char_u rline[MAXLINELEN];
3133 char_u *line;
3134 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003135#define MAXITEMCNT 7
3136 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003137 int itemcnt;
3138 char_u *p;
3139 int lnum = 0;
3140 affheader_T *cur_aff = NULL;
3141 int aff_todo = 0;
3142 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003143 char_u *low = NULL;
3144 char_u *fol = NULL;
3145 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003146 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003147 int do_rep;
3148 int do_sal;
3149 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003150 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003151 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003152 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003153 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003154
Bram Moolenaar51485f02005-06-04 21:55:20 +00003155 /*
3156 * Open the file.
3157 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003158 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003159 if (fd == NULL)
3160 {
3161 EMSG2(_(e_notopen), fname);
3162 return NULL;
3163 }
3164
Bram Moolenaarb765d632005-06-07 21:00:02 +00003165 if (spin->si_verbose || p_verbose > 2)
3166 {
3167 if (!spin->si_verbose)
3168 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003169 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003170 out_flush();
3171 if (!spin->si_verbose)
3172 verbose_leave();
3173 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003175 /* Only do REP lines when not done in another .aff file already. */
3176 do_rep = spin->si_rep.ga_len == 0;
3177
3178 /* Only do SAL lines when not done in another .aff file already. */
3179 do_sal = spin->si_sal.ga_len == 0;
3180
3181 /* Only do MAP lines when not done in another .aff file already. */
3182 do_map = spin->si_map.ga_len == 0;
3183
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003184 /* Only do MIDWORD line when not done in another .aff file already */
3185 do_midword = spin->si_midword == NULL;
3186
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003187 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3188 do_sofo = spin->si_sofofr == NULL;
3189
Bram Moolenaar51485f02005-06-04 21:55:20 +00003190 /*
3191 * Allocate and init the afffile_T structure.
3192 */
3193 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003194 if (aff == NULL)
3195 return NULL;
3196 hash_init(&aff->af_pref);
3197 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003198
3199 /*
3200 * Read all the lines in the file one by one.
3201 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003202 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003203 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003204 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003205 ++lnum;
3206
3207 /* Skip comment lines. */
3208 if (*rline == '#')
3209 continue;
3210
3211 /* Convert from "SET" to 'encoding' when needed. */
3212 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003213#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003214 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003215 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003216 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003217 if (pc == NULL)
3218 {
3219 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3220 fname, lnum, rline);
3221 continue;
3222 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003223 line = pc;
3224 }
3225 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003226#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003227 {
3228 pc = NULL;
3229 line = rline;
3230 }
3231
3232 /* Split the line up in white separated items. Put a NUL after each
3233 * item. */
3234 itemcnt = 0;
3235 for (p = line; ; )
3236 {
3237 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3238 ++p;
3239 if (*p == NUL)
3240 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003241 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003242 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003243 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003244 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003245 ++p;
3246 if (*p == NUL)
3247 break;
3248 *p++ = NUL;
3249 }
3250
3251 /* Handle non-empty lines. */
3252 if (itemcnt > 0)
3253 {
3254 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3255 && aff->af_enc == NULL)
3256 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003257#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003258 /* Setup for conversion from "ENC" to 'encoding'. */
3259 aff->af_enc = enc_canonize(items[1]);
3260 if (aff->af_enc != NULL && !spin->si_ascii
3261 && convert_setup(&spin->si_conv, aff->af_enc,
3262 p_enc) == FAIL)
3263 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3264 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003265 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003266#else
3267 smsg((char_u *)_("Conversion in %s not supported"), fname);
3268#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003269 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003270 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3271 {
3272 if (do_midword)
3273 spin->si_midword = vim_strsave(items[1]);
3274 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003275 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003278 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003279 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003281 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003282 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003283 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3284 && aff->af_rar == 0)
3285 {
3286 aff->af_rar = items[1][0];
3287 if (items[1][1] != NUL)
3288 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3289 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003290 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3291 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003292 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003293 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003294 if (items[1][1] != NUL)
3295 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3296 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003297 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3298 && aff->af_bad == 0)
3299 {
3300 aff->af_bad = items[1][0];
3301 if (items[1][1] != NUL)
3302 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3303 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003304 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3305 {
3306 aff->af_pfxpostpone = TRUE;
3307 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003308 else if ((STRCMP(items[0], "PFX") == 0
3309 || STRCMP(items[0], "SFX") == 0)
3310 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003311 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003312 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003313 /* Myspell allows extra text after the item, but that might
3314 * mean mistakes go unnoticed. Require a comment-starter. */
3315 if (itemcnt > 4 && *items[4] != '#')
3316 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3317 fname, lnum, items[4]);
3318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003319 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003320 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
3321 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003322 if (cur_aff == NULL)
3323 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003324 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003325 cur_aff->ah_key[1] = NUL;
3326 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003327 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003328 if (*items[2] == 'Y')
3329 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003330 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003331 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3332 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003333
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003334 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003335 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003336 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003337 /* Use a new number in the .spl file later, to be able to
3338 * handle multiple .aff files. */
3339 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003340 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003341 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003342 else
3343 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003344 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003345 hi = hash_find(tp, cur_aff->ah_key);
3346 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003347 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003348 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3349 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003350 aff_todo = 0;
3351 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003352 else
3353 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003354 }
3355 else if ((STRCMP(items[0], "PFX") == 0
3356 || STRCMP(items[0], "SFX") == 0)
3357 && aff_todo > 0
3358 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003359 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003360 {
3361 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003362 int rare = FALSE;
3363 int lasti = 5;
3364
3365 /* Check for "rare" after the other info. */
3366 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3367 {
3368 rare = TRUE;
3369 lasti = 6;
3370 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003371
Bram Moolenaar8db73182005-06-17 21:51:16 +00003372 /* Myspell allows extra text after the item, but that might
3373 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003374 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003375 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003376 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003377
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003378 /* New item for an affix letter. */
3379 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003380 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
3381 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003382 if (aff_entry == NULL)
3383 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003384 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003385
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003386 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003387 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3388 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003389 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003390 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3391 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003392
Bram Moolenaar51485f02005-06-04 21:55:20 +00003393 /* Don't use an affix entry with non-ASCII characters when
3394 * "spin->si_ascii" is TRUE. */
3395 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003396 || has_non_ascii(aff_entry->ae_add)))
3397 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003398 aff_entry->ae_next = cur_aff->ah_first;
3399 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003400
3401 if (STRCMP(items[4], ".") != 0)
3402 {
3403 char_u buf[MAXLINELEN];
3404
3405 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3406 items[4]);
3407 if (*items[0] == 'P')
3408 sprintf((char *)buf, "^%s", items[4]);
3409 else
3410 sprintf((char *)buf, "%s$", items[4]);
3411 aff_entry->ae_prog = vim_regcomp(buf,
3412 RE_MAGIC + RE_STRING);
3413 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003414
3415 /* For postponed prefixes we need an entry in si_prefcond
3416 * for the condition. Use an existing one if possible. */
3417 if (*items[0] == 'P' && aff->af_pfxpostpone
3418 && aff_entry->ae_chop == NULL)
3419 {
3420 int idx;
3421 char_u **pp;
3422
3423 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3424 --idx)
3425 {
3426 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3427 if (str_equal(p, aff_entry->ae_cond))
3428 break;
3429 }
3430 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3431 {
3432 /* Not found, add a new condition. */
3433 idx = spin->si_prefcond.ga_len++;
3434 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
3435 if (aff_entry->ae_cond == NULL)
3436 *pp = NULL;
3437 else
3438 *pp = getroom_save(&spin->si_blocks,
3439 aff_entry->ae_cond);
3440 }
3441
3442 /* Add the prefix to the prefix tree. */
3443 if (aff_entry->ae_add == NULL)
3444 p = (char_u *)"";
3445 else
3446 p = aff_entry->ae_add;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003447 tree_add_word(p, spin->si_prefroot, rare ? -2 : -1,
3448 idx, cur_aff->ah_newID, &spin->si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003449 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003450 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003451 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003452 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3453 {
3454 if (fol != NULL)
3455 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3456 fname, lnum);
3457 else
3458 fol = vim_strsave(items[1]);
3459 }
3460 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3461 {
3462 if (low != NULL)
3463 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3464 fname, lnum);
3465 else
3466 low = vim_strsave(items[1]);
3467 }
3468 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3469 {
3470 if (upp != NULL)
3471 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3472 fname, lnum);
3473 else
3474 upp = vim_strsave(items[1]);
3475 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003476 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003477 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003478 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003479 if (!isdigit(*items[1]))
3480 smsg((char_u *)_("Expected REP count in %s line %d"),
3481 fname, lnum);
3482 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003483 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3484 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003485 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003486 if (do_rep)
3487 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3488 }
3489 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3490 {
3491 /* MAP item or count */
3492 if (!found_map)
3493 {
3494 /* First line contains the count. */
3495 found_map = TRUE;
3496 if (!isdigit(*items[1]))
3497 smsg((char_u *)_("Expected MAP count in %s line %d"),
3498 fname, lnum);
3499 }
3500 else if (do_map)
3501 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003502 int c;
3503
3504 /* Check that every character appears only once. */
3505 for (p = items[1]; *p != NUL; )
3506 {
3507#ifdef FEAT_MBYTE
3508 c = mb_ptr2char_adv(&p);
3509#else
3510 c = *p++;
3511#endif
3512 if ((spin->si_map.ga_len > 0
3513 && vim_strchr(spin->si_map.ga_data, c)
3514 != NULL)
3515 || vim_strchr(p, c) != NULL)
3516 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3517 fname, lnum);
3518 }
3519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520 /* We simply concatenate all the MAP strings, separated by
3521 * slashes. */
3522 ga_concat(&spin->si_map, items[1]);
3523 ga_append(&spin->si_map, '/');
3524 }
3525 }
3526 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3527 {
3528 if (do_sal)
3529 {
3530 /* SAL item (sounds-a-like)
3531 * Either one of the known keys or a from-to pair. */
3532 if (STRCMP(items[1], "followup") == 0)
3533 spin->si_followup = sal_to_bool(items[2]);
3534 else if (STRCMP(items[1], "collapse_result") == 0)
3535 spin->si_collapse = sal_to_bool(items[2]);
3536 else if (STRCMP(items[1], "remove_accents") == 0)
3537 spin->si_rem_accents = sal_to_bool(items[2]);
3538 else
3539 /* when "to" is "_" it means empty */
3540 add_fromto(spin, &spin->si_sal, items[1],
3541 STRCMP(items[2], "_") == 0 ? (char_u *)""
3542 : items[2]);
3543 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003544 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003545 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3546 && (!do_sofo || spin->si_sofofr == NULL))
3547 {
3548 if (do_sofo)
3549 spin->si_sofofr = vim_strsave(items[1]);
3550 }
3551 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3552 && (!do_sofo || spin->si_sofoto == NULL))
3553 {
3554 if (do_sofo)
3555 spin->si_sofoto = vim_strsave(items[1]);
3556 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003557 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003558 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3559 fname, lnum, items[0]);
3560 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 }
3562
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003563 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3564 smsg((char_u *)_("Missing SOFO%s line in %s"),
3565 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3566 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3567 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3568
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003569 if (fol != NULL || low != NULL || upp != NULL)
3570 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003571 if (spin->si_clear_chartab)
3572 {
3573 /* Clear the char type tables, don't want to use any of the
3574 * currently used spell properties. */
3575 init_spell_chartab();
3576 spin->si_clear_chartab = FALSE;
3577 }
3578
Bram Moolenaar3982c542005-06-08 21:56:31 +00003579 /*
3580 * Don't write a word table for an ASCII file, so that we don't check
3581 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003582 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003583 * mb_get_class(), the list of chars in the file will be incomplete.
3584 */
3585 if (!spin->si_ascii
3586#ifdef FEAT_MBYTE
3587 && !enc_utf8
3588#endif
3589 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003590 {
3591 if (fol == NULL || low == NULL || upp == NULL)
3592 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3593 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003594 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003595 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003596
3597 vim_free(fol);
3598 vim_free(low);
3599 vim_free(upp);
3600 }
3601
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003602 vim_free(pc);
3603 fclose(fd);
3604 return aff;
3605}
3606
3607/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003608 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3609 * NULL as equal.
3610 */
3611 static int
3612str_equal(s1, s2)
3613 char_u *s1;
3614 char_u *s2;
3615{
3616 if (s1 == NULL || s2 == NULL)
3617 return s1 == s2;
3618 return STRCMP(s1, s2) == 0;
3619}
3620
3621/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 * Add a from-to item to "gap". Used for REP and SAL items.
3623 * They are stored case-folded.
3624 */
3625 static void
3626add_fromto(spin, gap, from, to)
3627 spellinfo_T *spin;
3628 garray_T *gap;
3629 char_u *from;
3630 char_u *to;
3631{
3632 fromto_T *ftp;
3633 char_u word[MAXWLEN];
3634
3635 if (ga_grow(gap, 1) == OK)
3636 {
3637 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3638 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3639 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3640 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3641 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3642 ++gap->ga_len;
3643 }
3644}
3645
3646/*
3647 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3648 */
3649 static int
3650sal_to_bool(s)
3651 char_u *s;
3652{
3653 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3654}
3655
3656/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003657 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3658 * When "s" is NULL FALSE is returned.
3659 */
3660 static int
3661has_non_ascii(s)
3662 char_u *s;
3663{
3664 char_u *p;
3665
3666 if (s != NULL)
3667 for (p = s; *p != NUL; ++p)
3668 if (*p >= 128)
3669 return TRUE;
3670 return FALSE;
3671}
3672
3673/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003674 * Free the structure filled by spell_read_aff().
3675 */
3676 static void
3677spell_free_aff(aff)
3678 afffile_T *aff;
3679{
3680 hashtab_T *ht;
3681 hashitem_T *hi;
3682 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003683 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003684 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003685
3686 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003687
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003688 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003689 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3690 {
3691 todo = ht->ht_used;
3692 for (hi = ht->ht_array; todo > 0; ++hi)
3693 {
3694 if (!HASHITEM_EMPTY(hi))
3695 {
3696 --todo;
3697 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003698 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3699 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003700 }
3701 }
3702 if (ht == &aff->af_suff)
3703 break;
3704 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003705
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003706 hash_clear(&aff->af_pref);
3707 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003708}
3709
3710/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003711 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003712 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003713 */
3714 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003715spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003716 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003717 spellinfo_T *spin;
3718 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003719{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003720 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003721 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00003722 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003723 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003724 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725 char_u *pc;
3726 char_u *w;
3727 int l;
3728 hash_T hash;
3729 hashitem_T *hi;
3730 FILE *fd;
3731 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003732 int non_ascii = 0;
3733 int retval = OK;
3734 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003735 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003736
Bram Moolenaar51485f02005-06-04 21:55:20 +00003737 /*
3738 * Open the file.
3739 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003740 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003741 if (fd == NULL)
3742 {
3743 EMSG2(_(e_notopen), fname);
3744 return FAIL;
3745 }
3746
Bram Moolenaar51485f02005-06-04 21:55:20 +00003747 /* The hashtable is only used to detect duplicated words. */
3748 hash_init(&ht);
3749
Bram Moolenaar8db73182005-06-17 21:51:16 +00003750 spin->si_foldwcount = 0;
3751 spin->si_keepwcount = 0;
3752
Bram Moolenaarb765d632005-06-07 21:00:02 +00003753 if (spin->si_verbose || p_verbose > 2)
3754 {
3755 if (!spin->si_verbose)
3756 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003757 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003758 out_flush();
3759 if (!spin->si_verbose)
3760 verbose_leave();
3761 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003762
3763 /* Read and ignore the first line: word count. */
3764 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003765 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003766 EMSG2(_("E760: No word count in %s"), fname);
3767
3768 /*
3769 * Read all the lines in the file one by one.
3770 * The words are converted to 'encoding' here, before being added to
3771 * the hashtable.
3772 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003773 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003774 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003775 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003776 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003777 if (line[0] == '#')
3778 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003779
Bram Moolenaar51485f02005-06-04 21:55:20 +00003780 /* Remove CR, LF and white space from the end. White space halfway
3781 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003782 l = STRLEN(line);
3783 while (l > 0 && line[l - 1] <= ' ')
3784 --l;
3785 if (l == 0)
3786 continue; /* empty line */
3787 line[l] = NUL;
3788
Bram Moolenaar51485f02005-06-04 21:55:20 +00003789 /* Find the optional affix names. */
3790 afflist = vim_strchr(line, '/');
3791 if (afflist != NULL)
3792 *afflist++ = NUL;
3793
3794 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
3795 if (spin->si_ascii && has_non_ascii(line))
3796 {
3797 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003798 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003799 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003800
Bram Moolenaarb765d632005-06-07 21:00:02 +00003801#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003802 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003803 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003804 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003805 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003806 if (pc == NULL)
3807 {
3808 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3809 fname, lnum, line);
3810 continue;
3811 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003812 w = pc;
3813 }
3814 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003815#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003816 {
3817 pc = NULL;
3818 w = line;
3819 }
3820
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003821 /* This takes time, print a message now and then. */
3822 if (spin->si_verbose && (lnum & 0x3ff) == 0)
3823 {
3824 vim_snprintf((char *)message, sizeof(message),
3825 _("line %6d, word %6d - %s"),
3826 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3827 msg_start();
3828 msg_puts_long_attr(message, 0);
3829 msg_clr_eos();
3830 msg_didout = FALSE;
3831 msg_col = 0;
3832 out_flush();
3833 }
3834
Bram Moolenaar51485f02005-06-04 21:55:20 +00003835 /* Store the word in the hashtable to be able to find duplicates. */
3836 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003837 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003838 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003839 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003840 if (retval == FAIL)
3841 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003842
Bram Moolenaar51485f02005-06-04 21:55:20 +00003843 hash = hash_hash(dw);
3844 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003845 if (!HASHITEM_EMPTY(hi))
3846 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003847 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003848 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003849 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003850
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003851 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003852 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003853 if (afflist != NULL)
3854 {
3855 /* Check for affix name that stands for keep-case word and stands
3856 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003857 if (affile->af_kep != NUL
3858 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003859 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003860 if (affile->af_rar != NUL
3861 && vim_strchr(afflist, affile->af_rar) != NULL)
3862 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003863 if (affile->af_bad != NUL
3864 && vim_strchr(afflist, affile->af_bad) != NULL)
3865 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003866
3867 if (affile->af_pfxpostpone)
3868 /* Need to store the list of prefix IDs with the word. */
3869 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003870 }
3871
Bram Moolenaar51485f02005-06-04 21:55:20 +00003872 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003873 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003874 retval = FAIL;
3875
3876 if (afflist != NULL)
3877 {
3878 /* Find all matching suffixes and add the resulting words.
3879 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003880 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003881 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003882 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003883 retval = FAIL;
3884
3885 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003886 if (store_aff_word(dw, spin, afflist, affile,
3887 &affile->af_pref, NULL,
3888 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003889 retval = FAIL;
3890 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003891 }
3892
Bram Moolenaar51485f02005-06-04 21:55:20 +00003893 if (spin->si_ascii && non_ascii > 0)
3894 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
3895 non_ascii);
3896 hash_clear(&ht);
3897
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003898 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003899 return retval;
3900}
3901
3902/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003903 * Get the list of prefix IDs from the affix list "afflist".
3904 * Used for PFXPOSTPONE.
3905 * Returns a string allocated with getroom(). NULL when there are no prefixes
3906 * or when out of memory.
3907 */
3908 static char_u *
3909get_pfxlist(affile, afflist, blp)
3910 afffile_T *affile;
3911 char_u *afflist;
3912 sblock_T **blp;
3913{
3914 char_u *p;
3915 int cnt;
3916 int round;
3917 char_u *res = NULL;
3918 char_u key[2];
3919 hashitem_T *hi;
3920
3921 key[1] = NUL;
3922
3923 /* round 1: count the number of prefix IDs.
3924 * round 2: move prefix IDs to "res" */
3925 for (round = 1; round <= 2; ++round)
3926 {
3927 cnt = 0;
3928 for (p = afflist; *p != NUL; ++p)
3929 {
3930 key[0] = *p;
3931 hi = hash_find(&affile->af_pref, key);
3932 if (!HASHITEM_EMPTY(hi))
3933 {
3934 /* This is a prefix ID, use the new number. */
3935 if (round == 2)
3936 res[cnt] = HI2AH(hi)->ah_newID;
3937 ++cnt;
3938 }
3939 }
3940 if (round == 1 && cnt > 0)
3941 res = getroom(blp, cnt + 1);
3942 if (res == NULL)
3943 break;
3944 }
3945
3946 if (res != NULL)
3947 res[cnt] = NUL;
3948 return res;
3949}
3950
3951/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003952 * Apply affixes to a word and store the resulting words.
3953 * "ht" is the hashtable with affentry_T that need to be applied, either
3954 * prefixes or suffixes.
3955 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3956 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003957 *
3958 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003959 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003960 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003961store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003962 char_u *word; /* basic word start */
3963 spellinfo_T *spin; /* spell info */
3964 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003965 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003966 hashtab_T *ht;
3967 hashtab_T *xht;
3968 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003969 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003970 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971{
3972 int todo;
3973 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003974 affheader_T *ah;
3975 affentry_T *ae;
3976 regmatch_T regmatch;
3977 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003978 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003979 int i;
3980 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003981 int use_flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003982
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 todo = ht->ht_used;
3984 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003985 {
3986 if (!HASHITEM_EMPTY(hi))
3987 {
3988 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00003990
Bram Moolenaar51485f02005-06-04 21:55:20 +00003991 /* Check that the affix combines, if required, and that the word
3992 * supports this affix. */
3993 if ((!comb || ah->ah_combine)
3994 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003995 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003996 /* Loop over all affix entries with this name. */
3997 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003998 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003999 /* Check the condition. It's not logical to match case
4000 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004001 * Myspell.
4002 * For prefixes, when "PFXPOSTPONE" was used, only do
4003 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004004 regmatch.regprog = ae->ae_prog;
4005 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004006 if ((xht != NULL || !affile->af_pfxpostpone
4007 || ae->ae_chop != NULL)
4008 && (ae->ae_prog == NULL
4009 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004010 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004011 /* Match. Remove the chop and add the affix. */
4012 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004013 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004014 /* prefix: chop/add at the start of the word */
4015 if (ae->ae_add == NULL)
4016 *newword = NUL;
4017 else
4018 STRCPY(newword, ae->ae_add);
4019 p = word;
4020 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004021 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004022 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004023#ifdef FEAT_MBYTE
4024 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004025 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004026 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004027 for ( ; i > 0; --i)
4028 mb_ptr_adv(p);
4029 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004030 else
4031#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004032 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004033 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004034 STRCAT(newword, p);
4035 }
4036 else
4037 {
4038 /* suffix: chop/add at the end of the word */
4039 STRCPY(newword, word);
4040 if (ae->ae_chop != NULL)
4041 {
4042 /* Remove chop string. */
4043 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004044 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004045 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004046 mb_ptr_back(newword, p);
4047 *p = NUL;
4048 }
4049 if (ae->ae_add != NULL)
4050 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004051 }
4052
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004053 /* Obey the "rare" flag of the affix. */
4054 if (ae->ae_rare)
4055 use_flags = flags | WF_RARE;
4056 else
4057 use_flags = flags;
4058
Bram Moolenaar51485f02005-06-04 21:55:20 +00004059 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004060 if (store_word(newword, spin, use_flags,
4061 spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063
Bram Moolenaar51485f02005-06-04 21:55:20 +00004064 /* When added a suffix and combining is allowed also
4065 * try adding prefixes additionally. */
4066 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004067 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004068 xht, NULL, TRUE, use_flags, pfxlist)
4069 == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004070 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004071 }
4072 }
4073 }
4074 }
4075 }
4076
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004077 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004078}
4079
4080/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004081 * Read a file with a list of words.
4082 */
4083 static int
4084spell_read_wordfile(fname, spin)
4085 char_u *fname;
4086 spellinfo_T *spin;
4087{
4088 FILE *fd;
4089 long lnum = 0;
4090 char_u rline[MAXLINELEN];
4091 char_u *line;
4092 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004093 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004094 int l;
4095 int retval = OK;
4096 int did_word = FALSE;
4097 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004098 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004099 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004100
4101 /*
4102 * Open the file.
4103 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004104 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004105 if (fd == NULL)
4106 {
4107 EMSG2(_(e_notopen), fname);
4108 return FAIL;
4109 }
4110
Bram Moolenaarb765d632005-06-07 21:00:02 +00004111 if (spin->si_verbose || p_verbose > 2)
4112 {
4113 if (!spin->si_verbose)
4114 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004115 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004116 out_flush();
4117 if (!spin->si_verbose)
4118 verbose_leave();
4119 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004120
4121 /*
4122 * Read all the lines in the file one by one.
4123 */
4124 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4125 {
4126 line_breakcheck();
4127 ++lnum;
4128
4129 /* Skip comment lines. */
4130 if (*rline == '#')
4131 continue;
4132
4133 /* Remove CR, LF and white space from the end. */
4134 l = STRLEN(rline);
4135 while (l > 0 && rline[l - 1] <= ' ')
4136 --l;
4137 if (l == 0)
4138 continue; /* empty or blank line */
4139 rline[l] = NUL;
4140
4141 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4142 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004143#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004144 if (spin->si_conv.vc_type != CONV_NONE)
4145 {
4146 pc = string_convert(&spin->si_conv, rline, NULL);
4147 if (pc == NULL)
4148 {
4149 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4150 fname, lnum, rline);
4151 continue;
4152 }
4153 line = pc;
4154 }
4155 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004156#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 {
4158 pc = NULL;
4159 line = rline;
4160 }
4161
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004162 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004163 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004164 ++line;
4165 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004166 {
4167 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004168 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4169 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004170 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004171 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4172 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004173 else
4174 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004175#ifdef FEAT_MBYTE
4176 char_u *enc;
4177
Bram Moolenaar51485f02005-06-04 21:55:20 +00004178 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004179 line += 10;
4180 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004181 if (enc != NULL && !spin->si_ascii
4182 && convert_setup(&spin->si_conv, enc,
4183 p_enc) == FAIL)
4184 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004185 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004187 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004188#else
4189 smsg((char_u *)_("Conversion in %s not supported"), fname);
4190#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004191 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004192 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004193 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004194
Bram Moolenaar3982c542005-06-08 21:56:31 +00004195 if (STRNCMP(line, "regions=", 8) == 0)
4196 {
4197 if (spin->si_region_count > 1)
4198 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4199 fname, lnum, line);
4200 else
4201 {
4202 line += 8;
4203 if (STRLEN(line) > 16)
4204 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4205 fname, lnum, line);
4206 else
4207 {
4208 spin->si_region_count = STRLEN(line) / 2;
4209 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004210
4211 /* Adjust the mask for a word valid in all regions. */
4212 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004213 }
4214 }
4215 continue;
4216 }
4217
Bram Moolenaar7887d882005-07-01 22:33:52 +00004218 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4219 fname, lnum, line - 1);
4220 continue;
4221 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004222
Bram Moolenaar7887d882005-07-01 22:33:52 +00004223 flags = 0;
4224 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004225
Bram Moolenaar7887d882005-07-01 22:33:52 +00004226 /* Check for flags and region after a slash. */
4227 p = vim_strchr(line, '/');
4228 if (p != NULL)
4229 {
4230 *p++ = NUL;
4231 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004232 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004233 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004234 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004235 else if (*p == '!') /* Bad, bad, wicked word. */
4236 flags |= WF_BANNED;
4237 else if (*p == '?') /* Rare word. */
4238 flags |= WF_RARE;
4239 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004240 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004241 if ((flags & WF_REGION) == 0) /* first one */
4242 regionmask = 0;
4243 flags |= WF_REGION;
4244
4245 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004246 if (l > spin->si_region_count)
4247 {
4248 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004249 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004250 break;
4251 }
4252 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004253 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004254 else
4255 {
4256 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4257 fname, lnum, p);
4258 break;
4259 }
4260 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004261 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004262 }
4263
4264 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4265 if (spin->si_ascii && has_non_ascii(line))
4266 {
4267 ++non_ascii;
4268 continue;
4269 }
4270
4271 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004272 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004273 {
4274 retval = FAIL;
4275 break;
4276 }
4277 did_word = TRUE;
4278 }
4279
4280 vim_free(pc);
4281 fclose(fd);
4282
Bram Moolenaarb765d632005-06-07 21:00:02 +00004283 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4284 {
4285 if (p_verbose > 2)
4286 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004287 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4288 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004289 if (p_verbose > 2)
4290 verbose_leave();
4291 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004292 return retval;
4293}
4294
4295/*
4296 * Get part of an sblock_T, "len" bytes long.
4297 * This avoids calling free() for every little struct we use.
4298 * The memory is cleared to all zeros.
4299 * Returns NULL when out of memory.
4300 */
4301 static void *
4302getroom(blp, len)
4303 sblock_T **blp;
4304 size_t len; /* length needed */
4305{
4306 char_u *p;
4307 sblock_T *bl = *blp;
4308
4309 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4310 {
4311 /* Allocate a block of memory. This is not freed until much later. */
4312 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4313 if (bl == NULL)
4314 return NULL;
4315 bl->sb_next = *blp;
4316 *blp = bl;
4317 bl->sb_used = 0;
4318 }
4319
4320 p = bl->sb_data + bl->sb_used;
4321 bl->sb_used += len;
4322
4323 return p;
4324}
4325
4326/*
4327 * Make a copy of a string into memory allocated with getroom().
4328 */
4329 static char_u *
4330getroom_save(blp, s)
4331 sblock_T **blp;
4332 char_u *s;
4333{
4334 char_u *sc;
4335
4336 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
4337 if (sc != NULL)
4338 STRCPY(sc, s);
4339 return sc;
4340}
4341
4342
4343/*
4344 * Free the list of allocated sblock_T.
4345 */
4346 static void
4347free_blocks(bl)
4348 sblock_T *bl;
4349{
4350 sblock_T *next;
4351
4352 while (bl != NULL)
4353 {
4354 next = bl->sb_next;
4355 vim_free(bl);
4356 bl = next;
4357 }
4358}
4359
4360/*
4361 * Allocate the root of a word tree.
4362 */
4363 static wordnode_T *
4364wordtree_alloc(blp)
4365 sblock_T **blp;
4366{
4367 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4368}
4369
4370/*
4371 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004372 * Always store it in the case-folded tree. A keep-case word can also be used
4373 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004374 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004375 * When "pfxlist" is not NULL store the word for each prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004376 */
4377 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004378store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004379 char_u *word;
4380 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004381 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004382 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004383 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004384{
4385 int len = STRLEN(word);
4386 int ct = captype(word, word + len);
4387 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004388 int res = OK;
4389 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004392 for (p = pfxlist; res == OK; ++p)
4393 {
4394 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
4395 region, p == NULL ? 0 : *p, &spin->si_blocks);
4396 if (p == NULL || *p == NUL)
4397 break;
4398 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004399 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004400
4401 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004402 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004403 for (p = pfxlist; res == OK; ++p)
4404 {
4405 res = tree_add_word(word, spin->si_keeproot, flags,
4406 region, p == NULL ? 0 : *p, &spin->si_blocks);
4407 if (p == NULL || *p == NUL)
4408 break;
4409 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004410 ++spin->si_keepwcount;
4411 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004412 return res;
4413}
4414
4415/*
4416 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004417 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4418 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004419 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004420 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004421 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004422tree_add_word(word, root, flags, region, prefixID, blp)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004423 char_u *word;
4424 wordnode_T *root;
4425 int flags;
4426 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004427 int prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004428 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004429{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004430 wordnode_T *node = root;
4431 wordnode_T *np;
4432 wordnode_T **prev = NULL;
4433 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004434
Bram Moolenaar51485f02005-06-04 21:55:20 +00004435 /* Add each byte of the word to the tree, including the NUL at the end. */
4436 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004437 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004438 /* Look for the sibling that has the same character. They are sorted
4439 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004440 * higher byte value. For zero bytes (end of word) the sorting is
4441 * done on flags and then on prefixID
Bram Moolenaar51485f02005-06-04 21:55:20 +00004442 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004443 while (node != NULL
4444 && (node->wn_byte < word[i]
4445 || (node->wn_byte == NUL
4446 && (flags < 0
4447 ? node->wn_prefixID < prefixID
4448 : node->wn_flags < (flags & 0xff)
4449 || (node->wn_flags == (flags & 0xff)
4450 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004451 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004452 prev = &node->wn_sibling;
4453 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004454 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004455 if (node == NULL
4456 || node->wn_byte != word[i]
4457 || (word[i] == NUL
4458 && (flags < 0
4459 || node->wn_flags != (flags & 0xff)
4460 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004461 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004462 /* Allocate a new node. */
4463 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
4464 if (np == NULL)
4465 return FAIL;
4466 np->wn_byte = word[i];
4467 *prev = np;
4468 np->wn_sibling = node;
4469 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004470 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004471
Bram Moolenaar51485f02005-06-04 21:55:20 +00004472 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004473 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004474 node->wn_flags = flags;
4475 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004476 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004477 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004478 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004479 prev = &node->wn_child;
4480 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004481 }
4482
4483 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484}
4485
4486/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004487 * Compress a tree: find tails that are identical and can be shared.
4488 */
4489 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004490wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004491 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004492 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004493{
4494 hashtab_T ht;
4495 int n;
4496 int tot = 0;
4497
4498 if (root != NULL)
4499 {
4500 hash_init(&ht);
4501 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004502 if (spin->si_verbose || p_verbose > 2)
4503 {
4504 if (!spin->si_verbose)
4505 verbose_enter();
4506 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00004507 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004508 if (p_verbose > 2)
4509 verbose_leave();
4510 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004511 hash_clear(&ht);
4512 }
4513}
4514
4515/*
4516 * Compress a node, its siblings and its children, depth first.
4517 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004519 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004520node_compress(node, ht, tot)
4521 wordnode_T *node;
4522 hashtab_T *ht;
4523 int *tot; /* total count of nodes before compressing,
4524 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004526 wordnode_T *np;
4527 wordnode_T *tp;
4528 wordnode_T *child;
4529 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004530 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004531 int len = 0;
4532 unsigned nr, n;
4533 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004534
Bram Moolenaar51485f02005-06-04 21:55:20 +00004535 /*
4536 * Go through the list of siblings. Compress each child and then try
4537 * finding an identical child to replace it.
4538 * Note that with "child" we mean not just the node that is pointed to,
4539 * but the whole list of siblings, of which the node is the first.
4540 */
4541 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004542 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543 ++len;
4544 if ((child = np->wn_child) != NULL)
4545 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004546 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004547 compressed += node_compress(child, ht, tot);
4548
4549 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004550 hash = hash_hash(child->wn_u1.hashkey);
4551 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004552 tp = NULL;
4553 if (!HASHITEM_EMPTY(hi))
4554 {
4555 /* There are children with an identical hash value. Now check
4556 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004557 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004558 if (node_equal(child, tp))
4559 {
4560 /* Found one! Now use that child in place of the
4561 * current one. This means the current child is
4562 * dropped from the tree. */
4563 np->wn_child = tp;
4564 ++compressed;
4565 break;
4566 }
4567 if (tp == NULL)
4568 {
4569 /* No other child with this hash value equals the child of
4570 * the node, add it to the linked list after the first
4571 * item. */
4572 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004573 child->wn_u2.next = tp->wn_u2.next;
4574 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004575 }
4576 }
4577 else
4578 /* No other child has this hash value, add it to the
4579 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004580 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004581 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004582 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004583 *tot += len;
4584
4585 /*
4586 * Make a hash key for the node and its siblings, so that we can quickly
4587 * find a lookalike node. This must be done after compressing the sibling
4588 * list, otherwise the hash key would become invalid by the compression.
4589 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004590 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004591 nr = 0;
4592 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004594 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004595 /* end node: use wn_flags, wn_region and wn_prefixID */
4596 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004597 else
4598 /* byte node: use the byte value and the child pointer */
4599 n = np->wn_byte + ((long_u)np->wn_child << 8);
4600 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004601 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004602
4603 /* Avoid NUL bytes, it terminates the hash key. */
4604 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004605 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004607 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004608 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004609 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004610 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004611 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4612 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004613
4614 return compressed;
4615}
4616
4617/*
4618 * Return TRUE when two nodes have identical siblings and children.
4619 */
4620 static int
4621node_equal(n1, n2)
4622 wordnode_T *n1;
4623 wordnode_T *n2;
4624{
4625 wordnode_T *p1;
4626 wordnode_T *p2;
4627
4628 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4629 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4630 if (p1->wn_byte != p2->wn_byte
4631 || (p1->wn_byte == NUL
4632 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004633 || p1->wn_region != p2->wn_region
4634 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004635 : (p1->wn_child != p2->wn_child)))
4636 break;
4637
4638 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004639}
4640
4641/*
4642 * Write a number to file "fd", MSB first, in "len" bytes.
4643 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004644 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004645put_bytes(fd, nr, len)
4646 FILE *fd;
4647 long_u nr;
4648 int len;
4649{
4650 int i;
4651
4652 for (i = len - 1; i >= 0; --i)
4653 putc((int)(nr >> (i * 8)), fd);
4654}
4655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656static int
4657#ifdef __BORLANDC__
4658_RTLENTRYF
4659#endif
4660rep_compare __ARGS((const void *s1, const void *s2));
4661
4662/*
4663 * Function given to qsort() to sort the REP items on "from" string.
4664 */
4665 static int
4666#ifdef __BORLANDC__
4667_RTLENTRYF
4668#endif
4669rep_compare(s1, s2)
4670 const void *s1;
4671 const void *s2;
4672{
4673 fromto_T *p1 = (fromto_T *)s1;
4674 fromto_T *p2 = (fromto_T *)s2;
4675
4676 return STRCMP(p1->ft_from, p2->ft_from);
4677}
4678
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004679/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004680 * Write the Vim spell file "fname".
4681 */
4682 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00004683write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004684 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004685 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004686{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004687 FILE *fd;
4688 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004689 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004690 wordnode_T *tree;
4691 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 int i;
4693 int l;
4694 garray_T *gap;
4695 fromto_T *ftp;
4696 char_u *p;
4697 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004698
Bram Moolenaarb765d632005-06-07 21:00:02 +00004699 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004700 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004701 {
4702 EMSG2(_(e_notopen), fname);
4703 return;
4704 }
4705
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004706 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004707 * <charflagslen> <charflags>
4708 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004709 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004710 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004711
4712 /* <fileID> */
4713 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
4714 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004715
4716 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004717 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004718 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004719 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
4720 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
4721 (size_t)1, fd);
4722 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 }
4724 else
4725 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004726 putc(0, fd);
4727 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004728 }
4729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 /*
4731 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004732 * <charflagslen> <charflags> <fcharlen> <fchars>
4733 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 * 'encoding'.
4735 * Also skip this for an .add.spl file, the main spell file must contain
4736 * the table (avoids that it conflicts). File is shorter too.
4737 */
4738 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004739 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004740 putc(0, fd);
4741 putc(0, fd);
4742 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004743 }
4744 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004745 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004746
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004747
4748 if (spin->si_midword == NULL)
4749 put_bytes(fd, 0L, 2); /* <midwordlen> */
4750 else
4751 {
4752 i = STRLEN(spin->si_midword);
4753 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
4754 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
4755 }
4756
4757
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004758 /* Write the prefix conditions. */
4759 write_spell_prefcond(fd, &spin->si_prefcond);
4760
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004761 /* <SUGGEST> : <repcount> <rep> ...
4762 * <salflags> <salcount> <sal> ...
4763 * <maplen> <mapstr> */
4764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 /* Sort the REP items. */
4766 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
4767 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004769 /* round 1: REP items
4770 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 for (round = 1; round <= 2; ++round)
4772 {
4773 if (round == 1)
4774 gap = &spin->si_rep;
4775 else
4776 {
4777 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 i = 0;
4780 if (spin->si_followup)
4781 i |= SAL_F0LLOWUP;
4782 if (spin->si_collapse)
4783 i |= SAL_COLLAPSE;
4784 if (spin->si_rem_accents)
4785 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004786 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4787 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004789 if (i & SAL_SOFO)
4790 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
4792
4793 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
4794 for (i = 0; i < gap->ga_len; ++i)
4795 {
4796 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
4797 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
4798 ftp = &((fromto_T *)gap->ga_data)[i];
4799 for (rr = 1; rr <= 2; ++rr)
4800 {
4801 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
4802 l = STRLEN(p);
4803 putc(l, fd);
4804 fwrite(p, l, (size_t)1, fd);
4805 }
4806 }
4807 }
4808
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004809 /* SOFOFROM and SOFOTO */
4810 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4811 {
4812 put_bytes(fd, 1L, 2); /* <salcount> */
4813
4814 l = STRLEN(spin->si_sofofr);
4815 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
4816 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
4817
4818 l = STRLEN(spin->si_sofoto);
4819 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
4820 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
4821 }
4822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
4824 if (spin->si_map.ga_len > 0) /* <mapstr> */
4825 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
4826 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00004827
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004828 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004829 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004830 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004832 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004833 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004834 if (round == 1)
4835 tree = spin->si_foldroot;
4836 else if (round == 2)
4837 tree = spin->si_keeproot;
4838 else
4839 tree = spin->si_prefroot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004840
Bram Moolenaar0c405862005-06-22 22:26:26 +00004841 /* Clear the index and wnode fields in the tree. */
4842 clear_node(tree);
4843
Bram Moolenaar51485f02005-06-04 21:55:20 +00004844 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00004845 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00004846 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004847 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004848
Bram Moolenaar51485f02005-06-04 21:55:20 +00004849 /* number of nodes in 4 bytes */
4850 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004851 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004852
Bram Moolenaar51485f02005-06-04 21:55:20 +00004853 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004854 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004855 }
4856
Bram Moolenaar51485f02005-06-04 21:55:20 +00004857 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004858}
4859
4860/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004861 * Clear the index and wnode fields of "node", it siblings and its
4862 * children. This is needed because they are a union with other items to save
4863 * space.
4864 */
4865 static void
4866clear_node(node)
4867 wordnode_T *node;
4868{
4869 wordnode_T *np;
4870
4871 if (node != NULL)
4872 for (np = node; np != NULL; np = np->wn_sibling)
4873 {
4874 np->wn_u1.index = 0;
4875 np->wn_u2.wnode = NULL;
4876
4877 if (np->wn_byte != NUL)
4878 clear_node(np->wn_child);
4879 }
4880}
4881
4882
4883/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004884 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004885 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00004886 * This first writes the list of possible bytes (siblings). Then for each
4887 * byte recursively write the children.
4888 *
4889 * NOTE: The code here must match the code in read_tree(), since assumptions
4890 * are made about the indexes (so that we don't have to write them in the
4891 * file).
4892 *
4893 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004894 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004895 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00004896put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004897 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004898 wordnode_T *node;
4899 int index;
4900 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004901 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004902{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004903 int newindex = index;
4904 int siblingcount = 0;
4905 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004907
Bram Moolenaar51485f02005-06-04 21:55:20 +00004908 /* If "node" is zero the tree is empty. */
4909 if (node == NULL)
4910 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004913 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004914
4915 /* Count the number of siblings. */
4916 for (np = node; np != NULL; np = np->wn_sibling)
4917 ++siblingcount;
4918
4919 /* Write the sibling count. */
4920 if (fd != NULL)
4921 putc(siblingcount, fd); /* <siblingcount> */
4922
4923 /* Write each sibling byte and optionally extra info. */
4924 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004925 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004926 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004927 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004928 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004929 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004930 /* For a NUL byte (end of word) write the flags etc. */
4931 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004932 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004933 /* In PREFIXTREE write the required prefixID and the
4934 * associated condition nr (stored in wn_region). */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004935 if (np->wn_flags == (char_u)-2)
4936 putc(BY_FLAGS, fd); /* <byte> rare */
4937 else
4938 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004939 putc(np->wn_prefixID, fd); /* <prefixID> */
4940 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004941 }
4942 else
4943 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004944 /* For word trees we write the flag/region items. */
4945 flags = np->wn_flags;
4946 if (regionmask != 0 && np->wn_region != regionmask)
4947 flags |= WF_REGION;
4948 if (np->wn_prefixID != 0)
4949 flags |= WF_PFX;
4950 if (flags == 0)
4951 {
4952 /* word without flags or region */
4953 putc(BY_NOFLAGS, fd); /* <byte> */
4954 }
4955 else
4956 {
4957 putc(BY_FLAGS, fd); /* <byte> */
4958 putc(flags, fd); /* <flags> */
4959 if (flags & WF_REGION)
4960 putc(np->wn_region, fd); /* <region> */
4961 if (flags & WF_PFX)
4962 putc(np->wn_prefixID, fd); /* <prefixID> */
4963 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004964 }
4965 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004966 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004967 else
4968 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004969 if (np->wn_child->wn_u1.index != 0
4970 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004971 {
4972 /* The child is written elsewhere, write the reference. */
4973 if (fd != NULL)
4974 {
4975 putc(BY_INDEX, fd); /* <byte> */
4976 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004977 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004978 }
4979 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004980 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004981 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004982 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004983
Bram Moolenaar51485f02005-06-04 21:55:20 +00004984 if (fd != NULL)
4985 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
4986 {
4987 EMSG(_(e_write));
4988 return 0;
4989 }
4990 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004991 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004992
4993 /* Space used in the array when reading: one for each sibling and one for
4994 * the count. */
4995 newindex += siblingcount + 1;
4996
4997 /* Recursively dump the children of each sibling. */
4998 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004999 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5000 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005001 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005002
5003 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005004}
5005
5006
5007/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005008 * ":mkspell [-ascii] outfile infile ..."
5009 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010 */
5011 void
5012ex_mkspell(eap)
5013 exarg_T *eap;
5014{
5015 int fcount;
5016 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005017 char_u *arg = eap->arg;
5018 int ascii = FALSE;
5019
5020 if (STRNCMP(arg, "-ascii", 6) == 0)
5021 {
5022 ascii = TRUE;
5023 arg = skipwhite(arg + 6);
5024 }
5025
5026 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5027 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005029 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005030 FreeWild(fcount, fnames);
5031 }
5032}
5033
5034/*
5035 * Create a Vim spell file from one or more word lists.
5036 * "fnames[0]" is the output file name.
5037 * "fnames[fcount - 1]" is the last input file name.
5038 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5039 * and ".spl" is appended to make the output file name.
5040 */
5041 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005042mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005043 int fcount;
5044 char_u **fnames;
5045 int ascii; /* -ascii argument given */
5046 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005047 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005048{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005049 char_u fname[MAXPATHL];
5050 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005051 char_u **innames;
5052 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005054 int i;
5055 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005056 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005057 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005058 spellinfo_T spin;
5059
5060 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005061 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005062 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005063 spin.si_followup = TRUE;
5064 spin.si_rem_accents = TRUE;
5065 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5066 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5067 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005068 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005069
Bram Moolenaarb765d632005-06-07 21:00:02 +00005070 /* default: fnames[0] is output file, following are input files */
5071 innames = &fnames[1];
5072 incount = fcount - 1;
5073
5074 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005075 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005076 len = STRLEN(fnames[0]);
5077 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5078 {
5079 /* For ":mkspell path/en.latin1.add" output file is
5080 * "path/en.latin1.add.spl". */
5081 innames = &fnames[0];
5082 incount = 1;
5083 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5084 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005085 else if (fcount == 1)
5086 {
5087 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5088 innames = &fnames[0];
5089 incount = 1;
5090 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5091 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5092 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005093 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5094 {
5095 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005096 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005097 }
5098 else
5099 /* Name should be language, make the file name from it. */
5100 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5101 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5102
5103 /* Check for .ascii.spl. */
5104 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5105 spin.si_ascii = TRUE;
5106
5107 /* Check for .add.spl. */
5108 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5109 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005110 }
5111
Bram Moolenaarb765d632005-06-07 21:00:02 +00005112 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005113 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005114 else if (vim_strchr(gettail(wfname), '_') != NULL)
5115 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005116 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005117 EMSG(_("E754: Only up to 8 regions supported"));
5118 else
5119 {
5120 /* Check for overwriting before doing things that may take a lot of
5121 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005122 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005123 {
5124 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005125 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005126 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005127 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005128 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005129 EMSG2(_(e_isadir2), wfname);
5130 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005131 }
5132
5133 /*
5134 * Init the aff and dic pointers.
5135 * Get the region names if there are more than 2 arguments.
5136 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005137 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005138 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005139 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005140
Bram Moolenaar3982c542005-06-08 21:56:31 +00005141 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005142 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005143 len = STRLEN(innames[i]);
5144 if (STRLEN(gettail(innames[i])) < 5
5145 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005146 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005147 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5148 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005149 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005150 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5151 spin.si_region_name[i * 2 + 1] =
5152 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005153 }
5154 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005155 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005156
Bram Moolenaar51485f02005-06-04 21:55:20 +00005157 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5158 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005159 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5160 if (spin.si_foldroot == NULL
5161 || spin.si_keeproot == NULL
5162 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005163 {
5164 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005165 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005166 }
5167
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005168 /* When not producing a .add.spl file clear the character table when
5169 * we encounter one in the .aff file. This means we dump the current
5170 * one in the .spl file if the .aff file doesn't define one. That's
5171 * better than guessing the contents, the table will match a
5172 * previously loaded spell file. */
5173 if (!spin.si_add)
5174 spin.si_clear_chartab = TRUE;
5175
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005176 /*
5177 * Read all the .aff and .dic files.
5178 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005179 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005180 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005181 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005182 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005183 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005184 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005185
Bram Moolenaarb765d632005-06-07 21:00:02 +00005186 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005187 if (mch_stat((char *)fname, &st) >= 0)
5188 {
5189 /* Read the .aff file. Will init "spin->si_conv" based on the
5190 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 afile[i] = spell_read_aff(fname, &spin);
5192 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005193 error = TRUE;
5194 else
5195 {
5196 /* Read the .dic file and store the words in the trees. */
5197 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005198 innames[i]);
5199 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005200 error = TRUE;
5201 }
5202 }
5203 else
5204 {
5205 /* No .aff file, try reading the file as a word list. Store
5206 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005207 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005208 error = TRUE;
5209 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005210
Bram Moolenaarb765d632005-06-07 21:00:02 +00005211#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005212 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005213 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005214#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005215 }
5216
Bram Moolenaar51485f02005-06-04 21:55:20 +00005217 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005218 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005219 /*
5220 * Remove the dummy NUL from the start of the tree root.
5221 */
5222 spin.si_foldroot = spin.si_foldroot->wn_sibling;
5223 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005224 spin.si_prefroot = spin.si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005225
5226 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005227 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005228 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005232 verbose_enter();
5233 MSG(_("Compressing word tree..."));
5234 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005236 verbose_leave();
5237 }
5238 wordtree_compress(spin.si_foldroot, &spin);
5239 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005240 wordtree_compress(spin.si_prefroot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005241 }
5242
Bram Moolenaar51485f02005-06-04 21:55:20 +00005243 if (!error)
5244 {
5245 /*
5246 * Write the info in the spell file.
5247 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005250 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005251 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005252 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005253 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005255 verbose_leave();
5256 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005257
Bram Moolenaar3982c542005-06-08 21:56:31 +00005258 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005263 verbose_enter();
5264 MSG(_("Done!"));
5265 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005266 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005267 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005269 verbose_leave();
5270 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005271
Bram Moolenaarb765d632005-06-07 21:00:02 +00005272 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005274 }
5275
5276 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 ga_clear(&spin.si_rep);
5278 ga_clear(&spin.si_sal);
5279 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005280 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005281 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005282 vim_free(spin.si_sofofr);
5283 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005284
5285 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005286 for (i = 0; i < incount; ++i)
5287 if (afile[i] != NULL)
5288 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005289
5290 /* Free all the bits and pieces at once. */
5291 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005292 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005293}
5294
Bram Moolenaarb765d632005-06-07 21:00:02 +00005295
5296/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005297 * ":[count]spellgood {word}"
5298 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005299 */
5300 void
5301ex_spell(eap)
5302 exarg_T *eap;
5303{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005304 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005305 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005306}
5307
5308/*
5309 * Add "word[len]" to 'spellfile' as a good or bad word.
5310 */
5311 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005312spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005313 char_u *word;
5314 int len;
5315 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005316 int index; /* "zG" and "zW": zero, otherwise index in
5317 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005318{
5319 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005320 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005321 int new_spf = FALSE;
5322 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005323 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005324 char_u fnamebuf[MAXPATHL];
5325 char_u line[MAXWLEN * 2];
5326 long fpos, fpos_next = 0;
5327 int i;
5328 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005329
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005330 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005331 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005332 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005333 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005334 int_wordlist = vim_tempname('s');
5335 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005336 return;
5337 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005338 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005339 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005340 else
5341 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005342 /* If 'spellfile' isn't set figure out a good default value. */
5343 if (*curbuf->b_p_spf == NUL)
5344 {
5345 init_spellfile();
5346 new_spf = TRUE;
5347 }
5348
5349 if (*curbuf->b_p_spf == NUL)
5350 {
5351 EMSG(_("E764: 'spellfile' is not set"));
5352 return;
5353 }
5354
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005355 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5356 {
5357 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5358 if (i == index)
5359 break;
5360 if (*spf == NUL)
5361 {
5362 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5363 return;
5364 }
5365 }
5366
Bram Moolenaarb765d632005-06-07 21:00:02 +00005367 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005368 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005369 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5370 buf = NULL;
5371 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005372 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005373 EMSG(_(e_bufloaded));
5374 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005375 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005376
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005377 fname = fnamebuf;
5378 }
5379
5380 if (bad)
5381 {
5382 /* When the word also appears as good word we need to remove that one,
5383 * since its flags sort before the one with WF_BANNED. */
5384 fd = mch_fopen((char *)fname, "r");
5385 if (fd != NULL)
5386 {
5387 while (!vim_fgets(line, MAXWLEN * 2, fd))
5388 {
5389 fpos = fpos_next;
5390 fpos_next = ftell(fd);
5391 if (STRNCMP(word, line, len) == 0
5392 && (line[len] == '/' || line[len] < ' '))
5393 {
5394 /* Found duplicate word. Remove it by writing a '#' at
5395 * the start of the line. Mixing reading and writing
5396 * doesn't work for all systems, close the file first. */
5397 fclose(fd);
5398 fd = mch_fopen((char *)fname, "r+");
5399 if (fd == NULL)
5400 break;
5401 if (fseek(fd, fpos, SEEK_SET) == 0)
5402 fputc('#', fd);
5403 fseek(fd, fpos_next, SEEK_SET);
5404 }
5405 }
5406 fclose(fd);
5407 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005408 }
5409
5410 fd = mch_fopen((char *)fname, "a");
5411 if (fd == NULL && new_spf)
5412 {
5413 /* We just initialized the 'spellfile' option and can't open the file.
5414 * We may need to create the "spell" directory first. We already
5415 * checked the runtime directory is writable in init_spellfile(). */
5416 STRCPY(NameBuff, fname);
5417 *gettail_sep(NameBuff) = NUL;
5418 if (mch_stat((char *)NameBuff, &st) < 0)
5419 {
5420 /* The directory doesn't exist. Try creating it and opening the
5421 * file again. */
5422 vim_mkdir(NameBuff, 0755);
5423 fd = mch_fopen((char *)fname, "a");
5424 }
5425 }
5426
5427 if (fd == NULL)
5428 EMSG2(_(e_notopen), fname);
5429 else
5430 {
5431 if (bad)
5432 fprintf(fd, "%.*s/!\n", len, word);
5433 else
5434 fprintf(fd, "%.*s\n", len, word);
5435 fclose(fd);
5436
5437 /* Update the .add.spl file. */
5438 mkspell(1, &fname, FALSE, TRUE, TRUE);
5439
5440 /* If the .add file is edited somewhere, reload it. */
5441 if (buf != NULL)
5442 buf_reload(buf);
5443
5444 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005445 }
5446}
5447
5448/*
5449 * Initialize 'spellfile' for the current buffer.
5450 */
5451 static void
5452init_spellfile()
5453{
5454 char_u buf[MAXPATHL];
5455 int l;
5456 slang_T *sl;
5457 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005458 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005459
5460 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5461 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005462 /* Find the end of the language name. Exclude the region. */
5463 for (lend = curbuf->b_p_spl; *lend != NUL
5464 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5465 ;
5466
5467 /* Loop over all entries in 'runtimepath'. Use the first one where we
5468 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005469 rtp = p_rtp;
5470 while (*rtp != NUL)
5471 {
5472 /* Copy the path from 'runtimepath' to buf[]. */
5473 copy_option_part(&rtp, buf, MAXPATHL, ",");
5474 if (filewritable(buf) == 2)
5475 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005476 /* Use the first language name from 'spelllang' and the
5477 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005478 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5479 l = STRLEN(buf);
5480 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005481 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005482 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005483 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5484 ? (char_u *)"ascii" : spell_enc());
5485 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5486 break;
5487 }
5488 }
5489 }
5490}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005491
Bram Moolenaar51485f02005-06-04 21:55:20 +00005492
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005493/*
5494 * Init the chartab used for spelling for ASCII.
5495 * EBCDIC is not supported!
5496 */
5497 static void
5498clear_spell_chartab(sp)
5499 spelltab_T *sp;
5500{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005501 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005502
5503 /* Init everything to FALSE. */
5504 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5505 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5506 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005507 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005508 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005509 sp->st_upper[i] = i;
5510 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005511
5512 /* We include digits. A word shouldn't start with a digit, but handling
5513 * that is done separately. */
5514 for (i = '0'; i <= '9'; ++i)
5515 sp->st_isw[i] = TRUE;
5516 for (i = 'A'; i <= 'Z'; ++i)
5517 {
5518 sp->st_isw[i] = TRUE;
5519 sp->st_isu[i] = TRUE;
5520 sp->st_fold[i] = i + 0x20;
5521 }
5522 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005523 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005524 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005525 sp->st_upper[i] = i - 0x20;
5526 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005527}
5528
5529/*
5530 * Init the chartab used for spelling. Only depends on 'encoding'.
5531 * Called once while starting up and when 'encoding' changes.
5532 * The default is to use isalpha(), but the spell file should define the word
5533 * characters to make it possible that 'encoding' differs from the current
5534 * locale.
5535 */
5536 void
5537init_spell_chartab()
5538{
5539 int i;
5540
5541 did_set_spelltab = FALSE;
5542 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005543#ifdef FEAT_MBYTE
5544 if (enc_dbcs)
5545 {
5546 /* DBCS: assume double-wide characters are word characters. */
5547 for (i = 128; i <= 255; ++i)
5548 if (MB_BYTE2LEN(i) == 2)
5549 spelltab.st_isw[i] = TRUE;
5550 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005551 else if (enc_utf8)
5552 {
5553 for (i = 128; i < 256; ++i)
5554 {
5555 spelltab.st_isu[i] = utf_isupper(i);
5556 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
5557 spelltab.st_fold[i] = utf_fold(i);
5558 spelltab.st_upper[i] = utf_toupper(i);
5559 }
5560 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005561 else
5562#endif
5563 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005564 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005565 for (i = 128; i < 256; ++i)
5566 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005567 if (MB_ISUPPER(i))
5568 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005569 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005570 spelltab.st_isu[i] = TRUE;
5571 spelltab.st_fold[i] = MB_TOLOWER(i);
5572 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005573 else if (MB_ISLOWER(i))
5574 {
5575 spelltab.st_isw[i] = TRUE;
5576 spelltab.st_upper[i] = MB_TOUPPER(i);
5577 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005578 }
5579 }
5580}
5581
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005582static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
5583static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
5584
5585/*
5586 * Set the spell character tables from strings in the affix file.
5587 */
5588 static int
5589set_spell_chartab(fol, low, upp)
5590 char_u *fol;
5591 char_u *low;
5592 char_u *upp;
5593{
5594 /* We build the new tables here first, so that we can compare with the
5595 * previous one. */
5596 spelltab_T new_st;
5597 char_u *pf = fol, *pl = low, *pu = upp;
5598 int f, l, u;
5599
5600 clear_spell_chartab(&new_st);
5601
5602 while (*pf != NUL)
5603 {
5604 if (*pl == NUL || *pu == NUL)
5605 {
5606 EMSG(_(e_affform));
5607 return FAIL;
5608 }
5609#ifdef FEAT_MBYTE
5610 f = mb_ptr2char_adv(&pf);
5611 l = mb_ptr2char_adv(&pl);
5612 u = mb_ptr2char_adv(&pu);
5613#else
5614 f = *pf++;
5615 l = *pl++;
5616 u = *pu++;
5617#endif
5618 /* Every character that appears is a word character. */
5619 if (f < 256)
5620 new_st.st_isw[f] = TRUE;
5621 if (l < 256)
5622 new_st.st_isw[l] = TRUE;
5623 if (u < 256)
5624 new_st.st_isw[u] = TRUE;
5625
5626 /* if "LOW" and "FOL" are not the same the "LOW" char needs
5627 * case-folding */
5628 if (l < 256 && l != f)
5629 {
5630 if (f >= 256)
5631 {
5632 EMSG(_(e_affrange));
5633 return FAIL;
5634 }
5635 new_st.st_fold[l] = f;
5636 }
5637
5638 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005639 * case-folding, it's upper case and the "UPP" is the upper case of
5640 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005641 if (u < 256 && u != f)
5642 {
5643 if (f >= 256)
5644 {
5645 EMSG(_(e_affrange));
5646 return FAIL;
5647 }
5648 new_st.st_fold[u] = f;
5649 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005650 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005651 }
5652 }
5653
5654 if (*pl != NUL || *pu != NUL)
5655 {
5656 EMSG(_(e_affform));
5657 return FAIL;
5658 }
5659
5660 return set_spell_finish(&new_st);
5661}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005662
5663/*
5664 * Set the spell character tables from strings in the .spl file.
5665 */
5666 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005667set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005668 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005669 int cnt; /* length of "flags" */
5670 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005671{
5672 /* We build the new tables here first, so that we can compare with the
5673 * previous one. */
5674 spelltab_T new_st;
5675 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005676 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005677 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005678
5679 clear_spell_chartab(&new_st);
5680
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005681 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005682 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005683 if (i < cnt)
5684 {
5685 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
5686 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
5687 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005688
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005689 if (*p != NUL)
5690 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005691#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005692 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005693#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005694 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005695#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005696 new_st.st_fold[i + 128] = c;
5697 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
5698 new_st.st_upper[c] = i + 128;
5699 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005700 }
5701
5702 return set_spell_finish(&new_st);
5703}
5704
5705 static int
5706set_spell_finish(new_st)
5707 spelltab_T *new_st;
5708{
5709 int i;
5710
5711 if (did_set_spelltab)
5712 {
5713 /* check that it's the same table */
5714 for (i = 0; i < 256; ++i)
5715 {
5716 if (spelltab.st_isw[i] != new_st->st_isw[i]
5717 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005718 || spelltab.st_fold[i] != new_st->st_fold[i]
5719 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005720 {
5721 EMSG(_("E763: Word characters differ between spell files"));
5722 return FAIL;
5723 }
5724 }
5725 }
5726 else
5727 {
5728 /* copy the new spelltab into the one being used */
5729 spelltab = *new_st;
5730 did_set_spelltab = TRUE;
5731 }
5732
5733 return OK;
5734}
5735
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005736/*
Bram Moolenaarea408852005-06-25 22:49:46 +00005737 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005738 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00005739 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005740 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00005741 */
5742 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005743spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00005744 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005745 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00005746{
Bram Moolenaarea408852005-06-25 22:49:46 +00005747#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005748 char_u *s;
5749 int l;
5750 int c;
5751
5752 if (has_mbyte)
5753 {
5754 l = MB_BYTE2LEN(*p);
5755 s = p;
5756 if (l == 1)
5757 {
5758 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005759 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005760 {
5761 s = p + 1; /* skip a mid-word character */
5762 l = MB_BYTE2LEN(*s);
5763 }
5764 }
5765 else
5766 {
5767 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005768 if (c < 256 ? buf->b_spell_ismw[c]
5769 : (buf->b_spell_ismw_mb != NULL
5770 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005771 {
5772 s = p + l;
5773 l = MB_BYTE2LEN(*s);
5774 }
5775 }
5776
5777 if (l > 1)
5778 return mb_get_class(s) >= 2;
5779 return spelltab.st_isw[*s];
5780 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005781#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005782
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005783 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
5784}
5785
5786/*
5787 * Return TRUE if "p" points to a word character.
5788 * Unlike spell_iswordp() this doesn't check for "midword" characters.
5789 */
5790 static int
5791spell_iswordp_nmw(p)
5792 char_u *p;
5793{
5794#ifdef FEAT_MBYTE
5795 if (has_mbyte && MB_BYTE2LEN(*p) > 1)
5796 return mb_get_class(p) >= 2;
5797#endif
5798
5799 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00005800}
5801
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005802#ifdef FEAT_MBYTE
5803/*
5804 * Return TRUE if "p" points to a word character.
5805 * Wide version of spell_iswordp().
5806 */
5807 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005808spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005809 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005810 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005811{
5812 int *s;
5813
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005814 if (*p < 256 ? buf->b_spell_ismw[*p]
5815 : (buf->b_spell_ismw_mb != NULL
5816 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005817 s = p + 1;
5818 else
5819 s = p;
5820
5821 if (mb_char2len(*s) > 1)
5822 {
5823 if (enc_utf8)
5824 return utf_class(*s) >= 2;
5825 if (enc_dbcs)
5826 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
5827 return 0;
5828 }
5829 return spelltab.st_isw[*s];
5830}
5831#endif
5832
Bram Moolenaarea408852005-06-25 22:49:46 +00005833/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005834 * Write the table with prefix conditions to the .spl file.
5835 */
5836 static void
5837write_spell_prefcond(fd, gap)
5838 FILE *fd;
5839 garray_T *gap;
5840{
5841 int i;
5842 char_u *p;
5843 int len;
5844
5845 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
5846
5847 for (i = 0; i < gap->ga_len; ++i)
5848 {
5849 /* <prefcond> : <condlen> <condstr> */
5850 p = ((char_u **)gap->ga_data)[i];
5851 if (p == NULL)
5852 fputc(0, fd);
5853 else
5854 {
5855 len = STRLEN(p);
5856 fputc(len, fd);
5857 fwrite(p, (size_t)len, (size_t)1, fd);
5858 }
5859 }
5860}
5861
5862/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005863 * Write the current tables into the .spl file.
5864 * This makes sure the same characters are recognized as word characters when
5865 * generating an when using a spell file.
5866 */
5867 static void
5868write_spell_chartab(fd)
5869 FILE *fd;
5870{
5871 char_u charbuf[256 * 4];
5872 int len = 0;
5873 int flags;
5874 int i;
5875
5876 fputc(128, fd); /* <charflagslen> */
5877 for (i = 128; i < 256; ++i)
5878 {
5879 flags = 0;
5880 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005881 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005882 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005883 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005884 fputc(flags, fd); /* <charflags> */
5885
Bram Moolenaarb765d632005-06-07 21:00:02 +00005886#ifdef FEAT_MBYTE
5887 if (has_mbyte)
5888 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
5889 else
5890#endif
5891 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005892 }
5893
5894 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
5895 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
5896}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005897
5898/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005899 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
5900 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005901 * When using a multi-byte 'encoding' the length may change!
5902 * Returns FAIL when something wrong.
5903 */
5904 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005905spell_casefold(str, len, buf, buflen)
5906 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005907 int len;
5908 char_u *buf;
5909 int buflen;
5910{
5911 int i;
5912
5913 if (len >= buflen)
5914 {
5915 buf[0] = NUL;
5916 return FAIL; /* result will not fit */
5917 }
5918
5919#ifdef FEAT_MBYTE
5920 if (has_mbyte)
5921 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005922 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005923 char_u *p;
5924 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005925
5926 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005927 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005928 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005929 if (outi + MB_MAXBYTES > buflen)
5930 {
5931 buf[outi] = NUL;
5932 return FAIL;
5933 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005934 c = mb_ptr2char_adv(&p);
5935 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005936 }
5937 buf[outi] = NUL;
5938 }
5939 else
5940#endif
5941 {
5942 /* Be quick for non-multibyte encodings. */
5943 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005944 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005945 buf[i] = NUL;
5946 }
5947
5948 return OK;
5949}
5950
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005951#define SPS_BEST 1
5952#define SPS_FAST 2
5953#define SPS_DOUBLE 4
5954
5955static int sps_flags = SPS_BEST;
5956
5957/*
5958 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
5959 * Sets "sps_flags".
5960 */
5961 int
5962spell_check_sps()
5963{
5964 char_u *p;
5965 char_u buf[MAXPATHL];
5966 int f;
5967
5968 sps_flags = 0;
5969
5970 for (p = p_sps; *p != NUL; )
5971 {
5972 copy_option_part(&p, buf, MAXPATHL, ",");
5973
5974 f = 0;
5975 if (STRCMP(buf, "best") == 0)
5976 f = SPS_BEST;
5977 else if (STRCMP(buf, "fast") == 0)
5978 f = SPS_FAST;
5979 else if (STRCMP(buf, "double") == 0)
5980 f = SPS_DOUBLE;
5981 else if (STRNCMP(buf, "expr:", 5) != 0
5982 && STRNCMP(buf, "file:", 5) != 0)
5983 f = -1;
5984
5985 if (f == -1 || (sps_flags != 0 && f != 0))
5986 {
5987 sps_flags = SPS_BEST;
5988 return FAIL;
5989 }
5990 if (f != 0)
5991 sps_flags = f;
5992 }
5993
5994 if (sps_flags == 0)
5995 sps_flags = SPS_BEST;
5996
5997 return OK;
5998}
5999
6000/* Remember what "z?" replaced. */
6001static char_u *repl_from = NULL;
6002static char_u *repl_to = NULL;
6003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006004/*
6005 * "z?": Find badly spelled word under or after the cursor.
6006 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006007 */
6008 void
6009spell_suggest()
6010{
6011 char_u *line;
6012 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006013 char_u wcopy[MAXWLEN + 2];
6014 char_u *p;
6015 int i;
6016 int c;
6017 suginfo_T sug;
6018 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006019 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006020 int need_cap;
6021 regmatch_T regmatch;
6022 int endcol;
6023 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006024
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006025 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006026 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6027 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006028 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006029 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6030 return;
6031
6032 /* No bad word or it starts after the cursor: use the word under the
6033 * cursor. */
6034 curwin->w_cursor = prev_cursor;
6035 line = ml_get_curline();
6036 p = line + curwin->w_cursor.col;
6037 /* Backup to before start of word. */
6038 while (p > line && SPELL_ISWORDP(p))
6039 mb_ptr_back(line, p);
6040 /* Forward to start of word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006041 while (*p != NUL && !SPELL_ISWORDP(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006042 mb_ptr_adv(p);
6043
6044 if (!SPELL_ISWORDP(p)) /* No word found. */
6045 {
6046 beep_flush();
6047 return;
6048 }
6049 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006050 }
6051
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006052 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006053 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006054
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006055 /* Figure out if the word should be capitalised. */
6056 need_cap = FALSE;
6057 if (curbuf->b_cap_prog != NULL)
6058 {
6059 endcol = 0;
6060 if (skipwhite(line) - line == curwin->w_cursor.col)
6061 {
6062 /* At start of line, check if previous line is empty or sentence
6063 * ends there. */
6064 if (curwin->w_cursor.lnum == 1)
6065 need_cap = TRUE;
6066 else
6067 {
6068 line = ml_get(curwin->w_cursor.lnum - 1);
6069 if (*skipwhite(line) == NUL)
6070 need_cap = TRUE;
6071 else
6072 {
6073 /* Append a space in place of the line break. */
6074 line_copy = concat_str(line, (char_u *)" ");
6075 line = line_copy;
6076 endcol = STRLEN(line);
6077 }
6078 }
6079 }
6080 else
6081 endcol = curwin->w_cursor.col;
6082
6083 if (endcol > 0)
6084 {
6085 /* Check if sentence ends before the bad word. */
6086 regmatch.regprog = curbuf->b_cap_prog;
6087 regmatch.rm_ic = FALSE;
6088 p = line + endcol;
6089 for (;;)
6090 {
6091 mb_ptr_back(line, p);
6092 if (p == line || SPELL_ISWORDP(p))
6093 break;
6094 if (vim_regexec(&regmatch, p, 0)
6095 && regmatch.endp[0] == line + endcol)
6096 {
6097 need_cap = TRUE;
6098 break;
6099 }
6100 }
6101 }
6102
6103 /* get the line again, we may have been using the previous one */
6104 line = ml_get_curline();
6105 vim_free(line_copy);
6106 }
6107
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006108 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006109 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6110 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006111
6112 if (sug.su_ga.ga_len == 0)
6113 MSG(_("Sorry, no suggestions"));
6114 else
6115 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006116 vim_free(repl_from);
6117 repl_from = NULL;
6118 vim_free(repl_to);
6119 repl_to = NULL;
6120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006121 /* List the suggestions. */
6122 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006123 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006124 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6125 sug.su_badlen, sug.su_badptr);
6126 msg_puts(IObuff);
6127 msg_clr_eos();
6128 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006130 msg_scroll = TRUE;
6131 for (i = 0; i < sug.su_ga.ga_len; ++i)
6132 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006133 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006134
6135 /* The suggested word may replace only part of the bad word, add
6136 * the not replaced part. */
6137 STRCPY(wcopy, stp->st_word);
6138 if (sug.su_badlen > stp->st_orglen)
6139 vim_strncpy(wcopy + STRLEN(wcopy),
6140 sug.su_badptr + stp->st_orglen,
6141 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006142 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6143 msg_puts(IObuff);
6144
6145 /* The word may replace more than "su_badlen". */
6146 if (sug.su_badlen < stp->st_orglen)
6147 {
6148 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6149 stp->st_orglen, sug.su_badptr);
6150 msg_puts(IObuff);
6151 }
6152
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006153 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006154 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006155 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006156 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006157 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006158 stp->st_salscore ? "s " : "",
6159 stp->st_score, stp->st_altscore);
6160 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006161 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6162 stp->st_score);
6163 msg_advance(30);
6164 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006166 msg_putchar('\n');
6167 }
6168
6169 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006170 i = prompt_for_number(&mouse_used);
6171 if (mouse_used)
6172 i -= lines_left;
6173
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006174 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006175 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006176 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006177 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006178 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6179 repl_to = vim_strsave(stp->st_word);
6180
6181 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006182 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6183 if (p != NULL)
6184 {
6185 c = sug.su_badptr - line;
6186 mch_memmove(p, line, c);
6187 STRCPY(p + c, stp->st_word);
6188 STRCAT(p, sug.su_badptr + stp->st_orglen);
6189 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6190 curwin->w_cursor.col = c;
6191 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006192
6193 /* For redo we use a change-word command. */
6194 ResetRedobuff();
6195 AppendToRedobuff((char_u *)"ciw");
6196 AppendToRedobuff(stp->st_word);
6197 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006198 }
6199 }
6200 else
6201 curwin->w_cursor = prev_cursor;
6202 }
6203
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006204 spell_find_cleanup(&sug);
6205}
6206
6207/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006208 * ":spellrepall"
6209 */
6210/*ARGSUSED*/
6211 void
6212ex_spellrepall(eap)
6213 exarg_T *eap;
6214{
6215 pos_T pos = curwin->w_cursor;
6216 char_u *frompat;
6217 int addlen;
6218 char_u *line;
6219 char_u *p;
6220 int didone = FALSE;
6221 int save_ws = p_ws;
6222
6223 if (repl_from == NULL || repl_to == NULL)
6224 {
6225 EMSG(_("E752: No previous spell replacement"));
6226 return;
6227 }
6228 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6229
6230 frompat = alloc(STRLEN(repl_from) + 7);
6231 if (frompat == NULL)
6232 return;
6233 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6234 p_ws = FALSE;
6235
6236 curwin->w_cursor.lnum = 0;
6237 while (!got_int)
6238 {
6239 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6240 || u_save_cursor() == FAIL)
6241 break;
6242
6243 /* Only replace when the right word isn't there yet. This happens
6244 * when changing "etc" to "etc.". */
6245 line = ml_get_curline();
6246 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6247 repl_to, STRLEN(repl_to)) != 0)
6248 {
6249 p = alloc(STRLEN(line) + addlen + 1);
6250 if (p == NULL)
6251 break;
6252 mch_memmove(p, line, curwin->w_cursor.col);
6253 STRCPY(p + curwin->w_cursor.col, repl_to);
6254 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6255 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6256 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6257 didone = TRUE;
6258 }
6259 curwin->w_cursor.col += STRLEN(repl_to);
6260 }
6261
6262 p_ws = save_ws;
6263 curwin->w_cursor = pos;
6264 vim_free(frompat);
6265
6266 if (!didone)
6267 EMSG2(_("E753: Not found: %s"), repl_from);
6268}
6269
6270/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006271 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6272 * a list of allocated strings.
6273 */
6274 void
6275spell_suggest_list(gap, word, maxcount)
6276 garray_T *gap;
6277 char_u *word;
6278 int maxcount; /* maximum nr of suggestions */
6279{
6280 suginfo_T sug;
6281 int i;
6282 suggest_T *stp;
6283 char_u *wcopy;
6284
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006285 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006286
6287 /* Make room in "gap". */
6288 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6289 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6290 return;
6291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006292 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006293 {
6294 stp = &SUG(sug.su_ga, i);
6295
6296 /* The suggested word may replace only part of "word", add the not
6297 * replaced part. */
6298 wcopy = alloc(STRLEN(stp->st_word)
6299 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6300 if (wcopy == NULL)
6301 break;
6302 STRCPY(wcopy, stp->st_word);
6303 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6304 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6305 }
6306
6307 spell_find_cleanup(&sug);
6308}
6309
6310/*
6311 * Find spell suggestions for the word at the start of "badptr".
6312 * Return the suggestions in "su->su_ga".
6313 * The maximum number of suggestions is "maxcount".
6314 * Note: does use info for the current window.
6315 * This is based on the mechanisms of Aspell, but completely reimplemented.
6316 */
6317 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006318spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006319 char_u *badptr;
6320 suginfo_T *su;
6321 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006322 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006323 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006324{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006325 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006326 char_u buf[MAXPATHL];
6327 char_u *p;
6328 int do_combine = FALSE;
6329 char_u *sps_copy;
6330#ifdef FEAT_EVAL
6331 static int expr_busy = FALSE;
6332#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006333 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006334
6335 /*
6336 * Set the info in "*su".
6337 */
6338 vim_memset(su, 0, sizeof(suginfo_T));
6339 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6340 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006341 if (*badptr == NUL)
6342 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006343 hash_init(&su->su_banned);
6344
6345 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006346 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006347 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006348 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006349
6350 if (su->su_badlen >= MAXWLEN)
6351 su->su_badlen = MAXWLEN - 1; /* just in case */
6352 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6353 (void)spell_casefold(su->su_badptr, su->su_badlen,
6354 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006355 /* get caps flags for bad word */
6356 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006357 if (need_cap)
6358 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006359
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006360 /* If the word is not capitalised and spell_check() doesn't consider the
6361 * word to be bad then it might need to be capitalised. Add a suggestion
6362 * for that. */
6363#ifdef FEAT_MBYTE
6364 c = mb_ptr2char(su->su_badptr);
6365#else
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006366 c = *su->su_badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006367#endif
6368 if (!SPELL_ISUPPER(c) && attr == 0)
6369 {
6370 make_case_word(su->su_badword, buf, WF_ONECAP);
6371 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6372 0, TRUE);
6373 }
6374
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006375 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006376 if (banbadword)
6377 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006378
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006379 /* Make a copy of 'spellsuggest', because the expression may change it. */
6380 sps_copy = vim_strsave(p_sps);
6381 if (sps_copy == NULL)
6382 return;
6383
6384 /* Loop over the items in 'spellsuggest'. */
6385 for (p = sps_copy; *p != NUL; )
6386 {
6387 copy_option_part(&p, buf, MAXPATHL, ",");
6388
6389 if (STRNCMP(buf, "expr:", 5) == 0)
6390 {
6391#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006392 /* Evaluate an expression. Skip this when called recursively,
6393 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006394 if (!expr_busy)
6395 {
6396 expr_busy = TRUE;
6397 spell_suggest_expr(su, buf + 5);
6398 expr_busy = FALSE;
6399 }
6400#endif
6401 }
6402 else if (STRNCMP(buf, "file:", 5) == 0)
6403 /* Use list of suggestions in a file. */
6404 spell_suggest_file(su, buf + 5);
6405 else
6406 {
6407 /* Use internal method. */
6408 spell_suggest_intern(su);
6409 if (sps_flags & SPS_DOUBLE)
6410 do_combine = TRUE;
6411 }
6412 }
6413
6414 vim_free(sps_copy);
6415
6416 if (do_combine)
6417 /* Combine the two list of suggestions. This must be done last,
6418 * because sorting changes the order again. */
6419 score_combine(su);
6420}
6421
6422#ifdef FEAT_EVAL
6423/*
6424 * Find suggestions by evaluating expression "expr".
6425 */
6426 static void
6427spell_suggest_expr(su, expr)
6428 suginfo_T *su;
6429 char_u *expr;
6430{
6431 list_T *list;
6432 listitem_T *li;
6433 int score;
6434 char_u *p;
6435
6436 /* The work is split up in a few parts to avoid having to export
6437 * suginfo_T.
6438 * First evaluate the expression and get the resulting list. */
6439 list = eval_spell_expr(su->su_badword, expr);
6440 if (list != NULL)
6441 {
6442 /* Loop over the items in the list. */
6443 for (li = list->lv_first; li != NULL; li = li->li_next)
6444 if (li->li_tv.v_type == VAR_LIST)
6445 {
6446 /* Get the word and the score from the items. */
6447 score = get_spellword(li->li_tv.vval.v_list, &p);
6448 if (score >= 0)
6449 add_suggestion(su, &su->su_ga, p,
6450 su->su_badlen, score, 0, TRUE);
6451 }
6452 list_unref(list);
6453 }
6454
6455 /* Sort the suggestions and truncate at "maxcount". */
6456 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6457}
6458#endif
6459
6460/*
6461 * Find suggestions a file "fname".
6462 */
6463 static void
6464spell_suggest_file(su, fname)
6465 suginfo_T *su;
6466 char_u *fname;
6467{
6468 FILE *fd;
6469 char_u line[MAXWLEN * 2];
6470 char_u *p;
6471 int len;
6472 char_u cword[MAXWLEN];
6473
6474 /* Open the file. */
6475 fd = mch_fopen((char *)fname, "r");
6476 if (fd == NULL)
6477 {
6478 EMSG2(_(e_notopen), fname);
6479 return;
6480 }
6481
6482 /* Read it line by line. */
6483 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6484 {
6485 line_breakcheck();
6486
6487 p = vim_strchr(line, '/');
6488 if (p == NULL)
6489 continue; /* No Tab found, just skip the line. */
6490 *p++ = NUL;
6491 if (STRICMP(su->su_badword, line) == 0)
6492 {
6493 /* Match! Isolate the good word, until CR or NL. */
6494 for (len = 0; p[len] >= ' '; ++len)
6495 ;
6496 p[len] = NUL;
6497
6498 /* If the suggestion doesn't have specific case duplicate the case
6499 * of the bad word. */
6500 if (captype(p, NULL) == 0)
6501 {
6502 make_case_word(p, cword, su->su_badflags);
6503 p = cword;
6504 }
6505
6506 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6507 SCORE_FILE, 0, TRUE);
6508 }
6509 }
6510
6511 fclose(fd);
6512
6513 /* Sort the suggestions and truncate at "maxcount". */
6514 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6515}
6516
6517/*
6518 * Find suggestions for the internal method indicated by "sps_flags".
6519 */
6520 static void
6521spell_suggest_intern(su)
6522 suginfo_T *su;
6523{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006524 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006525 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006526 *
6527 * Set a maximum score to limit the combination of operations that is
6528 * tried.
6529 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006530 suggest_try_special(su);
6531
6532 /*
6533 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6534 * from the .aff file and inserting a space (split the word).
6535 */
6536 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006537
6538 /* For the resulting top-scorers compute the sound-a-like score. */
6539 if (sps_flags & SPS_DOUBLE)
6540 score_comp_sal(su);
6541
6542 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006543 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006544 *
6545 * Only do this when we don't have a lot of suggestions yet, because it's
6546 * very slow and often doesn't find new suggestions.
6547 */
6548 if ((sps_flags & SPS_DOUBLE)
6549 || (!(sps_flags & SPS_FAST)
6550 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
6551 {
6552 /* Allow a higher score now. */
6553 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006554 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006555 }
6556
6557 /* When CTRL-C was hit while searching do show the results. */
6558 ui_breakcheck();
6559 if (got_int)
6560 {
6561 (void)vgetc();
6562 got_int = FALSE;
6563 }
6564
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006565 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006566 {
6567 if (sps_flags & SPS_BEST)
6568 /* Adjust the word score for how it sounds like. */
6569 rescore_suggestions(su);
6570
6571 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006572 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006573 }
6574}
6575
6576/*
6577 * Free the info put in "*su" by spell_find_suggest().
6578 */
6579 static void
6580spell_find_cleanup(su)
6581 suginfo_T *su;
6582{
6583 int i;
6584
6585 /* Free the suggestions. */
6586 for (i = 0; i < su->su_ga.ga_len; ++i)
6587 vim_free(SUG(su->su_ga, i).st_word);
6588 ga_clear(&su->su_ga);
6589 for (i = 0; i < su->su_sga.ga_len; ++i)
6590 vim_free(SUG(su->su_sga, i).st_word);
6591 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006592
6593 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006594 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006595}
6596
6597/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006598 * Make a copy of "word", with the first letter upper or lower cased, to
6599 * "wcopy[MAXWLEN]". "word" must not be empty.
6600 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006601 */
6602 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006603onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006604 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006605 char_u *wcopy;
6606 int upper; /* TRUE: first letter made upper case */
6607{
6608 char_u *p;
6609 int c;
6610 int l;
6611
6612 p = word;
6613#ifdef FEAT_MBYTE
6614 if (has_mbyte)
6615 c = mb_ptr2char_adv(&p);
6616 else
6617#endif
6618 c = *p++;
6619 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006620 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006621 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006622 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006623#ifdef FEAT_MBYTE
6624 if (has_mbyte)
6625 l = mb_char2bytes(c, wcopy);
6626 else
6627#endif
6628 {
6629 l = 1;
6630 wcopy[0] = c;
6631 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006632 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006633}
6634
6635/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006636 * Make a copy of "word" with all the letters upper cased into
6637 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006638 */
6639 static void
6640allcap_copy(word, wcopy)
6641 char_u *word;
6642 char_u *wcopy;
6643{
6644 char_u *s;
6645 char_u *d;
6646 int c;
6647
6648 d = wcopy;
6649 for (s = word; *s != NUL; )
6650 {
6651#ifdef FEAT_MBYTE
6652 if (has_mbyte)
6653 c = mb_ptr2char_adv(&s);
6654 else
6655#endif
6656 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006657 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006658
6659#ifdef FEAT_MBYTE
6660 if (has_mbyte)
6661 {
6662 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
6663 break;
6664 d += mb_char2bytes(c, d);
6665 }
6666 else
6667#endif
6668 {
6669 if (d - wcopy >= MAXWLEN - 1)
6670 break;
6671 *d++ = c;
6672 }
6673 }
6674 *d = NUL;
6675}
6676
6677/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006678 * Try finding suggestions by recognizing specific situations.
6679 */
6680 static void
6681suggest_try_special(su)
6682 suginfo_T *su;
6683{
6684 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006685 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006686 int c;
6687 char_u word[MAXWLEN];
6688
6689 /*
6690 * Recognize a word that is repeated: "the the".
6691 */
6692 p = skiptowhite(su->su_fbadword);
6693 len = p - su->su_fbadword;
6694 p = skipwhite(p);
6695 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
6696 {
6697 /* Include badflags: if the badword is onecap or allcap
6698 * use that for the goodword too: "The the" -> "The". */
6699 c = su->su_fbadword[len];
6700 su->su_fbadword[len] = NUL;
6701 make_case_word(su->su_fbadword, word, su->su_badflags);
6702 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006703 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006704 }
6705}
6706
6707/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00006709 *
6710 * This uses a state machine. At each node in the tree we try various
6711 * operations. When trying if an operation work "depth" is increased and the
6712 * stack[] is used to store info. This allows combinations, thus insert one
6713 * character, replace one and delete another. The number of changes is
6714 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006715 */
6716 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00006717suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718 suginfo_T *su;
6719{
6720 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
6721 char_u tword[MAXWLEN]; /* good word collected so far */
6722 trystate_T stack[MAXWLEN];
6723 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
6724 * to for word split) */
6725 char_u prewordlen = 0; /* length of word in "preword" */
6726 int splitoff = 0; /* index in tword after last split */
6727 trystate_T *sp;
6728 int newscore;
6729 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006730 char_u *byts, *fbyts, *pbyts;
6731 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006732 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00006733 int c, c2, c3;
6734 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006735 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006736 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006737 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006738 int len;
6739 char_u *p;
6740 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00006741 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006742 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006743
6744 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00006745 * to find matches (esp. REP items). Append some more text, changing
6746 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006747 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006748 n = STRLEN(fword);
6749 p = su->su_badptr + su->su_badlen;
6750 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006751
6752 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
6753 lp->lp_slang != NULL; ++lp)
6754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006755 /*
6756 * Go through the whole case-fold tree, try changes at each node.
6757 * "tword[]" contains the word collected from nodes in the tree.
6758 * "fword[]" the word we are trying to match with (initially the bad
6759 * word).
6760 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006761 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006762 sp = &stack[0];
6763 sp->ts_state = STATE_START;
6764 sp->ts_score = 0;
6765 sp->ts_curi = 1;
6766 sp->ts_fidx = 0;
6767 sp->ts_fidxtry = 0;
6768 sp->ts_twordlen = 0;
6769 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006770#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006771 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00006772#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006773
Bram Moolenaarea424162005-06-16 21:51:00 +00006774 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006775 * When there are postponed prefixes we need to use these first. At
6776 * the end of the prefix we continue in the case-fold tree.
6777 */
6778 fbyts = lp->lp_slang->sl_fbyts;
6779 fidxs = lp->lp_slang->sl_fidxs;
6780 pbyts = lp->lp_slang->sl_pbyts;
6781 pidxs = lp->lp_slang->sl_pidxs;
6782 if (pbyts != NULL)
6783 {
6784 byts = pbyts;
6785 idxs = pidxs;
6786 sp->ts_prefixdepth = PREFIXTREE;
6787 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
6788 }
6789 else
6790 {
6791 byts = fbyts;
6792 idxs = fidxs;
6793 sp->ts_prefixdepth = NOPREFIX;
6794 }
6795
6796 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00006797 * Loop to find all suggestions. At each round we either:
6798 * - For the current state try one operation, advance "ts_curi",
6799 * increase "depth".
6800 * - When a state is done go to the next, set "ts_state".
6801 * - When all states are tried decrease "depth".
6802 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 while (depth >= 0 && !got_int)
6804 {
6805 sp = &stack[depth];
6806 switch (sp->ts_state)
6807 {
6808 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006809 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006810 /*
6811 * Start of node: Deal with NUL bytes, which means
6812 * tword[] may end here.
6813 */
6814 arridx = sp->ts_arridx; /* current node in the tree */
6815 len = byts[arridx]; /* bytes in this node */
6816 arridx += sp->ts_curi; /* index of current byte */
6817
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006818 if (sp->ts_prefixdepth == PREFIXTREE)
6819 {
6820 /* Skip over the NUL bytes, we use them later. */
6821 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
6822 ;
6823 sp->ts_curi += n;
6824
6825 /* At end of a prefix or at start of prefixtree: check for
6826 * following word. */
6827 if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX)
6828 {
6829 sp->ts_state = STATE_START;
6830 ++depth;
6831 stack[depth] = stack[depth - 1];
6832 sp = &stack[depth];
6833 sp->ts_prefixdepth = depth - 1;
6834 byts = fbyts;
6835 idxs = fidxs;
6836 sp->ts_state = STATE_START;
6837 sp->ts_curi = 1; /* start just after length byte */
6838 sp->ts_arridx = 0;
6839
6840 /* Move the prefix to preword[] so that
6841 * find_keepcap_word() works. */
6842 prewordlen = splitoff = sp->ts_twordlen;
6843 mch_memmove(preword, tword, splitoff);
6844 break;
6845 }
6846
6847 /* Always past NUL bytes now. */
6848 sp->ts_state = STATE_ENDNUL;
6849 break;
6850 }
6851
Bram Moolenaar0c405862005-06-22 22:26:26 +00006852 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006853 {
6854 /* Past bytes in node and/or past NUL bytes. */
6855 sp->ts_state = STATE_ENDNUL;
6856 break;
6857 }
6858
6859 /*
6860 * End of word in tree.
6861 */
6862 ++sp->ts_curi; /* eat one NUL byte */
6863
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006864 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006865
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006866 if (sp->ts_prefixdepth < MAXWLEN)
6867 {
6868 /* There was a prefix before the word. Check that the
6869 * prefix can be used with this word. */
6870 /* Count the length of the NULs in the prefix. If there
6871 * are none this must be the first try without a prefix.
6872 */
6873 n = stack[sp->ts_prefixdepth].ts_arridx;
6874 len = pbyts[n++];
6875 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
6876 ;
6877 if (c > 0)
6878 {
6879 /* The prefix ID is stored two bytes above the flags. */
6880 c = valid_word_prefix(c, n, (unsigned)flags >> 16,
6881 tword + splitoff, lp->lp_slang);
6882 if (c == 0)
6883 break;
6884
6885 /* Use the WF_RARE flag for a rare prefix. */
6886 if (c & WF_RAREPFX)
6887 flags |= WF_RARE;
6888 }
6889 }
6890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006891 /*
6892 * Form the word with proper case in preword.
6893 * If there is a word from a previous split, append.
6894 */
6895 tword[sp->ts_twordlen] = NUL;
6896 if (flags & WF_KEEPCAP)
6897 /* Must find the word in the keep-case tree. */
6898 find_keepcap_word(lp->lp_slang, tword + splitoff,
6899 preword + prewordlen);
6900 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006902 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00006903 * use that for the goodword too. But if the badword is
6904 * allcap and it's only one char long use onecap. */
6905 c = su->su_badflags;
6906 if ((c & WF_ALLCAP)
6907#ifdef FEAT_MBYTE
6908 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
6909#else
6910 && su->su_badlen == 1
6911#endif
6912 )
6913 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006914 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006915 preword + prewordlen, flags | c);
6916 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006917
6918 /* Don't use a banned word. It may appear again as a good
6919 * word, thus remember it. */
6920 if (flags & WF_BANNED)
6921 {
6922 add_banned(su, preword + prewordlen);
6923 break;
6924 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006925 if (was_banned(su, preword + prewordlen)
6926 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006927 break;
6928
6929 newscore = 0;
6930 if ((flags & WF_REGION)
6931 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
6932 newscore += SCORE_REGION;
6933 if (flags & WF_RARE)
6934 newscore += SCORE_RARE;
6935
Bram Moolenaar0c405862005-06-22 22:26:26 +00006936 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006937 captype(preword + prewordlen, NULL)))
6938 newscore += SCORE_ICASE;
6939
Bram Moolenaar0c405862005-06-22 22:26:26 +00006940 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006941 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006942 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006943 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006944 /* The badword also ends: add suggestions. Give a penalty
6945 * when changing non-word char to word char, e.g., "thes,"
6946 * -> "these". */
6947 p = fword + sp->ts_fidx;
6948#ifdef FEAT_MBYTE
6949 if (has_mbyte)
6950 mb_ptr_back(fword, p);
6951 else
6952#endif
6953 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006954 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006955 {
6956 p = preword + STRLEN(preword);
6957#ifdef FEAT_MBYTE
6958 if (has_mbyte)
6959 mb_ptr_back(preword, p);
6960 else
6961#endif
6962 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006963 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006964 newscore += SCORE_NONWORD;
6965 }
6966
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006967 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00006968 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006969 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006970 }
Bram Moolenaarea424162005-06-16 21:51:00 +00006971 else if (sp->ts_fidx >= sp->ts_fidxtry
6972#ifdef FEAT_MBYTE
6973 /* Don't split halfway a character. */
6974 && (!has_mbyte || sp->ts_tcharlen == 0)
6975#endif
6976 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006977 {
6978 /* The word in the tree ends but the badword
6979 * continues: try inserting a space and check that a valid
6980 * words starts at fword[sp->ts_fidx]. */
6981 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
6982 {
6983 /* Save things to be restored at STATE_SPLITUNDO. */
6984 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006985 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006986 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006987 sp->ts_state = STATE_SPLITUNDO;
6988
6989 ++depth;
6990 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006991
6992 /* Append a space to preword. */
6993 STRCAT(preword, " ");
6994 prewordlen = STRLEN(preword);
6995 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006996
6997 /* If the badword has a non-word character at this
6998 * position skip it. That means replacing the
6999 * non-word character with a space. */
7000 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7001 {
7002 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7003#ifdef FEAT_MBYTE
7004 if (has_mbyte)
7005 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7006 else
7007#endif
7008 ++sp->ts_fidx;
7009 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007010#ifdef FEAT_MBYTE
7011 if (has_mbyte)
7012 {
7013 int i = 0;
7014
7015 /* Case-folding may change the number of bytes:
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007016 * Count nr of chars in fword[ts_fidx] and
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007017 * advance that many chars in su->su_badptr. */
7018 for (p = fword; p < fword + sp->ts_fidx;
7019 mb_ptr_adv(p))
7020 ++i;
7021 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
7022 --i;
7023 }
7024 else
7025#endif
7026 p = su->su_badptr + sp->ts_fidx;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007027 su->su_badflags = captype(p, su->su_badptr
7028 + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007030 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007031 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007032 }
7033 }
7034 break;
7035
7036 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007037 /* Undo the changes done for word split. */
7038 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007039 splitoff = sp->ts_save_splitoff;
7040 prewordlen = sp->ts_save_prewordlen;
7041
7042 /* Continue looking for NUL bytes. */
7043 sp->ts_state = STATE_START;
7044 break;
7045
7046 case STATE_ENDNUL:
7047 /* Past the NUL bytes in the node. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007048 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007049 {
7050 /* The badword ends, can't use the bytes in this node. */
7051 sp->ts_state = STATE_DEL;
7052 break;
7053 }
7054 sp->ts_state = STATE_PLAIN;
7055 /*FALLTHROUGH*/
7056
7057 case STATE_PLAIN:
7058 /*
7059 * Go over all possible bytes at this node, add each to
7060 * tword[] and use child node. "ts_curi" is the index.
7061 */
7062 arridx = sp->ts_arridx;
7063 if (sp->ts_curi > byts[arridx])
7064 {
7065 /* Done all bytes at this node, do next state. When still
7066 * at already changed bytes skip the other tricks. */
7067 if (sp->ts_fidx >= sp->ts_fidxtry)
7068 sp->ts_state = STATE_DEL;
7069 else
7070 sp->ts_state = STATE_FINAL;
7071 }
7072 else
7073 {
7074 arridx += sp->ts_curi++;
7075 c = byts[arridx];
7076
7077 /* Normal byte, go one level deeper. If it's not equal to
7078 * the byte in the bad word adjust the score. But don't
7079 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007080 if (c == fword[sp->ts_fidx]
7081#ifdef FEAT_MBYTE
7082 || (sp->ts_tcharlen > 0
7083 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007084#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007085 )
7086 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007087 else
7088 newscore = SCORE_SUBST;
7089 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7090 && try_deeper(su, stack, depth, newscore))
7091 {
7092 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007093 sp = &stack[depth];
7094 ++sp->ts_fidx;
7095 tword[sp->ts_twordlen++] = c;
7096 sp->ts_arridx = idxs[arridx];
7097#ifdef FEAT_MBYTE
7098 if (newscore == SCORE_SUBST)
7099 sp->ts_isdiff = DIFF_YES;
7100 if (has_mbyte)
7101 {
7102 /* Multi-byte characters are a bit complicated to
7103 * handle: They differ when any of the bytes
7104 * differ and then their length may also differ. */
7105 if (sp->ts_tcharlen == 0)
7106 {
7107 /* First byte. */
7108 sp->ts_tcharidx = 0;
7109 sp->ts_tcharlen = MB_BYTE2LEN(c);
7110 sp->ts_fcharstart = sp->ts_fidx - 1;
7111 sp->ts_isdiff = (newscore != 0)
7112 ? DIFF_YES : DIFF_NONE;
7113 }
7114 else if (sp->ts_isdiff == DIFF_INSERT)
7115 /* When inserting trail bytes don't advance in
7116 * the bad word. */
7117 --sp->ts_fidx;
7118 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7119 {
7120 /* Last byte of character. */
7121 if (sp->ts_isdiff == DIFF_YES)
7122 {
7123 /* Correct ts_fidx for the byte length of
7124 * the character (we didn't check that
7125 * before). */
7126 sp->ts_fidx = sp->ts_fcharstart
7127 + MB_BYTE2LEN(
7128 fword[sp->ts_fcharstart]);
7129
7130 /* For a similar character adjust score
7131 * from SCORE_SUBST to SCORE_SIMILAR. */
7132 if (lp->lp_slang->sl_has_map
7133 && similar_chars(lp->lp_slang,
7134 mb_ptr2char(tword
7135 + sp->ts_twordlen
7136 - sp->ts_tcharlen),
7137 mb_ptr2char(fword
7138 + sp->ts_fcharstart)))
7139 sp->ts_score -=
7140 SCORE_SUBST - SCORE_SIMILAR;
7141 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007142 else if (sp->ts_isdiff == DIFF_INSERT
7143 && sp->ts_twordlen > sp->ts_tcharlen)
7144 {
7145 /* If the previous character was the same,
7146 * thus doubling a character, give a bonus
7147 * to the score. */
7148 p = tword + sp->ts_twordlen
7149 - sp->ts_tcharlen;
7150 c = mb_ptr2char(p);
7151 mb_ptr_back(tword, p);
7152 if (c == mb_ptr2char(p))
7153 sp->ts_score -= SCORE_INS
7154 - SCORE_INSDUP;
7155 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007156
7157 /* Starting a new char, reset the length. */
7158 sp->ts_tcharlen = 0;
7159 }
7160 }
7161 else
7162#endif
7163 {
7164 /* If we found a similar char adjust the score.
7165 * We do this after calling try_deeper() because
7166 * it's slow. */
7167 if (newscore != 0
7168 && lp->lp_slang->sl_has_map
7169 && similar_chars(lp->lp_slang,
7170 c, fword[sp->ts_fidx - 1]))
7171 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7172 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007173 }
7174 }
7175 break;
7176
7177 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007178#ifdef FEAT_MBYTE
7179 /* When past the first byte of a multi-byte char don't try
7180 * delete/insert/swap a character. */
7181 if (has_mbyte && sp->ts_tcharlen > 0)
7182 {
7183 sp->ts_state = STATE_FINAL;
7184 break;
7185 }
7186#endif
7187 /*
7188 * Try skipping one character in the bad word (delete it).
7189 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007190 sp->ts_state = STATE_INS;
7191 sp->ts_curi = 1;
7192 if (fword[sp->ts_fidx] != NUL
7193 && try_deeper(su, stack, depth, SCORE_DEL))
7194 {
7195 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007196
7197 /* Advance over the character in fword[]. Give a bonus to
7198 * the score if the same character is following "nn" ->
7199 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007200#ifdef FEAT_MBYTE
7201 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007202 {
7203 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007204 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007205 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7206 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7207 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007208 else
7209#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007210 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007211 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007212 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7213 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007215 break;
7216 }
7217 /*FALLTHROUGH*/
7218
7219 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007220 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007221 * node. */
7222 n = sp->ts_arridx;
7223 if (sp->ts_curi > byts[n])
7224 {
7225 /* Done all bytes at this node, do next state. */
7226 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 }
7228 else
7229 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007230 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007231 n += sp->ts_curi++;
7232 c = byts[n];
7233 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7234 {
7235 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007236 sp = &stack[depth];
7237 tword[sp->ts_twordlen++] = c;
7238 sp->ts_arridx = idxs[n];
7239#ifdef FEAT_MBYTE
7240 if (has_mbyte)
7241 {
7242 fl = MB_BYTE2LEN(c);
7243 if (fl > 1)
7244 {
7245 /* There are following bytes for the same
7246 * character. We must find all bytes before
7247 * trying delete/insert/swap/etc. */
7248 sp->ts_tcharlen = fl;
7249 sp->ts_tcharidx = 1;
7250 sp->ts_isdiff = DIFF_INSERT;
7251 }
7252 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007253 else
7254 fl = 1;
7255 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007256#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007257 {
7258 /* If the previous character was the same, thus
7259 * doubling a character, give a bonus to the
7260 * score. */
7261 if (sp->ts_twordlen >= 2
7262 && tword[sp->ts_twordlen - 2] == c)
7263 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265 }
7266 }
7267 break;
7268
7269 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007270 /*
7271 * Swap two bytes in the bad word: "12" -> "21".
7272 * We change "fword" here, it's changed back afterwards.
7273 */
7274 p = fword + sp->ts_fidx;
7275 c = *p;
7276 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007277 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007278 /* End of word, can't swap or replace. */
7279 sp->ts_state = STATE_FINAL;
7280 break;
7281 }
7282#ifdef FEAT_MBYTE
7283 if (has_mbyte)
7284 {
7285 n = mb_ptr2len_check(p);
7286 c = mb_ptr2char(p);
7287 c2 = mb_ptr2char(p + n);
7288 }
7289 else
7290#endif
7291 c2 = p[1];
7292 if (c == c2)
7293 {
7294 /* Characters are identical, swap won't do anything. */
7295 sp->ts_state = STATE_SWAP3;
7296 break;
7297 }
7298 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7299 {
7300 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007301 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007302#ifdef FEAT_MBYTE
7303 if (has_mbyte)
7304 {
7305 fl = mb_char2len(c2);
7306 mch_memmove(p, p + n, fl);
7307 mb_char2bytes(c, p + fl);
7308 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7309 }
7310 else
7311#endif
7312 {
7313 p[0] = c2;
7314 p[1] = c;
7315 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 }
7318 else
7319 /* If this swap doesn't work then SWAP3 won't either. */
7320 sp->ts_state = STATE_REP_INI;
7321 break;
7322
Bram Moolenaarea424162005-06-16 21:51:00 +00007323 case STATE_UNSWAP:
7324 /* Undo the STATE_SWAP swap: "21" -> "12". */
7325 p = fword + sp->ts_fidx;
7326#ifdef FEAT_MBYTE
7327 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007329 n = MB_BYTE2LEN(*p);
7330 c = mb_ptr2char(p + n);
7331 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7332 mb_char2bytes(c, p);
7333 }
7334 else
7335#endif
7336 {
7337 c = *p;
7338 *p = p[1];
7339 p[1] = c;
7340 }
7341 /*FALLTHROUGH*/
7342
7343 case STATE_SWAP3:
7344 /* Swap two bytes, skipping one: "123" -> "321". We change
7345 * "fword" here, it's changed back afterwards. */
7346 p = fword + sp->ts_fidx;
7347#ifdef FEAT_MBYTE
7348 if (has_mbyte)
7349 {
7350 n = mb_ptr2len_check(p);
7351 c = mb_ptr2char(p);
7352 fl = mb_ptr2len_check(p + n);
7353 c2 = mb_ptr2char(p + n);
7354 c3 = mb_ptr2char(p + n + fl);
7355 }
7356 else
7357#endif
7358 {
7359 c = *p;
7360 c2 = p[1];
7361 c3 = p[2];
7362 }
7363
7364 /* When characters are identical: "121" then SWAP3 result is
7365 * identical, ROT3L result is same as SWAP: "211", ROT3L
7366 * result is same as SWAP on next char: "112". Thus skip all
7367 * swapping. Also skip when c3 is NUL. */
7368 if (c == c3 || c3 == NUL)
7369 {
7370 sp->ts_state = STATE_REP_INI;
7371 break;
7372 }
7373 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7374 {
7375 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007377#ifdef FEAT_MBYTE
7378 if (has_mbyte)
7379 {
7380 tl = mb_char2len(c3);
7381 mch_memmove(p, p + n + fl, tl);
7382 mb_char2bytes(c2, p + tl);
7383 mb_char2bytes(c, p + fl + tl);
7384 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7385 }
7386 else
7387#endif
7388 {
7389 p[0] = p[2];
7390 p[2] = c;
7391 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007393 }
7394 else
7395 sp->ts_state = STATE_REP_INI;
7396 break;
7397
Bram Moolenaarea424162005-06-16 21:51:00 +00007398 case STATE_UNSWAP3:
7399 /* Undo STATE_SWAP3: "321" -> "123" */
7400 p = fword + sp->ts_fidx;
7401#ifdef FEAT_MBYTE
7402 if (has_mbyte)
7403 {
7404 n = MB_BYTE2LEN(*p);
7405 c2 = mb_ptr2char(p + n);
7406 fl = MB_BYTE2LEN(p[n]);
7407 c = mb_ptr2char(p + n + fl);
7408 tl = MB_BYTE2LEN(p[n + fl]);
7409 mch_memmove(p + fl + tl, p, n);
7410 mb_char2bytes(c, p);
7411 mb_char2bytes(c2, p + tl);
7412 }
7413 else
7414#endif
7415 {
7416 c = *p;
7417 *p = p[2];
7418 p[2] = c;
7419 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007420
Bram Moolenaarea424162005-06-16 21:51:00 +00007421 /* Rotate three characters left: "123" -> "231". We change
7422 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007423 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7424 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007425 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007426 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007427 p = fword + sp->ts_fidx;
7428#ifdef FEAT_MBYTE
7429 if (has_mbyte)
7430 {
7431 n = mb_ptr2len_check(p);
7432 c = mb_ptr2char(p);
7433 fl = mb_ptr2len_check(p + n);
7434 fl += mb_ptr2len_check(p + n + fl);
7435 mch_memmove(p, p + n, fl);
7436 mb_char2bytes(c, p + fl);
7437 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7438 }
7439 else
7440#endif
7441 {
7442 c = *p;
7443 *p = p[1];
7444 p[1] = p[2];
7445 p[2] = c;
7446 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007448 }
7449 else
7450 sp->ts_state = STATE_REP_INI;
7451 break;
7452
Bram Moolenaarea424162005-06-16 21:51:00 +00007453 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007454 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007455 p = fword + sp->ts_fidx;
7456#ifdef FEAT_MBYTE
7457 if (has_mbyte)
7458 {
7459 n = MB_BYTE2LEN(*p);
7460 n += MB_BYTE2LEN(p[n]);
7461 c = mb_ptr2char(p + n);
7462 tl = MB_BYTE2LEN(p[n]);
7463 mch_memmove(p + tl, p, n);
7464 mb_char2bytes(c, p);
7465 }
7466 else
7467#endif
7468 {
7469 c = p[2];
7470 p[2] = p[1];
7471 p[1] = *p;
7472 *p = c;
7473 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007475 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007476 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007477 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7478 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007479 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007480 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007481 p = fword + sp->ts_fidx;
7482#ifdef FEAT_MBYTE
7483 if (has_mbyte)
7484 {
7485 n = mb_ptr2len_check(p);
7486 n += mb_ptr2len_check(p + n);
7487 c = mb_ptr2char(p + n);
7488 tl = mb_ptr2len_check(p + n);
7489 mch_memmove(p + tl, p, n);
7490 mb_char2bytes(c, p);
7491 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7492 }
7493 else
7494#endif
7495 {
7496 c = p[2];
7497 p[2] = p[1];
7498 p[1] = *p;
7499 *p = c;
7500 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007502 }
7503 else
7504 sp->ts_state = STATE_REP_INI;
7505 break;
7506
Bram Moolenaarea424162005-06-16 21:51:00 +00007507 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007508 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007509 p = fword + sp->ts_fidx;
7510#ifdef FEAT_MBYTE
7511 if (has_mbyte)
7512 {
7513 c = mb_ptr2char(p);
7514 tl = MB_BYTE2LEN(*p);
7515 n = MB_BYTE2LEN(p[tl]);
7516 n += MB_BYTE2LEN(p[tl + n]);
7517 mch_memmove(p, p + tl, n);
7518 mb_char2bytes(c, p + n);
7519 }
7520 else
7521#endif
7522 {
7523 c = *p;
7524 *p = p[1];
7525 p[1] = p[2];
7526 p[2] = c;
7527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007528 /*FALLTHROUGH*/
7529
7530 case STATE_REP_INI:
7531 /* Check if matching with REP items from the .aff file would
7532 * work. Quickly skip if there are no REP items or the score
7533 * is going to be too high anyway. */
7534 gap = &lp->lp_slang->sl_rep;
7535 if (gap->ga_len == 0
7536 || sp->ts_score + SCORE_REP >= su->su_maxscore)
7537 {
7538 sp->ts_state = STATE_FINAL;
7539 break;
7540 }
7541
7542 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00007543 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007544 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
7545 if (sp->ts_curi < 0)
7546 {
7547 sp->ts_state = STATE_FINAL;
7548 break;
7549 }
7550
7551 sp->ts_state = STATE_REP;
7552 /*FALLTHROUGH*/
7553
7554 case STATE_REP:
7555 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00007556 * match replace the characters and check if the resulting
7557 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007558 p = fword + sp->ts_fidx;
7559
7560 gap = &lp->lp_slang->sl_rep;
7561 while (sp->ts_curi < gap->ga_len)
7562 {
7563 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
7564 if (*ftp->ft_from != *p)
7565 {
7566 /* past possible matching entries */
7567 sp->ts_curi = gap->ga_len;
7568 break;
7569 }
7570 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
7571 && try_deeper(su, stack, depth, SCORE_REP))
7572 {
7573 /* Need to undo this afterwards. */
7574 sp->ts_state = STATE_REP_UNDO;
7575
7576 /* Change the "from" to the "to" string. */
7577 ++depth;
7578 fl = STRLEN(ftp->ft_from);
7579 tl = STRLEN(ftp->ft_to);
7580 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007582 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007583 repextra += tl - fl;
7584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007585 mch_memmove(p, ftp->ft_to, tl);
7586 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00007587#ifdef FEAT_MBYTE
7588 stack[depth].ts_tcharlen = 0;
7589#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590 break;
7591 }
7592 }
7593
7594 if (sp->ts_curi >= gap->ga_len)
7595 /* No (more) matches. */
7596 sp->ts_state = STATE_FINAL;
7597
7598 break;
7599
7600 case STATE_REP_UNDO:
7601 /* Undo a REP replacement and continue with the next one. */
7602 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
7603 + sp->ts_curi - 1;
7604 fl = STRLEN(ftp->ft_from);
7605 tl = STRLEN(ftp->ft_to);
7606 p = fword + sp->ts_fidx;
7607 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007609 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007610 repextra -= tl - fl;
7611 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007612 mch_memmove(p, ftp->ft_from, fl);
7613 sp->ts_state = STATE_REP;
7614 break;
7615
7616 default:
7617 /* Did all possible states at this level, go up one level. */
7618 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007619
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007620 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
7621 {
7622 /* Continue in or go back to the prefix tree. */
7623 byts = pbyts;
7624 idxs = pidxs;
7625 splitoff = 0;
7626 }
7627
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007628 /* Don't check for CTRL-C too often, it takes time. */
7629 line_breakcheck();
7630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007631 }
7632 }
7633}
7634
7635/*
7636 * Try going one level deeper in the tree.
7637 */
7638 static int
7639try_deeper(su, stack, depth, score_add)
7640 suginfo_T *su;
7641 trystate_T *stack;
7642 int depth;
7643 int score_add;
7644{
7645 int newscore;
7646
7647 /* Refuse to go deeper if the scrore is getting too big. */
7648 newscore = stack[depth].ts_score + score_add;
7649 if (newscore >= su->su_maxscore)
7650 return FALSE;
7651
Bram Moolenaarea424162005-06-16 21:51:00 +00007652 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007653 stack[depth + 1].ts_state = STATE_START;
7654 stack[depth + 1].ts_score = newscore;
7655 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007656 return TRUE;
7657}
7658
7659/*
7660 * "fword" is a good word with case folded. Find the matching keep-case
7661 * words and put it in "kword".
7662 * Theoretically there could be several keep-case words that result in the
7663 * same case-folded word, but we only find one...
7664 */
7665 static void
7666find_keepcap_word(slang, fword, kword)
7667 slang_T *slang;
7668 char_u *fword;
7669 char_u *kword;
7670{
7671 char_u uword[MAXWLEN]; /* "fword" in upper-case */
7672 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007673 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007674
7675 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007676 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007677 int round[MAXWLEN];
7678 int fwordidx[MAXWLEN];
7679 int uwordidx[MAXWLEN];
7680 int kwordlen[MAXWLEN];
7681
7682 int flen, ulen;
7683 int l;
7684 int len;
7685 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007686 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007687 char_u *p;
7688 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007689 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007690
7691 if (byts == NULL)
7692 {
7693 /* array is empty: "cannot happen" */
7694 *kword = NUL;
7695 return;
7696 }
7697
7698 /* Make an all-cap version of "fword". */
7699 allcap_copy(fword, uword);
7700
7701 /*
7702 * Each character needs to be tried both case-folded and upper-case.
7703 * All this gets very complicated if we keep in mind that changing case
7704 * may change the byte length of a multi-byte character...
7705 */
7706 depth = 0;
7707 arridx[0] = 0;
7708 round[0] = 0;
7709 fwordidx[0] = 0;
7710 uwordidx[0] = 0;
7711 kwordlen[0] = 0;
7712 while (depth >= 0)
7713 {
7714 if (fword[fwordidx[depth]] == NUL)
7715 {
7716 /* We are at the end of "fword". If the tree allows a word to end
7717 * here we have found a match. */
7718 if (byts[arridx[depth] + 1] == 0)
7719 {
7720 kword[kwordlen[depth]] = NUL;
7721 return;
7722 }
7723
7724 /* kword is getting too long, continue one level up */
7725 --depth;
7726 }
7727 else if (++round[depth] > 2)
7728 {
7729 /* tried both fold-case and upper-case character, continue one
7730 * level up */
7731 --depth;
7732 }
7733 else
7734 {
7735 /*
7736 * round[depth] == 1: Try using the folded-case character.
7737 * round[depth] == 2: Try using the upper-case character.
7738 */
7739#ifdef FEAT_MBYTE
7740 if (has_mbyte)
7741 {
7742 flen = mb_ptr2len_check(fword + fwordidx[depth]);
7743 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
7744 }
7745 else
7746#endif
7747 ulen = flen = 1;
7748 if (round[depth] == 1)
7749 {
7750 p = fword + fwordidx[depth];
7751 l = flen;
7752 }
7753 else
7754 {
7755 p = uword + uwordidx[depth];
7756 l = ulen;
7757 }
7758
7759 for (tryidx = arridx[depth]; l > 0; --l)
7760 {
7761 /* Perform a binary search in the list of accepted bytes. */
7762 len = byts[tryidx++];
7763 c = *p++;
7764 lo = tryidx;
7765 hi = tryidx + len - 1;
7766 while (lo < hi)
7767 {
7768 m = (lo + hi) / 2;
7769 if (byts[m] > c)
7770 hi = m - 1;
7771 else if (byts[m] < c)
7772 lo = m + 1;
7773 else
7774 {
7775 lo = hi = m;
7776 break;
7777 }
7778 }
7779
7780 /* Stop if there is no matching byte. */
7781 if (hi < lo || byts[lo] != c)
7782 break;
7783
7784 /* Continue at the child (if there is one). */
7785 tryidx = idxs[lo];
7786 }
7787
7788 if (l == 0)
7789 {
7790 /*
7791 * Found the matching char. Copy it to "kword" and go a
7792 * level deeper.
7793 */
7794 if (round[depth] == 1)
7795 {
7796 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
7797 flen);
7798 kwordlen[depth + 1] = kwordlen[depth] + flen;
7799 }
7800 else
7801 {
7802 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
7803 ulen);
7804 kwordlen[depth + 1] = kwordlen[depth] + ulen;
7805 }
7806 fwordidx[depth + 1] = fwordidx[depth] + flen;
7807 uwordidx[depth + 1] = uwordidx[depth] + ulen;
7808
7809 ++depth;
7810 arridx[depth] = tryidx;
7811 round[depth] = 0;
7812 }
7813 }
7814 }
7815
7816 /* Didn't find it: "cannot happen". */
7817 *kword = NUL;
7818}
7819
7820/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007821 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
7822 * su->su_sga.
7823 */
7824 static void
7825score_comp_sal(su)
7826 suginfo_T *su;
7827{
7828 langp_T *lp;
7829 char_u badsound[MAXWLEN];
7830 int i;
7831 suggest_T *stp;
7832 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007833 int score;
7834
7835 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
7836 return;
7837
7838 /* Use the sound-folding of the first language that supports it. */
7839 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7840 lp->lp_slang != NULL; ++lp)
7841 if (lp->lp_slang->sl_sal.ga_len > 0)
7842 {
7843 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007844 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007845
7846 for (i = 0; i < su->su_ga.ga_len; ++i)
7847 {
7848 stp = &SUG(su->su_ga, i);
7849
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007850 /* Case-fold the suggested word, sound-fold it and compute the
7851 * sound-a-like score. */
7852 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007853 if (score < SCORE_MAXMAX)
7854 {
7855 /* Add the suggestion. */
7856 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
7857 sstp->st_word = vim_strsave(stp->st_word);
7858 if (sstp->st_word != NULL)
7859 {
7860 sstp->st_score = score;
7861 sstp->st_altscore = 0;
7862 sstp->st_orglen = stp->st_orglen;
7863 ++su->su_sga.ga_len;
7864 }
7865 }
7866 }
7867 break;
7868 }
7869}
7870
7871/*
7872 * Combine the list of suggestions in su->su_ga and su->su_sga.
7873 * They are intwined.
7874 */
7875 static void
7876score_combine(su)
7877 suginfo_T *su;
7878{
7879 int i;
7880 int j;
7881 garray_T ga;
7882 garray_T *gap;
7883 langp_T *lp;
7884 suggest_T *stp;
7885 char_u *p;
7886 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007887 int round;
7888
7889 /* Add the alternate score to su_ga. */
7890 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7891 lp->lp_slang != NULL; ++lp)
7892 {
7893 if (lp->lp_slang->sl_sal.ga_len > 0)
7894 {
7895 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007896 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007897
7898 for (i = 0; i < su->su_ga.ga_len; ++i)
7899 {
7900 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007901 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
7902 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007903 if (stp->st_altscore == SCORE_MAXMAX)
7904 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
7905 else
7906 stp->st_score = (stp->st_score * 3
7907 + stp->st_altscore) / 4;
7908 stp->st_salscore = FALSE;
7909 }
7910 break;
7911 }
7912 }
7913
7914 /* Add the alternate score to su_sga. */
7915 for (i = 0; i < su->su_sga.ga_len; ++i)
7916 {
7917 stp = &SUG(su->su_sga, i);
7918 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
7919 if (stp->st_score == SCORE_MAXMAX)
7920 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
7921 else
7922 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
7923 stp->st_salscore = TRUE;
7924 }
7925
7926 /* Sort the suggestions and truncate at "maxcount" for both lists. */
7927 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
7928 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
7929
7930 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
7931 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
7932 return;
7933
7934 stp = &SUG(ga, 0);
7935 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
7936 {
7937 /* round 1: get a suggestion from su_ga
7938 * round 2: get a suggestion from su_sga */
7939 for (round = 1; round <= 2; ++round)
7940 {
7941 gap = round == 1 ? &su->su_ga : &su->su_sga;
7942 if (i < gap->ga_len)
7943 {
7944 /* Don't add a word if it's already there. */
7945 p = SUG(*gap, i).st_word;
7946 for (j = 0; j < ga.ga_len; ++j)
7947 if (STRCMP(stp[j].st_word, p) == 0)
7948 break;
7949 if (j == ga.ga_len)
7950 stp[ga.ga_len++] = SUG(*gap, i);
7951 else
7952 vim_free(p);
7953 }
7954 }
7955 }
7956
7957 ga_clear(&su->su_ga);
7958 ga_clear(&su->su_sga);
7959
7960 /* Truncate the list to the number of suggestions that will be displayed. */
7961 if (ga.ga_len > su->su_maxcount)
7962 {
7963 for (i = su->su_maxcount; i < ga.ga_len; ++i)
7964 vim_free(stp[i].st_word);
7965 ga.ga_len = su->su_maxcount;
7966 }
7967
7968 su->su_ga = ga;
7969}
7970
7971/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007972 * For the goodword in "stp" compute the soundalike score compared to the
7973 * badword.
7974 */
7975 static int
7976stp_sal_score(stp, su, slang, badsound)
7977 suggest_T *stp;
7978 suginfo_T *su;
7979 slang_T *slang;
7980 char_u *badsound; /* sound-folded badword */
7981{
7982 char_u *p;
7983 char_u badsound2[MAXWLEN];
7984 char_u fword[MAXWLEN];
7985 char_u goodsound[MAXWLEN];
7986
7987 if (stp->st_orglen <= su->su_badlen)
7988 p = badsound;
7989 else
7990 {
7991 /* soundfold the bad word with more characters following */
7992 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
7993
7994 /* When joining two words the sound often changes a lot. E.g., "t he"
7995 * sounds like "t h" while "the" sounds like "@". Avoid that by
7996 * removing the space. Don't do it when the good word also contains a
7997 * space. */
7998 if (vim_iswhite(su->su_badptr[su->su_badlen])
7999 && *skiptowhite(stp->st_word) == NUL)
8000 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8001 mch_memmove(p, p + 1, STRLEN(p));
8002
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008003 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008004 p = badsound2;
8005 }
8006
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008007 /* Sound-fold the word and compute the score for the difference. */
8008 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008009
8010 return soundalike_score(goodsound, p);
8011}
8012
8013/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 * Find suggestions by comparing the word in a sound-a-like form.
8015 */
8016 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008017suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008018 suginfo_T *su;
8019{
8020 char_u salword[MAXWLEN];
8021 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008023 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008024 int curi[MAXWLEN];
8025 langp_T *lp;
8026 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008027 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008028 int depth;
8029 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008030 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008031 int round;
8032 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008033 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008034
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008035 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008036 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8037 lp->lp_slang != NULL; ++lp)
8038 {
8039 if (lp->lp_slang->sl_sal.ga_len > 0)
8040 {
8041 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008042 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008043
8044 /*
8045 * Go through the whole tree, soundfold each word and compare.
8046 * round 1: use the case-folded tree.
8047 * round 2: use the keep-case tree.
8048 */
8049 for (round = 1; round <= 2; ++round)
8050 {
8051 if (round == 1)
8052 {
8053 byts = lp->lp_slang->sl_fbyts;
8054 idxs = lp->lp_slang->sl_fidxs;
8055 }
8056 else
8057 {
8058 byts = lp->lp_slang->sl_kbyts;
8059 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008060 if (byts == NULL) /* no keep-case words */
8061 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008062 }
8063
8064 depth = 0;
8065 arridx[0] = 0;
8066 curi[0] = 1;
8067 while (depth >= 0 && !got_int)
8068 {
8069 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008071 /* Done all bytes at this node, go up one level. */
8072 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008073 line_breakcheck();
8074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008075 else
8076 {
8077 /* Do one more byte at this node. */
8078 n = arridx[depth] + curi[depth];
8079 ++curi[depth];
8080 c = byts[n];
8081 if (c == 0)
8082 {
8083 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008084 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008085 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8086 {
8087 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008088 /* Sound-fold. Only in keep-case tree need to
8089 * case-fold the word. */
8090 spell_soundfold(lp->lp_slang, tword,
8091 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008092
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008093 /* Compute the edit distance between the
8094 * sound-a-like words. */
8095 sound_score = soundalike_score(salword,
8096 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008097 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008098 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008099 char_u cword[MAXWLEN];
8100 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008101 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008102
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008103 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008104 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008105 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008106 /* Need to fix case according to
8107 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008108 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008109 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008110 }
8111 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008112 p = tword;
8113
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008114 if (sps_flags & SPS_DOUBLE)
8115 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008116 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008117 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008118 else
8119 {
8120 /* Compute the score. */
8121 score = spell_edit_score(
8122 su->su_badword, p);
8123 if (sps_flags & SPS_BEST)
8124 /* give a bonus for the good word
8125 * sounding the same as the bad
8126 * word */
8127 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008128 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008129 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008130 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008131 else
8132 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008133 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008134 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008135 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008136 }
8137 }
8138
8139 /* Skip over other NUL bytes. */
8140 while (byts[n + 1] == 0)
8141 {
8142 ++n;
8143 ++curi[depth];
8144 }
8145 }
8146 else
8147 {
8148 /* Normal char, go one level deeper. */
8149 tword[depth++] = c;
8150 arridx[depth] = idxs[n];
8151 curi[depth] = 1;
8152 }
8153 }
8154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008155 }
8156 }
8157 }
8158}
8159
8160/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008161 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008162 */
8163 static void
8164make_case_word(fword, cword, flags)
8165 char_u *fword;
8166 char_u *cword;
8167 int flags;
8168{
8169 if (flags & WF_ALLCAP)
8170 /* Make it all upper-case */
8171 allcap_copy(fword, cword);
8172 else if (flags & WF_ONECAP)
8173 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008174 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008175 else
8176 /* Use goodword as-is. */
8177 STRCPY(cword, fword);
8178}
8179
Bram Moolenaarea424162005-06-16 21:51:00 +00008180/*
8181 * Use map string "map" for languages "lp".
8182 */
8183 static void
8184set_map_str(lp, map)
8185 slang_T *lp;
8186 char_u *map;
8187{
8188 char_u *p;
8189 int headc = 0;
8190 int c;
8191 int i;
8192
8193 if (*map == NUL)
8194 {
8195 lp->sl_has_map = FALSE;
8196 return;
8197 }
8198 lp->sl_has_map = TRUE;
8199
8200 /* Init the array and hash table empty. */
8201 for (i = 0; i < 256; ++i)
8202 lp->sl_map_array[i] = 0;
8203#ifdef FEAT_MBYTE
8204 hash_init(&lp->sl_map_hash);
8205#endif
8206
8207 /*
8208 * The similar characters are stored separated with slashes:
8209 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8210 * before the same slash. For characters above 255 sl_map_hash is used.
8211 */
8212 for (p = map; *p != NUL; )
8213 {
8214#ifdef FEAT_MBYTE
8215 c = mb_ptr2char_adv(&p);
8216#else
8217 c = *p++;
8218#endif
8219 if (c == '/')
8220 headc = 0;
8221 else
8222 {
8223 if (headc == 0)
8224 headc = c;
8225
8226#ifdef FEAT_MBYTE
8227 /* Characters above 255 don't fit in sl_map_array[], put them in
8228 * the hash table. Each entry is the char, a NUL the headchar and
8229 * a NUL. */
8230 if (c >= 256)
8231 {
8232 int cl = mb_char2len(c);
8233 int headcl = mb_char2len(headc);
8234 char_u *b;
8235 hash_T hash;
8236 hashitem_T *hi;
8237
8238 b = alloc((unsigned)(cl + headcl + 2));
8239 if (b == NULL)
8240 return;
8241 mb_char2bytes(c, b);
8242 b[cl] = NUL;
8243 mb_char2bytes(headc, b + cl + 1);
8244 b[cl + 1 + headcl] = NUL;
8245 hash = hash_hash(b);
8246 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8247 if (HASHITEM_EMPTY(hi))
8248 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8249 else
8250 {
8251 /* This should have been checked when generating the .spl
8252 * file. */
8253 EMSG(_("E999: duplicate char in MAP entry"));
8254 vim_free(b);
8255 }
8256 }
8257 else
8258#endif
8259 lp->sl_map_array[c] = headc;
8260 }
8261 }
8262}
8263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008264/*
8265 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8266 * lines in the .aff file.
8267 */
8268 static int
8269similar_chars(slang, c1, c2)
8270 slang_T *slang;
8271 int c1;
8272 int c2;
8273{
Bram Moolenaarea424162005-06-16 21:51:00 +00008274 int m1, m2;
8275#ifdef FEAT_MBYTE
8276 char_u buf[MB_MAXBYTES];
8277 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008278
Bram Moolenaarea424162005-06-16 21:51:00 +00008279 if (c1 >= 256)
8280 {
8281 buf[mb_char2bytes(c1, buf)] = 0;
8282 hi = hash_find(&slang->sl_map_hash, buf);
8283 if (HASHITEM_EMPTY(hi))
8284 m1 = 0;
8285 else
8286 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8287 }
8288 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008289#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008290 m1 = slang->sl_map_array[c1];
8291 if (m1 == 0)
8292 return FALSE;
8293
8294
8295#ifdef FEAT_MBYTE
8296 if (c2 >= 256)
8297 {
8298 buf[mb_char2bytes(c2, buf)] = 0;
8299 hi = hash_find(&slang->sl_map_hash, buf);
8300 if (HASHITEM_EMPTY(hi))
8301 m2 = 0;
8302 else
8303 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8304 }
8305 else
8306#endif
8307 m2 = slang->sl_map_array[c2];
8308
8309 return m1 == m2;
8310}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311
8312/*
8313 * Add a suggestion to the list of suggestions.
8314 * Do not add a duplicate suggestion or suggestions with a bad score.
8315 * When "use_score" is not zero it's used, otherwise the score is computed
8316 * with spell_edit_score().
8317 */
8318 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008319add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008321 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008323 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008324 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008325 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008326 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327{
8328 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008329 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008330 char_u *p = NULL;
8331 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008332
8333 /* Check that the word wasn't banned. */
8334 if (was_banned(su, goodword))
8335 return;
8336
Bram Moolenaar0c405862005-06-22 22:26:26 +00008337 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8338 * Remove the common part from "goodword". */
8339 i = badlen - su->su_badlen;
8340 if (i > 0)
8341 {
8342 /* This assumes there was no case folding or it didn't change the
8343 * length... */
8344 p = goodword + STRLEN(goodword) - i;
8345 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8346 {
8347 badlen = su->su_badlen;
8348 c = *p;
8349 *p = NUL;
8350 }
8351 else
8352 p = NULL;
8353 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008354 else if (i < 0)
8355 {
8356 /* When replacing part of the word check that we actually change
8357 * something. For "the the" a suggestion can be replacing the first
8358 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008359 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008360 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8361 return;
8362 }
8363
Bram Moolenaar0c405862005-06-22 22:26:26 +00008364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008365 if (score <= su->su_maxscore)
8366 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008367 /* Check if the word is already there. Also check the length that is
8368 * being replaced "thes," -> "these" is a different suggestion from
8369 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008370 stp = &SUG(*gap, 0);
8371 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008372 if (STRCMP(stp[i].st_word, goodword) == 0
8373 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008374 {
8375 /* Found it. Remember the lowest score. */
8376 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008379 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008380 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008381 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008382 break;
8383 }
8384
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008385 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008386 {
8387 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008388 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008389 stp->st_word = vim_strsave(goodword);
8390 if (stp->st_word != NULL)
8391 {
8392 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008393 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008394 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008395 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008396 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008397
8398 /* If we have too many suggestions now, sort the list and keep
8399 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008400 if (gap->ga_len > SUG_MAX_COUNT(su))
8401 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8402 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008403 }
8404 }
8405 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008406
8407 if (p != NULL)
8408 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409}
8410
8411/*
8412 * Add a word to be banned.
8413 */
8414 static void
8415add_banned(su, word)
8416 suginfo_T *su;
8417 char_u *word;
8418{
8419 char_u *s = vim_strsave(word);
8420 hash_T hash;
8421 hashitem_T *hi;
8422
8423 if (s != NULL)
8424 {
8425 hash = hash_hash(s);
8426 hi = hash_lookup(&su->su_banned, s, hash);
8427 if (HASHITEM_EMPTY(hi))
8428 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008429 else
8430 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 }
8432}
8433
8434/*
8435 * Return TRUE if a word appears in the list of banned words.
8436 */
8437 static int
8438was_banned(su, word)
8439 suginfo_T *su;
8440 char_u *word;
8441{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008442 hashitem_T *hi = hash_find(&su->su_banned, word);
8443
8444 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445}
8446
8447/*
8448 * Free the banned words in "su".
8449 */
8450 static void
8451free_banned(su)
8452 suginfo_T *su;
8453{
8454 int todo;
8455 hashitem_T *hi;
8456
8457 todo = su->su_banned.ht_used;
8458 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8459 {
8460 if (!HASHITEM_EMPTY(hi))
8461 {
8462 vim_free(hi->hi_key);
8463 --todo;
8464 }
8465 }
8466 hash_clear(&su->su_banned);
8467}
8468
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008469/*
8470 * Recompute the score if sound-folding is possible. This is slow,
8471 * thus only done for the final results.
8472 */
8473 static void
8474rescore_suggestions(su)
8475 suginfo_T *su;
8476{
8477 langp_T *lp;
8478 suggest_T *stp;
8479 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008480 int i;
8481
8482 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8483 lp->lp_slang != NULL; ++lp)
8484 {
8485 if (lp->lp_slang->sl_sal.ga_len > 0)
8486 {
8487 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008488 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008489
8490 for (i = 0; i < su->su_ga.ga_len; ++i)
8491 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008492 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008493 if (!stp->st_had_bonus)
8494 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008495 stp->st_altscore = stp_sal_score(stp, su,
8496 lp->lp_slang, sal_badword);
8497 if (stp->st_altscore == SCORE_MAXMAX)
8498 stp->st_altscore = SCORE_BIG;
8499 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008500 }
8501 }
8502 break;
8503 }
8504 }
8505}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008507static int
8508#ifdef __BORLANDC__
8509_RTLENTRYF
8510#endif
8511sug_compare __ARGS((const void *s1, const void *s2));
8512
8513/*
8514 * Function given to qsort() to sort the suggestions on st_score.
8515 */
8516 static int
8517#ifdef __BORLANDC__
8518_RTLENTRYF
8519#endif
8520sug_compare(s1, s2)
8521 const void *s1;
8522 const void *s2;
8523{
8524 suggest_T *p1 = (suggest_T *)s1;
8525 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008526 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008527
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008528 if (n == 0)
8529 return p1->st_altscore - p2->st_altscore;
8530 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008531}
8532
8533/*
8534 * Cleanup the suggestions:
8535 * - Sort on score.
8536 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008537 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008539 static int
8540cleanup_suggestions(gap, maxscore, keep)
8541 garray_T *gap;
8542 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008543 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008544{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008545 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008546 int i;
8547
8548 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008549 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008550
8551 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008552 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008554 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008555 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008556 gap->ga_len = keep;
8557 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008559 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008560}
8561
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008562#if defined(FEAT_EVAL) || defined(PROTO)
8563/*
8564 * Soundfold a string, for soundfold().
8565 * Result is in allocated memory, NULL for an error.
8566 */
8567 char_u *
8568eval_soundfold(word)
8569 char_u *word;
8570{
8571 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008572 char_u sound[MAXWLEN];
8573
8574 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
8575 /* Use the sound-folding of the first language that supports it. */
8576 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8577 lp->lp_slang != NULL; ++lp)
8578 if (lp->lp_slang->sl_sal.ga_len > 0)
8579 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008580 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008581 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008582 return vim_strsave(sound);
8583 }
8584
8585 /* No language with sound folding, return word as-is. */
8586 return vim_strsave(word);
8587}
8588#endif
8589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008590/*
8591 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8592 */
8593 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008594spell_soundfold(slang, inword, folded, res)
8595 slang_T *slang;
8596 char_u *inword;
8597 int folded; /* "inword" is already case-folded */
8598 char_u *res;
8599{
8600 char_u fword[MAXWLEN];
8601 char_u *word;
8602
8603 if (slang->sl_sofo)
8604 /* SOFOFROM and SOFOTO used */
8605 spell_soundfold_sofo(slang, inword, res);
8606 else
8607 {
8608 /* SAL items used. Requires the word to be case-folded. */
8609 if (folded)
8610 word = inword;
8611 else
8612 {
8613 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
8614 word = fword;
8615 }
8616
8617#ifdef FEAT_MBYTE
8618 if (has_mbyte)
8619 spell_soundfold_wsal(slang, word, res);
8620 else
8621#endif
8622 spell_soundfold_sal(slang, word, res);
8623 }
8624}
8625
8626/*
8627 * Perform sound folding of "inword" into "res" according to SOFOFROM and
8628 * SOFOTO lines.
8629 */
8630 static void
8631spell_soundfold_sofo(slang, inword, res)
8632 slang_T *slang;
8633 char_u *inword;
8634 char_u *res;
8635{
8636 char_u *s;
8637 int ri = 0;
8638 int c;
8639
8640#ifdef FEAT_MBYTE
8641 if (has_mbyte)
8642 {
8643 int prevc = 0;
8644 int *ip;
8645
8646 /* The sl_sal_first[] table contains the translation for chars up to
8647 * 255, sl_sal the rest. */
8648 for (s = inword; *s != NUL; )
8649 {
8650 c = mb_ptr2char_adv(&s);
8651 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
8652 c = ' ';
8653 else if (c < 256)
8654 c = slang->sl_sal_first[c];
8655 else
8656 {
8657 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
8658 if (ip == NULL) /* empty list, can't match */
8659 c = NUL;
8660 else
8661 for (;;) /* find "c" in the list */
8662 {
8663 if (*ip == 0) /* not found */
8664 {
8665 c = NUL;
8666 break;
8667 }
8668 if (*ip == c) /* match! */
8669 {
8670 c = ip[1];
8671 break;
8672 }
8673 ip += 2;
8674 }
8675 }
8676
8677 if (c != NUL && c != prevc)
8678 {
8679 ri += mb_char2bytes(c, res + ri);
8680 if (ri + MB_MAXBYTES > MAXWLEN)
8681 break;
8682 prevc = c;
8683 }
8684 }
8685 }
8686 else
8687#endif
8688 {
8689 /* The sl_sal_first[] table contains the translation. */
8690 for (s = inword; (c = *s) != NUL; ++s)
8691 {
8692 if (vim_iswhite(c))
8693 c = ' ';
8694 else
8695 c = slang->sl_sal_first[c];
8696 if (c != NUL && (ri == 0 || res[ri - 1] != c))
8697 res[ri++] = c;
8698 }
8699 }
8700
8701 res[ri] = NUL;
8702}
8703
8704 static void
8705spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008706 slang_T *slang;
8707 char_u *inword;
8708 char_u *res;
8709{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008710 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008711 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008712 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008713 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008714 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008715 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008716 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008717 int n, k = 0;
8718 int z0;
8719 int k0;
8720 int n0;
8721 int c;
8722 int pri;
8723 int p0 = -333;
8724 int c0;
8725
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008726 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008727 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 if (slang->sl_rem_accents)
8729 {
8730 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008731 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008732 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008733 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008734 {
8735 *t++ = ' ';
8736 s = skipwhite(s);
8737 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008738 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008740 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 *t++ = *s;
8742 ++s;
8743 }
8744 }
8745 *t = NUL;
8746 }
8747 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008748 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008749
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008750 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008751
8752 /*
8753 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008754 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008755 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008756 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008757 while ((c = word[i]) != NUL)
8758 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008759 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008760 n = slang->sl_sal_first[c];
8761 z0 = 0;
8762
8763 if (n >= 0)
8764 {
8765 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008766 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008767 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008768 /* Quickly skip entries that don't match the word. Most
8769 * entries are less then three chars, optimize for that. */
8770 k = smp[n].sm_leadlen;
8771 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008773 if (word[i + 1] != s[1])
8774 continue;
8775 if (k > 2)
8776 {
8777 for (j = 2; j < k; ++j)
8778 if (word[i + j] != s[j])
8779 break;
8780 if (j < k)
8781 continue;
8782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 }
8784
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008785 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008787 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008788 while (*pf != NUL && *pf != word[i + k])
8789 ++pf;
8790 if (*pf == NUL)
8791 continue;
8792 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008793 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008794 s = smp[n].sm_rules;
8795 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796
8797 p0 = *s;
8798 k0 = k;
8799 while (*s == '-' && k > 1)
8800 {
8801 k--;
8802 s++;
8803 }
8804 if (*s == '<')
8805 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008806 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 {
8808 /* determine priority */
8809 pri = *s - '0';
8810 s++;
8811 }
8812 if (*s == '^' && *(s + 1) == '^')
8813 s++;
8814
8815 if (*s == NUL
8816 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008817 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008818 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008820 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008821 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008822 && spell_iswordp(word + i - 1, curbuf)
8823 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008824 {
8825 /* search for followup rules, if: */
8826 /* followup and k > 1 and NO '-' in searchstring */
8827 c0 = word[i + k - 1];
8828 n0 = slang->sl_sal_first[c0];
8829
8830 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008831 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008832 {
8833 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008834 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008836 /* Quickly skip entries that don't match the word.
8837 * */
8838 k0 = smp[n0].sm_leadlen;
8839 if (k0 > 1)
8840 {
8841 if (word[i + k] != s[1])
8842 continue;
8843 if (k0 > 2)
8844 {
8845 pf = word + i + k + 1;
8846 for (j = 2; j < k0; ++j)
8847 if (*pf++ != s[j])
8848 break;
8849 if (j < k0)
8850 continue;
8851 }
8852 }
8853 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008855 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008856 {
8857 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008858 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008859 while (*pf != NUL && *pf != word[i + k0])
8860 ++pf;
8861 if (*pf == NUL)
8862 continue;
8863 ++k0;
8864 }
8865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008866 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008867 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 while (*s == '-')
8869 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008870 /* "k0" gets NOT reduced because
8871 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008872 s++;
8873 }
8874 if (*s == '<')
8875 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008876 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 {
8878 p0 = *s - '0';
8879 s++;
8880 }
8881
8882 if (*s == NUL
8883 /* *s == '^' cuts */
8884 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008885 && !spell_iswordp(word + i + k0,
8886 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 {
8888 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891
8892 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 /* rule fits; stop search */
8896 break;
8897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008898 }
8899
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008900 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008901 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 }
8903
8904 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008905 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008906 if (s == NULL)
8907 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008908 pf = smp[n].sm_rules;
8909 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 if (p0 == 1 && z == 0)
8911 {
8912 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008913 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
8914 || res[reslen - 1] == *s))
8915 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 z0 = 1;
8917 z = 1;
8918 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008919 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 {
8921 word[i + k0] = *s;
8922 k0++;
8923 s++;
8924 }
8925 if (k > k0)
8926 mch_memmove(word + i + k0, word + i + k,
8927 STRLEN(word + i + k) + 1);
8928
8929 /* new "actual letter" */
8930 c = word[i];
8931 }
8932 else
8933 {
8934 /* no '<' rule used */
8935 i += k - 1;
8936 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008937 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008938 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008939 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008940 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 s++;
8942 }
8943 /* new "actual letter" */
8944 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008945 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 {
8947 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008948 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008949 mch_memmove(word, word + i + 1,
8950 STRLEN(word + i + 1) + 1);
8951 i = 0;
8952 z0 = 1;
8953 }
8954 }
8955 break;
8956 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 }
8958 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008959 else if (vim_iswhite(c))
8960 {
8961 c = ' ';
8962 k = 1;
8963 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964
8965 if (z0 == 0)
8966 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008967 if (k && !p0 && reslen < MAXWLEN && c != NUL
8968 && (!slang->sl_collapse || reslen == 0
8969 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008971 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972
8973 i++;
8974 z = 0;
8975 k = 0;
8976 }
8977 }
8978
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008979 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980}
8981
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008982#ifdef FEAT_MBYTE
8983/*
8984 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
8985 * Multi-byte version of spell_soundfold().
8986 */
8987 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008988spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008989 slang_T *slang;
8990 char_u *inword;
8991 char_u *res;
8992{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008993 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008994 int word[MAXWLEN];
8995 int wres[MAXWLEN];
8996 int l;
8997 char_u *s;
8998 int *ws;
8999 char_u *t;
9000 int *pf;
9001 int i, j, z;
9002 int reslen;
9003 int n, k = 0;
9004 int z0;
9005 int k0;
9006 int n0;
9007 int c;
9008 int pri;
9009 int p0 = -333;
9010 int c0;
9011 int did_white = FALSE;
9012
9013 /*
9014 * Convert the multi-byte string to a wide-character string.
9015 * Remove accents, if wanted. We actually remove all non-word characters.
9016 * But keep white space.
9017 */
9018 n = 0;
9019 for (s = inword; *s != NUL; )
9020 {
9021 t = s;
9022 c = mb_ptr2char_adv(&s);
9023 if (slang->sl_rem_accents)
9024 {
9025 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9026 {
9027 if (did_white)
9028 continue;
9029 c = ' ';
9030 did_white = TRUE;
9031 }
9032 else
9033 {
9034 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009035 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009036 continue;
9037 }
9038 }
9039 word[n++] = c;
9040 }
9041 word[n] = NUL;
9042
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009043 /*
9044 * This comes from Aspell phonet.cpp.
9045 * Converted from C++ to C. Added support for multi-byte chars.
9046 * Changed to keep spaces.
9047 */
9048 i = reslen = z = 0;
9049 while ((c = word[i]) != NUL)
9050 {
9051 /* Start with the first rule that has the character in the word. */
9052 n = slang->sl_sal_first[c & 0xff];
9053 z0 = 0;
9054
9055 if (n >= 0)
9056 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009057 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009058 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9059 {
9060 /* Quickly skip entries that don't match the word. Most
9061 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009062 if (c != ws[0])
9063 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009064 k = smp[n].sm_leadlen;
9065 if (k > 1)
9066 {
9067 if (word[i + 1] != ws[1])
9068 continue;
9069 if (k > 2)
9070 {
9071 for (j = 2; j < k; ++j)
9072 if (word[i + j] != ws[j])
9073 break;
9074 if (j < k)
9075 continue;
9076 }
9077 }
9078
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009079 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009080 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009081 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009082 while (*pf != NUL && *pf != word[i + k])
9083 ++pf;
9084 if (*pf == NUL)
9085 continue;
9086 ++k;
9087 }
9088 s = smp[n].sm_rules;
9089 pri = 5; /* default priority */
9090
9091 p0 = *s;
9092 k0 = k;
9093 while (*s == '-' && k > 1)
9094 {
9095 k--;
9096 s++;
9097 }
9098 if (*s == '<')
9099 s++;
9100 if (VIM_ISDIGIT(*s))
9101 {
9102 /* determine priority */
9103 pri = *s - '0';
9104 s++;
9105 }
9106 if (*s == '^' && *(s + 1) == '^')
9107 s++;
9108
9109 if (*s == NUL
9110 || (*s == '^'
9111 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009112 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009113 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009114 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009115 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009116 && spell_iswordp_w(word + i - 1, curbuf)
9117 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009118 {
9119 /* search for followup rules, if: */
9120 /* followup and k > 1 and NO '-' in searchstring */
9121 c0 = word[i + k - 1];
9122 n0 = slang->sl_sal_first[c0 & 0xff];
9123
9124 if (slang->sl_followup && k > 1 && n0 >= 0
9125 && p0 != '-' && word[i + k] != NUL)
9126 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009127 /* Test follow-up rule for "word[i + k]"; loop over
9128 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009129 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9130 == (c0 & 0xff); ++n0)
9131 {
9132 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009133 */
9134 if (c0 != ws[0])
9135 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009136 k0 = smp[n0].sm_leadlen;
9137 if (k0 > 1)
9138 {
9139 if (word[i + k] != ws[1])
9140 continue;
9141 if (k0 > 2)
9142 {
9143 pf = word + i + k + 1;
9144 for (j = 2; j < k0; ++j)
9145 if (*pf++ != ws[j])
9146 break;
9147 if (j < k0)
9148 continue;
9149 }
9150 }
9151 k0 += k - 1;
9152
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009153 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009154 {
9155 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009156 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009157 while (*pf != NUL && *pf != word[i + k0])
9158 ++pf;
9159 if (*pf == NUL)
9160 continue;
9161 ++k0;
9162 }
9163
9164 p0 = 5;
9165 s = smp[n0].sm_rules;
9166 while (*s == '-')
9167 {
9168 /* "k0" gets NOT reduced because
9169 * "if (k0 == k)" */
9170 s++;
9171 }
9172 if (*s == '<')
9173 s++;
9174 if (VIM_ISDIGIT(*s))
9175 {
9176 p0 = *s - '0';
9177 s++;
9178 }
9179
9180 if (*s == NUL
9181 /* *s == '^' cuts */
9182 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009183 && !spell_iswordp_w(word + i + k0,
9184 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009185 {
9186 if (k0 == k)
9187 /* this is just a piece of the string */
9188 continue;
9189
9190 if (p0 < pri)
9191 /* priority too low */
9192 continue;
9193 /* rule fits; stop search */
9194 break;
9195 }
9196 }
9197
9198 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9199 == (c0 & 0xff))
9200 continue;
9201 }
9202
9203 /* replace string */
9204 ws = smp[n].sm_to_w;
9205 s = smp[n].sm_rules;
9206 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9207 if (p0 == 1 && z == 0)
9208 {
9209 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009210 if (reslen > 0 && ws != NULL && *ws != NUL
9211 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009212 || wres[reslen - 1] == *ws))
9213 reslen--;
9214 z0 = 1;
9215 z = 1;
9216 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009217 if (ws != NULL)
9218 while (*ws != NUL && word[i + k0] != NUL)
9219 {
9220 word[i + k0] = *ws;
9221 k0++;
9222 ws++;
9223 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009224 if (k > k0)
9225 mch_memmove(word + i + k0, word + i + k,
9226 sizeof(int) * (STRLEN(word + i + k) + 1));
9227
9228 /* new "actual letter" */
9229 c = word[i];
9230 }
9231 else
9232 {
9233 /* no '<' rule used */
9234 i += k - 1;
9235 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009236 if (ws != NULL)
9237 while (*ws != NUL && ws[1] != NUL
9238 && reslen < MAXWLEN)
9239 {
9240 if (reslen == 0 || wres[reslen - 1] != *ws)
9241 wres[reslen++] = *ws;
9242 ws++;
9243 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009244 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009245 if (ws == NULL)
9246 c = NUL;
9247 else
9248 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009249 if (strstr((char *)s, "^^") != NULL)
9250 {
9251 if (c != NUL)
9252 wres[reslen++] = c;
9253 mch_memmove(word, word + i + 1,
9254 sizeof(int) * (STRLEN(word + i + 1) + 1));
9255 i = 0;
9256 z0 = 1;
9257 }
9258 }
9259 break;
9260 }
9261 }
9262 }
9263 else if (vim_iswhite(c))
9264 {
9265 c = ' ';
9266 k = 1;
9267 }
9268
9269 if (z0 == 0)
9270 {
9271 if (k && !p0 && reslen < MAXWLEN && c != NUL
9272 && (!slang->sl_collapse || reslen == 0
9273 || wres[reslen - 1] != c))
9274 /* condense only double letters */
9275 wres[reslen++] = c;
9276
9277 i++;
9278 z = 0;
9279 k = 0;
9280 }
9281 }
9282
9283 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9284 l = 0;
9285 for (n = 0; n < reslen; ++n)
9286 {
9287 l += mb_char2bytes(wres[n], res + l);
9288 if (l + MB_MAXBYTES > MAXWLEN)
9289 break;
9290 }
9291 res[l] = NUL;
9292}
9293#endif
9294
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009295/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009296 * Compute a score for two sound-a-like words.
9297 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9298 * Instead of a generic loop we write out the code. That keeps it fast by
9299 * avoiding checks that will not be possible.
9300 */
9301 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009302soundalike_score(goodstart, badstart)
9303 char_u *goodstart; /* sound-folded good word */
9304 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009305{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009306 char_u *goodsound = goodstart;
9307 char_u *badsound = badstart;
9308 int goodlen;
9309 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009310 int n;
9311 char_u *pl, *ps;
9312 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009313 int score = 0;
9314
9315 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9316 * counted so much, vowels halfway the word aren't counted at all. */
9317 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9318 {
9319 score = SCORE_DEL / 2;
9320 if (*badsound == '*')
9321 ++badsound;
9322 else
9323 ++goodsound;
9324 }
9325
9326 goodlen = STRLEN(goodsound);
9327 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009328
9329 /* Return quickly if the lenghts are too different to be fixed by two
9330 * changes. */
9331 n = goodlen - badlen;
9332 if (n < -2 || n > 2)
9333 return SCORE_MAXMAX;
9334
9335 if (n > 0)
9336 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009337 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009338 ps = badsound;
9339 }
9340 else
9341 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009342 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009343 ps = goodsound;
9344 }
9345
9346 /* Skip over the identical part. */
9347 while (*pl == *ps && *pl != NUL)
9348 {
9349 ++pl;
9350 ++ps;
9351 }
9352
9353 switch (n)
9354 {
9355 case -2:
9356 case 2:
9357 /*
9358 * Must delete two characters from "pl".
9359 */
9360 ++pl; /* first delete */
9361 while (*pl == *ps)
9362 {
9363 ++pl;
9364 ++ps;
9365 }
9366 /* strings must be equal after second delete */
9367 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009368 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009369
9370 /* Failed to compare. */
9371 break;
9372
9373 case -1:
9374 case 1:
9375 /*
9376 * Minimal one delete from "pl" required.
9377 */
9378
9379 /* 1: delete */
9380 pl2 = pl + 1;
9381 ps2 = ps;
9382 while (*pl2 == *ps2)
9383 {
9384 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009385 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009386 ++pl2;
9387 ++ps2;
9388 }
9389
9390 /* 2: delete then swap, then rest must be equal */
9391 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9392 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009393 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009394
9395 /* 3: delete then substitute, then the rest must be equal */
9396 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009397 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009398
9399 /* 4: first swap then delete */
9400 if (pl[0] == ps[1] && pl[1] == ps[0])
9401 {
9402 pl2 = pl + 2; /* swap, skip two chars */
9403 ps2 = ps + 2;
9404 while (*pl2 == *ps2)
9405 {
9406 ++pl2;
9407 ++ps2;
9408 }
9409 /* delete a char and then strings must be equal */
9410 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009411 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009412 }
9413
9414 /* 5: first substitute then delete */
9415 pl2 = pl + 1; /* substitute, skip one char */
9416 ps2 = ps + 1;
9417 while (*pl2 == *ps2)
9418 {
9419 ++pl2;
9420 ++ps2;
9421 }
9422 /* delete a char and then strings must be equal */
9423 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009424 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009425
9426 /* Failed to compare. */
9427 break;
9428
9429 case 0:
9430 /*
9431 * Lenghts are equal, thus changes must result in same length: An
9432 * insert is only possible in combination with a delete.
9433 * 1: check if for identical strings
9434 */
9435 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009436 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009437
9438 /* 2: swap */
9439 if (pl[0] == ps[1] && pl[1] == ps[0])
9440 {
9441 pl2 = pl + 2; /* swap, skip two chars */
9442 ps2 = ps + 2;
9443 while (*pl2 == *ps2)
9444 {
9445 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009446 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009447 ++pl2;
9448 ++ps2;
9449 }
9450 /* 3: swap and swap again */
9451 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9452 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009453 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009454
9455 /* 4: swap and substitute */
9456 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009457 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009458 }
9459
9460 /* 5: substitute */
9461 pl2 = pl + 1;
9462 ps2 = ps + 1;
9463 while (*pl2 == *ps2)
9464 {
9465 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009466 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009467 ++pl2;
9468 ++ps2;
9469 }
9470
9471 /* 6: substitute and swap */
9472 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9473 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009474 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009475
9476 /* 7: substitute and substitute */
9477 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009478 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009479
9480 /* 8: insert then delete */
9481 pl2 = pl;
9482 ps2 = ps + 1;
9483 while (*pl2 == *ps2)
9484 {
9485 ++pl2;
9486 ++ps2;
9487 }
9488 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009489 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009490
9491 /* 9: delete then insert */
9492 pl2 = pl + 1;
9493 ps2 = ps;
9494 while (*pl2 == *ps2)
9495 {
9496 ++pl2;
9497 ++ps2;
9498 }
9499 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009500 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009501
9502 /* Failed to compare. */
9503 break;
9504 }
9505
9506 return SCORE_MAXMAX;
9507}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509/*
9510 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009511 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009512 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009514 * It has been converted from C++ to C and modified to support multi-byte
9515 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009516 */
9517 static int
9518spell_edit_score(badword, goodword)
9519 char_u *badword;
9520 char_u *goodword;
9521{
9522 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009523 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 int j, i;
9525 int t;
9526 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009527 int pbc, pgc;
9528#ifdef FEAT_MBYTE
9529 char_u *p;
9530 int wbadword[MAXWLEN];
9531 int wgoodword[MAXWLEN];
9532
9533 if (has_mbyte)
9534 {
9535 /* Get the characters from the multi-byte strings and put them in an
9536 * int array for easy access. */
9537 for (p = badword, badlen = 0; *p != NUL; )
9538 wbadword[badlen++] = mb_ptr2char_adv(&p);
9539 ++badlen;
9540 for (p = goodword, goodlen = 0; *p != NUL; )
9541 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
9542 ++goodlen;
9543 }
9544 else
9545#endif
9546 {
9547 badlen = STRLEN(badword) + 1;
9548 goodlen = STRLEN(goodword) + 1;
9549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550
9551 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
9552#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
9554 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009555 if (cnt == NULL)
9556 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557
9558 CNT(0, 0) = 0;
9559 for (j = 1; j <= goodlen; ++j)
9560 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
9561
9562 for (i = 1; i <= badlen; ++i)
9563 {
9564 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
9565 for (j = 1; j <= goodlen; ++j)
9566 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009567#ifdef FEAT_MBYTE
9568 if (has_mbyte)
9569 {
9570 bc = wbadword[i - 1];
9571 gc = wgoodword[j - 1];
9572 }
9573 else
9574#endif
9575 {
9576 bc = badword[i - 1];
9577 gc = goodword[j - 1];
9578 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 if (bc == gc)
9580 CNT(i, j) = CNT(i - 1, j - 1);
9581 else
9582 {
9583 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009584 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
9586 else
9587 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
9588
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009589 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009591#ifdef FEAT_MBYTE
9592 if (has_mbyte)
9593 {
9594 pbc = wbadword[i - 2];
9595 pgc = wgoodword[j - 2];
9596 }
9597 else
9598#endif
9599 {
9600 pbc = badword[i - 2];
9601 pgc = goodword[j - 2];
9602 }
9603 if (bc == pgc && pbc == gc)
9604 {
9605 t = SCORE_SWAP + CNT(i - 2, j - 2);
9606 if (t < CNT(i, j))
9607 CNT(i, j) = t;
9608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 }
9610 t = SCORE_DEL + CNT(i - 1, j);
9611 if (t < CNT(i, j))
9612 CNT(i, j) = t;
9613 t = SCORE_INS + CNT(i, j - 1);
9614 if (t < CNT(i, j))
9615 CNT(i, j) = t;
9616 }
9617 }
9618 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009619
9620 i = CNT(badlen - 1, goodlen - 1);
9621 vim_free(cnt);
9622 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009624
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009625/*
9626 * ":spelldump"
9627 */
9628/*ARGSUSED*/
9629 void
9630ex_spelldump(eap)
9631 exarg_T *eap;
9632{
9633 buf_T *buf = curbuf;
9634 langp_T *lp;
9635 slang_T *slang;
9636 idx_T arridx[MAXWLEN];
9637 int curi[MAXWLEN];
9638 char_u word[MAXWLEN];
9639 int c;
9640 char_u *byts;
9641 idx_T *idxs;
9642 linenr_T lnum = 0;
9643 int round;
9644 int depth;
9645 int n;
9646 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009647 char_u *region_names = NULL; /* region names being used */
9648 int do_region = TRUE; /* dump region names and numbers */
9649 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009650
9651 if (no_spell_checking())
9652 return;
9653
9654 /* Create a new empty buffer by splitting the window. */
9655 do_cmdline_cmd((char_u *)"new");
9656 if (!bufempty() || !buf_valid(buf))
9657 return;
9658
Bram Moolenaar7887d882005-07-01 22:33:52 +00009659 /* Find out if we can support regions: All languages must support the same
9660 * regions or none at all. */
9661 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9662 {
9663 p = lp->lp_slang->sl_regions;
9664 if (p[0] != 0)
9665 {
9666 if (region_names == NULL) /* first language with regions */
9667 region_names = p;
9668 else if (STRCMP(region_names, p) != 0)
9669 {
9670 do_region = FALSE; /* region names are different */
9671 break;
9672 }
9673 }
9674 }
9675
9676 if (do_region && region_names != NULL)
9677 {
9678 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
9679 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9680 }
9681 else
9682 do_region = FALSE;
9683
9684 /*
9685 * Loop over all files loaded for the entries in 'spelllang'.
9686 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009687 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
9688 {
9689 slang = lp->lp_slang;
9690
9691 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
9692 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
9693
9694 /* round 1: case-folded tree
9695 * round 2: keep-case tree */
9696 for (round = 1; round <= 2; ++round)
9697 {
9698 if (round == 1)
9699 {
9700 byts = slang->sl_fbyts;
9701 idxs = slang->sl_fidxs;
9702 }
9703 else
9704 {
9705 byts = slang->sl_kbyts;
9706 idxs = slang->sl_kidxs;
9707 }
9708 if (byts == NULL)
9709 continue; /* array is empty */
9710
9711 depth = 0;
9712 arridx[0] = 0;
9713 curi[0] = 1;
9714 while (depth >= 0 && !got_int)
9715 {
9716 if (curi[depth] > byts[arridx[depth]])
9717 {
9718 /* Done all bytes at this node, go up one level. */
9719 --depth;
9720 line_breakcheck();
9721 }
9722 else
9723 {
9724 /* Do one more byte at this node. */
9725 n = arridx[depth] + curi[depth];
9726 ++curi[depth];
9727 c = byts[n];
9728 if (c == 0)
9729 {
9730 /* End of word, deal with the word.
9731 * Don't use keep-case words in the fold-case tree,
9732 * they will appear in the keep-case tree.
9733 * Only use the word when the region matches. */
9734 flags = (int)idxs[n];
9735 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009736 && (do_region
9737 || (flags & WF_REGION) == 0
9738 || (((unsigned)flags >> 8)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009739 & lp->lp_region) != 0))
9740 {
9741 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009742 if (!do_region)
9743 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009744
9745 /* Dump the basic word if there is no prefix or
9746 * when it's the first one. */
9747 c = (unsigned)flags >> 16;
9748 if (c == 0 || curi[depth] == 2)
9749 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009750
9751 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00009752 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009753 lnum = apply_prefixes(slang, word, round,
9754 flags, lnum);
9755 }
9756 }
9757 else
9758 {
9759 /* Normal char, go one level deeper. */
9760 word[depth++] = c;
9761 arridx[depth] = idxs[n];
9762 curi[depth] = 1;
9763 }
9764 }
9765 }
9766 }
9767 }
9768
9769 /* Delete the empty line that we started with. */
9770 if (curbuf->b_ml.ml_line_count > 1)
9771 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
9772
9773 redraw_later(NOT_VALID);
9774}
9775
9776/*
9777 * Dump one word: apply case modifications and append a line to the buffer.
9778 */
9779 static void
9780dump_word(word, round, flags, lnum)
9781 char_u *word;
9782 int round;
9783 int flags;
9784 linenr_T lnum;
9785{
9786 int keepcap = FALSE;
9787 char_u *p;
9788 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00009789 char_u badword[MAXWLEN + 10];
9790 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009791
9792 if (round == 1 && (flags & WF_CAPMASK) != 0)
9793 {
9794 /* Need to fix case according to "flags". */
9795 make_case_word(word, cword, flags);
9796 p = cword;
9797 }
9798 else
9799 {
9800 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009801 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
9802 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009803 keepcap = TRUE;
9804 }
9805
Bram Moolenaar7887d882005-07-01 22:33:52 +00009806 /* Add flags and regions after a slash. */
9807 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009808 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009809 STRCPY(badword, p);
9810 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009811 if (keepcap)
9812 STRCAT(badword, "=");
9813 if (flags & WF_BANNED)
9814 STRCAT(badword, "!");
9815 else if (flags & WF_RARE)
9816 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009817 if (flags & WF_REGION)
9818 for (i = 0; i < 7; ++i)
9819 if (flags & (0x100 << i))
9820 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009821 p = badword;
9822 }
9823
9824 ml_append(lnum, p, (colnr_T)0, FALSE);
9825}
9826
9827/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009828 * For ":spelldump": Find matching prefixes for "word". Prepend each to
9829 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009830 * Return the updated line number.
9831 */
9832 static linenr_T
9833apply_prefixes(slang, word, round, flags, startlnum)
9834 slang_T *slang;
9835 char_u *word; /* case-folded word */
9836 int round;
9837 int flags; /* flags with prefix ID */
9838 linenr_T startlnum;
9839{
9840 idx_T arridx[MAXWLEN];
9841 int curi[MAXWLEN];
9842 char_u prefix[MAXWLEN];
9843 int c;
9844 char_u *byts;
9845 idx_T *idxs;
9846 linenr_T lnum = startlnum;
9847 int depth;
9848 int n;
9849 int len;
9850 int prefid = (unsigned)flags >> 16;
9851 int i;
9852
9853 byts = slang->sl_pbyts;
9854 idxs = slang->sl_pidxs;
9855 if (byts != NULL) /* array not is empty */
9856 {
9857 /*
9858 * Loop over all prefixes, building them byte-by-byte in prefix[].
9859 * When at the end of a prefix check that it supports "prefid".
9860 */
9861 depth = 0;
9862 arridx[0] = 0;
9863 curi[0] = 1;
9864 while (depth >= 0 && !got_int)
9865 {
9866 len = arridx[depth];
9867 if (curi[depth] > byts[len])
9868 {
9869 /* Done all bytes at this node, go up one level. */
9870 --depth;
9871 line_breakcheck();
9872 }
9873 else
9874 {
9875 /* Do one more byte at this node. */
9876 n = len + curi[depth];
9877 ++curi[depth];
9878 c = byts[n];
9879 if (c == 0)
9880 {
9881 /* End of prefix, find out how many IDs there are. */
9882 for (i = 1; i < len; ++i)
9883 if (byts[n + i] != 0)
9884 break;
9885 curi[depth] += i - 1;
9886
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009887 i = valid_word_prefix(i, n, prefid, word, slang);
9888 if (i != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009889 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009890 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009891 dump_word(prefix, round,
9892 (i & WF_RAREPFX) ? (flags | WF_RARE)
9893 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009894 }
9895 }
9896 else
9897 {
9898 /* Normal char, go one level deeper. */
9899 prefix[depth++] = c;
9900 arridx[depth] = idxs[n];
9901 curi[depth] = 1;
9902 }
9903 }
9904 }
9905 }
9906
9907 return lnum;
9908}
9909
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009910#endif /* FEAT_SYN_HL */