Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 1 | /* 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 Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 12 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 13 | * 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 Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 16 | * |
| 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 Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 33 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 34 | * There are two word trees: one with case-folded words and one with words in |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 35 | * original case. The second one is only used for keep-case words and is |
| 36 | * usually small. |
| 37 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 38 | * 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 Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 44 | * Thanks to Olaf Seibert for providing an example implementation of this tree |
| 45 | * and the compression mechanism. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 46 | * |
| 47 | * Matching involves checking the caps type: Onecap ALLCAP KeepCap. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 48 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 49 | * Why doesn't Vim use aspell/ispell/myspell/etc.? |
| 50 | * See ":help develop-spell". |
| 51 | */ |
| 52 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 53 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 54 | * 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 Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 59 | * 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 Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
| 78 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 79 | * Vim spell file format: <HEADER> |
| 80 | * <SUGGEST> |
| 81 | * <LWORDTREE> |
| 82 | * <KWORDTREE> |
| 83 | * <PREFIXTREE> |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 84 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 85 | * <HEADER>: <fileID> |
| 86 | * <regioncnt> <regionname> ... |
| 87 | * <charflagslen> <charflags> |
| 88 | * <fcharslen> <fchars> |
| 89 | * <prefcondcnt> <prefcond> ... |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 90 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 91 | * <fileID> 10 bytes "VIMspell07" |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 92 | * <regioncnt> 1 byte number of regions following (8 supported) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 93 | * <regionname> 2 bytes Region name: ca, au, etc. Lower case. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 94 | * First <regionname> is region 1. |
| 95 | * |
| 96 | * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128). |
| 97 | * <charflags> N bytes List of flags (first one is for character 128): |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 98 | * 0x01 word character CF_WORD |
| 99 | * 0x02 upper-case character CF_UPPER |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 100 | * <fcharslen> 2 bytes Number of bytes in <fchars>. |
| 101 | * <fchars> N bytes Folded characters, first one is for character 128. |
| 102 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 103 | * <prefcondcnt> 2 bytes Number of <prefcond> items following. |
| 104 | * |
| 105 | * <prefcond> : <condlen> <condstr> |
| 106 | * |
| 107 | * <condlen> 1 byte Length of <condstr>. |
| 108 | * |
| 109 | * <condstr> N bytes Condition for the prefix. |
| 110 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 111 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 112 | * <SUGGEST> : <repcount> <rep> ... |
| 113 | * <salflags> <salcount> <sal> ... |
| 114 | * <maplen> <mapstr> |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 115 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 116 | * <repcount> 2 bytes number of <rep> items, MSB first. |
| 117 | * |
| 118 | * <rep> : <repfromlen> <repfrom> <reptolen> <repto> |
| 119 | * |
| 120 | * <repfromlen> 1 byte length of <repfrom> |
| 121 | * |
| 122 | * <repfrom> N bytes "from" part of replacement |
| 123 | * |
| 124 | * <reptolen> 1 byte length of <repto> |
| 125 | * |
| 126 | * <repto> N bytes "to" part of replacement |
| 127 | * |
| 128 | * <salflags> 1 byte flags for soundsalike conversion: |
| 129 | * SAL_F0LLOWUP |
| 130 | * SAL_COLLAPSE |
| 131 | * SAL_REM_ACCENTS |
| 132 | * |
| 133 | * <sal> : <salfromlen> <salfrom> <saltolen> <salto> |
| 134 | * |
| 135 | * <salfromlen> 1 byte length of <salfrom> |
| 136 | * |
| 137 | * <salfrom> N bytes "from" part of soundsalike |
| 138 | * |
| 139 | * <saltolen> 1 byte length of <salto> |
| 140 | * |
| 141 | * <salto> N bytes "to" part of soundsalike |
| 142 | * |
| 143 | * <maplen> 2 bytes length of <mapstr>, MSB first |
| 144 | * |
| 145 | * <mapstr> N bytes String with sequences of similar characters, |
| 146 | * separated by slashes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 147 | * |
| 148 | * |
| 149 | * <LWORDTREE>: <wordtree> |
| 150 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 151 | * <KWORDTREE>: <wordtree> |
| 152 | * |
| 153 | * <PREFIXTREE>: <wordtree> |
| 154 | * |
| 155 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 156 | * <wordtree>: <nodecount> <nodedata> ... |
| 157 | * |
| 158 | * <nodecount> 4 bytes Number of nodes following. MSB first. |
| 159 | * |
| 160 | * <nodedata>: <siblingcount> <sibling> ... |
| 161 | * |
| 162 | * <siblingcount> 1 byte Number of siblings in this node. The siblings |
| 163 | * follow in sorted order. |
| 164 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 165 | * <sibling>: <byte> [ <nodeidx> <xbyte> |
| 166 | * | <flags> [<region>] [<prefixID>] |
| 167 | * | <prefixID> <prefcondnr> ] |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 168 | * |
| 169 | * <byte> 1 byte Byte value of the sibling. Special cases: |
| 170 | * BY_NOFLAGS: End of word without flags and for all |
| 171 | * regions. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 172 | * BY_FLAGS: End of word, <flags> follow. For |
| 173 | * PREFIXTREE <prefixID> and <prefcondnr> |
| 174 | * follow. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 175 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
| 176 | * and <xbyte> follow. |
| 177 | * |
| 178 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 179 | * |
| 180 | * <xbyte> 1 byte byte value of the sibling. |
| 181 | * |
| 182 | * <flags> 1 byte bitmask of: |
| 183 | * WF_ALLCAP word must have only capitals |
| 184 | * WF_ONECAP first char of word must be capital |
| 185 | * WF_RARE rare word |
| 186 | * WF_REGION <region> follows |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 187 | * WF_PFX <prefixID> follows |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 188 | * |
| 189 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 190 | * omitted it's valid in all regions. |
| 191 | * Lowest bit is for region 1. |
| 192 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 193 | * <prefixID> 1 byte ID of prefix that can be used with this word. For |
| 194 | * PREFIXTREE used for the required prefix ID. |
| 195 | * |
| 196 | * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list |
| 197 | * from HEADER. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 198 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 199 | * All text characters are in 'encoding', but stored as single bytes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 200 | */ |
| 201 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 202 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 203 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 204 | #endif |
| 205 | |
| 206 | #include "vim.h" |
| 207 | |
| 208 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 209 | |
| 210 | #ifdef HAVE_FCNTL_H |
| 211 | # include <fcntl.h> |
| 212 | #endif |
| 213 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 214 | #define MAXWLEN 250 /* Assume max. word len is this many bytes. |
| 215 | Some places assume a word length fits in a |
| 216 | byte, thus it can't be above 255. */ |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 217 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 218 | /* Type used for indexes in the word tree need to be at least 3 bytes. If int |
| 219 | * is 8 bytes we could use something smaller, but what? */ |
| 220 | #if SIZEOF_INT > 2 |
| 221 | typedef int idx_T; |
| 222 | #else |
| 223 | typedef long idx_T; |
| 224 | #endif |
| 225 | |
| 226 | /* Flags used for a word. Only the lowest byte can be used, the region byte |
| 227 | * comes above it. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 228 | #define WF_REGION 0x01 /* region byte follows */ |
| 229 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 230 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 231 | #define WF_RARE 0x08 /* rare word */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 232 | #define WF_BANNED 0x10 /* bad word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 233 | #define WF_PFX 0x20 /* prefix ID list follows */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 234 | #define WF_KEEPCAP 0x80 /* keep-case word */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 235 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 236 | #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 237 | |
| 238 | #define BY_NOFLAGS 0 /* end of word without flags or region */ |
| 239 | #define BY_FLAGS 1 /* end of word, flag byte follows */ |
| 240 | #define BY_INDEX 2 /* child is shared, index follows */ |
| 241 | #define BY_SPECIAL BY_INDEX /* hightest special byte value */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 242 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 243 | /* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 244 | * and si_sal. Not for sl_sal! |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 245 | * One replacement: from "ft_from" to "ft_to". */ |
| 246 | typedef struct fromto_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 247 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 248 | char_u *ft_from; |
| 249 | char_u *ft_to; |
| 250 | } fromto_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 251 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 252 | /* Info from "SAL" entries in ".aff" file used in sl_sal. |
| 253 | * The info is split for quick processing by spell_soundfold(). |
| 254 | * Note that "sm_oneof" and "sm_rules" point into sm_lead. */ |
| 255 | typedef struct salitem_S |
| 256 | { |
| 257 | char_u *sm_lead; /* leading letters */ |
| 258 | int sm_leadlen; /* length of "sm_lead" */ |
| 259 | char_u *sm_oneoff; /* letters from () or NULL */ |
| 260 | char_u *sm_rules; /* rules like ^, $, priority */ |
| 261 | char_u *sm_to; /* replacement. */ |
| 262 | } salitem_T; |
| 263 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 264 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 265 | * Structure used to store words and other info for one language, loaded from |
| 266 | * a .spl file. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 267 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 268 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 269 | * |
| 270 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 271 | * the number of possible bytes, sorted on byte value: |
| 272 | * <len> <byte1> <byte2> ... |
| 273 | * The "idxs" array stores the index of the child node corresponding to the |
| 274 | * byte in "byts". |
| 275 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
| 276 | * the flags and region for the word. There may be several zeros in sequence |
| 277 | * for alternative flag/region combinations. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 278 | */ |
| 279 | typedef struct slang_S slang_T; |
| 280 | struct slang_S |
| 281 | { |
| 282 | slang_T *sl_next; /* next language */ |
| 283 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 284 | char_u *sl_fname; /* name of .spl file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 285 | int sl_add; /* TRUE if it's a .add file. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 286 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 287 | char_u *sl_fbyts; /* case-folded word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 288 | idx_T *sl_fidxs; /* case-folded word indexes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 289 | char_u *sl_kbyts; /* keep-case word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 290 | idx_T *sl_kidxs; /* keep-case word indexes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 291 | char_u *sl_pbyts; /* prefix tree word bytes */ |
| 292 | idx_T *sl_pidxs; /* prefix tree word indexes */ |
| 293 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 294 | char_u sl_regions[17]; /* table with up to 8 region names plus NUL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 295 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 296 | int sl_prefixcnt; /* number of items in "sl_prefprog" */ |
| 297 | regprog_T **sl_prefprog; /* table with regprogs for prefixes */ |
| 298 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 299 | garray_T sl_rep; /* list of fromto_T entries from REP lines */ |
| 300 | short sl_rep_first[256]; /* indexes where byte first appears, -1 if |
| 301 | there is none */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 302 | garray_T sl_sal; /* list of salitem_T entries from SAL lines */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 303 | short sl_sal_first[256]; /* indexes where byte first appears, -1 if |
| 304 | there is none */ |
| 305 | int sl_followup; /* SAL followup */ |
| 306 | int sl_collapse; /* SAL collapse_result */ |
| 307 | int sl_rem_accents; /* SAL remove_accents */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 308 | int sl_has_map; /* TRUE if there is a MAP line */ |
| 309 | #ifdef FEAT_MBYTE |
| 310 | hashtab_T sl_map_hash; /* MAP for multi-byte chars */ |
| 311 | int sl_map_array[256]; /* MAP for first 256 chars */ |
| 312 | #else |
| 313 | char_u sl_map_array[256]; /* MAP for first 256 chars */ |
| 314 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 317 | /* First language that is loaded, start of the linked list of loaded |
| 318 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 319 | static slang_T *first_lang = NULL; |
| 320 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 321 | /* Flags used in .spl file for soundsalike flags. */ |
| 322 | #define SAL_F0LLOWUP 1 |
| 323 | #define SAL_COLLAPSE 2 |
| 324 | #define SAL_REM_ACCENTS 4 |
| 325 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Structure used in "b_langp", filled from 'spelllang'. |
| 328 | */ |
| 329 | typedef struct langp_S |
| 330 | { |
| 331 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 332 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 333 | } langp_T; |
| 334 | |
| 335 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 336 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 337 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 338 | |
| 339 | /* Result values. Lower number is accepted over higher one. */ |
| 340 | #define SP_BANNED -1 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 341 | #define SP_OK 0 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 342 | #define SP_RARE 1 |
| 343 | #define SP_LOCAL 2 |
| 344 | #define SP_BAD 3 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 345 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 346 | #define VIMSPELLMAGIC "VIMspell07" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 347 | #define VIMSPELLMAGICL 10 |
| 348 | |
| 349 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 350 | * Information used when looking for suggestions. |
| 351 | */ |
| 352 | typedef struct suginfo_S |
| 353 | { |
| 354 | garray_T su_ga; /* suggestions, contains "suggest_T" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 355 | int su_maxcount; /* max. number of suggestions displayed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 356 | int su_maxscore; /* maximum score for adding to su_ga */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 357 | garray_T su_sga; /* like su_ga, sound-folded scoring */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 358 | char_u *su_badptr; /* start of bad word in line */ |
| 359 | int su_badlen; /* length of detected bad word in line */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 360 | int su_badflags; /* caps flags for bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 361 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 362 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| 363 | hashtab_T su_banned; /* table with banned words */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 364 | } suginfo_T; |
| 365 | |
| 366 | /* One word suggestion. Used in "si_ga". */ |
| 367 | typedef struct suggest_S |
| 368 | { |
| 369 | char_u *st_word; /* suggested word, allocated string */ |
| 370 | int st_orglen; /* length of replaced text */ |
| 371 | int st_score; /* lower is better */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 372 | int st_altscore; /* used when st_score compares equal */ |
| 373 | int st_salscore; /* st_score is for soundalike */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 374 | int st_had_bonus; /* bonus already included in score */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 375 | } suggest_T; |
| 376 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 377 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 378 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 379 | /* Number of suggestions kept when cleaning up. When rescore_suggestions() is |
| 380 | * called the score may change, thus we need to keep more than what is |
| 381 | * displayed. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 382 | #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 383 | |
| 384 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 385 | * of suggestions that are not going to be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 386 | #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 387 | |
| 388 | /* score for various changes */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 389 | #define SCORE_SPLIT 149 /* split bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 390 | #define SCORE_ICASE 52 /* slightly different case */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 391 | #define SCORE_REGION 70 /* word is for different region */ |
| 392 | #define SCORE_RARE 180 /* rare word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 393 | #define SCORE_SWAP 90 /* swap two characters */ |
| 394 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| 395 | #define SCORE_REP 87 /* REP replacement */ |
| 396 | #define SCORE_SUBST 93 /* substitute a character */ |
| 397 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 398 | #define SCORE_DEL 94 /* delete a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 399 | #define SCORE_DELDUP 64 /* delete a duplicated character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 400 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 401 | #define SCORE_INSDUP 66 /* insert a duplicate character */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 402 | |
| 403 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 404 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 405 | |
| 406 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 407 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 408 | |
| 409 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 410 | * Structure to store info for word matching. |
| 411 | */ |
| 412 | typedef struct matchinf_S |
| 413 | { |
| 414 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 415 | |
| 416 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 417 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 418 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 419 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 420 | char_u *mi_cend; /* char after what was used for |
| 421 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 422 | |
| 423 | /* case-folded text */ |
| 424 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 425 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 426 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 427 | /* for when checking word after a prefix */ |
| 428 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 429 | prefixID/condition */ |
| 430 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 431 | int mi_prefixlen; /* byte length of prefix */ |
| 432 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 433 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 434 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 435 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 436 | } matchinf_T; |
| 437 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 438 | /* |
| 439 | * The tables used for recognizing word characters according to spelling. |
| 440 | * These are only used for the first 256 characters of 'encoding'. |
| 441 | */ |
| 442 | typedef struct spelltab_S |
| 443 | { |
| 444 | char_u st_isw[256]; /* flags: is word char */ |
| 445 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 446 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 447 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 448 | } spelltab_T; |
| 449 | |
| 450 | static spelltab_T spelltab; |
| 451 | static int did_set_spelltab; |
| 452 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 453 | #define CF_WORD 0x01 |
| 454 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 455 | |
| 456 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 457 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 458 | static int spell_iswordp __ARGS((char_u *p)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 459 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 460 | |
| 461 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 462 | * Return TRUE if "p" points to a word character. Like spell_iswordp() but |
| 463 | * without the special handling of a single quote. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 464 | * Checking for a word character is done very often, avoid the function call |
| 465 | * overhead. |
| 466 | */ |
| 467 | #ifdef FEAT_MBYTE |
| 468 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 469 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 470 | #else |
| 471 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 472 | #endif |
| 473 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 474 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 475 | * For finding suggestions: At each node in the tree these states are tried: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 476 | */ |
| 477 | typedef enum |
| 478 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 479 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 480 | * ends); if badword ends there is a match, otherwise |
| 481 | * try splitting word. */ |
| 482 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 483 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 484 | STATE_PLAIN, /* Use each byte of the node. */ |
| 485 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 486 | STATE_INS, /* Insert a byte in the bad word. */ |
| 487 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 488 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 489 | STATE_SWAP3, /* Swap two characters over three. */ |
| 490 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 491 | STATE_UNROT3L, /* Undo rotate three characters left */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 492 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 493 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 494 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 495 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 496 | STATE_FINAL /* End of this node. */ |
| 497 | } state_T; |
| 498 | |
| 499 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 500 | * Struct to keep the state at each level in suggest_try_change(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 501 | */ |
| 502 | typedef struct trystate_S |
| 503 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 504 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 505 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 506 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 507 | short ts_curi; /* index in list of child nodes */ |
| 508 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 509 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 510 | char_u ts_twordlen; /* valid length of tword[] */ |
| 511 | #ifdef FEAT_MBYTE |
| 512 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 513 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 514 | char_u ts_isdiff; /* DIFF_ values */ |
| 515 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 516 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 517 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 518 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 519 | char_u ts_save_badflags; /* su_badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 520 | } trystate_T; |
| 521 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 522 | /* values for ts_isdiff */ |
| 523 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 524 | #define DIFF_YES 1 /* different byte found */ |
| 525 | #define DIFF_INSERT 2 /* inserting character */ |
| 526 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 527 | /* mode values for find_word */ |
| 528 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 529 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 530 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 531 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 532 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 533 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 534 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 535 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 536 | static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 537 | static void find_prefix __ARGS((matchinf_T *mip)); |
| 538 | static int fold_more __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 539 | static int spell_valid_case __ARGS((int origflags, int treeflags)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 540 | static int no_spell_checking __ARGS((void)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 541 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 542 | static char_u *spell_enc __ARGS((void)); |
| 543 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 544 | static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 545 | static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 546 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 547 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 548 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 549 | static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 550 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 551 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 552 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 553 | static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 554 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 555 | static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 556 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 557 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 558 | static void suggest_try_change __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 559 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
| 560 | static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 561 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 562 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 563 | static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 564 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 565 | static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags)); |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 566 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 567 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 568 | static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 569 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 570 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 571 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 572 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 573 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 574 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 575 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 576 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 577 | static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum)); |
| 578 | static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 579 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 580 | /* |
| 581 | * Use our own character-case definitions, because the current locale may |
| 582 | * differ from what the .spl file uses. |
| 583 | * These must not be called with negative number! |
| 584 | */ |
| 585 | #ifndef FEAT_MBYTE |
| 586 | /* Non-multi-byte implementation. */ |
| 587 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 588 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 589 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 590 | #else |
| 591 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 592 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 593 | * the "w" library function for characters above 255 if available. */ |
| 594 | # ifdef HAVE_TOWLOWER |
| 595 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 596 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 597 | # else |
| 598 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 599 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 600 | # endif |
| 601 | |
| 602 | # ifdef HAVE_TOWUPPER |
| 603 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 604 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 605 | # else |
| 606 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 607 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 608 | # endif |
| 609 | |
| 610 | # ifdef HAVE_ISWUPPER |
| 611 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 612 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 613 | # else |
| 614 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 615 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 616 | # endif |
| 617 | #endif |
| 618 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 619 | |
| 620 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 624 | * "ptr" points to a character that could be the start of a word. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 625 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 626 | * or when it's OK it remains unchanged. |
| 627 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 628 | * |
| 629 | * "sug" is normally NULL. When looking for suggestions it points to |
| 630 | * suginfo_T. It's passed as a void pointer to keep the struct local. |
| 631 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 632 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 633 | * caller can skip over the word. |
| 634 | */ |
| 635 | int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 636 | spell_check(wp, ptr, attrp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 637 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 638 | char_u *ptr; |
| 639 | int *attrp; |
| 640 | { |
| 641 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 642 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 643 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 644 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 645 | /* A word never starts at a space or a control character. Return quickly |
| 646 | * then, skipping over the character. */ |
| 647 | if (*ptr <= ' ') |
| 648 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 649 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 650 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 651 | * 0X99FF. But when a word character follows do check spelling to find |
| 652 | * "3GPP". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 653 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 654 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 655 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 656 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 657 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 658 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 659 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 660 | nrlen = mi.mi_end - ptr; |
| 661 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 662 | if (!spell_iswordp(mi.mi_end)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 663 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 664 | |
| 665 | /* Try including the digits in the word. */ |
| 666 | mi.mi_fend = ptr + nrlen; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 667 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 668 | else |
| 669 | mi.mi_fend = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 670 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 671 | /* Find the normal end of the word (until the next non-word character). */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 672 | mi.mi_word = ptr; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 673 | if (spell_iswordp(mi.mi_fend)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 674 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 675 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 676 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 677 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 678 | } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 679 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 680 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 681 | /* We always use the characters up to the next non-word character, |
| 682 | * also for bad words. */ |
| 683 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 684 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 685 | /* Check caps type later. */ |
| 686 | mi.mi_capflags = 0; |
| 687 | mi.mi_cend = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 688 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 689 | /* Include one non-word character so that we can check for the |
| 690 | * word end. */ |
| 691 | if (*mi.mi_fend != NUL) |
| 692 | mb_ptr_adv(mi.mi_fend); |
| 693 | |
| 694 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 695 | MAXWLEN + 1); |
| 696 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 697 | |
| 698 | /* The word is bad unless we recognize it. */ |
| 699 | mi.mi_result = SP_BAD; |
| 700 | |
| 701 | /* |
| 702 | * Loop over the languages specified in 'spelllang'. |
| 703 | * We check them all, because a matching word may be longer than an |
| 704 | * already found matching word. |
| 705 | */ |
| 706 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 707 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 708 | { |
| 709 | /* Check for a matching word in case-folded words. */ |
| 710 | find_word(&mi, FIND_FOLDWORD); |
| 711 | |
| 712 | /* Check for a matching word in keep-case words. */ |
| 713 | find_word(&mi, FIND_KEEPWORD); |
| 714 | |
| 715 | /* Check for matching prefixes. */ |
| 716 | find_prefix(&mi); |
| 717 | } |
| 718 | |
| 719 | if (mi.mi_result != SP_OK) |
| 720 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 721 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 722 | * rare and local words, e.g., "3GPP". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 723 | if (nrlen > 0) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 724 | { |
| 725 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 726 | return nrlen; |
| 727 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 728 | |
| 729 | /* When we are at a non-word character there is no error, just |
| 730 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 731 | else if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 732 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 733 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 734 | if (has_mbyte) |
| 735 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 736 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 737 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 738 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 739 | |
| 740 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 741 | *attrp = highlight_attr[HLF_SPB]; |
| 742 | else if (mi.mi_result == SP_RARE) |
| 743 | *attrp = highlight_attr[HLF_SPR]; |
| 744 | else |
| 745 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 748 | return (int)(mi.mi_end - ptr); |
| 749 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 750 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 751 | /* |
| 752 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 753 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 754 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 755 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 756 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 757 | * |
| 758 | * For a match mip->mi_result is updated. |
| 759 | */ |
| 760 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 761 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 762 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 763 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 764 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 765 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 766 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 767 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 768 | int endidxcnt = 0; |
| 769 | int len; |
| 770 | int wlen = 0; |
| 771 | int flen; |
| 772 | int c; |
| 773 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 774 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 775 | #ifdef FEAT_MBYTE |
| 776 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 777 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 778 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 779 | int res = SP_BAD; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 780 | slang_T *slang = mip->mi_lp->lp_slang; |
| 781 | unsigned flags; |
| 782 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 783 | idx_T *idxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 784 | int prefid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 785 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 786 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 787 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 788 | /* Check for word with matching case in keep-case tree. */ |
| 789 | ptr = mip->mi_word; |
| 790 | flen = 9999; /* no case folding, always enough bytes */ |
| 791 | byts = slang->sl_kbyts; |
| 792 | idxs = slang->sl_kidxs; |
| 793 | } |
| 794 | else |
| 795 | { |
| 796 | /* Check for case-folded in case-folded tree. */ |
| 797 | ptr = mip->mi_fword; |
| 798 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 799 | byts = slang->sl_fbyts; |
| 800 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 801 | |
| 802 | if (mode == FIND_PREFIX) |
| 803 | { |
| 804 | /* Skip over the prefix. */ |
| 805 | wlen = mip->mi_prefixlen; |
| 806 | flen -= mip->mi_prefixlen; |
| 807 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 810 | if (byts == NULL) |
| 811 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 812 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 813 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 814 | * Repeat advancing in the tree until: |
| 815 | * - there is a byte that doesn't match, |
| 816 | * - we reach the end of the tree, |
| 817 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 818 | */ |
| 819 | for (;;) |
| 820 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 821 | if (flen <= 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 822 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 823 | |
| 824 | len = byts[arridx++]; |
| 825 | |
| 826 | /* If the first possible byte is a zero the word could end here. |
| 827 | * Remember this index, we first check for the longest word. */ |
| 828 | if (byts[arridx] == 0) |
| 829 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 830 | if (endidxcnt == MAXWLEN) |
| 831 | { |
| 832 | /* Must be a corrupted spell file. */ |
| 833 | EMSG(_(e_format)); |
| 834 | return; |
| 835 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 836 | endlen[endidxcnt] = wlen; |
| 837 | endidx[endidxcnt++] = arridx++; |
| 838 | --len; |
| 839 | |
| 840 | /* Skip over the zeros, there can be several flag/region |
| 841 | * combinations. */ |
| 842 | while (len > 0 && byts[arridx] == 0) |
| 843 | { |
| 844 | ++arridx; |
| 845 | --len; |
| 846 | } |
| 847 | if (len == 0) |
| 848 | break; /* no children, word must end here */ |
| 849 | } |
| 850 | |
| 851 | /* Stop looking at end of the line. */ |
| 852 | if (ptr[wlen] == NUL) |
| 853 | break; |
| 854 | |
| 855 | /* Perform a binary search in the list of accepted bytes. */ |
| 856 | c = ptr[wlen]; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 857 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 858 | c = ' '; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 859 | lo = arridx; |
| 860 | hi = arridx + len - 1; |
| 861 | while (lo < hi) |
| 862 | { |
| 863 | m = (lo + hi) / 2; |
| 864 | if (byts[m] > c) |
| 865 | hi = m - 1; |
| 866 | else if (byts[m] < c) |
| 867 | lo = m + 1; |
| 868 | else |
| 869 | { |
| 870 | lo = hi = m; |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /* Stop if there is no matching byte. */ |
| 876 | if (hi < lo || byts[lo] != c) |
| 877 | break; |
| 878 | |
| 879 | /* Continue at the child (if there is one). */ |
| 880 | arridx = idxs[lo]; |
| 881 | ++wlen; |
| 882 | --flen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 883 | |
| 884 | /* One space in the good word may stand for several spaces in the |
| 885 | * checked word. */ |
| 886 | if (c == ' ') |
| 887 | { |
| 888 | for (;;) |
| 889 | { |
| 890 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 891 | flen = fold_more(mip); |
| 892 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 893 | break; |
| 894 | ++wlen; |
| 895 | --flen; |
| 896 | } |
| 897 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Verify that one of the possible endings is valid. Try the longest |
| 902 | * first. |
| 903 | */ |
| 904 | while (endidxcnt > 0) |
| 905 | { |
| 906 | --endidxcnt; |
| 907 | arridx = endidx[endidxcnt]; |
| 908 | wlen = endlen[endidxcnt]; |
| 909 | |
| 910 | #ifdef FEAT_MBYTE |
| 911 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 912 | continue; /* not at first byte of character */ |
| 913 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 914 | if (spell_iswordp(ptr + wlen)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 915 | continue; /* next char is a word character */ |
| 916 | |
| 917 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 918 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 919 | { |
| 920 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 921 | * when folding case. This can be slow, take a shortcut when the |
| 922 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 923 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 924 | if (STRNCMP(ptr, p, wlen) != 0) |
| 925 | { |
| 926 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 927 | mb_ptr_adv(p); |
| 928 | wlen = p - mip->mi_word; |
| 929 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 930 | } |
| 931 | #endif |
| 932 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 933 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 934 | * prefix ID. |
| 935 | * Repeat this if there are more flags/region alternatives until there |
| 936 | * is a match. */ |
| 937 | res = SP_BAD; |
| 938 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 939 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 940 | { |
| 941 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 942 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 943 | /* For the fold-case tree check that the case of the checked word |
| 944 | * matches with what the word in the tree requires. |
| 945 | * For keep-case tree the case is always right. For prefixes we |
| 946 | * don't bother to check. */ |
| 947 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 948 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 949 | if (mip->mi_cend != mip->mi_word + wlen) |
| 950 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 951 | /* mi_capflags was set for a different word length, need |
| 952 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 953 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 954 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 957 | if (mip->mi_capflags == WF_KEEPCAP |
| 958 | || !spell_valid_case(mip->mi_capflags, flags)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 959 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 962 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 963 | * check the prefix ID and the condition. Do that for the list at |
| 964 | * mip->mi_prefarridx. */ |
| 965 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 966 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 967 | /* The prefix ID is stored two bytes above the flags. */ |
| 968 | prefid = (unsigned)flags >> 16; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 969 | if (!valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
| 970 | prefid, mip->mi_fword + mip->mi_prefixlen, |
| 971 | slang)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 972 | continue; |
| 973 | } |
| 974 | |
| 975 | if (flags & WF_BANNED) |
| 976 | res = SP_BANNED; |
| 977 | else if (flags & WF_REGION) |
| 978 | { |
| 979 | /* Check region. */ |
| 980 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 981 | res = SP_OK; |
| 982 | else |
| 983 | res = SP_LOCAL; |
| 984 | } |
| 985 | else if (flags & WF_RARE) |
| 986 | res = SP_RARE; |
| 987 | else |
| 988 | res = SP_OK; |
| 989 | |
| 990 | /* Always use the longest match and the best result. */ |
| 991 | if (mip->mi_result > res) |
| 992 | { |
| 993 | mip->mi_result = res; |
| 994 | mip->mi_end = mip->mi_word + wlen; |
| 995 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 996 | else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 997 | mip->mi_end = mip->mi_word + wlen; |
| 998 | |
| 999 | if (res == SP_OK) |
| 1000 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1003 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1004 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1005 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1008 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1009 | * Return TRUE if the prefix indicated by "mip->mi_prefarridx" matches with |
| 1010 | * the prefix ID "prefid" for the word "word". |
| 1011 | */ |
| 1012 | static int |
| 1013 | valid_word_prefix(totprefcnt, arridx, prefid, word, slang) |
| 1014 | int totprefcnt; /* nr of prefix IDs */ |
| 1015 | int arridx; /* idx in sl_pidxs[] */ |
| 1016 | int prefid; |
| 1017 | char_u *word; |
| 1018 | slang_T *slang; |
| 1019 | { |
| 1020 | int prefcnt; |
| 1021 | int pidx; |
| 1022 | regprog_T *rp; |
| 1023 | regmatch_T regmatch; |
| 1024 | |
| 1025 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1026 | { |
| 1027 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1028 | |
| 1029 | /* Check the prefix ID. */ |
| 1030 | if (prefid != (pidx & 0xff)) |
| 1031 | continue; |
| 1032 | |
| 1033 | /* Check the condition, if there is one. The condition index is |
| 1034 | * stored above the prefix ID byte. */ |
| 1035 | rp = slang->sl_prefprog[(unsigned)pidx >> 8]; |
| 1036 | if (rp != NULL) |
| 1037 | { |
| 1038 | regmatch.regprog = rp; |
| 1039 | regmatch.rm_ic = FALSE; |
| 1040 | if (!vim_regexec(®match, word, 0)) |
| 1041 | continue; |
| 1042 | } |
| 1043 | |
| 1044 | /* It's a match! */ |
| 1045 | return TRUE; |
| 1046 | } |
| 1047 | return FALSE; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1051 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1052 | * If it does, then check the following word. |
| 1053 | * |
| 1054 | * For a match mip->mi_result is updated. |
| 1055 | */ |
| 1056 | static void |
| 1057 | find_prefix(mip) |
| 1058 | matchinf_T *mip; |
| 1059 | { |
| 1060 | idx_T arridx = 0; |
| 1061 | int len; |
| 1062 | int wlen = 0; |
| 1063 | int flen; |
| 1064 | int c; |
| 1065 | char_u *ptr; |
| 1066 | idx_T lo, hi, m; |
| 1067 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1068 | char_u *byts; |
| 1069 | idx_T *idxs; |
| 1070 | |
| 1071 | /* We use the case-folded word here, since prefixes are always |
| 1072 | * case-folded. */ |
| 1073 | ptr = mip->mi_fword; |
| 1074 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 1075 | byts = slang->sl_pbyts; |
| 1076 | idxs = slang->sl_pidxs; |
| 1077 | |
| 1078 | if (byts == NULL) |
| 1079 | return; /* array is empty */ |
| 1080 | |
| 1081 | /* |
| 1082 | * Repeat advancing in the tree until: |
| 1083 | * - there is a byte that doesn't match, |
| 1084 | * - we reach the end of the tree, |
| 1085 | * - or we reach the end of the line. |
| 1086 | */ |
| 1087 | for (;;) |
| 1088 | { |
| 1089 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1090 | flen = fold_more(mip); |
| 1091 | |
| 1092 | len = byts[arridx++]; |
| 1093 | |
| 1094 | /* If the first possible byte is a zero the prefix could end here. |
| 1095 | * Check if the following word matches and supports the prefix. */ |
| 1096 | if (byts[arridx] == 0) |
| 1097 | { |
| 1098 | /* There can be several prefixes with different conditions. We |
| 1099 | * try them all, since we don't know which one will give the |
| 1100 | * longest match. The word is the same each time, pass the list |
| 1101 | * of possible prefixes to find_word(). */ |
| 1102 | mip->mi_prefarridx = arridx; |
| 1103 | mip->mi_prefcnt = len; |
| 1104 | while (len > 0 && byts[arridx] == 0) |
| 1105 | { |
| 1106 | ++arridx; |
| 1107 | --len; |
| 1108 | } |
| 1109 | mip->mi_prefcnt -= len; |
| 1110 | |
| 1111 | /* Find the word that comes after the prefix. */ |
| 1112 | mip->mi_prefixlen = wlen; |
| 1113 | find_word(mip, FIND_PREFIX); |
| 1114 | |
| 1115 | |
| 1116 | if (len == 0) |
| 1117 | break; /* no children, word must end here */ |
| 1118 | } |
| 1119 | |
| 1120 | /* Stop looking at end of the line. */ |
| 1121 | if (ptr[wlen] == NUL) |
| 1122 | break; |
| 1123 | |
| 1124 | /* Perform a binary search in the list of accepted bytes. */ |
| 1125 | c = ptr[wlen]; |
| 1126 | lo = arridx; |
| 1127 | hi = arridx + len - 1; |
| 1128 | while (lo < hi) |
| 1129 | { |
| 1130 | m = (lo + hi) / 2; |
| 1131 | if (byts[m] > c) |
| 1132 | hi = m - 1; |
| 1133 | else if (byts[m] < c) |
| 1134 | lo = m + 1; |
| 1135 | else |
| 1136 | { |
| 1137 | lo = hi = m; |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | /* Stop if there is no matching byte. */ |
| 1143 | if (hi < lo || byts[lo] != c) |
| 1144 | break; |
| 1145 | |
| 1146 | /* Continue at the child (if there is one). */ |
| 1147 | arridx = idxs[lo]; |
| 1148 | ++wlen; |
| 1149 | --flen; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Need to fold at least one more character. Do until next non-word character |
| 1155 | * for efficiency. |
| 1156 | * Return the length of the folded chars in bytes. |
| 1157 | */ |
| 1158 | static int |
| 1159 | fold_more(mip) |
| 1160 | matchinf_T *mip; |
| 1161 | { |
| 1162 | int flen; |
| 1163 | char_u *p; |
| 1164 | |
| 1165 | p = mip->mi_fend; |
| 1166 | do |
| 1167 | { |
| 1168 | mb_ptr_adv(mip->mi_fend); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1169 | } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1170 | |
| 1171 | /* Include the non-word character so that we can check for the |
| 1172 | * word end. */ |
| 1173 | if (*mip->mi_fend != NUL) |
| 1174 | mb_ptr_adv(mip->mi_fend); |
| 1175 | |
| 1176 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1177 | mip->mi_fword + mip->mi_fwordlen, |
| 1178 | MAXWLEN - mip->mi_fwordlen); |
| 1179 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1180 | mip->mi_fwordlen += flen; |
| 1181 | return flen; |
| 1182 | } |
| 1183 | |
| 1184 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1185 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1186 | * case. |
| 1187 | */ |
| 1188 | static int |
| 1189 | spell_valid_case(origflags, treeflags) |
| 1190 | int origflags; /* flags for the checked word. */ |
| 1191 | int treeflags; /* flags for the word in the spell tree */ |
| 1192 | { |
| 1193 | return (origflags == WF_ALLCAP |
| 1194 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
| 1195 | && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP))); |
| 1196 | } |
| 1197 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1198 | /* |
| 1199 | * Return TRUE if spell checking is not enabled. |
| 1200 | */ |
| 1201 | static int |
| 1202 | no_spell_checking() |
| 1203 | { |
| 1204 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 1205 | { |
| 1206 | EMSG(_("E756: Spell checking is not enabled")); |
| 1207 | return TRUE; |
| 1208 | } |
| 1209 | return FALSE; |
| 1210 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1211 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1212 | /* |
| 1213 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1214 | * "curline" is TRUE for "z?": find word under/after cursor in the same line. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1215 | * Return OK if found, FAIL otherwise. |
| 1216 | */ |
| 1217 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1218 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1219 | int dir; /* FORWARD or BACKWARD */ |
| 1220 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1221 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1222 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1223 | linenr_T lnum; |
| 1224 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1225 | char_u *line; |
| 1226 | char_u *p; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1227 | char_u *endp; |
| 1228 | int attr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1229 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1230 | int has_syntax = syntax_present(curbuf); |
| 1231 | int col; |
| 1232 | int can_spell; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1233 | char_u *buf = NULL; |
| 1234 | int buflen = 0; |
| 1235 | int skip = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1236 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1237 | if (no_spell_checking()) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1238 | return FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1239 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1240 | /* |
| 1241 | * Start looking for bad word at the start of the line, because we can't |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1242 | * start halfway a word, we don't know where the it starts or ends. |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1243 | * |
| 1244 | * When searching backwards, we continue in the line to find the last |
| 1245 | * bad word (in the cursor line: before the cursor). |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1246 | * |
| 1247 | * We concatenate the start of the next line, so that wrapped words work |
| 1248 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 1249 | * though... |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1250 | */ |
| 1251 | lnum = curwin->w_cursor.lnum; |
| 1252 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1253 | |
| 1254 | while (!got_int) |
| 1255 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1256 | line = ml_get(lnum); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1257 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1258 | len = STRLEN(line); |
| 1259 | if (buflen < len + MAXWLEN + 2) |
| 1260 | { |
| 1261 | vim_free(buf); |
| 1262 | buflen = len + MAXWLEN + 2; |
| 1263 | buf = alloc(buflen); |
| 1264 | if (buf == NULL) |
| 1265 | break; |
| 1266 | } |
| 1267 | |
| 1268 | /* Copy the line into "buf" and append the start of the next line if |
| 1269 | * possible. */ |
| 1270 | STRCPY(buf, line); |
| 1271 | if (lnum < curbuf->b_ml.ml_line_count) |
| 1272 | spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN); |
| 1273 | |
| 1274 | p = buf + skip; |
| 1275 | endp = buf + len; |
| 1276 | while (p < endp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1277 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1278 | /* When searching backward don't search after the cursor. */ |
| 1279 | if (dir == BACKWARD |
| 1280 | && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1281 | && (colnr_T)(p - buf) >= curwin->w_cursor.col) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1282 | break; |
| 1283 | |
| 1284 | /* start of word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1285 | attr = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1286 | len = spell_check(curwin, p, &attr); |
| 1287 | |
| 1288 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1289 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1290 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1291 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1292 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1293 | /* When searching forward only accept a bad word after |
| 1294 | * the cursor. */ |
| 1295 | if (dir == BACKWARD |
| 1296 | || lnum > curwin->w_cursor.lnum |
| 1297 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1298 | && (colnr_T)(curline ? p - buf + len |
| 1299 | : p - buf) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1300 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1301 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1302 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1303 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1304 | col = p - buf; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1305 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1306 | FALSE, &can_spell); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1307 | } |
| 1308 | else |
| 1309 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1310 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1311 | if (can_spell) |
| 1312 | { |
| 1313 | found_pos.lnum = lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1314 | found_pos.col = p - buf; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1315 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1316 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1317 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1318 | if (dir == FORWARD) |
| 1319 | { |
| 1320 | /* No need to search further. */ |
| 1321 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1322 | vim_free(buf); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1323 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1326 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1327 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1330 | /* advance to character after the word */ |
| 1331 | p += len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1334 | if (curline) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1335 | break; /* only check cursor line */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1336 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1337 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1338 | if (dir == BACKWARD) |
| 1339 | { |
| 1340 | if (found_pos.lnum != 0) |
| 1341 | { |
| 1342 | /* Use the last match in the line. */ |
| 1343 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1344 | vim_free(buf); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1345 | return OK; |
| 1346 | } |
| 1347 | if (lnum == 1) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1348 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1349 | --lnum; |
| 1350 | } |
| 1351 | else |
| 1352 | { |
| 1353 | if (lnum == curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1354 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1355 | ++lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1356 | |
| 1357 | /* Skip the characters at the start of the next line that were |
| 1358 | * included in a match crossing line boundaries. */ |
| 1359 | if (attr == 0) |
| 1360 | skip = p - endp; |
| 1361 | else |
| 1362 | skip = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1363 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1364 | |
| 1365 | line_breakcheck(); |
| 1366 | } |
| 1367 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1368 | vim_free(buf); |
| 1369 | return FAIL; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * For spell checking: concatenate the start of the following line "line" into |
| 1374 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| 1375 | */ |
| 1376 | void |
| 1377 | spell_cat_line(buf, line, maxlen) |
| 1378 | char_u *buf; |
| 1379 | char_u *line; |
| 1380 | int maxlen; |
| 1381 | { |
| 1382 | char_u *p; |
| 1383 | int n; |
| 1384 | |
| 1385 | p = skipwhite(line); |
| 1386 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 1387 | p = skipwhite(p + 1); |
| 1388 | |
| 1389 | if (*p != NUL) |
| 1390 | { |
| 1391 | *buf = ' '; |
| 1392 | vim_strncpy(buf + 1, line, maxlen - 1); |
| 1393 | n = p - line; |
| 1394 | if (n >= maxlen) |
| 1395 | n = maxlen - 1; |
| 1396 | vim_memset(buf + 1, ' ', n); |
| 1397 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1401 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1402 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1403 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1404 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1405 | spell_load_lang(lang) |
| 1406 | char_u *lang; |
| 1407 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1408 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1409 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1410 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1411 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1412 | /* Copy the language name to pass it to spell_load_cb() as a cookie. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1413 | * It's truncated when an error is detected. */ |
| 1414 | STRCPY(langcp, lang); |
| 1415 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1416 | /* |
| 1417 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1418 | */ |
| 1419 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1420 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1421 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1422 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1423 | if (r == FAIL && *langcp != NUL) |
| 1424 | { |
| 1425 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1426 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1427 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1428 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1431 | if (r == FAIL) |
| 1432 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1433 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1434 | else if (*langcp != NUL) |
| 1435 | { |
| 1436 | /* Load all the additions. */ |
| 1437 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1438 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1444 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1445 | */ |
| 1446 | static char_u * |
| 1447 | spell_enc() |
| 1448 | { |
| 1449 | |
| 1450 | #ifdef FEAT_MBYTE |
| 1451 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1452 | return p_enc; |
| 1453 | #endif |
| 1454 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Allocate a new slang_T. |
| 1459 | * Caller must fill "sl_next". |
| 1460 | */ |
| 1461 | static slang_T * |
| 1462 | slang_alloc(lang) |
| 1463 | char_u *lang; |
| 1464 | { |
| 1465 | slang_T *lp; |
| 1466 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1467 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1468 | if (lp != NULL) |
| 1469 | { |
| 1470 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1471 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1472 | ga_init2(&lp->sl_sal, sizeof(salitem_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1473 | } |
| 1474 | return lp; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * Free the contents of an slang_T and the structure itself. |
| 1479 | */ |
| 1480 | static void |
| 1481 | slang_free(lp) |
| 1482 | slang_T *lp; |
| 1483 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1484 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1485 | vim_free(lp->sl_fname); |
| 1486 | slang_clear(lp); |
| 1487 | vim_free(lp); |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * Clear an slang_T so that the file can be reloaded. |
| 1492 | */ |
| 1493 | static void |
| 1494 | slang_clear(lp) |
| 1495 | slang_T *lp; |
| 1496 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1497 | garray_T *gap; |
| 1498 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1499 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1500 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1501 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1502 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1503 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1504 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1505 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1506 | vim_free(lp->sl_pbyts); |
| 1507 | lp->sl_pbyts = NULL; |
| 1508 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1509 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1510 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1511 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1512 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1513 | vim_free(lp->sl_pidxs); |
| 1514 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1515 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1516 | gap = &lp->sl_rep; |
| 1517 | while (gap->ga_len > 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1518 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1519 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1520 | vim_free(ftp->ft_from); |
| 1521 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1522 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1523 | ga_clear(gap); |
| 1524 | |
| 1525 | gap = &lp->sl_sal; |
| 1526 | while (gap->ga_len > 0) |
| 1527 | { |
| 1528 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1529 | vim_free(smp->sm_lead); |
| 1530 | vim_free(smp->sm_to); |
| 1531 | } |
| 1532 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1533 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1534 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1535 | vim_free(lp->sl_prefprog[i]); |
| 1536 | vim_free(lp->sl_prefprog); |
| 1537 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1538 | #ifdef FEAT_MBYTE |
| 1539 | { |
| 1540 | int todo = lp->sl_map_hash.ht_used; |
| 1541 | hashitem_T *hi; |
| 1542 | |
| 1543 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1544 | if (!HASHITEM_EMPTY(hi)) |
| 1545 | { |
| 1546 | --todo; |
| 1547 | vim_free(hi->hi_key); |
| 1548 | } |
| 1549 | } |
| 1550 | hash_clear(&lp->sl_map_hash); |
| 1551 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1555 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1556 | * Invoked through do_in_runtimepath(). |
| 1557 | */ |
| 1558 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1559 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1560 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1561 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1562 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1563 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * Load one spell file and store the info into a slang_T. |
| 1568 | * |
| 1569 | * This is invoked in two ways: |
| 1570 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1571 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1572 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1573 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1574 | * Returns the slang_T the spell file was loaded into. NULL for error. |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1575 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1576 | static slang_T * |
| 1577 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1578 | char_u *fname; |
| 1579 | char_u *lang; |
| 1580 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1581 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1582 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1583 | FILE *fd; |
| 1584 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1585 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1586 | char_u *bp; |
| 1587 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1588 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1589 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1590 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1591 | int round; |
| 1592 | char_u *save_sourcing_name = sourcing_name; |
| 1593 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1594 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1595 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1596 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1597 | garray_T *gap; |
| 1598 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1599 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1600 | int rr; |
| 1601 | short *first; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1602 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1603 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1604 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1605 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1606 | if (fd == NULL) |
| 1607 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1608 | if (!silent) |
| 1609 | EMSG2(_(e_notopen), fname); |
| 1610 | else if (p_verbose > 2) |
| 1611 | { |
| 1612 | verbose_enter(); |
| 1613 | smsg((char_u *)e_notopen, fname); |
| 1614 | verbose_leave(); |
| 1615 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1616 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1617 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1618 | if (p_verbose > 2) |
| 1619 | { |
| 1620 | verbose_enter(); |
| 1621 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1622 | verbose_leave(); |
| 1623 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1624 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1625 | if (old_lp == NULL) |
| 1626 | { |
| 1627 | lp = slang_alloc(lang); |
| 1628 | if (lp == NULL) |
| 1629 | goto endFAIL; |
| 1630 | |
| 1631 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1632 | lp->sl_fname = vim_strsave(fname); |
| 1633 | if (lp->sl_fname == NULL) |
| 1634 | goto endFAIL; |
| 1635 | |
| 1636 | /* Check for .add.spl. */ |
| 1637 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1638 | } |
| 1639 | else |
| 1640 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1641 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1642 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1643 | sourcing_name = fname; |
| 1644 | sourcing_lnum = 0; |
| 1645 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1646 | /* <HEADER>: <fileID> |
| 1647 | * <regioncnt> <regionname> ... |
| 1648 | * <charflagslen> <charflags> |
| 1649 | * <fcharslen> <fchars> |
| 1650 | * <prefcondcnt> <prefcond> ... |
| 1651 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1652 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1653 | buf[i] = getc(fd); /* <fileID> */ |
| 1654 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1655 | { |
| 1656 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1657 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1661 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1662 | { |
| 1663 | truncerr: |
| 1664 | EMSG(_("E758: Truncated spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1665 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1666 | } |
| 1667 | if (cnt > 8) |
| 1668 | { |
| 1669 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1670 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1671 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1672 | } |
| 1673 | for (i = 0; i < cnt; ++i) |
| 1674 | { |
| 1675 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1676 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1677 | } |
| 1678 | lp->sl_regions[cnt * 2] = NUL; |
| 1679 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1680 | cnt = getc(fd); /* <charflagslen> */ |
| 1681 | if (cnt > 0) |
| 1682 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1683 | p = alloc((unsigned)cnt); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1684 | if (p == NULL) |
| 1685 | goto endFAIL; |
| 1686 | for (i = 0; i < cnt; ++i) |
| 1687 | p[i] = getc(fd); /* <charflags> */ |
| 1688 | |
| 1689 | ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */ |
| 1690 | if (ccnt <= 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1691 | { |
| 1692 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1693 | goto formerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1694 | } |
| 1695 | fol = alloc((unsigned)ccnt + 1); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1696 | if (fol == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1697 | { |
| 1698 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1699 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1700 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1701 | for (i = 0; i < ccnt; ++i) |
| 1702 | fol[i] = getc(fd); /* <fchars> */ |
| 1703 | fol[i] = NUL; |
| 1704 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1705 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1706 | i = set_spell_charflags(p, cnt, fol); |
| 1707 | vim_free(p); |
| 1708 | vim_free(fol); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1709 | #if 0 /* tolerate the differences */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1710 | if (i == FAIL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1711 | goto formerr; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1712 | #endif |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1713 | } |
| 1714 | else |
| 1715 | { |
| 1716 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1717 | cnt = (getc(fd) << 8) + getc(fd); |
| 1718 | if (cnt != 0) |
| 1719 | goto formerr; |
| 1720 | } |
| 1721 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1722 | /* <prefcondcnt> <prefcond> ... */ |
| 1723 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1724 | if (cnt > 0) |
| 1725 | { |
| 1726 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1727 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1728 | if (lp->sl_prefprog == NULL) |
| 1729 | goto endFAIL; |
| 1730 | lp->sl_prefixcnt = cnt; |
| 1731 | |
| 1732 | for (i = 0; i < cnt; ++i) |
| 1733 | { |
| 1734 | /* <prefcond> : <condlen> <condstr> */ |
| 1735 | n = getc(fd); /* <condlen> */ |
| 1736 | if (n < 0) |
| 1737 | goto formerr; |
| 1738 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1739 | * compile the regexp program used to check for the condition. */ |
| 1740 | if (n > 0) |
| 1741 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1742 | buf[0] = '^'; /* always match at one position only */ |
| 1743 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1744 | while (n-- > 0) |
| 1745 | *p++ = getc(fd); /* <condstr> */ |
| 1746 | *p = NUL; |
| 1747 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1748 | } |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1753 | /* <SUGGEST> : <repcount> <rep> ... |
| 1754 | * <salflags> <salcount> <sal> ... |
| 1755 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1756 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1757 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1758 | if (cnt < 0) |
| 1759 | goto formerr; |
| 1760 | |
| 1761 | gap = &lp->sl_rep; |
| 1762 | if (ga_grow(gap, cnt) == FAIL) |
| 1763 | goto endFAIL; |
| 1764 | |
| 1765 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1766 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1767 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1768 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
| 1769 | for (rr = 1; rr <= 2; ++rr) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1770 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1771 | ccnt = getc(fd); |
| 1772 | if (ccnt < 0) |
| 1773 | { |
| 1774 | if (rr == 2) |
| 1775 | vim_free(ftp->ft_from); |
| 1776 | goto formerr; |
| 1777 | } |
| 1778 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1779 | { |
| 1780 | if (rr == 2) |
| 1781 | vim_free(ftp->ft_from); |
| 1782 | goto endFAIL; |
| 1783 | } |
| 1784 | for (i = 0; i < ccnt; ++i) |
| 1785 | p[i] = getc(fd); /* <repfrom> or <repto> */ |
| 1786 | p[i] = NUL; |
| 1787 | if (rr == 1) |
| 1788 | ftp->ft_from = p; |
| 1789 | else |
| 1790 | ftp->ft_to = p; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* Fill the first-index table. */ |
| 1795 | first = lp->sl_rep_first; |
| 1796 | for (i = 0; i < 256; ++i) |
| 1797 | first[i] = -1; |
| 1798 | for (i = 0; i < gap->ga_len; ++i) |
| 1799 | { |
| 1800 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1801 | if (first[*ftp->ft_from] == -1) |
| 1802 | first[*ftp->ft_from] = i; |
| 1803 | } |
| 1804 | |
| 1805 | i = getc(fd); /* <salflags> */ |
| 1806 | if (i & SAL_F0LLOWUP) |
| 1807 | lp->sl_followup = TRUE; |
| 1808 | if (i & SAL_COLLAPSE) |
| 1809 | lp->sl_collapse = TRUE; |
| 1810 | if (i & SAL_REM_ACCENTS) |
| 1811 | lp->sl_rem_accents = TRUE; |
| 1812 | |
| 1813 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1814 | if (cnt < 0) |
| 1815 | goto formerr; |
| 1816 | |
| 1817 | gap = &lp->sl_sal; |
| 1818 | if (ga_grow(gap, cnt) == FAIL) |
| 1819 | goto endFAIL; |
| 1820 | |
| 1821 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 1822 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 1823 | { |
| 1824 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 1825 | ccnt = getc(fd); /* <salfromlen> */ |
| 1826 | if (ccnt < 0) |
| 1827 | goto formerr; |
| 1828 | if ((p = alloc(ccnt + 2)) == NULL) |
| 1829 | goto endFAIL; |
| 1830 | smp->sm_lead = p; |
| 1831 | |
| 1832 | /* Read up to the first special char into sm_lead. */ |
| 1833 | for (i = 0; i < ccnt; ++i) |
| 1834 | { |
| 1835 | c = getc(fd); /* <salfrom> */ |
| 1836 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
| 1837 | break; |
| 1838 | *p++ = c; |
| 1839 | } |
| 1840 | smp->sm_leadlen = p - smp->sm_lead; |
| 1841 | *p++ = NUL; |
| 1842 | |
| 1843 | /* Put optional chars in sm_oneoff, if any. */ |
| 1844 | if (c == '(') |
| 1845 | { |
| 1846 | smp->sm_oneoff = p; |
| 1847 | for (++i; i < ccnt; ++i) |
| 1848 | { |
| 1849 | c = getc(fd); /* <salfrom> */ |
| 1850 | if (c == ')') |
| 1851 | break; |
| 1852 | *p++ = c; |
| 1853 | } |
| 1854 | *p++ = NUL; |
| 1855 | if (++i < ccnt) |
| 1856 | c = getc(fd); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1857 | } |
| 1858 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1859 | smp->sm_oneoff = NULL; |
| 1860 | |
| 1861 | /* Any following chars go in sm_rules. */ |
| 1862 | smp->sm_rules = p; |
| 1863 | if (i < ccnt) |
| 1864 | *p++ = c; |
| 1865 | for (++i; i < ccnt; ++i) |
| 1866 | *p++ = getc(fd); /* <salfrom> */ |
| 1867 | *p++ = NUL; |
| 1868 | |
| 1869 | ccnt = getc(fd); /* <saltolen> */ |
| 1870 | if (ccnt < 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1871 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1872 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1873 | goto formerr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1874 | } |
| 1875 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1876 | { |
| 1877 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1878 | goto endFAIL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1879 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1880 | smp->sm_to = p; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1881 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1882 | for (i = 0; i < ccnt; ++i) |
| 1883 | *p++ = getc(fd); /* <salto> */ |
| 1884 | *p++ = NUL; |
| 1885 | } |
| 1886 | |
| 1887 | /* Fill the first-index table. */ |
| 1888 | first = lp->sl_sal_first; |
| 1889 | for (i = 0; i < 256; ++i) |
| 1890 | first[i] = -1; |
| 1891 | for (i = 0; i < gap->ga_len; ++i) |
| 1892 | { |
| 1893 | smp = &((salitem_T *)gap->ga_data)[i]; |
| 1894 | if (first[*smp->sm_lead] == -1) |
| 1895 | first[*smp->sm_lead] = i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */ |
| 1899 | if (cnt < 0) |
| 1900 | goto formerr; |
| 1901 | p = alloc(cnt + 1); |
| 1902 | if (p == NULL) |
| 1903 | goto endFAIL; |
| 1904 | for (i = 0; i < cnt; ++i) |
| 1905 | p[i] = getc(fd); /* <mapstr> */ |
| 1906 | p[i] = NUL; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1907 | set_map_str(lp, p); |
| 1908 | vim_free(p); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1909 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1910 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1911 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1912 | * round 2: <KWORDTREE> |
| 1913 | * round 3: <PREFIXTREE> */ |
| 1914 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1915 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1916 | /* The tree size was computed when writing the file, so that we can |
| 1917 | * allocate it as one long block. <nodecount> */ |
| 1918 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 1919 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1920 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1921 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1922 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1923 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1924 | bp = lalloc((long_u)len, TRUE); |
| 1925 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1926 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1927 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1928 | lp->sl_fbyts = bp; |
| 1929 | else if (round == 2) |
| 1930 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1931 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1932 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1935 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 1936 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1937 | goto endFAIL; |
| 1938 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1939 | lp->sl_fidxs = ip; |
| 1940 | else if (round == 2) |
| 1941 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1942 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1943 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1944 | |
| 1945 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1946 | idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1947 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1948 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1949 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1950 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1951 | } |
| 1952 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1953 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1954 | /* For a new file link it in the list of spell files. */ |
| 1955 | if (old_lp == NULL) |
| 1956 | { |
| 1957 | lp->sl_next = first_lang; |
| 1958 | first_lang = lp; |
| 1959 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1960 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1961 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1962 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1963 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1964 | if (lang != NULL) |
| 1965 | /* truncating the name signals the error to spell_load_lang() */ |
| 1966 | *lang = NUL; |
| 1967 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1968 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1969 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1970 | lp = NULL; |
| 1971 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1972 | |
| 1973 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1974 | if (fd != NULL) |
| 1975 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1976 | sourcing_name = save_sourcing_name; |
| 1977 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1978 | |
| 1979 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1983 | * Read one row of siblings from the spell file and store it in the byte array |
| 1984 | * "byts" and index array "idxs". Recursively read the children. |
| 1985 | * |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1986 | * NOTE: The code here must match put_node(). |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1987 | * |
| 1988 | * Returns the index follosing the siblings. |
| 1989 | * Returns -1 if the file is shorter than expected. |
| 1990 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1991 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1992 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1993 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1994 | FILE *fd; |
| 1995 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1996 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1997 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1998 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1999 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 2000 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2001 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2002 | int len; |
| 2003 | int i; |
| 2004 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2005 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2006 | int c; |
| 2007 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2008 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2009 | len = getc(fd); /* <siblingcount> */ |
| 2010 | if (len <= 0) |
| 2011 | return -1; |
| 2012 | |
| 2013 | if (startidx + len >= maxidx) |
| 2014 | return -2; |
| 2015 | byts[idx++] = len; |
| 2016 | |
| 2017 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 2018 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2019 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2020 | c = getc(fd); /* <byte> */ |
| 2021 | if (c < 0) |
| 2022 | return -1; |
| 2023 | if (c <= BY_SPECIAL) |
| 2024 | { |
| 2025 | if (c == BY_NOFLAGS) |
| 2026 | { |
| 2027 | /* No flags, all regions. */ |
| 2028 | idxs[idx] = 0; |
| 2029 | c = 0; |
| 2030 | } |
| 2031 | else if (c == BY_FLAGS) |
| 2032 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2033 | if (prefixtree) |
| 2034 | { |
| 2035 | /* Read the prefix ID and the condition nr. In idxs[] |
| 2036 | * store the prefix ID in the low byte, the condition |
| 2037 | * index shifted up 8 bits. */ |
| 2038 | c = getc(fd); /* <prefixID> */ |
| 2039 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 2040 | if (n >= maxprefcondnr) |
| 2041 | return -2; |
| 2042 | c = (n << 8) + c; |
| 2043 | } |
| 2044 | else |
| 2045 | { |
| 2046 | /* Read flags and optional region and prefix ID. In |
| 2047 | * idxs[] the flags go in the low byte, region above that |
| 2048 | * and prefix ID above the region. */ |
| 2049 | c = getc(fd); /* <flags> */ |
| 2050 | if (c & WF_REGION) |
| 2051 | c = (getc(fd) << 8) + c; /* <region> */ |
| 2052 | if (c & WF_PFX) |
| 2053 | c = (getc(fd) << 16) + c; /* <prefixID> */ |
| 2054 | } |
| 2055 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2056 | idxs[idx] = c; |
| 2057 | c = 0; |
| 2058 | } |
| 2059 | else /* c == BY_INDEX */ |
| 2060 | { |
| 2061 | /* <nodeidx> */ |
| 2062 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2063 | if (n < 0 || n >= maxidx) |
| 2064 | return -2; |
| 2065 | idxs[idx] = n + SHARED_MASK; |
| 2066 | c = getc(fd); /* <xbyte> */ |
| 2067 | } |
| 2068 | } |
| 2069 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2072 | /* Recursively read the children for non-shared siblings. |
| 2073 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 2074 | * remove SHARED_MASK) */ |
| 2075 | for (i = 1; i <= len; ++i) |
| 2076 | if (byts[startidx + i] != 0) |
| 2077 | { |
| 2078 | if (idxs[startidx + i] & SHARED_MASK) |
| 2079 | idxs[startidx + i] &= ~SHARED_MASK; |
| 2080 | else |
| 2081 | { |
| 2082 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2083 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 2084 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2085 | if (idx < 0) |
| 2086 | break; |
| 2087 | } |
| 2088 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2089 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2090 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * Parse 'spelllang' and set buf->b_langp accordingly. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2095 | * Returns NULL if it's OK, an error message otherwise. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2096 | */ |
| 2097 | char_u * |
| 2098 | did_set_spelllang(buf) |
| 2099 | buf_T *buf; |
| 2100 | { |
| 2101 | garray_T ga; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2102 | char_u *splp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2103 | char_u *region; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2104 | int filename; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2105 | int region_mask; |
| 2106 | slang_T *lp; |
| 2107 | int c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2108 | char_u lang[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2109 | char_u spf_name[MAXPATHL]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2110 | int load_spf; |
| 2111 | int len; |
| 2112 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2113 | |
| 2114 | ga_init2(&ga, sizeof(langp_T), 2); |
| 2115 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2116 | /* Make the name of the .spl file associated with 'spellfile'. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2117 | if (*buf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2118 | load_spf = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2119 | else |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2120 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2121 | vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl", |
| 2122 | buf->b_p_spf); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2123 | load_spf = TRUE; |
| 2124 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2125 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2126 | /* loop over comma separated language names. */ |
| 2127 | for (splp = buf->b_p_spl; *splp != NUL; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2128 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2129 | /* Get one language name. */ |
| 2130 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 2131 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2132 | region = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2133 | len = STRLEN(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2134 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2135 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 2136 | * If there is a region name let "region" point to it and remove it |
| 2137 | * from the name. */ |
| 2138 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 2139 | { |
| 2140 | filename = TRUE; |
| 2141 | |
| 2142 | /* Check if we loaded this language before. */ |
| 2143 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2144 | if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME) |
| 2145 | break; |
| 2146 | } |
| 2147 | else |
| 2148 | { |
| 2149 | filename = FALSE; |
| 2150 | if (len > 3 && lang[len - 3] == '_') |
| 2151 | { |
| 2152 | region = lang + len - 2; |
| 2153 | len -= 3; |
| 2154 | lang[len] = NUL; |
| 2155 | } |
| 2156 | |
| 2157 | /* Check if we loaded this language before. */ |
| 2158 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2159 | if (STRICMP(lang, lp->sl_name) == 0) |
| 2160 | break; |
| 2161 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2162 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2163 | /* If not found try loading the language now. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2164 | if (lp == NULL) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2165 | { |
| 2166 | if (filename) |
| 2167 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 2168 | else |
| 2169 | spell_load_lang(lang); |
| 2170 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2171 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2172 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2173 | * Loop over the languages, there can be several files for "lang". |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2174 | */ |
| 2175 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2176 | if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME |
| 2177 | : STRICMP(lang, lp->sl_name) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2178 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2179 | region_mask = REGION_ALL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2180 | if (!filename && region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2181 | { |
| 2182 | /* find region in sl_regions */ |
| 2183 | c = find_region(lp->sl_regions, region); |
| 2184 | if (c == REGION_ALL) |
| 2185 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2186 | if (!lp->sl_add) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2187 | smsg((char_u *) |
| 2188 | _("Warning: region %s not supported"), |
| 2189 | region); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2190 | } |
| 2191 | else |
| 2192 | region_mask = 1 << c; |
| 2193 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2194 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2195 | if (ga_grow(&ga, 1) == FAIL) |
| 2196 | { |
| 2197 | ga_clear(&ga); |
| 2198 | return e_outofmem; |
| 2199 | } |
| 2200 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2201 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2202 | ++ga.ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2203 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2204 | /* Check if this is the spell file related to 'spellfile'. */ |
| 2205 | if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE) |
| 2206 | == FPC_SAME) |
| 2207 | load_spf = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2208 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2211 | /* |
| 2212 | * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath', |
| 2213 | * then it's probably loaded above already. Otherwise load it here. |
| 2214 | */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2215 | if (load_spf) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2216 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2217 | /* Check if it was loaded already. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2218 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2219 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2220 | break; |
| 2221 | if (lp == NULL) |
| 2222 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2223 | /* Not loaded, try loading it now. The language name includes the |
| 2224 | * region name, the region is ignored otherwise. */ |
| 2225 | vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN); |
| 2226 | p = vim_strchr(lang, '.'); |
| 2227 | if (p != NULL) |
| 2228 | *p = NUL; /* truncate at ".encoding.add" */ |
| 2229 | lp = spell_load_file(spf_name, lang, NULL, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2230 | } |
| 2231 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2232 | { |
| 2233 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2234 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL; |
| 2235 | ++ga.ga_len; |
| 2236 | } |
| 2237 | } |
| 2238 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2239 | /* Add a NULL entry to mark the end of the list. */ |
| 2240 | if (ga_grow(&ga, 1) == FAIL) |
| 2241 | { |
| 2242 | ga_clear(&ga); |
| 2243 | return e_outofmem; |
| 2244 | } |
| 2245 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2246 | ++ga.ga_len; |
| 2247 | |
| 2248 | /* Everything is fine, store the new b_langp value. */ |
| 2249 | ga_clear(&buf->b_langp); |
| 2250 | buf->b_langp = ga; |
| 2251 | |
| 2252 | return NULL; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2257 | * Each region is simply stored as the two characters of it's name. |
| 2258 | * Returns the index if found, REGION_ALL if not found. |
| 2259 | */ |
| 2260 | static int |
| 2261 | find_region(rp, region) |
| 2262 | char_u *rp; |
| 2263 | char_u *region; |
| 2264 | { |
| 2265 | int i; |
| 2266 | |
| 2267 | for (i = 0; ; i += 2) |
| 2268 | { |
| 2269 | if (rp[i] == NUL) |
| 2270 | return REGION_ALL; |
| 2271 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2272 | break; |
| 2273 | } |
| 2274 | return i / 2; |
| 2275 | } |
| 2276 | |
| 2277 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2278 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2279 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2280 | * Word WF_ONECAP |
| 2281 | * W WORD WF_ALLCAP |
| 2282 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2283 | */ |
| 2284 | static int |
| 2285 | captype(word, end) |
| 2286 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2287 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2288 | { |
| 2289 | char_u *p; |
| 2290 | int c; |
| 2291 | int firstcap; |
| 2292 | int allcap; |
| 2293 | int past_second = FALSE; /* past second word char */ |
| 2294 | |
| 2295 | /* find first letter */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2296 | for (p = word; !spell_iswordp(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2297 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2298 | return 0; /* only non-word characters, illegal word */ |
| 2299 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2300 | if (has_mbyte) |
| 2301 | c = mb_ptr2char_adv(&p); |
| 2302 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2303 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2304 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2305 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2306 | |
| 2307 | /* |
| 2308 | * Need to check all letters to find a word with mixed upper/lower. |
| 2309 | * But a word with an upper char only at start is a ONECAP. |
| 2310 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2311 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2312 | if (spell_iswordp(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2313 | { |
| 2314 | #ifdef FEAT_MBYTE |
| 2315 | c = mb_ptr2char(p); |
| 2316 | #else |
| 2317 | c = *p; |
| 2318 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2319 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2320 | { |
| 2321 | /* UUl -> KEEPCAP */ |
| 2322 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2323 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2324 | allcap = FALSE; |
| 2325 | } |
| 2326 | else if (!allcap) |
| 2327 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2328 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2329 | past_second = TRUE; |
| 2330 | } |
| 2331 | |
| 2332 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2333 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2334 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2335 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
| 2338 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2339 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 2340 | /* |
| 2341 | * Free all languages. |
| 2342 | */ |
| 2343 | void |
| 2344 | spell_free_all() |
| 2345 | { |
| 2346 | slang_T *lp; |
| 2347 | buf_T *buf; |
| 2348 | |
| 2349 | /* Go through all buffers and handle 'spelllang'. */ |
| 2350 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2351 | ga_clear(&buf->b_langp); |
| 2352 | |
| 2353 | while (first_lang != NULL) |
| 2354 | { |
| 2355 | lp = first_lang; |
| 2356 | first_lang = lp->sl_next; |
| 2357 | slang_free(lp); |
| 2358 | } |
| 2359 | } |
| 2360 | # endif |
| 2361 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2362 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2363 | /* |
| 2364 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2365 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2366 | */ |
| 2367 | void |
| 2368 | spell_reload() |
| 2369 | { |
| 2370 | buf_T *buf; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2371 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2372 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2373 | /* Initialize the table for spell_iswordp(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2374 | init_spell_chartab(); |
| 2375 | |
| 2376 | /* Unload all allocated memory. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2377 | spell_free_all(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2378 | |
| 2379 | /* Go through all buffers and handle 'spelllang'. */ |
| 2380 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2381 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2382 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2383 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2384 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2385 | { |
| 2386 | FOR_ALL_WINDOWS(wp) |
| 2387 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2388 | { |
| 2389 | (void)did_set_spelllang(buf); |
| 2390 | # ifdef FEAT_WINDOWS |
| 2391 | break; |
| 2392 | # endif |
| 2393 | } |
| 2394 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2395 | } |
| 2396 | } |
| 2397 | # endif |
| 2398 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2399 | /* |
| 2400 | * Reload the spell file "fname" if it's loaded. |
| 2401 | */ |
| 2402 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2403 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2404 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2405 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2406 | { |
| 2407 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2408 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2409 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2410 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2411 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 2412 | { |
| 2413 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2414 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2415 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2416 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2417 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2418 | |
| 2419 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 2420 | * 'spelllang' to get it loaded. */ |
| 2421 | if (added_word && !didit) |
| 2422 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2426 | /* |
| 2427 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2428 | */ |
| 2429 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2430 | #define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2431 | and .dic file. */ |
| 2432 | /* |
| 2433 | * Main structure to store the contents of a ".aff" file. |
| 2434 | */ |
| 2435 | typedef struct afffile_S |
| 2436 | { |
| 2437 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2438 | int af_rar; /* RAR ID for rare word */ |
| 2439 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2440 | int af_bad; /* BAD ID for banned word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2441 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2442 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2443 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2444 | } afffile_T; |
| 2445 | |
| 2446 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2447 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2448 | struct affentry_S |
| 2449 | { |
| 2450 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2451 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 2452 | char_u *ae_add; /* text to add to basic word (can be NULL) */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2453 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 2454 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2455 | }; |
| 2456 | |
| 2457 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 2458 | typedef struct affheader_S |
| 2459 | { |
| 2460 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2461 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2462 | int ah_combine; /* suffix may combine with prefix */ |
| 2463 | affentry_T *ah_first; /* first affix entry */ |
| 2464 | } affheader_T; |
| 2465 | |
| 2466 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 2467 | |
| 2468 | /* |
| 2469 | * Structure that is used to store the items in the word tree. This avoids |
| 2470 | * the need to keep track of each allocated thing, it's freed all at once |
| 2471 | * after ":mkspell" is done. |
| 2472 | */ |
| 2473 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 2474 | typedef struct sblock_S sblock_T; |
| 2475 | struct sblock_S |
| 2476 | { |
| 2477 | sblock_T *sb_next; /* next block in list */ |
| 2478 | int sb_used; /* nr of bytes already in use */ |
| 2479 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2480 | }; |
| 2481 | |
| 2482 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2483 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2484 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2485 | typedef struct wordnode_S wordnode_T; |
| 2486 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2487 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2488 | union /* shared to save space */ |
| 2489 | { |
| 2490 | char_u hashkey[6]; /* room for the hash key */ |
| 2491 | int index; /* index in written nodes (valid after first |
| 2492 | round) */ |
| 2493 | } wn_u1; |
| 2494 | union /* shared to save space */ |
| 2495 | { |
| 2496 | wordnode_T *next; /* next node with same hash key */ |
| 2497 | wordnode_T *wnode; /* parent node that will write this node */ |
| 2498 | } wn_u2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2499 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 2500 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 2501 | always sorted) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2502 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 2503 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2504 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 2505 | PREFIXTREE it's the prefcondnr */ |
| 2506 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2507 | }; |
| 2508 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2509 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2510 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2511 | /* |
| 2512 | * Info used while reading the spell files. |
| 2513 | */ |
| 2514 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2515 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2516 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2517 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2518 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2519 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2520 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2521 | sblock_T *si_blocks; /* memory blocks used */ |
| 2522 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2523 | int si_add; /* addition file */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2524 | int si_clear_chartab; /* when TRUE clear char tables */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2525 | int si_region; /* region mask */ |
| 2526 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2527 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2528 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2529 | int si_region_count; /* number of regions supported (1 when there |
| 2530 | are no regions) */ |
| 2531 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2532 | |
| 2533 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 2534 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
| 2535 | int si_followup; /* soundsalike: ? */ |
| 2536 | int si_collapse; /* soundsalike: ? */ |
| 2537 | int si_rem_accents; /* soundsalike: remove accents */ |
| 2538 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2539 | garray_T si_prefcond; /* table with conditions for postponed |
| 2540 | * prefixes, each stored as a string */ |
| 2541 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2542 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2543 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2544 | static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2545 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2546 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 2547 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2548 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2549 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 2550 | static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2551 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 2552 | static 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 Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2553 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 2554 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 2555 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 2556 | static void free_blocks __ARGS((sblock_T *bl)); |
| 2557 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2558 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 2559 | static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, int prefixID, sblock_T **blp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2560 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2561 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 2562 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2563 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2564 | static void clear_node __ARGS((wordnode_T *node)); |
| 2565 | static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2566 | static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2567 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2568 | |
| 2569 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2570 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2571 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2572 | */ |
| 2573 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2574 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2575 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2576 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2577 | { |
| 2578 | FILE *fd; |
| 2579 | afffile_T *aff; |
| 2580 | char_u rline[MAXLINELEN]; |
| 2581 | char_u *line; |
| 2582 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2583 | #define MAXITEMCNT 7 |
| 2584 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2585 | int itemcnt; |
| 2586 | char_u *p; |
| 2587 | int lnum = 0; |
| 2588 | affheader_T *cur_aff = NULL; |
| 2589 | int aff_todo = 0; |
| 2590 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2591 | char_u *low = NULL; |
| 2592 | char_u *fol = NULL; |
| 2593 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2594 | static char *e_affname = N_("Affix name too long in %s line %d: %s"); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2595 | int do_rep; |
| 2596 | int do_sal; |
| 2597 | int do_map; |
| 2598 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2599 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2600 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2601 | /* |
| 2602 | * Open the file. |
| 2603 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2604 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2605 | if (fd == NULL) |
| 2606 | { |
| 2607 | EMSG2(_(e_notopen), fname); |
| 2608 | return NULL; |
| 2609 | } |
| 2610 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2611 | if (spin->si_verbose || p_verbose > 2) |
| 2612 | { |
| 2613 | if (!spin->si_verbose) |
| 2614 | verbose_enter(); |
| 2615 | smsg((char_u *)_("Reading affix file %s..."), fname); |
| 2616 | out_flush(); |
| 2617 | if (!spin->si_verbose) |
| 2618 | verbose_leave(); |
| 2619 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2620 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2621 | /* Only do REP lines when not done in another .aff file already. */ |
| 2622 | do_rep = spin->si_rep.ga_len == 0; |
| 2623 | |
| 2624 | /* Only do SAL lines when not done in another .aff file already. */ |
| 2625 | do_sal = spin->si_sal.ga_len == 0; |
| 2626 | |
| 2627 | /* Only do MAP lines when not done in another .aff file already. */ |
| 2628 | do_map = spin->si_map.ga_len == 0; |
| 2629 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2630 | /* |
| 2631 | * Allocate and init the afffile_T structure. |
| 2632 | */ |
| 2633 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2634 | if (aff == NULL) |
| 2635 | return NULL; |
| 2636 | hash_init(&aff->af_pref); |
| 2637 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2638 | |
| 2639 | /* |
| 2640 | * Read all the lines in the file one by one. |
| 2641 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2642 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2643 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2644 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2645 | ++lnum; |
| 2646 | |
| 2647 | /* Skip comment lines. */ |
| 2648 | if (*rline == '#') |
| 2649 | continue; |
| 2650 | |
| 2651 | /* Convert from "SET" to 'encoding' when needed. */ |
| 2652 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2653 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2654 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2655 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2656 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2657 | if (pc == NULL) |
| 2658 | { |
| 2659 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 2660 | fname, lnum, rline); |
| 2661 | continue; |
| 2662 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2663 | line = pc; |
| 2664 | } |
| 2665 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2666 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2667 | { |
| 2668 | pc = NULL; |
| 2669 | line = rline; |
| 2670 | } |
| 2671 | |
| 2672 | /* Split the line up in white separated items. Put a NUL after each |
| 2673 | * item. */ |
| 2674 | itemcnt = 0; |
| 2675 | for (p = line; ; ) |
| 2676 | { |
| 2677 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 2678 | ++p; |
| 2679 | if (*p == NUL) |
| 2680 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2681 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2682 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2683 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2684 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2685 | ++p; |
| 2686 | if (*p == NUL) |
| 2687 | break; |
| 2688 | *p++ = NUL; |
| 2689 | } |
| 2690 | |
| 2691 | /* Handle non-empty lines. */ |
| 2692 | if (itemcnt > 0) |
| 2693 | { |
| 2694 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 2695 | && aff->af_enc == NULL) |
| 2696 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2697 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2698 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 2699 | aff->af_enc = enc_canonize(items[1]); |
| 2700 | if (aff->af_enc != NULL && !spin->si_ascii |
| 2701 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 2702 | p_enc) == FAIL) |
| 2703 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 2704 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2705 | #else |
| 2706 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 2707 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2708 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2709 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 2710 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2711 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2712 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2713 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2714 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2715 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2716 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2717 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 2718 | && aff->af_rar == 0) |
| 2719 | { |
| 2720 | aff->af_rar = items[1][0]; |
| 2721 | if (items[1][1] != NUL) |
| 2722 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2723 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2724 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 2725 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2726 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2727 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2728 | if (items[1][1] != NUL) |
| 2729 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2730 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2731 | else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2 |
| 2732 | && aff->af_bad == 0) |
| 2733 | { |
| 2734 | aff->af_bad = items[1][0]; |
| 2735 | if (items[1][1] != NUL) |
| 2736 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2737 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2738 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 2739 | { |
| 2740 | aff->af_pfxpostpone = TRUE; |
| 2741 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2742 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2743 | || STRCMP(items[0], "SFX") == 0) |
| 2744 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2745 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2746 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2747 | /* Myspell allows extra text after the item, but that might |
| 2748 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2749 | if (itemcnt > 4 && *items[4] != '#') |
| 2750 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2751 | fname, lnum, items[4]); |
| 2752 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2753 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2754 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 2755 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2756 | if (cur_aff == NULL) |
| 2757 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2758 | cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2759 | cur_aff->ah_key[1] = NUL; |
| 2760 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2761 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2762 | if (*items[2] == 'Y') |
| 2763 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2764 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2765 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 2766 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2767 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2768 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2769 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2770 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2771 | /* Use a new number in the .spl file later, to be able to |
| 2772 | * handle multiple .aff files. */ |
| 2773 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2774 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2775 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2776 | else |
| 2777 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2778 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2779 | hi = hash_find(tp, cur_aff->ah_key); |
| 2780 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2781 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2782 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 2783 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2784 | aff_todo = 0; |
| 2785 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2786 | else |
| 2787 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2788 | } |
| 2789 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2790 | || STRCMP(items[0], "SFX") == 0) |
| 2791 | && aff_todo > 0 |
| 2792 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2793 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2794 | { |
| 2795 | affentry_T *aff_entry; |
| 2796 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2797 | /* Myspell allows extra text after the item, but that might |
| 2798 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2799 | if (itemcnt > 5 && *items[5] != '#') |
| 2800 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2801 | fname, lnum, items[5]); |
| 2802 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2803 | /* New item for an affix letter. */ |
| 2804 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2805 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 2806 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2807 | if (aff_entry == NULL) |
| 2808 | break; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2809 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2810 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2811 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 2812 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2813 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2814 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 2815 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2816 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2817 | /* Don't use an affix entry with non-ASCII characters when |
| 2818 | * "spin->si_ascii" is TRUE. */ |
| 2819 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2820 | || has_non_ascii(aff_entry->ae_add))) |
| 2821 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2822 | aff_entry->ae_next = cur_aff->ah_first; |
| 2823 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2824 | |
| 2825 | if (STRCMP(items[4], ".") != 0) |
| 2826 | { |
| 2827 | char_u buf[MAXLINELEN]; |
| 2828 | |
| 2829 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 2830 | items[4]); |
| 2831 | if (*items[0] == 'P') |
| 2832 | sprintf((char *)buf, "^%s", items[4]); |
| 2833 | else |
| 2834 | sprintf((char *)buf, "%s$", items[4]); |
| 2835 | aff_entry->ae_prog = vim_regcomp(buf, |
| 2836 | RE_MAGIC + RE_STRING); |
| 2837 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2838 | |
| 2839 | /* For postponed prefixes we need an entry in si_prefcond |
| 2840 | * for the condition. Use an existing one if possible. */ |
| 2841 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 2842 | && aff_entry->ae_chop == NULL) |
| 2843 | { |
| 2844 | int idx; |
| 2845 | char_u **pp; |
| 2846 | |
| 2847 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 2848 | --idx) |
| 2849 | { |
| 2850 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 2851 | if (str_equal(p, aff_entry->ae_cond)) |
| 2852 | break; |
| 2853 | } |
| 2854 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 2855 | { |
| 2856 | /* Not found, add a new condition. */ |
| 2857 | idx = spin->si_prefcond.ga_len++; |
| 2858 | pp = ((char_u **)spin->si_prefcond.ga_data) + idx; |
| 2859 | if (aff_entry->ae_cond == NULL) |
| 2860 | *pp = NULL; |
| 2861 | else |
| 2862 | *pp = getroom_save(&spin->si_blocks, |
| 2863 | aff_entry->ae_cond); |
| 2864 | } |
| 2865 | |
| 2866 | /* Add the prefix to the prefix tree. */ |
| 2867 | if (aff_entry->ae_add == NULL) |
| 2868 | p = (char_u *)""; |
| 2869 | else |
| 2870 | p = aff_entry->ae_add; |
| 2871 | tree_add_word(p, spin->si_prefroot, -1, idx, |
| 2872 | cur_aff->ah_newID, &spin->si_blocks); |
| 2873 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2874 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2875 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2876 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 2877 | { |
| 2878 | if (fol != NULL) |
| 2879 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 2880 | fname, lnum); |
| 2881 | else |
| 2882 | fol = vim_strsave(items[1]); |
| 2883 | } |
| 2884 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 2885 | { |
| 2886 | if (low != NULL) |
| 2887 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 2888 | fname, lnum); |
| 2889 | else |
| 2890 | low = vim_strsave(items[1]); |
| 2891 | } |
| 2892 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 2893 | { |
| 2894 | if (upp != NULL) |
| 2895 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 2896 | fname, lnum); |
| 2897 | else |
| 2898 | upp = vim_strsave(items[1]); |
| 2899 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2900 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2901 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2902 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2903 | if (!isdigit(*items[1])) |
| 2904 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 2905 | fname, lnum); |
| 2906 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2907 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 2908 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2909 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2910 | if (do_rep) |
| 2911 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 2912 | } |
| 2913 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 2914 | { |
| 2915 | /* MAP item or count */ |
| 2916 | if (!found_map) |
| 2917 | { |
| 2918 | /* First line contains the count. */ |
| 2919 | found_map = TRUE; |
| 2920 | if (!isdigit(*items[1])) |
| 2921 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 2922 | fname, lnum); |
| 2923 | } |
| 2924 | else if (do_map) |
| 2925 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2926 | int c; |
| 2927 | |
| 2928 | /* Check that every character appears only once. */ |
| 2929 | for (p = items[1]; *p != NUL; ) |
| 2930 | { |
| 2931 | #ifdef FEAT_MBYTE |
| 2932 | c = mb_ptr2char_adv(&p); |
| 2933 | #else |
| 2934 | c = *p++; |
| 2935 | #endif |
| 2936 | if ((spin->si_map.ga_len > 0 |
| 2937 | && vim_strchr(spin->si_map.ga_data, c) |
| 2938 | != NULL) |
| 2939 | || vim_strchr(p, c) != NULL) |
| 2940 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 2941 | fname, lnum); |
| 2942 | } |
| 2943 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2944 | /* We simply concatenate all the MAP strings, separated by |
| 2945 | * slashes. */ |
| 2946 | ga_concat(&spin->si_map, items[1]); |
| 2947 | ga_append(&spin->si_map, '/'); |
| 2948 | } |
| 2949 | } |
| 2950 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 2951 | { |
| 2952 | if (do_sal) |
| 2953 | { |
| 2954 | /* SAL item (sounds-a-like) |
| 2955 | * Either one of the known keys or a from-to pair. */ |
| 2956 | if (STRCMP(items[1], "followup") == 0) |
| 2957 | spin->si_followup = sal_to_bool(items[2]); |
| 2958 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 2959 | spin->si_collapse = sal_to_bool(items[2]); |
| 2960 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 2961 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 2962 | else |
| 2963 | /* when "to" is "_" it means empty */ |
| 2964 | add_fromto(spin, &spin->si_sal, items[1], |
| 2965 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 2966 | : items[2]); |
| 2967 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2968 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2969 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2970 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 2971 | fname, lnum, items[0]); |
| 2972 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2975 | if (fol != NULL || low != NULL || upp != NULL) |
| 2976 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2977 | if (spin->si_clear_chartab) |
| 2978 | { |
| 2979 | /* Clear the char type tables, don't want to use any of the |
| 2980 | * currently used spell properties. */ |
| 2981 | init_spell_chartab(); |
| 2982 | spin->si_clear_chartab = FALSE; |
| 2983 | } |
| 2984 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2985 | /* |
| 2986 | * Don't write a word table for an ASCII file, so that we don't check |
| 2987 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2988 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2989 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 2990 | */ |
| 2991 | if (!spin->si_ascii |
| 2992 | #ifdef FEAT_MBYTE |
| 2993 | && !enc_utf8 |
| 2994 | #endif |
| 2995 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2996 | { |
| 2997 | if (fol == NULL || low == NULL || upp == NULL) |
| 2998 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 2999 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3000 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3001 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3002 | |
| 3003 | vim_free(fol); |
| 3004 | vim_free(low); |
| 3005 | vim_free(upp); |
| 3006 | } |
| 3007 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3008 | vim_free(pc); |
| 3009 | fclose(fd); |
| 3010 | return aff; |
| 3011 | } |
| 3012 | |
| 3013 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3014 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 3015 | * NULL as equal. |
| 3016 | */ |
| 3017 | static int |
| 3018 | str_equal(s1, s2) |
| 3019 | char_u *s1; |
| 3020 | char_u *s2; |
| 3021 | { |
| 3022 | if (s1 == NULL || s2 == NULL) |
| 3023 | return s1 == s2; |
| 3024 | return STRCMP(s1, s2) == 0; |
| 3025 | } |
| 3026 | |
| 3027 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3028 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 3029 | * They are stored case-folded. |
| 3030 | */ |
| 3031 | static void |
| 3032 | add_fromto(spin, gap, from, to) |
| 3033 | spellinfo_T *spin; |
| 3034 | garray_T *gap; |
| 3035 | char_u *from; |
| 3036 | char_u *to; |
| 3037 | { |
| 3038 | fromto_T *ftp; |
| 3039 | char_u word[MAXWLEN]; |
| 3040 | |
| 3041 | if (ga_grow(gap, 1) == OK) |
| 3042 | { |
| 3043 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 3044 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 3045 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 3046 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 3047 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 3048 | ++gap->ga_len; |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | /* |
| 3053 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 3054 | */ |
| 3055 | static int |
| 3056 | sal_to_bool(s) |
| 3057 | char_u *s; |
| 3058 | { |
| 3059 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 3060 | } |
| 3061 | |
| 3062 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3063 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 3064 | * When "s" is NULL FALSE is returned. |
| 3065 | */ |
| 3066 | static int |
| 3067 | has_non_ascii(s) |
| 3068 | char_u *s; |
| 3069 | { |
| 3070 | char_u *p; |
| 3071 | |
| 3072 | if (s != NULL) |
| 3073 | for (p = s; *p != NUL; ++p) |
| 3074 | if (*p >= 128) |
| 3075 | return TRUE; |
| 3076 | return FALSE; |
| 3077 | } |
| 3078 | |
| 3079 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3080 | * Free the structure filled by spell_read_aff(). |
| 3081 | */ |
| 3082 | static void |
| 3083 | spell_free_aff(aff) |
| 3084 | afffile_T *aff; |
| 3085 | { |
| 3086 | hashtab_T *ht; |
| 3087 | hashitem_T *hi; |
| 3088 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3089 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3090 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3091 | |
| 3092 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3093 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3094 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3095 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 3096 | { |
| 3097 | todo = ht->ht_used; |
| 3098 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3099 | { |
| 3100 | if (!HASHITEM_EMPTY(hi)) |
| 3101 | { |
| 3102 | --todo; |
| 3103 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3104 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3105 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | if (ht == &aff->af_suff) |
| 3109 | break; |
| 3110 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3111 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3112 | hash_clear(&aff->af_pref); |
| 3113 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3117 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3118 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3119 | */ |
| 3120 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3121 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3122 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3123 | spellinfo_T *spin; |
| 3124 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3125 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3126 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3127 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3128 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3129 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3130 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3131 | char_u *pc; |
| 3132 | char_u *w; |
| 3133 | int l; |
| 3134 | hash_T hash; |
| 3135 | hashitem_T *hi; |
| 3136 | FILE *fd; |
| 3137 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3138 | int non_ascii = 0; |
| 3139 | int retval = OK; |
| 3140 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3141 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3142 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3143 | /* |
| 3144 | * Open the file. |
| 3145 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3146 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3147 | if (fd == NULL) |
| 3148 | { |
| 3149 | EMSG2(_(e_notopen), fname); |
| 3150 | return FAIL; |
| 3151 | } |
| 3152 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3153 | /* The hashtable is only used to detect duplicated words. */ |
| 3154 | hash_init(&ht); |
| 3155 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3156 | spin->si_foldwcount = 0; |
| 3157 | spin->si_keepwcount = 0; |
| 3158 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3159 | if (spin->si_verbose || p_verbose > 2) |
| 3160 | { |
| 3161 | if (!spin->si_verbose) |
| 3162 | verbose_enter(); |
| 3163 | smsg((char_u *)_("Reading dictionary file %s..."), fname); |
| 3164 | out_flush(); |
| 3165 | if (!spin->si_verbose) |
| 3166 | verbose_leave(); |
| 3167 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3168 | |
| 3169 | /* Read and ignore the first line: word count. */ |
| 3170 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3171 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3172 | EMSG2(_("E760: No word count in %s"), fname); |
| 3173 | |
| 3174 | /* |
| 3175 | * Read all the lines in the file one by one. |
| 3176 | * The words are converted to 'encoding' here, before being added to |
| 3177 | * the hashtable. |
| 3178 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3179 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3180 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3181 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3182 | ++lnum; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3183 | if (line[0] == '#') |
| 3184 | continue; /* comment line */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3185 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3186 | /* Remove CR, LF and white space from the end. White space halfway |
| 3187 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3188 | l = STRLEN(line); |
| 3189 | while (l > 0 && line[l - 1] <= ' ') |
| 3190 | --l; |
| 3191 | if (l == 0) |
| 3192 | continue; /* empty line */ |
| 3193 | line[l] = NUL; |
| 3194 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3195 | /* Find the optional affix names. */ |
| 3196 | afflist = vim_strchr(line, '/'); |
| 3197 | if (afflist != NULL) |
| 3198 | *afflist++ = NUL; |
| 3199 | |
| 3200 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3201 | if (spin->si_ascii && has_non_ascii(line)) |
| 3202 | { |
| 3203 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3204 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3205 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3206 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3207 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3208 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3209 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3210 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3211 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3212 | if (pc == NULL) |
| 3213 | { |
| 3214 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3215 | fname, lnum, line); |
| 3216 | continue; |
| 3217 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3218 | w = pc; |
| 3219 | } |
| 3220 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3221 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3222 | { |
| 3223 | pc = NULL; |
| 3224 | w = line; |
| 3225 | } |
| 3226 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3227 | /* This takes time, print a message now and then. */ |
| 3228 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3229 | { |
| 3230 | vim_snprintf((char *)message, sizeof(message), |
| 3231 | _("line %6d, word %6d - %s"), |
| 3232 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3233 | msg_start(); |
| 3234 | msg_puts_long_attr(message, 0); |
| 3235 | msg_clr_eos(); |
| 3236 | msg_didout = FALSE; |
| 3237 | msg_col = 0; |
| 3238 | out_flush(); |
| 3239 | } |
| 3240 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3241 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 3242 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3243 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3244 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3245 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3246 | if (retval == FAIL) |
| 3247 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3248 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3249 | hash = hash_hash(dw); |
| 3250 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3251 | if (!HASHITEM_EMPTY(hi)) |
| 3252 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3253 | fname, lnum, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3254 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3255 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3256 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3257 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3258 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3259 | if (afflist != NULL) |
| 3260 | { |
| 3261 | /* Check for affix name that stands for keep-case word and stands |
| 3262 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3263 | if (affile->af_kep != NUL |
| 3264 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3265 | flags |= WF_KEEPCAP; |
| 3266 | if (affile->af_rar != NUL |
| 3267 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 3268 | flags |= WF_RARE; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3269 | if (affile->af_bad != NUL |
| 3270 | && vim_strchr(afflist, affile->af_bad) != NULL) |
| 3271 | flags |= WF_BANNED; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3272 | |
| 3273 | if (affile->af_pfxpostpone) |
| 3274 | /* Need to store the list of prefix IDs with the word. */ |
| 3275 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3278 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3279 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3280 | retval = FAIL; |
| 3281 | |
| 3282 | if (afflist != NULL) |
| 3283 | { |
| 3284 | /* Find all matching suffixes and add the resulting words. |
| 3285 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3286 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3287 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3288 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3289 | retval = FAIL; |
| 3290 | |
| 3291 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3292 | if (store_aff_word(dw, spin, afflist, affile, |
| 3293 | &affile->af_pref, NULL, |
| 3294 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3295 | retval = FAIL; |
| 3296 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3299 | if (spin->si_ascii && non_ascii > 0) |
| 3300 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3301 | non_ascii); |
| 3302 | hash_clear(&ht); |
| 3303 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3304 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3305 | return retval; |
| 3306 | } |
| 3307 | |
| 3308 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3309 | * Get the list of prefix IDs from the affix list "afflist". |
| 3310 | * Used for PFXPOSTPONE. |
| 3311 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 3312 | * or when out of memory. |
| 3313 | */ |
| 3314 | static char_u * |
| 3315 | get_pfxlist(affile, afflist, blp) |
| 3316 | afffile_T *affile; |
| 3317 | char_u *afflist; |
| 3318 | sblock_T **blp; |
| 3319 | { |
| 3320 | char_u *p; |
| 3321 | int cnt; |
| 3322 | int round; |
| 3323 | char_u *res = NULL; |
| 3324 | char_u key[2]; |
| 3325 | hashitem_T *hi; |
| 3326 | |
| 3327 | key[1] = NUL; |
| 3328 | |
| 3329 | /* round 1: count the number of prefix IDs. |
| 3330 | * round 2: move prefix IDs to "res" */ |
| 3331 | for (round = 1; round <= 2; ++round) |
| 3332 | { |
| 3333 | cnt = 0; |
| 3334 | for (p = afflist; *p != NUL; ++p) |
| 3335 | { |
| 3336 | key[0] = *p; |
| 3337 | hi = hash_find(&affile->af_pref, key); |
| 3338 | if (!HASHITEM_EMPTY(hi)) |
| 3339 | { |
| 3340 | /* This is a prefix ID, use the new number. */ |
| 3341 | if (round == 2) |
| 3342 | res[cnt] = HI2AH(hi)->ah_newID; |
| 3343 | ++cnt; |
| 3344 | } |
| 3345 | } |
| 3346 | if (round == 1 && cnt > 0) |
| 3347 | res = getroom(blp, cnt + 1); |
| 3348 | if (res == NULL) |
| 3349 | break; |
| 3350 | } |
| 3351 | |
| 3352 | if (res != NULL) |
| 3353 | res[cnt] = NUL; |
| 3354 | return res; |
| 3355 | } |
| 3356 | |
| 3357 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3358 | * Apply affixes to a word and store the resulting words. |
| 3359 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 3360 | * prefixes or suffixes. |
| 3361 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 3362 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3363 | * |
| 3364 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3365 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3366 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3367 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3368 | char_u *word; /* basic word start */ |
| 3369 | spellinfo_T *spin; /* spell info */ |
| 3370 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3371 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3372 | hashtab_T *ht; |
| 3373 | hashtab_T *xht; |
| 3374 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3375 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3376 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3377 | { |
| 3378 | int todo; |
| 3379 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3380 | affheader_T *ah; |
| 3381 | affentry_T *ae; |
| 3382 | regmatch_T regmatch; |
| 3383 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3384 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3385 | int i; |
| 3386 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3387 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3388 | todo = ht->ht_used; |
| 3389 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3390 | { |
| 3391 | if (!HASHITEM_EMPTY(hi)) |
| 3392 | { |
| 3393 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3394 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3395 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3396 | /* Check that the affix combines, if required, and that the word |
| 3397 | * supports this affix. */ |
| 3398 | if ((!comb || ah->ah_combine) |
| 3399 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3400 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3401 | /* Loop over all affix entries with this name. */ |
| 3402 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3403 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3404 | /* Check the condition. It's not logical to match case |
| 3405 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3406 | * Myspell. |
| 3407 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 3408 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3409 | regmatch.regprog = ae->ae_prog; |
| 3410 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3411 | if ((xht != NULL || !affile->af_pfxpostpone |
| 3412 | || ae->ae_chop != NULL) |
| 3413 | && (ae->ae_prog == NULL |
| 3414 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3415 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3416 | /* Match. Remove the chop and add the affix. */ |
| 3417 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3418 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3419 | /* prefix: chop/add at the start of the word */ |
| 3420 | if (ae->ae_add == NULL) |
| 3421 | *newword = NUL; |
| 3422 | else |
| 3423 | STRCPY(newword, ae->ae_add); |
| 3424 | p = word; |
| 3425 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3426 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3427 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3428 | #ifdef FEAT_MBYTE |
| 3429 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3430 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3431 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3432 | for ( ; i > 0; --i) |
| 3433 | mb_ptr_adv(p); |
| 3434 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3435 | else |
| 3436 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3437 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3438 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3439 | STRCAT(newword, p); |
| 3440 | } |
| 3441 | else |
| 3442 | { |
| 3443 | /* suffix: chop/add at the end of the word */ |
| 3444 | STRCPY(newword, word); |
| 3445 | if (ae->ae_chop != NULL) |
| 3446 | { |
| 3447 | /* Remove chop string. */ |
| 3448 | p = newword + STRLEN(newword); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3449 | #ifdef FEAT_MBYTE |
| 3450 | if (has_mbyte) |
| 3451 | i = mb_charlen(ae->ae_chop); |
| 3452 | else |
| 3453 | #endif |
| 3454 | i = STRLEN(ae->ae_chop); |
| 3455 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3456 | mb_ptr_back(newword, p); |
| 3457 | *p = NUL; |
| 3458 | } |
| 3459 | if (ae->ae_add != NULL) |
| 3460 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3463 | /* Store the modified word. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3464 | if (store_word(newword, spin, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3465 | flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3466 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3467 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3468 | /* When added a suffix and combining is allowed also |
| 3469 | * try adding prefixes additionally. */ |
| 3470 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3471 | if (store_aff_word(newword, spin, afflist, affile, |
| 3472 | xht, NULL, TRUE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3473 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | } |
| 3478 | } |
| 3479 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3480 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3484 | * Read a file with a list of words. |
| 3485 | */ |
| 3486 | static int |
| 3487 | spell_read_wordfile(fname, spin) |
| 3488 | char_u *fname; |
| 3489 | spellinfo_T *spin; |
| 3490 | { |
| 3491 | FILE *fd; |
| 3492 | long lnum = 0; |
| 3493 | char_u rline[MAXLINELEN]; |
| 3494 | char_u *line; |
| 3495 | char_u *pc = NULL; |
| 3496 | int l; |
| 3497 | int retval = OK; |
| 3498 | int did_word = FALSE; |
| 3499 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3500 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3501 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3502 | |
| 3503 | /* |
| 3504 | * Open the file. |
| 3505 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3506 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3507 | if (fd == NULL) |
| 3508 | { |
| 3509 | EMSG2(_(e_notopen), fname); |
| 3510 | return FAIL; |
| 3511 | } |
| 3512 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3513 | if (spin->si_verbose || p_verbose > 2) |
| 3514 | { |
| 3515 | if (!spin->si_verbose) |
| 3516 | verbose_enter(); |
| 3517 | smsg((char_u *)_("Reading word file %s..."), fname); |
| 3518 | out_flush(); |
| 3519 | if (!spin->si_verbose) |
| 3520 | verbose_leave(); |
| 3521 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3522 | |
| 3523 | /* |
| 3524 | * Read all the lines in the file one by one. |
| 3525 | */ |
| 3526 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 3527 | { |
| 3528 | line_breakcheck(); |
| 3529 | ++lnum; |
| 3530 | |
| 3531 | /* Skip comment lines. */ |
| 3532 | if (*rline == '#') |
| 3533 | continue; |
| 3534 | |
| 3535 | /* Remove CR, LF and white space from the end. */ |
| 3536 | l = STRLEN(rline); |
| 3537 | while (l > 0 && rline[l - 1] <= ' ') |
| 3538 | --l; |
| 3539 | if (l == 0) |
| 3540 | continue; /* empty or blank line */ |
| 3541 | rline[l] = NUL; |
| 3542 | |
| 3543 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 3544 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3545 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3546 | if (spin->si_conv.vc_type != CONV_NONE) |
| 3547 | { |
| 3548 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 3549 | if (pc == NULL) |
| 3550 | { |
| 3551 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3552 | fname, lnum, rline); |
| 3553 | continue; |
| 3554 | } |
| 3555 | line = pc; |
| 3556 | } |
| 3557 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3558 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3559 | { |
| 3560 | pc = NULL; |
| 3561 | line = rline; |
| 3562 | } |
| 3563 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3564 | flags = 0; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3565 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3566 | |
| 3567 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3568 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3569 | ++line; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3570 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3571 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3572 | { |
| 3573 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3574 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 3575 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3576 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3577 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 3578 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3579 | else |
| 3580 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3581 | #ifdef FEAT_MBYTE |
| 3582 | char_u *enc; |
| 3583 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3584 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3585 | line += 10; |
| 3586 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3587 | if (enc != NULL && !spin->si_ascii |
| 3588 | && convert_setup(&spin->si_conv, enc, |
| 3589 | p_enc) == FAIL) |
| 3590 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3591 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3592 | vim_free(enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3593 | #else |
| 3594 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3595 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3596 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3597 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3598 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3599 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3600 | if (STRNCMP(line, "regions=", 8) == 0) |
| 3601 | { |
| 3602 | if (spin->si_region_count > 1) |
| 3603 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 3604 | fname, lnum, line); |
| 3605 | else |
| 3606 | { |
| 3607 | line += 8; |
| 3608 | if (STRLEN(line) > 16) |
| 3609 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 3610 | fname, lnum, line); |
| 3611 | else |
| 3612 | { |
| 3613 | spin->si_region_count = STRLEN(line) / 2; |
| 3614 | STRCPY(spin->si_region_name, line); |
| 3615 | } |
| 3616 | } |
| 3617 | continue; |
| 3618 | } |
| 3619 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3620 | if (*line == '=') |
| 3621 | { |
| 3622 | /* keep-case word */ |
| 3623 | flags |= WF_KEEPCAP; |
| 3624 | ++line; |
| 3625 | } |
| 3626 | |
| 3627 | if (*line == '!') |
| 3628 | { |
| 3629 | /* Bad, bad, wicked word. */ |
| 3630 | flags |= WF_BANNED; |
| 3631 | ++line; |
| 3632 | } |
| 3633 | else if (*line == '?') |
| 3634 | { |
| 3635 | /* Rare word. */ |
| 3636 | flags |= WF_RARE; |
| 3637 | ++line; |
| 3638 | } |
| 3639 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3640 | if (VIM_ISDIGIT(*line)) |
| 3641 | { |
| 3642 | /* region number(s) */ |
| 3643 | regionmask = 0; |
| 3644 | while (VIM_ISDIGIT(*line)) |
| 3645 | { |
| 3646 | l = *line - '0'; |
| 3647 | if (l > spin->si_region_count) |
| 3648 | { |
| 3649 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
| 3650 | fname, lnum, line); |
| 3651 | break; |
| 3652 | } |
| 3653 | regionmask |= 1 << (l - 1); |
| 3654 | ++line; |
| 3655 | } |
| 3656 | flags |= WF_REGION; |
| 3657 | } |
| 3658 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3659 | if (flags == 0) |
| 3660 | { |
| 3661 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3662 | fname, lnum, line); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3663 | continue; |
| 3664 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3668 | if (spin->si_ascii && has_non_ascii(line)) |
| 3669 | { |
| 3670 | ++non_ascii; |
| 3671 | continue; |
| 3672 | } |
| 3673 | |
| 3674 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3675 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3676 | { |
| 3677 | retval = FAIL; |
| 3678 | break; |
| 3679 | } |
| 3680 | did_word = TRUE; |
| 3681 | } |
| 3682 | |
| 3683 | vim_free(pc); |
| 3684 | fclose(fd); |
| 3685 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3686 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 3687 | { |
| 3688 | if (p_verbose > 2) |
| 3689 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3690 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3691 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3692 | if (p_verbose > 2) |
| 3693 | verbose_leave(); |
| 3694 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3695 | return retval; |
| 3696 | } |
| 3697 | |
| 3698 | /* |
| 3699 | * Get part of an sblock_T, "len" bytes long. |
| 3700 | * This avoids calling free() for every little struct we use. |
| 3701 | * The memory is cleared to all zeros. |
| 3702 | * Returns NULL when out of memory. |
| 3703 | */ |
| 3704 | static void * |
| 3705 | getroom(blp, len) |
| 3706 | sblock_T **blp; |
| 3707 | size_t len; /* length needed */ |
| 3708 | { |
| 3709 | char_u *p; |
| 3710 | sblock_T *bl = *blp; |
| 3711 | |
| 3712 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 3713 | { |
| 3714 | /* Allocate a block of memory. This is not freed until much later. */ |
| 3715 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 3716 | if (bl == NULL) |
| 3717 | return NULL; |
| 3718 | bl->sb_next = *blp; |
| 3719 | *blp = bl; |
| 3720 | bl->sb_used = 0; |
| 3721 | } |
| 3722 | |
| 3723 | p = bl->sb_data + bl->sb_used; |
| 3724 | bl->sb_used += len; |
| 3725 | |
| 3726 | return p; |
| 3727 | } |
| 3728 | |
| 3729 | /* |
| 3730 | * Make a copy of a string into memory allocated with getroom(). |
| 3731 | */ |
| 3732 | static char_u * |
| 3733 | getroom_save(blp, s) |
| 3734 | sblock_T **blp; |
| 3735 | char_u *s; |
| 3736 | { |
| 3737 | char_u *sc; |
| 3738 | |
| 3739 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 3740 | if (sc != NULL) |
| 3741 | STRCPY(sc, s); |
| 3742 | return sc; |
| 3743 | } |
| 3744 | |
| 3745 | |
| 3746 | /* |
| 3747 | * Free the list of allocated sblock_T. |
| 3748 | */ |
| 3749 | static void |
| 3750 | free_blocks(bl) |
| 3751 | sblock_T *bl; |
| 3752 | { |
| 3753 | sblock_T *next; |
| 3754 | |
| 3755 | while (bl != NULL) |
| 3756 | { |
| 3757 | next = bl->sb_next; |
| 3758 | vim_free(bl); |
| 3759 | bl = next; |
| 3760 | } |
| 3761 | } |
| 3762 | |
| 3763 | /* |
| 3764 | * Allocate the root of a word tree. |
| 3765 | */ |
| 3766 | static wordnode_T * |
| 3767 | wordtree_alloc(blp) |
| 3768 | sblock_T **blp; |
| 3769 | { |
| 3770 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3771 | } |
| 3772 | |
| 3773 | /* |
| 3774 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3775 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 3776 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3777 | * For a keep-case word also store it in the keep-case tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3778 | * When "pfxlist" is not NULL store the word for each prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3779 | */ |
| 3780 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3781 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3782 | char_u *word; |
| 3783 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3784 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3785 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3786 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3787 | { |
| 3788 | int len = STRLEN(word); |
| 3789 | int ct = captype(word, word + len); |
| 3790 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3791 | int res = OK; |
| 3792 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3793 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3794 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3795 | for (p = pfxlist; res == OK; ++p) |
| 3796 | { |
| 3797 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 3798 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 3799 | if (p == NULL || *p == NUL) |
| 3800 | break; |
| 3801 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3802 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3803 | |
| 3804 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3805 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3806 | for (p = pfxlist; res == OK; ++p) |
| 3807 | { |
| 3808 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 3809 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 3810 | if (p == NULL || *p == NUL) |
| 3811 | break; |
| 3812 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3813 | ++spin->si_keepwcount; |
| 3814 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3815 | return res; |
| 3816 | } |
| 3817 | |
| 3818 | /* |
| 3819 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3820 | * When "flags" is -1 we are adding to the prefix tree where flags don't |
| 3821 | * matter and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3822 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3823 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3824 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3825 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3826 | char_u *word; |
| 3827 | wordnode_T *root; |
| 3828 | int flags; |
| 3829 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3830 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3831 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3832 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3833 | wordnode_T *node = root; |
| 3834 | wordnode_T *np; |
| 3835 | wordnode_T **prev = NULL; |
| 3836 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3837 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3838 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 3839 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3840 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3841 | /* Look for the sibling that has the same character. They are sorted |
| 3842 | * on byte value, thus stop searching when a sibling is found with a |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3843 | * higher byte value. For zero bytes (end of word) the sorting is |
| 3844 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3845 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3846 | while (node != NULL |
| 3847 | && (node->wn_byte < word[i] |
| 3848 | || (node->wn_byte == NUL |
| 3849 | && (flags < 0 |
| 3850 | ? node->wn_prefixID < prefixID |
| 3851 | : node->wn_flags < (flags & 0xff) |
| 3852 | || (node->wn_flags == (flags & 0xff) |
| 3853 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3854 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3855 | prev = &node->wn_sibling; |
| 3856 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3857 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3858 | if (node == NULL |
| 3859 | || node->wn_byte != word[i] |
| 3860 | || (word[i] == NUL |
| 3861 | && (flags < 0 |
| 3862 | || node->wn_flags != (flags & 0xff) |
| 3863 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3864 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3865 | /* Allocate a new node. */ |
| 3866 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3867 | if (np == NULL) |
| 3868 | return FAIL; |
| 3869 | np->wn_byte = word[i]; |
| 3870 | *prev = np; |
| 3871 | np->wn_sibling = node; |
| 3872 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3873 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3874 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3875 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3876 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3877 | node->wn_flags = flags; |
| 3878 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3879 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3880 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3881 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3882 | prev = &node->wn_child; |
| 3883 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3890 | * Compress a tree: find tails that are identical and can be shared. |
| 3891 | */ |
| 3892 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3893 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3894 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3895 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3896 | { |
| 3897 | hashtab_T ht; |
| 3898 | int n; |
| 3899 | int tot = 0; |
| 3900 | |
| 3901 | if (root != NULL) |
| 3902 | { |
| 3903 | hash_init(&ht); |
| 3904 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3905 | if (spin->si_verbose || p_verbose > 2) |
| 3906 | { |
| 3907 | if (!spin->si_verbose) |
| 3908 | verbose_enter(); |
| 3909 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3910 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3911 | if (p_verbose > 2) |
| 3912 | verbose_leave(); |
| 3913 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3914 | hash_clear(&ht); |
| 3915 | } |
| 3916 | } |
| 3917 | |
| 3918 | /* |
| 3919 | * Compress a node, its siblings and its children, depth first. |
| 3920 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3921 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3922 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3923 | node_compress(node, ht, tot) |
| 3924 | wordnode_T *node; |
| 3925 | hashtab_T *ht; |
| 3926 | int *tot; /* total count of nodes before compressing, |
| 3927 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3928 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3929 | wordnode_T *np; |
| 3930 | wordnode_T *tp; |
| 3931 | wordnode_T *child; |
| 3932 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3933 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3934 | int len = 0; |
| 3935 | unsigned nr, n; |
| 3936 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3937 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3938 | /* |
| 3939 | * Go through the list of siblings. Compress each child and then try |
| 3940 | * finding an identical child to replace it. |
| 3941 | * Note that with "child" we mean not just the node that is pointed to, |
| 3942 | * but the whole list of siblings, of which the node is the first. |
| 3943 | */ |
| 3944 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3945 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3946 | ++len; |
| 3947 | if ((child = np->wn_child) != NULL) |
| 3948 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3949 | /* Compress the child. This fills hashkey. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3950 | compressed += node_compress(child, ht, tot); |
| 3951 | |
| 3952 | /* Try to find an identical child. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3953 | hash = hash_hash(child->wn_u1.hashkey); |
| 3954 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3955 | tp = NULL; |
| 3956 | if (!HASHITEM_EMPTY(hi)) |
| 3957 | { |
| 3958 | /* There are children with an identical hash value. Now check |
| 3959 | * if there is one that is really identical. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3960 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3961 | if (node_equal(child, tp)) |
| 3962 | { |
| 3963 | /* Found one! Now use that child in place of the |
| 3964 | * current one. This means the current child is |
| 3965 | * dropped from the tree. */ |
| 3966 | np->wn_child = tp; |
| 3967 | ++compressed; |
| 3968 | break; |
| 3969 | } |
| 3970 | if (tp == NULL) |
| 3971 | { |
| 3972 | /* No other child with this hash value equals the child of |
| 3973 | * the node, add it to the linked list after the first |
| 3974 | * item. */ |
| 3975 | tp = HI2WN(hi); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3976 | child->wn_u2.next = tp->wn_u2.next; |
| 3977 | tp->wn_u2.next = child; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | else |
| 3981 | /* No other child has this hash value, add it to the |
| 3982 | * hashtable. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3983 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3984 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3985 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3986 | *tot += len; |
| 3987 | |
| 3988 | /* |
| 3989 | * Make a hash key for the node and its siblings, so that we can quickly |
| 3990 | * find a lookalike node. This must be done after compressing the sibling |
| 3991 | * list, otherwise the hash key would become invalid by the compression. |
| 3992 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3993 | node->wn_u1.hashkey[0] = len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3994 | nr = 0; |
| 3995 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3996 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3997 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3998 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 3999 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4000 | else |
| 4001 | /* byte node: use the byte value and the child pointer */ |
| 4002 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 4003 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4004 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4005 | |
| 4006 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 4007 | n = nr & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4008 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4009 | n = (nr >> 8) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4010 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4011 | n = (nr >> 16) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4012 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4013 | n = (nr >> 24) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4014 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 4015 | node->wn_u1.hashkey[5] = NUL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4016 | |
| 4017 | return compressed; |
| 4018 | } |
| 4019 | |
| 4020 | /* |
| 4021 | * Return TRUE when two nodes have identical siblings and children. |
| 4022 | */ |
| 4023 | static int |
| 4024 | node_equal(n1, n2) |
| 4025 | wordnode_T *n1; |
| 4026 | wordnode_T *n2; |
| 4027 | { |
| 4028 | wordnode_T *p1; |
| 4029 | wordnode_T *p2; |
| 4030 | |
| 4031 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 4032 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 4033 | if (p1->wn_byte != p2->wn_byte |
| 4034 | || (p1->wn_byte == NUL |
| 4035 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4036 | || p1->wn_region != p2->wn_region |
| 4037 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4038 | : (p1->wn_child != p2->wn_child))) |
| 4039 | break; |
| 4040 | |
| 4041 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | /* |
| 4045 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 4046 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4047 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4048 | put_bytes(fd, nr, len) |
| 4049 | FILE *fd; |
| 4050 | long_u nr; |
| 4051 | int len; |
| 4052 | { |
| 4053 | int i; |
| 4054 | |
| 4055 | for (i = len - 1; i >= 0; --i) |
| 4056 | putc((int)(nr >> (i * 8)), fd); |
| 4057 | } |
| 4058 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4059 | static int |
| 4060 | #ifdef __BORLANDC__ |
| 4061 | _RTLENTRYF |
| 4062 | #endif |
| 4063 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 4064 | |
| 4065 | /* |
| 4066 | * Function given to qsort() to sort the REP items on "from" string. |
| 4067 | */ |
| 4068 | static int |
| 4069 | #ifdef __BORLANDC__ |
| 4070 | _RTLENTRYF |
| 4071 | #endif |
| 4072 | rep_compare(s1, s2) |
| 4073 | const void *s1; |
| 4074 | const void *s2; |
| 4075 | { |
| 4076 | fromto_T *p1 = (fromto_T *)s1; |
| 4077 | fromto_T *p2 = (fromto_T *)s2; |
| 4078 | |
| 4079 | return STRCMP(p1->ft_from, p2->ft_from); |
| 4080 | } |
| 4081 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4082 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4083 | * Write the Vim spell file "fname". |
| 4084 | */ |
| 4085 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4086 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4087 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4088 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4089 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4090 | FILE *fd; |
| 4091 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4092 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4093 | wordnode_T *tree; |
| 4094 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4095 | int i; |
| 4096 | int l; |
| 4097 | garray_T *gap; |
| 4098 | fromto_T *ftp; |
| 4099 | char_u *p; |
| 4100 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4101 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4102 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4103 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4104 | { |
| 4105 | EMSG2(_(e_notopen), fname); |
| 4106 | return; |
| 4107 | } |
| 4108 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4109 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4110 | * <charflagslen> <charflags> |
| 4111 | * <fcharslen> <fchars> |
| 4112 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4113 | |
| 4114 | /* <fileID> */ |
| 4115 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 4116 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4117 | |
| 4118 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4119 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4120 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4121 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 4122 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 4123 | (size_t)1, fd); |
| 4124 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4125 | } |
| 4126 | else |
| 4127 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4128 | putc(0, fd); |
| 4129 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4132 | /* |
| 4133 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4134 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 4135 | * Skip this for ASCII, the table may conflict with the one used for |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4136 | * 'encoding'. |
| 4137 | * Also skip this for an .add.spl file, the main spell file must contain |
| 4138 | * the table (avoids that it conflicts). File is shorter too. |
| 4139 | */ |
| 4140 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4141 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4142 | putc(0, fd); |
| 4143 | putc(0, fd); |
| 4144 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4145 | } |
| 4146 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4147 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4148 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4149 | /* Write the prefix conditions. */ |
| 4150 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 4151 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4152 | /* Sort the REP items. */ |
| 4153 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 4154 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4155 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4156 | /* <SUGGEST> : <repcount> <rep> ... |
| 4157 | * <salflags> <salcount> <sal> ... |
| 4158 | * <maplen> <mapstr> */ |
| 4159 | for (round = 1; round <= 2; ++round) |
| 4160 | { |
| 4161 | if (round == 1) |
| 4162 | gap = &spin->si_rep; |
| 4163 | else |
| 4164 | { |
| 4165 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4166 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4167 | i = 0; |
| 4168 | if (spin->si_followup) |
| 4169 | i |= SAL_F0LLOWUP; |
| 4170 | if (spin->si_collapse) |
| 4171 | i |= SAL_COLLAPSE; |
| 4172 | if (spin->si_rem_accents) |
| 4173 | i |= SAL_REM_ACCENTS; |
| 4174 | putc(i, fd); /* <salflags> */ |
| 4175 | } |
| 4176 | |
| 4177 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 4178 | for (i = 0; i < gap->ga_len; ++i) |
| 4179 | { |
| 4180 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 4181 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 4182 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 4183 | for (rr = 1; rr <= 2; ++rr) |
| 4184 | { |
| 4185 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 4186 | l = STRLEN(p); |
| 4187 | putc(l, fd); |
| 4188 | fwrite(p, l, (size_t)1, fd); |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 4194 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 4195 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 4196 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4197 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4198 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4199 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4200 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4201 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4202 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4203 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4204 | if (round == 1) |
| 4205 | tree = spin->si_foldroot; |
| 4206 | else if (round == 2) |
| 4207 | tree = spin->si_keeproot; |
| 4208 | else |
| 4209 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4210 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4211 | /* Clear the index and wnode fields in the tree. */ |
| 4212 | clear_node(tree); |
| 4213 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4214 | /* Count the number of nodes. Needed to be able to allocate the |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4215 | * memory when reading the nodes. Also fills in index for shared |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4216 | * nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4217 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4218 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4219 | /* number of nodes in 4 bytes */ |
| 4220 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4221 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4222 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4223 | /* Write the nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4224 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4227 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4231 | * Clear the index and wnode fields of "node", it siblings and its |
| 4232 | * children. This is needed because they are a union with other items to save |
| 4233 | * space. |
| 4234 | */ |
| 4235 | static void |
| 4236 | clear_node(node) |
| 4237 | wordnode_T *node; |
| 4238 | { |
| 4239 | wordnode_T *np; |
| 4240 | |
| 4241 | if (node != NULL) |
| 4242 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4243 | { |
| 4244 | np->wn_u1.index = 0; |
| 4245 | np->wn_u2.wnode = NULL; |
| 4246 | |
| 4247 | if (np->wn_byte != NUL) |
| 4248 | clear_node(np->wn_child); |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | |
| 4253 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4254 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4255 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4256 | * This first writes the list of possible bytes (siblings). Then for each |
| 4257 | * byte recursively write the children. |
| 4258 | * |
| 4259 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 4260 | * are made about the indexes (so that we don't have to write them in the |
| 4261 | * file). |
| 4262 | * |
| 4263 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4264 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4265 | static int |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4266 | put_node(fd, node, index, regionmask, prefixtree) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4267 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4268 | wordnode_T *node; |
| 4269 | int index; |
| 4270 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4271 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4272 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4273 | int newindex = index; |
| 4274 | int siblingcount = 0; |
| 4275 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4276 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4277 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4278 | /* If "node" is zero the tree is empty. */ |
| 4279 | if (node == NULL) |
| 4280 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4281 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4282 | /* Store the index where this node is written. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4283 | node->wn_u1.index = index; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4284 | |
| 4285 | /* Count the number of siblings. */ |
| 4286 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4287 | ++siblingcount; |
| 4288 | |
| 4289 | /* Write the sibling count. */ |
| 4290 | if (fd != NULL) |
| 4291 | putc(siblingcount, fd); /* <siblingcount> */ |
| 4292 | |
| 4293 | /* Write each sibling byte and optionally extra info. */ |
| 4294 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4295 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4296 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4297 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4298 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4299 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4300 | /* For a NUL byte (end of word) write the flags etc. */ |
| 4301 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4302 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4303 | /* In PREFIXTREE write the required prefixID and the |
| 4304 | * associated condition nr (stored in wn_region). */ |
| 4305 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4306 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4307 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4308 | } |
| 4309 | else |
| 4310 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4311 | /* For word trees we write the flag/region items. */ |
| 4312 | flags = np->wn_flags; |
| 4313 | if (regionmask != 0 && np->wn_region != regionmask) |
| 4314 | flags |= WF_REGION; |
| 4315 | if (np->wn_prefixID != 0) |
| 4316 | flags |= WF_PFX; |
| 4317 | if (flags == 0) |
| 4318 | { |
| 4319 | /* word without flags or region */ |
| 4320 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 4321 | } |
| 4322 | else |
| 4323 | { |
| 4324 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4325 | putc(flags, fd); /* <flags> */ |
| 4326 | if (flags & WF_REGION) |
| 4327 | putc(np->wn_region, fd); /* <region> */ |
| 4328 | if (flags & WF_PFX) |
| 4329 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4330 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4331 | } |
| 4332 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4333 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4334 | else |
| 4335 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4336 | if (np->wn_child->wn_u1.index != 0 |
| 4337 | && np->wn_child->wn_u2.wnode != node) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4338 | { |
| 4339 | /* The child is written elsewhere, write the reference. */ |
| 4340 | if (fd != NULL) |
| 4341 | { |
| 4342 | putc(BY_INDEX, fd); /* <byte> */ |
| 4343 | /* <nodeidx> */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4344 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4345 | } |
| 4346 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4347 | else if (np->wn_child->wn_u2.wnode == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4348 | /* We will write the child below and give it an index. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4349 | np->wn_child->wn_u2.wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4350 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4351 | if (fd != NULL) |
| 4352 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 4353 | { |
| 4354 | EMSG(_(e_write)); |
| 4355 | return 0; |
| 4356 | } |
| 4357 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4358 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4359 | |
| 4360 | /* Space used in the array when reading: one for each sibling and one for |
| 4361 | * the count. */ |
| 4362 | newindex += siblingcount + 1; |
| 4363 | |
| 4364 | /* Recursively dump the children of each sibling. */ |
| 4365 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4366 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 4367 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4368 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4369 | |
| 4370 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | |
| 4374 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4375 | * ":mkspell [-ascii] outfile infile ..." |
| 4376 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4377 | */ |
| 4378 | void |
| 4379 | ex_mkspell(eap) |
| 4380 | exarg_T *eap; |
| 4381 | { |
| 4382 | int fcount; |
| 4383 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4384 | char_u *arg = eap->arg; |
| 4385 | int ascii = FALSE; |
| 4386 | |
| 4387 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 4388 | { |
| 4389 | ascii = TRUE; |
| 4390 | arg = skipwhite(arg + 6); |
| 4391 | } |
| 4392 | |
| 4393 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 4394 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 4395 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4396 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4397 | FreeWild(fcount, fnames); |
| 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | /* |
| 4402 | * Create a Vim spell file from one or more word lists. |
| 4403 | * "fnames[0]" is the output file name. |
| 4404 | * "fnames[fcount - 1]" is the last input file name. |
| 4405 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 4406 | * and ".spl" is appended to make the output file name. |
| 4407 | */ |
| 4408 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4409 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4410 | int fcount; |
| 4411 | char_u **fnames; |
| 4412 | int ascii; /* -ascii argument given */ |
| 4413 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4414 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4415 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4416 | char_u fname[MAXPATHL]; |
| 4417 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4418 | char_u **innames; |
| 4419 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4420 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4421 | int i; |
| 4422 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4423 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4424 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4425 | spellinfo_T spin; |
| 4426 | |
| 4427 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4428 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4429 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4430 | spin.si_followup = TRUE; |
| 4431 | spin.si_rem_accents = TRUE; |
| 4432 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 4433 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 4434 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4435 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4436 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4437 | /* default: fnames[0] is output file, following are input files */ |
| 4438 | innames = &fnames[1]; |
| 4439 | incount = fcount - 1; |
| 4440 | |
| 4441 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4442 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4443 | len = STRLEN(fnames[0]); |
| 4444 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 4445 | { |
| 4446 | /* For ":mkspell path/en.latin1.add" output file is |
| 4447 | * "path/en.latin1.add.spl". */ |
| 4448 | innames = &fnames[0]; |
| 4449 | incount = 1; |
| 4450 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 4451 | } |
| 4452 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 4453 | { |
| 4454 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4455 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4456 | } |
| 4457 | else |
| 4458 | /* Name should be language, make the file name from it. */ |
| 4459 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4460 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4461 | |
| 4462 | /* Check for .ascii.spl. */ |
| 4463 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 4464 | spin.si_ascii = TRUE; |
| 4465 | |
| 4466 | /* Check for .add.spl. */ |
| 4467 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 4468 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4471 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4472 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4473 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 4474 | EMSG(_("E751: Output file name must not have region name")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4475 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4476 | EMSG(_("E754: Only up to 8 regions supported")); |
| 4477 | else |
| 4478 | { |
| 4479 | /* Check for overwriting before doing things that may take a lot of |
| 4480 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4481 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4482 | { |
| 4483 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4484 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4485 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4486 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4487 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4488 | EMSG2(_(e_isadir2), wfname); |
| 4489 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | /* |
| 4493 | * Init the aff and dic pointers. |
| 4494 | * Get the region names if there are more than 2 arguments. |
| 4495 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4496 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4497 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4498 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4499 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4500 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4501 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4502 | len = STRLEN(innames[i]); |
| 4503 | if (STRLEN(gettail(innames[i])) < 5 |
| 4504 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4505 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4506 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 4507 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4508 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4509 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 4510 | spin.si_region_name[i * 2 + 1] = |
| 4511 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4512 | } |
| 4513 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4514 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4515 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4516 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 4517 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4518 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 4519 | if (spin.si_foldroot == NULL |
| 4520 | || spin.si_keeproot == NULL |
| 4521 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4522 | { |
| 4523 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4524 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4527 | /* When not producing a .add.spl file clear the character table when |
| 4528 | * we encounter one in the .aff file. This means we dump the current |
| 4529 | * one in the .spl file if the .aff file doesn't define one. That's |
| 4530 | * better than guessing the contents, the table will match a |
| 4531 | * previously loaded spell file. */ |
| 4532 | if (!spin.si_add) |
| 4533 | spin.si_clear_chartab = TRUE; |
| 4534 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4535 | /* |
| 4536 | * Read all the .aff and .dic files. |
| 4537 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4538 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4539 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4540 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4541 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4542 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4543 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4544 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4545 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4546 | if (mch_stat((char *)fname, &st) >= 0) |
| 4547 | { |
| 4548 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 4549 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4550 | afile[i] = spell_read_aff(fname, &spin); |
| 4551 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4552 | error = TRUE; |
| 4553 | else |
| 4554 | { |
| 4555 | /* Read the .dic file and store the words in the trees. */ |
| 4556 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4557 | innames[i]); |
| 4558 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4559 | error = TRUE; |
| 4560 | } |
| 4561 | } |
| 4562 | else |
| 4563 | { |
| 4564 | /* No .aff file, try reading the file as a word list. Store |
| 4565 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4566 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4567 | error = TRUE; |
| 4568 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4569 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4570 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4571 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4572 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4573 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4576 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4577 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4578 | /* |
| 4579 | * Remove the dummy NUL from the start of the tree root. |
| 4580 | */ |
| 4581 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 4582 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4583 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4584 | |
| 4585 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4586 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4587 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4588 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4589 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4590 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4591 | verbose_enter(); |
| 4592 | MSG(_("Compressing word tree...")); |
| 4593 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4594 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4595 | verbose_leave(); |
| 4596 | } |
| 4597 | wordtree_compress(spin.si_foldroot, &spin); |
| 4598 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4599 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4602 | if (!error) |
| 4603 | { |
| 4604 | /* |
| 4605 | * Write the info in the spell file. |
| 4606 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4607 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4608 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4609 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4610 | verbose_enter(); |
| 4611 | smsg((char_u *)_("Writing spell file %s..."), wfname); |
| 4612 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4613 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4614 | verbose_leave(); |
| 4615 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4616 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4617 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4618 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4619 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4620 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4621 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4622 | verbose_enter(); |
| 4623 | MSG(_("Done!")); |
| 4624 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4625 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4626 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4627 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4628 | verbose_leave(); |
| 4629 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4630 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4631 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4632 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4636 | ga_clear(&spin.si_rep); |
| 4637 | ga_clear(&spin.si_sal); |
| 4638 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4639 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4640 | |
| 4641 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4642 | for (i = 0; i < incount; ++i) |
| 4643 | if (afile[i] != NULL) |
| 4644 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4645 | |
| 4646 | /* Free all the bits and pieces at once. */ |
| 4647 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4648 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4651 | |
| 4652 | /* |
| 4653 | * ":spellgood {word}" |
| 4654 | * ":spellwrong {word}" |
| 4655 | */ |
| 4656 | void |
| 4657 | ex_spell(eap) |
| 4658 | exarg_T *eap; |
| 4659 | { |
| 4660 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong); |
| 4661 | } |
| 4662 | |
| 4663 | /* |
| 4664 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 4665 | */ |
| 4666 | void |
| 4667 | spell_add_word(word, len, bad) |
| 4668 | char_u *word; |
| 4669 | int len; |
| 4670 | int bad; |
| 4671 | { |
| 4672 | FILE *fd; |
| 4673 | buf_T *buf; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4674 | int new_spf = FALSE; |
| 4675 | struct stat st; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4676 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4677 | /* If 'spellfile' isn't set figure out a good default value. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4678 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4679 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4680 | init_spellfile(); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4681 | new_spf = TRUE; |
| 4682 | } |
| 4683 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4684 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4685 | EMSG(_("E764: 'spellfile' is not set")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4686 | else |
| 4687 | { |
| 4688 | /* Check that the user isn't editing the .add file somewhere. */ |
| 4689 | buf = buflist_findname_exp(curbuf->b_p_spf); |
| 4690 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 4691 | buf = NULL; |
| 4692 | if (buf != NULL && bufIsChanged(buf)) |
| 4693 | EMSG(_(e_bufloaded)); |
| 4694 | else |
| 4695 | { |
| 4696 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4697 | if (fd == NULL && new_spf) |
| 4698 | { |
| 4699 | /* We just initialized the 'spellfile' option and can't open |
| 4700 | * the file. We may need to create the "spell" directory |
| 4701 | * first. We already checked the runtime directory is |
| 4702 | * writable in init_spellfile(). */ |
| 4703 | STRCPY(NameBuff, curbuf->b_p_spf); |
| 4704 | *gettail_sep(NameBuff) = NUL; |
| 4705 | if (mch_stat((char *)NameBuff, &st) < 0) |
| 4706 | { |
| 4707 | /* The directory doesn't exist. Try creating it and |
| 4708 | * opening the file again. */ |
| 4709 | vim_mkdir(NameBuff, 0755); |
| 4710 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
| 4711 | } |
| 4712 | } |
| 4713 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4714 | if (fd == NULL) |
| 4715 | EMSG2(_(e_notopen), curbuf->b_p_spf); |
| 4716 | else |
| 4717 | { |
| 4718 | if (bad) |
| 4719 | fprintf(fd, "/!%.*s\n", len, word); |
| 4720 | else |
| 4721 | fprintf(fd, "%.*s\n", len, word); |
| 4722 | fclose(fd); |
| 4723 | |
| 4724 | /* Update the .add.spl file. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4725 | mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4726 | |
| 4727 | /* If the .add file is edited somewhere, reload it. */ |
| 4728 | if (buf != NULL) |
| 4729 | buf_reload(buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4730 | |
| 4731 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4732 | } |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | |
| 4737 | /* |
| 4738 | * Initialize 'spellfile' for the current buffer. |
| 4739 | */ |
| 4740 | static void |
| 4741 | init_spellfile() |
| 4742 | { |
| 4743 | char_u buf[MAXPATHL]; |
| 4744 | int l; |
| 4745 | slang_T *sl; |
| 4746 | char_u *rtp; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4747 | char_u *lend; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4748 | |
| 4749 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 4750 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4751 | /* Find the end of the language name. Exclude the region. */ |
| 4752 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 4753 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| 4754 | ; |
| 4755 | |
| 4756 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 4757 | * are allowed to write. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4758 | rtp = p_rtp; |
| 4759 | while (*rtp != NUL) |
| 4760 | { |
| 4761 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 4762 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 4763 | if (filewritable(buf) == 2) |
| 4764 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4765 | /* Use the first language name from 'spelllang' and the |
| 4766 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4767 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 4768 | l = STRLEN(buf); |
| 4769 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4770 | "/spell/%.*s.%s.add", |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4771 | (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4772 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 4773 | ? (char_u *)"ascii" : spell_enc()); |
| 4774 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 4775 | break; |
| 4776 | } |
| 4777 | } |
| 4778 | } |
| 4779 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4780 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4781 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4782 | /* |
| 4783 | * Init the chartab used for spelling for ASCII. |
| 4784 | * EBCDIC is not supported! |
| 4785 | */ |
| 4786 | static void |
| 4787 | clear_spell_chartab(sp) |
| 4788 | spelltab_T *sp; |
| 4789 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4790 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4791 | |
| 4792 | /* Init everything to FALSE. */ |
| 4793 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 4794 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 4795 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4796 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4797 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4798 | sp->st_upper[i] = i; |
| 4799 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4800 | |
| 4801 | /* We include digits. A word shouldn't start with a digit, but handling |
| 4802 | * that is done separately. */ |
| 4803 | for (i = '0'; i <= '9'; ++i) |
| 4804 | sp->st_isw[i] = TRUE; |
| 4805 | for (i = 'A'; i <= 'Z'; ++i) |
| 4806 | { |
| 4807 | sp->st_isw[i] = TRUE; |
| 4808 | sp->st_isu[i] = TRUE; |
| 4809 | sp->st_fold[i] = i + 0x20; |
| 4810 | } |
| 4811 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4812 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4813 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4814 | sp->st_upper[i] = i - 0x20; |
| 4815 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | /* |
| 4819 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 4820 | * Called once while starting up and when 'encoding' changes. |
| 4821 | * The default is to use isalpha(), but the spell file should define the word |
| 4822 | * characters to make it possible that 'encoding' differs from the current |
| 4823 | * locale. |
| 4824 | */ |
| 4825 | void |
| 4826 | init_spell_chartab() |
| 4827 | { |
| 4828 | int i; |
| 4829 | |
| 4830 | did_set_spelltab = FALSE; |
| 4831 | clear_spell_chartab(&spelltab); |
| 4832 | |
| 4833 | #ifdef FEAT_MBYTE |
| 4834 | if (enc_dbcs) |
| 4835 | { |
| 4836 | /* DBCS: assume double-wide characters are word characters. */ |
| 4837 | for (i = 128; i <= 255; ++i) |
| 4838 | if (MB_BYTE2LEN(i) == 2) |
| 4839 | spelltab.st_isw[i] = TRUE; |
| 4840 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4841 | else if (enc_utf8) |
| 4842 | { |
| 4843 | for (i = 128; i < 256; ++i) |
| 4844 | { |
| 4845 | spelltab.st_isu[i] = utf_isupper(i); |
| 4846 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 4847 | spelltab.st_fold[i] = utf_fold(i); |
| 4848 | spelltab.st_upper[i] = utf_toupper(i); |
| 4849 | } |
| 4850 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4851 | else |
| 4852 | #endif |
| 4853 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4854 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4855 | for (i = 128; i < 256; ++i) |
| 4856 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4857 | if (MB_ISUPPER(i)) |
| 4858 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4859 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4860 | spelltab.st_isu[i] = TRUE; |
| 4861 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 4862 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4863 | else if (MB_ISLOWER(i)) |
| 4864 | { |
| 4865 | spelltab.st_isw[i] = TRUE; |
| 4866 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 4867 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4868 | } |
| 4869 | } |
| 4870 | } |
| 4871 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4872 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 4873 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 4874 | |
| 4875 | /* |
| 4876 | * Set the spell character tables from strings in the affix file. |
| 4877 | */ |
| 4878 | static int |
| 4879 | set_spell_chartab(fol, low, upp) |
| 4880 | char_u *fol; |
| 4881 | char_u *low; |
| 4882 | char_u *upp; |
| 4883 | { |
| 4884 | /* We build the new tables here first, so that we can compare with the |
| 4885 | * previous one. */ |
| 4886 | spelltab_T new_st; |
| 4887 | char_u *pf = fol, *pl = low, *pu = upp; |
| 4888 | int f, l, u; |
| 4889 | |
| 4890 | clear_spell_chartab(&new_st); |
| 4891 | |
| 4892 | while (*pf != NUL) |
| 4893 | { |
| 4894 | if (*pl == NUL || *pu == NUL) |
| 4895 | { |
| 4896 | EMSG(_(e_affform)); |
| 4897 | return FAIL; |
| 4898 | } |
| 4899 | #ifdef FEAT_MBYTE |
| 4900 | f = mb_ptr2char_adv(&pf); |
| 4901 | l = mb_ptr2char_adv(&pl); |
| 4902 | u = mb_ptr2char_adv(&pu); |
| 4903 | #else |
| 4904 | f = *pf++; |
| 4905 | l = *pl++; |
| 4906 | u = *pu++; |
| 4907 | #endif |
| 4908 | /* Every character that appears is a word character. */ |
| 4909 | if (f < 256) |
| 4910 | new_st.st_isw[f] = TRUE; |
| 4911 | if (l < 256) |
| 4912 | new_st.st_isw[l] = TRUE; |
| 4913 | if (u < 256) |
| 4914 | new_st.st_isw[u] = TRUE; |
| 4915 | |
| 4916 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 4917 | * case-folding */ |
| 4918 | if (l < 256 && l != f) |
| 4919 | { |
| 4920 | if (f >= 256) |
| 4921 | { |
| 4922 | EMSG(_(e_affrange)); |
| 4923 | return FAIL; |
| 4924 | } |
| 4925 | new_st.st_fold[l] = f; |
| 4926 | } |
| 4927 | |
| 4928 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4929 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 4930 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4931 | if (u < 256 && u != f) |
| 4932 | { |
| 4933 | if (f >= 256) |
| 4934 | { |
| 4935 | EMSG(_(e_affrange)); |
| 4936 | return FAIL; |
| 4937 | } |
| 4938 | new_st.st_fold[u] = f; |
| 4939 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4940 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | if (*pl != NUL || *pu != NUL) |
| 4945 | { |
| 4946 | EMSG(_(e_affform)); |
| 4947 | return FAIL; |
| 4948 | } |
| 4949 | |
| 4950 | return set_spell_finish(&new_st); |
| 4951 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4952 | |
| 4953 | /* |
| 4954 | * Set the spell character tables from strings in the .spl file. |
| 4955 | */ |
| 4956 | static int |
| 4957 | set_spell_charflags(flags, cnt, upp) |
| 4958 | char_u *flags; |
| 4959 | int cnt; |
| 4960 | char_u *upp; |
| 4961 | { |
| 4962 | /* We build the new tables here first, so that we can compare with the |
| 4963 | * previous one. */ |
| 4964 | spelltab_T new_st; |
| 4965 | int i; |
| 4966 | char_u *p = upp; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4967 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4968 | |
| 4969 | clear_spell_chartab(&new_st); |
| 4970 | |
| 4971 | for (i = 0; i < cnt; ++i) |
| 4972 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4973 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 4974 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4975 | |
| 4976 | if (*p == NUL) |
| 4977 | return FAIL; |
| 4978 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4979 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4980 | #else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4981 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4982 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4983 | new_st.st_fold[i + 128] = c; |
| 4984 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 4985 | new_st.st_upper[c] = i + 128; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | return set_spell_finish(&new_st); |
| 4989 | } |
| 4990 | |
| 4991 | static int |
| 4992 | set_spell_finish(new_st) |
| 4993 | spelltab_T *new_st; |
| 4994 | { |
| 4995 | int i; |
| 4996 | |
| 4997 | if (did_set_spelltab) |
| 4998 | { |
| 4999 | /* check that it's the same table */ |
| 5000 | for (i = 0; i < 256; ++i) |
| 5001 | { |
| 5002 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 5003 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5004 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 5005 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5006 | { |
| 5007 | EMSG(_("E763: Word characters differ between spell files")); |
| 5008 | return FAIL; |
| 5009 | } |
| 5010 | } |
| 5011 | } |
| 5012 | else |
| 5013 | { |
| 5014 | /* copy the new spelltab into the one being used */ |
| 5015 | spelltab = *new_st; |
| 5016 | did_set_spelltab = TRUE; |
| 5017 | } |
| 5018 | |
| 5019 | return OK; |
| 5020 | } |
| 5021 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5022 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5023 | * Return TRUE if "p" points to a word character. |
| 5024 | * As a special case we see a single quote as a word character when it is |
| 5025 | * followed by a word character. This finds they'there but not 'they there'. |
| 5026 | */ |
| 5027 | static int |
| 5028 | spell_iswordp(p) |
| 5029 | char_u *p; |
| 5030 | { |
| 5031 | char_u *s; |
| 5032 | |
| 5033 | if (*p == '\'') |
| 5034 | s = p + 1; |
| 5035 | else |
| 5036 | s = p; |
| 5037 | #ifdef FEAT_MBYTE |
| 5038 | if (has_mbyte && MB_BYTE2LEN(*s) > 1) |
| 5039 | return mb_get_class(s) >= 2; |
| 5040 | #endif |
| 5041 | return spelltab.st_isw[*s]; |
| 5042 | } |
| 5043 | |
| 5044 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5045 | * Write the table with prefix conditions to the .spl file. |
| 5046 | */ |
| 5047 | static void |
| 5048 | write_spell_prefcond(fd, gap) |
| 5049 | FILE *fd; |
| 5050 | garray_T *gap; |
| 5051 | { |
| 5052 | int i; |
| 5053 | char_u *p; |
| 5054 | int len; |
| 5055 | |
| 5056 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 5057 | |
| 5058 | for (i = 0; i < gap->ga_len; ++i) |
| 5059 | { |
| 5060 | /* <prefcond> : <condlen> <condstr> */ |
| 5061 | p = ((char_u **)gap->ga_data)[i]; |
| 5062 | if (p == NULL) |
| 5063 | fputc(0, fd); |
| 5064 | else |
| 5065 | { |
| 5066 | len = STRLEN(p); |
| 5067 | fputc(len, fd); |
| 5068 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 5069 | } |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5074 | * Write the current tables into the .spl file. |
| 5075 | * This makes sure the same characters are recognized as word characters when |
| 5076 | * generating an when using a spell file. |
| 5077 | */ |
| 5078 | static void |
| 5079 | write_spell_chartab(fd) |
| 5080 | FILE *fd; |
| 5081 | { |
| 5082 | char_u charbuf[256 * 4]; |
| 5083 | int len = 0; |
| 5084 | int flags; |
| 5085 | int i; |
| 5086 | |
| 5087 | fputc(128, fd); /* <charflagslen> */ |
| 5088 | for (i = 128; i < 256; ++i) |
| 5089 | { |
| 5090 | flags = 0; |
| 5091 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5092 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5093 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5094 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5095 | fputc(flags, fd); /* <charflags> */ |
| 5096 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5097 | #ifdef FEAT_MBYTE |
| 5098 | if (has_mbyte) |
| 5099 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 5100 | else |
| 5101 | #endif |
| 5102 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
| 5105 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 5106 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 5107 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5108 | |
| 5109 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5110 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 5111 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5112 | * When using a multi-byte 'encoding' the length may change! |
| 5113 | * Returns FAIL when something wrong. |
| 5114 | */ |
| 5115 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5116 | spell_casefold(str, len, buf, buflen) |
| 5117 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5118 | int len; |
| 5119 | char_u *buf; |
| 5120 | int buflen; |
| 5121 | { |
| 5122 | int i; |
| 5123 | |
| 5124 | if (len >= buflen) |
| 5125 | { |
| 5126 | buf[0] = NUL; |
| 5127 | return FAIL; /* result will not fit */ |
| 5128 | } |
| 5129 | |
| 5130 | #ifdef FEAT_MBYTE |
| 5131 | if (has_mbyte) |
| 5132 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5133 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5134 | char_u *p; |
| 5135 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5136 | |
| 5137 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5138 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5139 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5140 | if (outi + MB_MAXBYTES > buflen) |
| 5141 | { |
| 5142 | buf[outi] = NUL; |
| 5143 | return FAIL; |
| 5144 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5145 | c = mb_ptr2char_adv(&p); |
| 5146 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5147 | } |
| 5148 | buf[outi] = NUL; |
| 5149 | } |
| 5150 | else |
| 5151 | #endif |
| 5152 | { |
| 5153 | /* Be quick for non-multibyte encodings. */ |
| 5154 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5155 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5156 | buf[i] = NUL; |
| 5157 | } |
| 5158 | |
| 5159 | return OK; |
| 5160 | } |
| 5161 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5162 | /* |
| 5163 | * "z?": Find badly spelled word under or after the cursor. |
| 5164 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5165 | */ |
| 5166 | void |
| 5167 | spell_suggest() |
| 5168 | { |
| 5169 | char_u *line; |
| 5170 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5171 | char_u wcopy[MAXWLEN + 2]; |
| 5172 | char_u *p; |
| 5173 | int i; |
| 5174 | int c; |
| 5175 | suginfo_T sug; |
| 5176 | suggest_T *stp; |
| 5177 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5178 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5179 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL |
| 5180 | || curwin->w_cursor.col > prev_cursor.col) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5181 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5182 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 5183 | return; |
| 5184 | |
| 5185 | /* No bad word or it starts after the cursor: use the word under the |
| 5186 | * cursor. */ |
| 5187 | curwin->w_cursor = prev_cursor; |
| 5188 | line = ml_get_curline(); |
| 5189 | p = line + curwin->w_cursor.col; |
| 5190 | /* Backup to before start of word. */ |
| 5191 | while (p > line && SPELL_ISWORDP(p)) |
| 5192 | mb_ptr_back(line, p); |
| 5193 | /* Forward to start of word. */ |
| 5194 | while (!SPELL_ISWORDP(p)) |
| 5195 | mb_ptr_adv(p); |
| 5196 | |
| 5197 | if (!SPELL_ISWORDP(p)) /* No word found. */ |
| 5198 | { |
| 5199 | beep_flush(); |
| 5200 | return; |
| 5201 | } |
| 5202 | curwin->w_cursor.col = p - line; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5205 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5206 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5207 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5208 | /* Get the list of suggestions */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5209 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5210 | |
| 5211 | if (sug.su_ga.ga_len == 0) |
| 5212 | MSG(_("Sorry, no suggestions")); |
| 5213 | else |
| 5214 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5215 | /* List the suggestions. */ |
| 5216 | msg_start(); |
| 5217 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 5218 | sug.su_badlen, sug.su_badptr); |
| 5219 | msg_puts(IObuff); |
| 5220 | msg_clr_eos(); |
| 5221 | msg_putchar('\n'); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5222 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5223 | msg_scroll = TRUE; |
| 5224 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 5225 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5226 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5227 | |
| 5228 | /* The suggested word may replace only part of the bad word, add |
| 5229 | * the not replaced part. */ |
| 5230 | STRCPY(wcopy, stp->st_word); |
| 5231 | if (sug.su_badlen > stp->st_orglen) |
| 5232 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 5233 | sug.su_badptr + stp->st_orglen, |
| 5234 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5235 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy); |
| 5236 | msg_puts(IObuff); |
| 5237 | |
| 5238 | /* The word may replace more than "su_badlen". */ |
| 5239 | if (sug.su_badlen < stp->st_orglen) |
| 5240 | { |
| 5241 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 5242 | stp->st_orglen, sug.su_badptr); |
| 5243 | msg_puts(IObuff); |
| 5244 | } |
| 5245 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5246 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5247 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5248 | /* Add the score. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5249 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5250 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"), |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5251 | stp->st_salscore ? "s " : "", |
| 5252 | stp->st_score, stp->st_altscore); |
| 5253 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5254 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"), |
| 5255 | stp->st_score); |
| 5256 | msg_advance(30); |
| 5257 | msg_puts(IObuff); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5258 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5259 | lines_left = 3; /* avoid more prompt */ |
| 5260 | msg_putchar('\n'); |
| 5261 | } |
| 5262 | |
| 5263 | /* Ask for choice. */ |
| 5264 | i = prompt_for_number(); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5265 | if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5266 | { |
| 5267 | /* Replace the word. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5268 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5269 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 5270 | if (p != NULL) |
| 5271 | { |
| 5272 | c = sug.su_badptr - line; |
| 5273 | mch_memmove(p, line, c); |
| 5274 | STRCPY(p + c, stp->st_word); |
| 5275 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 5276 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 5277 | curwin->w_cursor.col = c; |
| 5278 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5279 | |
| 5280 | /* For redo we use a change-word command. */ |
| 5281 | ResetRedobuff(); |
| 5282 | AppendToRedobuff((char_u *)"ciw"); |
| 5283 | AppendToRedobuff(stp->st_word); |
| 5284 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | else |
| 5288 | curwin->w_cursor = prev_cursor; |
| 5289 | } |
| 5290 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5291 | spell_find_cleanup(&sug); |
| 5292 | } |
| 5293 | |
| 5294 | /* |
| 5295 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 5296 | * a list of allocated strings. |
| 5297 | */ |
| 5298 | void |
| 5299 | spell_suggest_list(gap, word, maxcount) |
| 5300 | garray_T *gap; |
| 5301 | char_u *word; |
| 5302 | int maxcount; /* maximum nr of suggestions */ |
| 5303 | { |
| 5304 | suginfo_T sug; |
| 5305 | int i; |
| 5306 | suggest_T *stp; |
| 5307 | char_u *wcopy; |
| 5308 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5309 | spell_find_suggest(word, &sug, maxcount, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5310 | |
| 5311 | /* Make room in "gap". */ |
| 5312 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 5313 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 5314 | return; |
| 5315 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5316 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5317 | { |
| 5318 | stp = &SUG(sug.su_ga, i); |
| 5319 | |
| 5320 | /* The suggested word may replace only part of "word", add the not |
| 5321 | * replaced part. */ |
| 5322 | wcopy = alloc(STRLEN(stp->st_word) |
| 5323 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 5324 | if (wcopy == NULL) |
| 5325 | break; |
| 5326 | STRCPY(wcopy, stp->st_word); |
| 5327 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 5328 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 5329 | } |
| 5330 | |
| 5331 | spell_find_cleanup(&sug); |
| 5332 | } |
| 5333 | |
| 5334 | /* |
| 5335 | * Find spell suggestions for the word at the start of "badptr". |
| 5336 | * Return the suggestions in "su->su_ga". |
| 5337 | * The maximum number of suggestions is "maxcount". |
| 5338 | * Note: does use info for the current window. |
| 5339 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 5340 | */ |
| 5341 | static void |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5342 | spell_find_suggest(badptr, su, maxcount, banbadword) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5343 | char_u *badptr; |
| 5344 | suginfo_T *su; |
| 5345 | int maxcount; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5346 | int banbadword; /* don't include badword in suggestions */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5347 | { |
| 5348 | int attr; |
| 5349 | |
| 5350 | /* |
| 5351 | * Set the info in "*su". |
| 5352 | */ |
| 5353 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 5354 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 5355 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 5356 | if (*badptr == NUL) |
| 5357 | return; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5358 | hash_init(&su->su_banned); |
| 5359 | |
| 5360 | su->su_badptr = badptr; |
| 5361 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr); |
| 5362 | su->su_maxcount = maxcount; |
| 5363 | |
| 5364 | if (su->su_badlen >= MAXWLEN) |
| 5365 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 5366 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 5367 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 5368 | su->su_fbadword, MAXWLEN); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5369 | /* get caps flags for bad word */ |
| 5370 | su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5371 | |
| 5372 | /* Ban the bad word itself. It may appear in another region. */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5373 | if (banbadword) |
| 5374 | add_banned(su, su->su_badword); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5375 | |
| 5376 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5377 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5378 | * |
| 5379 | * Set a maximum score to limit the combination of operations that is |
| 5380 | * tried. |
| 5381 | */ |
| 5382 | su->su_maxscore = SCORE_MAXINIT; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5383 | suggest_try_special(su); |
| 5384 | |
| 5385 | /* |
| 5386 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 5387 | * from the .aff file and inserting a space (split the word). |
| 5388 | */ |
| 5389 | suggest_try_change(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5390 | |
| 5391 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 5392 | if (sps_flags & SPS_DOUBLE) |
| 5393 | score_comp_sal(su); |
| 5394 | |
| 5395 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5396 | * 3. Try finding sound-a-like words. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5397 | * |
| 5398 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 5399 | * very slow and often doesn't find new suggestions. |
| 5400 | */ |
| 5401 | if ((sps_flags & SPS_DOUBLE) |
| 5402 | || (!(sps_flags & SPS_FAST) |
| 5403 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 5404 | { |
| 5405 | /* Allow a higher score now. */ |
| 5406 | su->su_maxscore = SCORE_MAXMAX; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5407 | suggest_try_soundalike(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /* When CTRL-C was hit while searching do show the results. */ |
| 5411 | ui_breakcheck(); |
| 5412 | if (got_int) |
| 5413 | { |
| 5414 | (void)vgetc(); |
| 5415 | got_int = FALSE; |
| 5416 | } |
| 5417 | |
| 5418 | if (sps_flags & SPS_DOUBLE) |
| 5419 | { |
| 5420 | /* Combine the two list of suggestions. */ |
| 5421 | score_combine(su); |
| 5422 | } |
| 5423 | else if (su->su_ga.ga_len != 0) |
| 5424 | { |
| 5425 | if (sps_flags & SPS_BEST) |
| 5426 | /* Adjust the word score for how it sounds like. */ |
| 5427 | rescore_suggestions(su); |
| 5428 | |
| 5429 | /* Sort the suggestions and truncate at "maxcount". */ |
| 5430 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount); |
| 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * Free the info put in "*su" by spell_find_suggest(). |
| 5436 | */ |
| 5437 | static void |
| 5438 | spell_find_cleanup(su) |
| 5439 | suginfo_T *su; |
| 5440 | { |
| 5441 | int i; |
| 5442 | |
| 5443 | /* Free the suggestions. */ |
| 5444 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 5445 | vim_free(SUG(su->su_ga, i).st_word); |
| 5446 | ga_clear(&su->su_ga); |
| 5447 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 5448 | vim_free(SUG(su->su_sga, i).st_word); |
| 5449 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5450 | |
| 5451 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5452 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5456 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 5457 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 5458 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5459 | */ |
| 5460 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5461 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5462 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5463 | char_u *wcopy; |
| 5464 | int upper; /* TRUE: first letter made upper case */ |
| 5465 | { |
| 5466 | char_u *p; |
| 5467 | int c; |
| 5468 | int l; |
| 5469 | |
| 5470 | p = word; |
| 5471 | #ifdef FEAT_MBYTE |
| 5472 | if (has_mbyte) |
| 5473 | c = mb_ptr2char_adv(&p); |
| 5474 | else |
| 5475 | #endif |
| 5476 | c = *p++; |
| 5477 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5478 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5479 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5480 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5481 | #ifdef FEAT_MBYTE |
| 5482 | if (has_mbyte) |
| 5483 | l = mb_char2bytes(c, wcopy); |
| 5484 | else |
| 5485 | #endif |
| 5486 | { |
| 5487 | l = 1; |
| 5488 | wcopy[0] = c; |
| 5489 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5490 | vim_strncpy(wcopy + l, p, MAXWLEN - l); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5494 | * Make a copy of "word" with all the letters upper cased into |
| 5495 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5496 | */ |
| 5497 | static void |
| 5498 | allcap_copy(word, wcopy) |
| 5499 | char_u *word; |
| 5500 | char_u *wcopy; |
| 5501 | { |
| 5502 | char_u *s; |
| 5503 | char_u *d; |
| 5504 | int c; |
| 5505 | |
| 5506 | d = wcopy; |
| 5507 | for (s = word; *s != NUL; ) |
| 5508 | { |
| 5509 | #ifdef FEAT_MBYTE |
| 5510 | if (has_mbyte) |
| 5511 | c = mb_ptr2char_adv(&s); |
| 5512 | else |
| 5513 | #endif |
| 5514 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5515 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5516 | |
| 5517 | #ifdef FEAT_MBYTE |
| 5518 | if (has_mbyte) |
| 5519 | { |
| 5520 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 5521 | break; |
| 5522 | d += mb_char2bytes(c, d); |
| 5523 | } |
| 5524 | else |
| 5525 | #endif |
| 5526 | { |
| 5527 | if (d - wcopy >= MAXWLEN - 1) |
| 5528 | break; |
| 5529 | *d++ = c; |
| 5530 | } |
| 5531 | } |
| 5532 | *d = NUL; |
| 5533 | } |
| 5534 | |
| 5535 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5536 | * Try finding suggestions by recognizing specific situations. |
| 5537 | */ |
| 5538 | static void |
| 5539 | suggest_try_special(su) |
| 5540 | suginfo_T *su; |
| 5541 | { |
| 5542 | char_u *p; |
| 5543 | int len; |
| 5544 | int c; |
| 5545 | char_u word[MAXWLEN]; |
| 5546 | |
| 5547 | /* |
| 5548 | * Recognize a word that is repeated: "the the". |
| 5549 | */ |
| 5550 | p = skiptowhite(su->su_fbadword); |
| 5551 | len = p - su->su_fbadword; |
| 5552 | p = skipwhite(p); |
| 5553 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 5554 | { |
| 5555 | /* Include badflags: if the badword is onecap or allcap |
| 5556 | * use that for the goodword too: "The the" -> "The". */ |
| 5557 | c = su->su_fbadword[len]; |
| 5558 | su->su_fbadword[len] = NUL; |
| 5559 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 5560 | su->su_fbadword[len] = c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5561 | add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5566 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5567 | * |
| 5568 | * This uses a state machine. At each node in the tree we try various |
| 5569 | * operations. When trying if an operation work "depth" is increased and the |
| 5570 | * stack[] is used to store info. This allows combinations, thus insert one |
| 5571 | * character, replace one and delete another. The number of changes is |
| 5572 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5573 | */ |
| 5574 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5575 | suggest_try_change(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5576 | suginfo_T *su; |
| 5577 | { |
| 5578 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 5579 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 5580 | trystate_T stack[MAXWLEN]; |
| 5581 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 5582 | * to for word split) */ |
| 5583 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 5584 | int splitoff = 0; /* index in tword after last split */ |
| 5585 | trystate_T *sp; |
| 5586 | int newscore; |
| 5587 | langp_T *lp; |
| 5588 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5589 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5590 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5591 | int c, c2, c3; |
| 5592 | int n = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5593 | int flags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5594 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5595 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5596 | int len; |
| 5597 | char_u *p; |
| 5598 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5599 | int fl = 0, tl; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5600 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5601 | |
| 5602 | /* We make a copy of the case-folded bad word, so that we can modify it |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5603 | * to find matches (esp. REP items). Append some more text, changing |
| 5604 | * chars after the bad word may help. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5605 | STRCPY(fword, su->su_fbadword); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5606 | n = STRLEN(fword); |
| 5607 | p = su->su_badptr + su->su_badlen; |
| 5608 | (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5609 | |
| 5610 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 5611 | lp->lp_slang != NULL; ++lp) |
| 5612 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5613 | /* |
| 5614 | * Go through the whole case-fold tree, try changes at each node. |
| 5615 | * "tword[]" contains the word collected from nodes in the tree. |
| 5616 | * "fword[]" the word we are trying to match with (initially the bad |
| 5617 | * word). |
| 5618 | */ |
| 5619 | byts = lp->lp_slang->sl_fbyts; |
| 5620 | idxs = lp->lp_slang->sl_fidxs; |
| 5621 | |
| 5622 | depth = 0; |
| 5623 | stack[0].ts_state = STATE_START; |
| 5624 | stack[0].ts_score = 0; |
| 5625 | stack[0].ts_curi = 1; |
| 5626 | stack[0].ts_fidx = 0; |
| 5627 | stack[0].ts_fidxtry = 0; |
| 5628 | stack[0].ts_twordlen = 0; |
| 5629 | stack[0].ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5630 | #ifdef FEAT_MBYTE |
| 5631 | stack[0].ts_tcharlen = 0; |
| 5632 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5633 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5634 | /* |
| 5635 | * Loop to find all suggestions. At each round we either: |
| 5636 | * - For the current state try one operation, advance "ts_curi", |
| 5637 | * increase "depth". |
| 5638 | * - When a state is done go to the next, set "ts_state". |
| 5639 | * - When all states are tried decrease "depth". |
| 5640 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5641 | while (depth >= 0 && !got_int) |
| 5642 | { |
| 5643 | sp = &stack[depth]; |
| 5644 | switch (sp->ts_state) |
| 5645 | { |
| 5646 | case STATE_START: |
| 5647 | /* |
| 5648 | * Start of node: Deal with NUL bytes, which means |
| 5649 | * tword[] may end here. |
| 5650 | */ |
| 5651 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 5652 | len = byts[arridx]; /* bytes in this node */ |
| 5653 | arridx += sp->ts_curi; /* index of current byte */ |
| 5654 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5655 | if (sp->ts_curi > len || byts[arridx] != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5656 | { |
| 5657 | /* Past bytes in node and/or past NUL bytes. */ |
| 5658 | sp->ts_state = STATE_ENDNUL; |
| 5659 | break; |
| 5660 | } |
| 5661 | |
| 5662 | /* |
| 5663 | * End of word in tree. |
| 5664 | */ |
| 5665 | ++sp->ts_curi; /* eat one NUL byte */ |
| 5666 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5667 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5668 | |
| 5669 | /* |
| 5670 | * Form the word with proper case in preword. |
| 5671 | * If there is a word from a previous split, append. |
| 5672 | */ |
| 5673 | tword[sp->ts_twordlen] = NUL; |
| 5674 | if (flags & WF_KEEPCAP) |
| 5675 | /* Must find the word in the keep-case tree. */ |
| 5676 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 5677 | preword + prewordlen); |
| 5678 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5679 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5680 | /* Include badflags: if the badword is onecap or allcap |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5681 | * use that for the goodword too. But if the badword is |
| 5682 | * allcap and it's only one char long use onecap. */ |
| 5683 | c = su->su_badflags; |
| 5684 | if ((c & WF_ALLCAP) |
| 5685 | #ifdef FEAT_MBYTE |
| 5686 | && su->su_badlen == mb_ptr2len_check(su->su_badptr) |
| 5687 | #else |
| 5688 | && su->su_badlen == 1 |
| 5689 | #endif |
| 5690 | ) |
| 5691 | c = WF_ONECAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5692 | make_case_word(tword + splitoff, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5693 | preword + prewordlen, flags | c); |
| 5694 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5695 | |
| 5696 | /* Don't use a banned word. It may appear again as a good |
| 5697 | * word, thus remember it. */ |
| 5698 | if (flags & WF_BANNED) |
| 5699 | { |
| 5700 | add_banned(su, preword + prewordlen); |
| 5701 | break; |
| 5702 | } |
| 5703 | if (was_banned(su, preword + prewordlen)) |
| 5704 | break; |
| 5705 | |
| 5706 | newscore = 0; |
| 5707 | if ((flags & WF_REGION) |
| 5708 | && (((unsigned)flags >> 8) & lp->lp_region) == 0) |
| 5709 | newscore += SCORE_REGION; |
| 5710 | if (flags & WF_RARE) |
| 5711 | newscore += SCORE_RARE; |
| 5712 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5713 | if (!spell_valid_case(su->su_badflags, |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5714 | captype(preword + prewordlen, NULL))) |
| 5715 | newscore += SCORE_ICASE; |
| 5716 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5717 | if ((fword[sp->ts_fidx] == NUL |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5718 | || !spell_iswordp(fword + sp->ts_fidx)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5719 | && sp->ts_fidx >= sp->ts_fidxtry) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5720 | { |
| 5721 | /* The badword also ends: add suggestions, */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5722 | add_suggestion(su, &su->su_ga, preword, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5723 | sp->ts_fidx - repextra, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5724 | sp->ts_score + newscore, 0, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5725 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5726 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 5727 | #ifdef FEAT_MBYTE |
| 5728 | /* Don't split halfway a character. */ |
| 5729 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 5730 | #endif |
| 5731 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5732 | { |
| 5733 | /* The word in the tree ends but the badword |
| 5734 | * continues: try inserting a space and check that a valid |
| 5735 | * words starts at fword[sp->ts_fidx]. */ |
| 5736 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 5737 | { |
| 5738 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 5739 | sp->ts_save_prewordlen = prewordlen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5740 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5741 | sp->ts_save_splitoff = splitoff; |
| 5742 | |
| 5743 | /* Append a space to preword. */ |
| 5744 | STRCAT(preword, " "); |
| 5745 | prewordlen = STRLEN(preword); |
| 5746 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5747 | #ifdef FEAT_MBYTE |
| 5748 | if (has_mbyte) |
| 5749 | { |
| 5750 | int i = 0; |
| 5751 | |
| 5752 | /* Case-folding may change the number of bytes: |
| 5753 | * Count nr of chars in fword[sp->ts_fidx] and |
| 5754 | * advance that many chars in su->su_badptr. */ |
| 5755 | for (p = fword; p < fword + sp->ts_fidx; |
| 5756 | mb_ptr_adv(p)) |
| 5757 | ++i; |
| 5758 | for (p = su->su_badptr; i > 0; mb_ptr_adv(p)) |
| 5759 | --i; |
| 5760 | } |
| 5761 | else |
| 5762 | #endif |
| 5763 | p = su->su_badptr + sp->ts_fidx; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5764 | su->su_badflags = captype(p, su->su_badptr |
| 5765 | + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5766 | |
| 5767 | sp->ts_state = STATE_SPLITUNDO; |
| 5768 | ++depth; |
| 5769 | /* Restart at top of the tree. */ |
| 5770 | stack[depth].ts_arridx = 0; |
| 5771 | } |
| 5772 | } |
| 5773 | break; |
| 5774 | |
| 5775 | case STATE_SPLITUNDO: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5776 | /* Undo the changes done for word split. */ |
| 5777 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5778 | splitoff = sp->ts_save_splitoff; |
| 5779 | prewordlen = sp->ts_save_prewordlen; |
| 5780 | |
| 5781 | /* Continue looking for NUL bytes. */ |
| 5782 | sp->ts_state = STATE_START; |
| 5783 | break; |
| 5784 | |
| 5785 | case STATE_ENDNUL: |
| 5786 | /* Past the NUL bytes in the node. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5787 | if (fword[sp->ts_fidx] == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5788 | { |
| 5789 | /* The badword ends, can't use the bytes in this node. */ |
| 5790 | sp->ts_state = STATE_DEL; |
| 5791 | break; |
| 5792 | } |
| 5793 | sp->ts_state = STATE_PLAIN; |
| 5794 | /*FALLTHROUGH*/ |
| 5795 | |
| 5796 | case STATE_PLAIN: |
| 5797 | /* |
| 5798 | * Go over all possible bytes at this node, add each to |
| 5799 | * tword[] and use child node. "ts_curi" is the index. |
| 5800 | */ |
| 5801 | arridx = sp->ts_arridx; |
| 5802 | if (sp->ts_curi > byts[arridx]) |
| 5803 | { |
| 5804 | /* Done all bytes at this node, do next state. When still |
| 5805 | * at already changed bytes skip the other tricks. */ |
| 5806 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 5807 | sp->ts_state = STATE_DEL; |
| 5808 | else |
| 5809 | sp->ts_state = STATE_FINAL; |
| 5810 | } |
| 5811 | else |
| 5812 | { |
| 5813 | arridx += sp->ts_curi++; |
| 5814 | c = byts[arridx]; |
| 5815 | |
| 5816 | /* Normal byte, go one level deeper. If it's not equal to |
| 5817 | * the byte in the bad word adjust the score. But don't |
| 5818 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5819 | if (c == fword[sp->ts_fidx] |
| 5820 | #ifdef FEAT_MBYTE |
| 5821 | || (sp->ts_tcharlen > 0 |
| 5822 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5823 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5824 | ) |
| 5825 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5826 | else |
| 5827 | newscore = SCORE_SUBST; |
| 5828 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 5829 | && try_deeper(su, stack, depth, newscore)) |
| 5830 | { |
| 5831 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5832 | sp = &stack[depth]; |
| 5833 | ++sp->ts_fidx; |
| 5834 | tword[sp->ts_twordlen++] = c; |
| 5835 | sp->ts_arridx = idxs[arridx]; |
| 5836 | #ifdef FEAT_MBYTE |
| 5837 | if (newscore == SCORE_SUBST) |
| 5838 | sp->ts_isdiff = DIFF_YES; |
| 5839 | if (has_mbyte) |
| 5840 | { |
| 5841 | /* Multi-byte characters are a bit complicated to |
| 5842 | * handle: They differ when any of the bytes |
| 5843 | * differ and then their length may also differ. */ |
| 5844 | if (sp->ts_tcharlen == 0) |
| 5845 | { |
| 5846 | /* First byte. */ |
| 5847 | sp->ts_tcharidx = 0; |
| 5848 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 5849 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 5850 | sp->ts_isdiff = (newscore != 0) |
| 5851 | ? DIFF_YES : DIFF_NONE; |
| 5852 | } |
| 5853 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 5854 | /* When inserting trail bytes don't advance in |
| 5855 | * the bad word. */ |
| 5856 | --sp->ts_fidx; |
| 5857 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 5858 | { |
| 5859 | /* Last byte of character. */ |
| 5860 | if (sp->ts_isdiff == DIFF_YES) |
| 5861 | { |
| 5862 | /* Correct ts_fidx for the byte length of |
| 5863 | * the character (we didn't check that |
| 5864 | * before). */ |
| 5865 | sp->ts_fidx = sp->ts_fcharstart |
| 5866 | + MB_BYTE2LEN( |
| 5867 | fword[sp->ts_fcharstart]); |
| 5868 | |
| 5869 | /* For a similar character adjust score |
| 5870 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 5871 | if (lp->lp_slang->sl_has_map |
| 5872 | && similar_chars(lp->lp_slang, |
| 5873 | mb_ptr2char(tword |
| 5874 | + sp->ts_twordlen |
| 5875 | - sp->ts_tcharlen), |
| 5876 | mb_ptr2char(fword |
| 5877 | + sp->ts_fcharstart))) |
| 5878 | sp->ts_score -= |
| 5879 | SCORE_SUBST - SCORE_SIMILAR; |
| 5880 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5881 | else if (sp->ts_isdiff == DIFF_INSERT |
| 5882 | && sp->ts_twordlen > sp->ts_tcharlen) |
| 5883 | { |
| 5884 | /* If the previous character was the same, |
| 5885 | * thus doubling a character, give a bonus |
| 5886 | * to the score. */ |
| 5887 | p = tword + sp->ts_twordlen |
| 5888 | - sp->ts_tcharlen; |
| 5889 | c = mb_ptr2char(p); |
| 5890 | mb_ptr_back(tword, p); |
| 5891 | if (c == mb_ptr2char(p)) |
| 5892 | sp->ts_score -= SCORE_INS |
| 5893 | - SCORE_INSDUP; |
| 5894 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5895 | |
| 5896 | /* Starting a new char, reset the length. */ |
| 5897 | sp->ts_tcharlen = 0; |
| 5898 | } |
| 5899 | } |
| 5900 | else |
| 5901 | #endif |
| 5902 | { |
| 5903 | /* If we found a similar char adjust the score. |
| 5904 | * We do this after calling try_deeper() because |
| 5905 | * it's slow. */ |
| 5906 | if (newscore != 0 |
| 5907 | && lp->lp_slang->sl_has_map |
| 5908 | && similar_chars(lp->lp_slang, |
| 5909 | c, fword[sp->ts_fidx - 1])) |
| 5910 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 5911 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5912 | } |
| 5913 | } |
| 5914 | break; |
| 5915 | |
| 5916 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5917 | #ifdef FEAT_MBYTE |
| 5918 | /* When past the first byte of a multi-byte char don't try |
| 5919 | * delete/insert/swap a character. */ |
| 5920 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 5921 | { |
| 5922 | sp->ts_state = STATE_FINAL; |
| 5923 | break; |
| 5924 | } |
| 5925 | #endif |
| 5926 | /* |
| 5927 | * Try skipping one character in the bad word (delete it). |
| 5928 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5929 | sp->ts_state = STATE_INS; |
| 5930 | sp->ts_curi = 1; |
| 5931 | if (fword[sp->ts_fidx] != NUL |
| 5932 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 5933 | { |
| 5934 | ++depth; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5935 | |
| 5936 | /* Advance over the character in fword[]. Give a bonus to |
| 5937 | * the score if the same character is following "nn" -> |
| 5938 | * "n". */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5939 | #ifdef FEAT_MBYTE |
| 5940 | if (has_mbyte) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5941 | { |
| 5942 | c = mb_ptr2char(fword + sp->ts_fidx); |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5943 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5944 | if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) |
| 5945 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 5946 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5947 | else |
| 5948 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5949 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5950 | ++stack[depth].ts_fidx; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5951 | if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) |
| 5952 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 5953 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5954 | break; |
| 5955 | } |
| 5956 | /*FALLTHROUGH*/ |
| 5957 | |
| 5958 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5959 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5960 | * node. */ |
| 5961 | n = sp->ts_arridx; |
| 5962 | if (sp->ts_curi > byts[n]) |
| 5963 | { |
| 5964 | /* Done all bytes at this node, do next state. */ |
| 5965 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5966 | } |
| 5967 | else |
| 5968 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5969 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5970 | n += sp->ts_curi++; |
| 5971 | c = byts[n]; |
| 5972 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 5973 | { |
| 5974 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5975 | sp = &stack[depth]; |
| 5976 | tword[sp->ts_twordlen++] = c; |
| 5977 | sp->ts_arridx = idxs[n]; |
| 5978 | #ifdef FEAT_MBYTE |
| 5979 | if (has_mbyte) |
| 5980 | { |
| 5981 | fl = MB_BYTE2LEN(c); |
| 5982 | if (fl > 1) |
| 5983 | { |
| 5984 | /* There are following bytes for the same |
| 5985 | * character. We must find all bytes before |
| 5986 | * trying delete/insert/swap/etc. */ |
| 5987 | sp->ts_tcharlen = fl; |
| 5988 | sp->ts_tcharidx = 1; |
| 5989 | sp->ts_isdiff = DIFF_INSERT; |
| 5990 | } |
| 5991 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5992 | else |
| 5993 | fl = 1; |
| 5994 | if (fl == 1) |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5995 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5996 | { |
| 5997 | /* If the previous character was the same, thus |
| 5998 | * doubling a character, give a bonus to the |
| 5999 | * score. */ |
| 6000 | if (sp->ts_twordlen >= 2 |
| 6001 | && tword[sp->ts_twordlen - 2] == c) |
| 6002 | sp->ts_score -= SCORE_INS - SCORE_INSDUP; |
| 6003 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6004 | } |
| 6005 | } |
| 6006 | break; |
| 6007 | |
| 6008 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6009 | /* |
| 6010 | * Swap two bytes in the bad word: "12" -> "21". |
| 6011 | * We change "fword" here, it's changed back afterwards. |
| 6012 | */ |
| 6013 | p = fword + sp->ts_fidx; |
| 6014 | c = *p; |
| 6015 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6016 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6017 | /* End of word, can't swap or replace. */ |
| 6018 | sp->ts_state = STATE_FINAL; |
| 6019 | break; |
| 6020 | } |
| 6021 | #ifdef FEAT_MBYTE |
| 6022 | if (has_mbyte) |
| 6023 | { |
| 6024 | n = mb_ptr2len_check(p); |
| 6025 | c = mb_ptr2char(p); |
| 6026 | c2 = mb_ptr2char(p + n); |
| 6027 | } |
| 6028 | else |
| 6029 | #endif |
| 6030 | c2 = p[1]; |
| 6031 | if (c == c2) |
| 6032 | { |
| 6033 | /* Characters are identical, swap won't do anything. */ |
| 6034 | sp->ts_state = STATE_SWAP3; |
| 6035 | break; |
| 6036 | } |
| 6037 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 6038 | { |
| 6039 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6040 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6041 | #ifdef FEAT_MBYTE |
| 6042 | if (has_mbyte) |
| 6043 | { |
| 6044 | fl = mb_char2len(c2); |
| 6045 | mch_memmove(p, p + n, fl); |
| 6046 | mb_char2bytes(c, p + fl); |
| 6047 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 6048 | } |
| 6049 | else |
| 6050 | #endif |
| 6051 | { |
| 6052 | p[0] = c2; |
| 6053 | p[1] = c; |
| 6054 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 6055 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6056 | } |
| 6057 | else |
| 6058 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 6059 | sp->ts_state = STATE_REP_INI; |
| 6060 | break; |
| 6061 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6062 | case STATE_UNSWAP: |
| 6063 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 6064 | p = fword + sp->ts_fidx; |
| 6065 | #ifdef FEAT_MBYTE |
| 6066 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6067 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6068 | n = MB_BYTE2LEN(*p); |
| 6069 | c = mb_ptr2char(p + n); |
| 6070 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 6071 | mb_char2bytes(c, p); |
| 6072 | } |
| 6073 | else |
| 6074 | #endif |
| 6075 | { |
| 6076 | c = *p; |
| 6077 | *p = p[1]; |
| 6078 | p[1] = c; |
| 6079 | } |
| 6080 | /*FALLTHROUGH*/ |
| 6081 | |
| 6082 | case STATE_SWAP3: |
| 6083 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 6084 | * "fword" here, it's changed back afterwards. */ |
| 6085 | p = fword + sp->ts_fidx; |
| 6086 | #ifdef FEAT_MBYTE |
| 6087 | if (has_mbyte) |
| 6088 | { |
| 6089 | n = mb_ptr2len_check(p); |
| 6090 | c = mb_ptr2char(p); |
| 6091 | fl = mb_ptr2len_check(p + n); |
| 6092 | c2 = mb_ptr2char(p + n); |
| 6093 | c3 = mb_ptr2char(p + n + fl); |
| 6094 | } |
| 6095 | else |
| 6096 | #endif |
| 6097 | { |
| 6098 | c = *p; |
| 6099 | c2 = p[1]; |
| 6100 | c3 = p[2]; |
| 6101 | } |
| 6102 | |
| 6103 | /* When characters are identical: "121" then SWAP3 result is |
| 6104 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 6105 | * result is same as SWAP on next char: "112". Thus skip all |
| 6106 | * swapping. Also skip when c3 is NUL. */ |
| 6107 | if (c == c3 || c3 == NUL) |
| 6108 | { |
| 6109 | sp->ts_state = STATE_REP_INI; |
| 6110 | break; |
| 6111 | } |
| 6112 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6113 | { |
| 6114 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6115 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6116 | #ifdef FEAT_MBYTE |
| 6117 | if (has_mbyte) |
| 6118 | { |
| 6119 | tl = mb_char2len(c3); |
| 6120 | mch_memmove(p, p + n + fl, tl); |
| 6121 | mb_char2bytes(c2, p + tl); |
| 6122 | mb_char2bytes(c, p + fl + tl); |
| 6123 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 6124 | } |
| 6125 | else |
| 6126 | #endif |
| 6127 | { |
| 6128 | p[0] = p[2]; |
| 6129 | p[2] = c; |
| 6130 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6131 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6132 | } |
| 6133 | else |
| 6134 | sp->ts_state = STATE_REP_INI; |
| 6135 | break; |
| 6136 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6137 | case STATE_UNSWAP3: |
| 6138 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 6139 | p = fword + sp->ts_fidx; |
| 6140 | #ifdef FEAT_MBYTE |
| 6141 | if (has_mbyte) |
| 6142 | { |
| 6143 | n = MB_BYTE2LEN(*p); |
| 6144 | c2 = mb_ptr2char(p + n); |
| 6145 | fl = MB_BYTE2LEN(p[n]); |
| 6146 | c = mb_ptr2char(p + n + fl); |
| 6147 | tl = MB_BYTE2LEN(p[n + fl]); |
| 6148 | mch_memmove(p + fl + tl, p, n); |
| 6149 | mb_char2bytes(c, p); |
| 6150 | mb_char2bytes(c2, p + tl); |
| 6151 | } |
| 6152 | else |
| 6153 | #endif |
| 6154 | { |
| 6155 | c = *p; |
| 6156 | *p = p[2]; |
| 6157 | p[2] = c; |
| 6158 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6159 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6160 | /* Rotate three characters left: "123" -> "231". We change |
| 6161 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6162 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6163 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6164 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6165 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6166 | p = fword + sp->ts_fidx; |
| 6167 | #ifdef FEAT_MBYTE |
| 6168 | if (has_mbyte) |
| 6169 | { |
| 6170 | n = mb_ptr2len_check(p); |
| 6171 | c = mb_ptr2char(p); |
| 6172 | fl = mb_ptr2len_check(p + n); |
| 6173 | fl += mb_ptr2len_check(p + n + fl); |
| 6174 | mch_memmove(p, p + n, fl); |
| 6175 | mb_char2bytes(c, p + fl); |
| 6176 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 6177 | } |
| 6178 | else |
| 6179 | #endif |
| 6180 | { |
| 6181 | c = *p; |
| 6182 | *p = p[1]; |
| 6183 | p[1] = p[2]; |
| 6184 | p[2] = c; |
| 6185 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6186 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6187 | } |
| 6188 | else |
| 6189 | sp->ts_state = STATE_REP_INI; |
| 6190 | break; |
| 6191 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6192 | case STATE_UNROT3L: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6193 | /* Undo ROT3L: "231" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6194 | p = fword + sp->ts_fidx; |
| 6195 | #ifdef FEAT_MBYTE |
| 6196 | if (has_mbyte) |
| 6197 | { |
| 6198 | n = MB_BYTE2LEN(*p); |
| 6199 | n += MB_BYTE2LEN(p[n]); |
| 6200 | c = mb_ptr2char(p + n); |
| 6201 | tl = MB_BYTE2LEN(p[n]); |
| 6202 | mch_memmove(p + tl, p, n); |
| 6203 | mb_char2bytes(c, p); |
| 6204 | } |
| 6205 | else |
| 6206 | #endif |
| 6207 | { |
| 6208 | c = p[2]; |
| 6209 | p[2] = p[1]; |
| 6210 | p[1] = *p; |
| 6211 | *p = c; |
| 6212 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6213 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6214 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6215 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6216 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6217 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6218 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6219 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6220 | p = fword + sp->ts_fidx; |
| 6221 | #ifdef FEAT_MBYTE |
| 6222 | if (has_mbyte) |
| 6223 | { |
| 6224 | n = mb_ptr2len_check(p); |
| 6225 | n += mb_ptr2len_check(p + n); |
| 6226 | c = mb_ptr2char(p + n); |
| 6227 | tl = mb_ptr2len_check(p + n); |
| 6228 | mch_memmove(p + tl, p, n); |
| 6229 | mb_char2bytes(c, p); |
| 6230 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 6231 | } |
| 6232 | else |
| 6233 | #endif |
| 6234 | { |
| 6235 | c = p[2]; |
| 6236 | p[2] = p[1]; |
| 6237 | p[1] = *p; |
| 6238 | *p = c; |
| 6239 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6240 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6241 | } |
| 6242 | else |
| 6243 | sp->ts_state = STATE_REP_INI; |
| 6244 | break; |
| 6245 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6246 | case STATE_UNROT3R: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6247 | /* Undo ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6248 | p = fword + sp->ts_fidx; |
| 6249 | #ifdef FEAT_MBYTE |
| 6250 | if (has_mbyte) |
| 6251 | { |
| 6252 | c = mb_ptr2char(p); |
| 6253 | tl = MB_BYTE2LEN(*p); |
| 6254 | n = MB_BYTE2LEN(p[tl]); |
| 6255 | n += MB_BYTE2LEN(p[tl + n]); |
| 6256 | mch_memmove(p, p + tl, n); |
| 6257 | mb_char2bytes(c, p + n); |
| 6258 | } |
| 6259 | else |
| 6260 | #endif |
| 6261 | { |
| 6262 | c = *p; |
| 6263 | *p = p[1]; |
| 6264 | p[1] = p[2]; |
| 6265 | p[2] = c; |
| 6266 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6267 | /*FALLTHROUGH*/ |
| 6268 | |
| 6269 | case STATE_REP_INI: |
| 6270 | /* Check if matching with REP items from the .aff file would |
| 6271 | * work. Quickly skip if there are no REP items or the score |
| 6272 | * is going to be too high anyway. */ |
| 6273 | gap = &lp->lp_slang->sl_rep; |
| 6274 | if (gap->ga_len == 0 |
| 6275 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 6276 | { |
| 6277 | sp->ts_state = STATE_FINAL; |
| 6278 | break; |
| 6279 | } |
| 6280 | |
| 6281 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6282 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6283 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 6284 | if (sp->ts_curi < 0) |
| 6285 | { |
| 6286 | sp->ts_state = STATE_FINAL; |
| 6287 | break; |
| 6288 | } |
| 6289 | |
| 6290 | sp->ts_state = STATE_REP; |
| 6291 | /*FALLTHROUGH*/ |
| 6292 | |
| 6293 | case STATE_REP: |
| 6294 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6295 | * match replace the characters and check if the resulting |
| 6296 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6297 | p = fword + sp->ts_fidx; |
| 6298 | |
| 6299 | gap = &lp->lp_slang->sl_rep; |
| 6300 | while (sp->ts_curi < gap->ga_len) |
| 6301 | { |
| 6302 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 6303 | if (*ftp->ft_from != *p) |
| 6304 | { |
| 6305 | /* past possible matching entries */ |
| 6306 | sp->ts_curi = gap->ga_len; |
| 6307 | break; |
| 6308 | } |
| 6309 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 6310 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 6311 | { |
| 6312 | /* Need to undo this afterwards. */ |
| 6313 | sp->ts_state = STATE_REP_UNDO; |
| 6314 | |
| 6315 | /* Change the "from" to the "to" string. */ |
| 6316 | ++depth; |
| 6317 | fl = STRLEN(ftp->ft_from); |
| 6318 | tl = STRLEN(ftp->ft_to); |
| 6319 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6320 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6321 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6322 | repextra += tl - fl; |
| 6323 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6324 | mch_memmove(p, ftp->ft_to, tl); |
| 6325 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6326 | #ifdef FEAT_MBYTE |
| 6327 | stack[depth].ts_tcharlen = 0; |
| 6328 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6329 | break; |
| 6330 | } |
| 6331 | } |
| 6332 | |
| 6333 | if (sp->ts_curi >= gap->ga_len) |
| 6334 | /* No (more) matches. */ |
| 6335 | sp->ts_state = STATE_FINAL; |
| 6336 | |
| 6337 | break; |
| 6338 | |
| 6339 | case STATE_REP_UNDO: |
| 6340 | /* Undo a REP replacement and continue with the next one. */ |
| 6341 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 6342 | + sp->ts_curi - 1; |
| 6343 | fl = STRLEN(ftp->ft_from); |
| 6344 | tl = STRLEN(ftp->ft_to); |
| 6345 | p = fword + sp->ts_fidx; |
| 6346 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6347 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6348 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6349 | repextra -= tl - fl; |
| 6350 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6351 | mch_memmove(p, ftp->ft_from, fl); |
| 6352 | sp->ts_state = STATE_REP; |
| 6353 | break; |
| 6354 | |
| 6355 | default: |
| 6356 | /* Did all possible states at this level, go up one level. */ |
| 6357 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6358 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6359 | /* Don't check for CTRL-C too often, it takes time. */ |
| 6360 | line_breakcheck(); |
| 6361 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6362 | } |
| 6363 | } |
| 6364 | } |
| 6365 | |
| 6366 | /* |
| 6367 | * Try going one level deeper in the tree. |
| 6368 | */ |
| 6369 | static int |
| 6370 | try_deeper(su, stack, depth, score_add) |
| 6371 | suginfo_T *su; |
| 6372 | trystate_T *stack; |
| 6373 | int depth; |
| 6374 | int score_add; |
| 6375 | { |
| 6376 | int newscore; |
| 6377 | |
| 6378 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 6379 | newscore = stack[depth].ts_score + score_add; |
| 6380 | if (newscore >= su->su_maxscore) |
| 6381 | return FALSE; |
| 6382 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6383 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6384 | stack[depth + 1].ts_state = STATE_START; |
| 6385 | stack[depth + 1].ts_score = newscore; |
| 6386 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6387 | return TRUE; |
| 6388 | } |
| 6389 | |
| 6390 | /* |
| 6391 | * "fword" is a good word with case folded. Find the matching keep-case |
| 6392 | * words and put it in "kword". |
| 6393 | * Theoretically there could be several keep-case words that result in the |
| 6394 | * same case-folded word, but we only find one... |
| 6395 | */ |
| 6396 | static void |
| 6397 | find_keepcap_word(slang, fword, kword) |
| 6398 | slang_T *slang; |
| 6399 | char_u *fword; |
| 6400 | char_u *kword; |
| 6401 | { |
| 6402 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 6403 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6404 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6405 | |
| 6406 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6407 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6408 | int round[MAXWLEN]; |
| 6409 | int fwordidx[MAXWLEN]; |
| 6410 | int uwordidx[MAXWLEN]; |
| 6411 | int kwordlen[MAXWLEN]; |
| 6412 | |
| 6413 | int flen, ulen; |
| 6414 | int l; |
| 6415 | int len; |
| 6416 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6417 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6418 | char_u *p; |
| 6419 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6420 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6421 | |
| 6422 | if (byts == NULL) |
| 6423 | { |
| 6424 | /* array is empty: "cannot happen" */ |
| 6425 | *kword = NUL; |
| 6426 | return; |
| 6427 | } |
| 6428 | |
| 6429 | /* Make an all-cap version of "fword". */ |
| 6430 | allcap_copy(fword, uword); |
| 6431 | |
| 6432 | /* |
| 6433 | * Each character needs to be tried both case-folded and upper-case. |
| 6434 | * All this gets very complicated if we keep in mind that changing case |
| 6435 | * may change the byte length of a multi-byte character... |
| 6436 | */ |
| 6437 | depth = 0; |
| 6438 | arridx[0] = 0; |
| 6439 | round[0] = 0; |
| 6440 | fwordidx[0] = 0; |
| 6441 | uwordidx[0] = 0; |
| 6442 | kwordlen[0] = 0; |
| 6443 | while (depth >= 0) |
| 6444 | { |
| 6445 | if (fword[fwordidx[depth]] == NUL) |
| 6446 | { |
| 6447 | /* We are at the end of "fword". If the tree allows a word to end |
| 6448 | * here we have found a match. */ |
| 6449 | if (byts[arridx[depth] + 1] == 0) |
| 6450 | { |
| 6451 | kword[kwordlen[depth]] = NUL; |
| 6452 | return; |
| 6453 | } |
| 6454 | |
| 6455 | /* kword is getting too long, continue one level up */ |
| 6456 | --depth; |
| 6457 | } |
| 6458 | else if (++round[depth] > 2) |
| 6459 | { |
| 6460 | /* tried both fold-case and upper-case character, continue one |
| 6461 | * level up */ |
| 6462 | --depth; |
| 6463 | } |
| 6464 | else |
| 6465 | { |
| 6466 | /* |
| 6467 | * round[depth] == 1: Try using the folded-case character. |
| 6468 | * round[depth] == 2: Try using the upper-case character. |
| 6469 | */ |
| 6470 | #ifdef FEAT_MBYTE |
| 6471 | if (has_mbyte) |
| 6472 | { |
| 6473 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 6474 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 6475 | } |
| 6476 | else |
| 6477 | #endif |
| 6478 | ulen = flen = 1; |
| 6479 | if (round[depth] == 1) |
| 6480 | { |
| 6481 | p = fword + fwordidx[depth]; |
| 6482 | l = flen; |
| 6483 | } |
| 6484 | else |
| 6485 | { |
| 6486 | p = uword + uwordidx[depth]; |
| 6487 | l = ulen; |
| 6488 | } |
| 6489 | |
| 6490 | for (tryidx = arridx[depth]; l > 0; --l) |
| 6491 | { |
| 6492 | /* Perform a binary search in the list of accepted bytes. */ |
| 6493 | len = byts[tryidx++]; |
| 6494 | c = *p++; |
| 6495 | lo = tryidx; |
| 6496 | hi = tryidx + len - 1; |
| 6497 | while (lo < hi) |
| 6498 | { |
| 6499 | m = (lo + hi) / 2; |
| 6500 | if (byts[m] > c) |
| 6501 | hi = m - 1; |
| 6502 | else if (byts[m] < c) |
| 6503 | lo = m + 1; |
| 6504 | else |
| 6505 | { |
| 6506 | lo = hi = m; |
| 6507 | break; |
| 6508 | } |
| 6509 | } |
| 6510 | |
| 6511 | /* Stop if there is no matching byte. */ |
| 6512 | if (hi < lo || byts[lo] != c) |
| 6513 | break; |
| 6514 | |
| 6515 | /* Continue at the child (if there is one). */ |
| 6516 | tryidx = idxs[lo]; |
| 6517 | } |
| 6518 | |
| 6519 | if (l == 0) |
| 6520 | { |
| 6521 | /* |
| 6522 | * Found the matching char. Copy it to "kword" and go a |
| 6523 | * level deeper. |
| 6524 | */ |
| 6525 | if (round[depth] == 1) |
| 6526 | { |
| 6527 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 6528 | flen); |
| 6529 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 6530 | } |
| 6531 | else |
| 6532 | { |
| 6533 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 6534 | ulen); |
| 6535 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 6536 | } |
| 6537 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 6538 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 6539 | |
| 6540 | ++depth; |
| 6541 | arridx[depth] = tryidx; |
| 6542 | round[depth] = 0; |
| 6543 | } |
| 6544 | } |
| 6545 | } |
| 6546 | |
| 6547 | /* Didn't find it: "cannot happen". */ |
| 6548 | *kword = NUL; |
| 6549 | } |
| 6550 | |
| 6551 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6552 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 6553 | * su->su_sga. |
| 6554 | */ |
| 6555 | static void |
| 6556 | score_comp_sal(su) |
| 6557 | suginfo_T *su; |
| 6558 | { |
| 6559 | langp_T *lp; |
| 6560 | char_u badsound[MAXWLEN]; |
| 6561 | int i; |
| 6562 | suggest_T *stp; |
| 6563 | suggest_T *sstp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6564 | int score; |
| 6565 | |
| 6566 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 6567 | return; |
| 6568 | |
| 6569 | /* Use the sound-folding of the first language that supports it. */ |
| 6570 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6571 | lp->lp_slang != NULL; ++lp) |
| 6572 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6573 | { |
| 6574 | /* soundfold the bad word */ |
| 6575 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 6576 | |
| 6577 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6578 | { |
| 6579 | stp = &SUG(su->su_ga, i); |
| 6580 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6581 | /* Case-fold the suggested word, sound-fold it and compute the |
| 6582 | * sound-a-like score. */ |
| 6583 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6584 | if (score < SCORE_MAXMAX) |
| 6585 | { |
| 6586 | /* Add the suggestion. */ |
| 6587 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 6588 | sstp->st_word = vim_strsave(stp->st_word); |
| 6589 | if (sstp->st_word != NULL) |
| 6590 | { |
| 6591 | sstp->st_score = score; |
| 6592 | sstp->st_altscore = 0; |
| 6593 | sstp->st_orglen = stp->st_orglen; |
| 6594 | ++su->su_sga.ga_len; |
| 6595 | } |
| 6596 | } |
| 6597 | } |
| 6598 | break; |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | /* |
| 6603 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 6604 | * They are intwined. |
| 6605 | */ |
| 6606 | static void |
| 6607 | score_combine(su) |
| 6608 | suginfo_T *su; |
| 6609 | { |
| 6610 | int i; |
| 6611 | int j; |
| 6612 | garray_T ga; |
| 6613 | garray_T *gap; |
| 6614 | langp_T *lp; |
| 6615 | suggest_T *stp; |
| 6616 | char_u *p; |
| 6617 | char_u badsound[MAXWLEN]; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6618 | int round; |
| 6619 | |
| 6620 | /* Add the alternate score to su_ga. */ |
| 6621 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6622 | lp->lp_slang != NULL; ++lp) |
| 6623 | { |
| 6624 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6625 | { |
| 6626 | /* soundfold the bad word */ |
| 6627 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 6628 | |
| 6629 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6630 | { |
| 6631 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6632 | stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang, |
| 6633 | badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6634 | if (stp->st_altscore == SCORE_MAXMAX) |
| 6635 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 6636 | else |
| 6637 | stp->st_score = (stp->st_score * 3 |
| 6638 | + stp->st_altscore) / 4; |
| 6639 | stp->st_salscore = FALSE; |
| 6640 | } |
| 6641 | break; |
| 6642 | } |
| 6643 | } |
| 6644 | |
| 6645 | /* Add the alternate score to su_sga. */ |
| 6646 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6647 | { |
| 6648 | stp = &SUG(su->su_sga, i); |
| 6649 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 6650 | if (stp->st_score == SCORE_MAXMAX) |
| 6651 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 6652 | else |
| 6653 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 6654 | stp->st_salscore = TRUE; |
| 6655 | } |
| 6656 | |
| 6657 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 6658 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6659 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 6660 | |
| 6661 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 6662 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 6663 | return; |
| 6664 | |
| 6665 | stp = &SUG(ga, 0); |
| 6666 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 6667 | { |
| 6668 | /* round 1: get a suggestion from su_ga |
| 6669 | * round 2: get a suggestion from su_sga */ |
| 6670 | for (round = 1; round <= 2; ++round) |
| 6671 | { |
| 6672 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 6673 | if (i < gap->ga_len) |
| 6674 | { |
| 6675 | /* Don't add a word if it's already there. */ |
| 6676 | p = SUG(*gap, i).st_word; |
| 6677 | for (j = 0; j < ga.ga_len; ++j) |
| 6678 | if (STRCMP(stp[j].st_word, p) == 0) |
| 6679 | break; |
| 6680 | if (j == ga.ga_len) |
| 6681 | stp[ga.ga_len++] = SUG(*gap, i); |
| 6682 | else |
| 6683 | vim_free(p); |
| 6684 | } |
| 6685 | } |
| 6686 | } |
| 6687 | |
| 6688 | ga_clear(&su->su_ga); |
| 6689 | ga_clear(&su->su_sga); |
| 6690 | |
| 6691 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 6692 | if (ga.ga_len > su->su_maxcount) |
| 6693 | { |
| 6694 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 6695 | vim_free(stp[i].st_word); |
| 6696 | ga.ga_len = su->su_maxcount; |
| 6697 | } |
| 6698 | |
| 6699 | su->su_ga = ga; |
| 6700 | } |
| 6701 | |
| 6702 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6703 | * For the goodword in "stp" compute the soundalike score compared to the |
| 6704 | * badword. |
| 6705 | */ |
| 6706 | static int |
| 6707 | stp_sal_score(stp, su, slang, badsound) |
| 6708 | suggest_T *stp; |
| 6709 | suginfo_T *su; |
| 6710 | slang_T *slang; |
| 6711 | char_u *badsound; /* sound-folded badword */ |
| 6712 | { |
| 6713 | char_u *p; |
| 6714 | char_u badsound2[MAXWLEN]; |
| 6715 | char_u fword[MAXWLEN]; |
| 6716 | char_u goodsound[MAXWLEN]; |
| 6717 | |
| 6718 | if (stp->st_orglen <= su->su_badlen) |
| 6719 | p = badsound; |
| 6720 | else |
| 6721 | { |
| 6722 | /* soundfold the bad word with more characters following */ |
| 6723 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 6724 | |
| 6725 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 6726 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 6727 | * removing the space. Don't do it when the good word also contains a |
| 6728 | * space. */ |
| 6729 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 6730 | && *skiptowhite(stp->st_word) == NUL) |
| 6731 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| 6732 | mch_memmove(p, p + 1, STRLEN(p)); |
| 6733 | |
| 6734 | spell_soundfold(slang, fword, badsound2); |
| 6735 | p = badsound2; |
| 6736 | } |
| 6737 | |
| 6738 | /* Case-fold the word, sound-fold the word and compute the score for the |
| 6739 | * difference. */ |
| 6740 | (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), fword, MAXWLEN); |
| 6741 | spell_soundfold(slang, fword, goodsound); |
| 6742 | |
| 6743 | return soundalike_score(goodsound, p); |
| 6744 | } |
| 6745 | |
| 6746 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6747 | * Find suggestions by comparing the word in a sound-a-like form. |
| 6748 | */ |
| 6749 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6750 | suggest_try_soundalike(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6751 | suginfo_T *su; |
| 6752 | { |
| 6753 | char_u salword[MAXWLEN]; |
| 6754 | char_u tword[MAXWLEN]; |
| 6755 | char_u tfword[MAXWLEN]; |
| 6756 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6757 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6758 | int curi[MAXWLEN]; |
| 6759 | langp_T *lp; |
| 6760 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6761 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6762 | int depth; |
| 6763 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6764 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6765 | int round; |
| 6766 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6767 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6768 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6769 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6770 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6771 | lp->lp_slang != NULL; ++lp) |
| 6772 | { |
| 6773 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6774 | { |
| 6775 | /* soundfold the bad word */ |
| 6776 | spell_soundfold(lp->lp_slang, su->su_fbadword, salword); |
| 6777 | |
| 6778 | /* |
| 6779 | * Go through the whole tree, soundfold each word and compare. |
| 6780 | * round 1: use the case-folded tree. |
| 6781 | * round 2: use the keep-case tree. |
| 6782 | */ |
| 6783 | for (round = 1; round <= 2; ++round) |
| 6784 | { |
| 6785 | if (round == 1) |
| 6786 | { |
| 6787 | byts = lp->lp_slang->sl_fbyts; |
| 6788 | idxs = lp->lp_slang->sl_fidxs; |
| 6789 | } |
| 6790 | else |
| 6791 | { |
| 6792 | byts = lp->lp_slang->sl_kbyts; |
| 6793 | idxs = lp->lp_slang->sl_kidxs; |
| 6794 | } |
| 6795 | |
| 6796 | depth = 0; |
| 6797 | arridx[0] = 0; |
| 6798 | curi[0] = 1; |
| 6799 | while (depth >= 0 && !got_int) |
| 6800 | { |
| 6801 | if (curi[depth] > byts[arridx[depth]]) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6802 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6803 | /* Done all bytes at this node, go up one level. */ |
| 6804 | --depth; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6805 | line_breakcheck(); |
| 6806 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6807 | else |
| 6808 | { |
| 6809 | /* Do one more byte at this node. */ |
| 6810 | n = arridx[depth] + curi[depth]; |
| 6811 | ++curi[depth]; |
| 6812 | c = byts[n]; |
| 6813 | if (c == 0) |
| 6814 | { |
| 6815 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6816 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6817 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 6818 | { |
| 6819 | tword[depth] = NUL; |
| 6820 | if (round == 1) |
| 6821 | spell_soundfold(lp->lp_slang, |
| 6822 | tword, tsalword); |
| 6823 | else |
| 6824 | { |
| 6825 | /* In keep-case tree need to case-fold the |
| 6826 | * word. */ |
| 6827 | (void)spell_casefold(tword, depth, |
| 6828 | tfword, MAXWLEN); |
| 6829 | spell_soundfold(lp->lp_slang, |
| 6830 | tfword, tsalword); |
| 6831 | } |
| 6832 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6833 | /* Compute the edit distance between the |
| 6834 | * sound-a-like words. */ |
| 6835 | sound_score = soundalike_score(salword, |
| 6836 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6837 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6838 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6839 | char_u cword[MAXWLEN]; |
| 6840 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6841 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6842 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6843 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6844 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6845 | /* Need to fix case according to |
| 6846 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6847 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6848 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6849 | } |
| 6850 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6851 | p = tword; |
| 6852 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6853 | if (sps_flags & SPS_DOUBLE) |
| 6854 | add_suggestion(su, &su->su_sga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6855 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6856 | sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6857 | else |
| 6858 | { |
| 6859 | /* Compute the score. */ |
| 6860 | score = spell_edit_score( |
| 6861 | su->su_badword, p); |
| 6862 | if (sps_flags & SPS_BEST) |
| 6863 | /* give a bonus for the good word |
| 6864 | * sounding the same as the bad |
| 6865 | * word */ |
| 6866 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6867 | su->su_badlen, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6868 | RESCORE(score, sound_score), |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6869 | sound_score, TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6870 | else |
| 6871 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6872 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6873 | score + sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6874 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6875 | } |
| 6876 | } |
| 6877 | |
| 6878 | /* Skip over other NUL bytes. */ |
| 6879 | while (byts[n + 1] == 0) |
| 6880 | { |
| 6881 | ++n; |
| 6882 | ++curi[depth]; |
| 6883 | } |
| 6884 | } |
| 6885 | else |
| 6886 | { |
| 6887 | /* Normal char, go one level deeper. */ |
| 6888 | tword[depth++] = c; |
| 6889 | arridx[depth] = idxs[n]; |
| 6890 | curi[depth] = 1; |
| 6891 | } |
| 6892 | } |
| 6893 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6894 | } |
| 6895 | } |
| 6896 | } |
| 6897 | } |
| 6898 | |
| 6899 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6900 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6901 | */ |
| 6902 | static void |
| 6903 | make_case_word(fword, cword, flags) |
| 6904 | char_u *fword; |
| 6905 | char_u *cword; |
| 6906 | int flags; |
| 6907 | { |
| 6908 | if (flags & WF_ALLCAP) |
| 6909 | /* Make it all upper-case */ |
| 6910 | allcap_copy(fword, cword); |
| 6911 | else if (flags & WF_ONECAP) |
| 6912 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6913 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6914 | else |
| 6915 | /* Use goodword as-is. */ |
| 6916 | STRCPY(cword, fword); |
| 6917 | } |
| 6918 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6919 | /* |
| 6920 | * Use map string "map" for languages "lp". |
| 6921 | */ |
| 6922 | static void |
| 6923 | set_map_str(lp, map) |
| 6924 | slang_T *lp; |
| 6925 | char_u *map; |
| 6926 | { |
| 6927 | char_u *p; |
| 6928 | int headc = 0; |
| 6929 | int c; |
| 6930 | int i; |
| 6931 | |
| 6932 | if (*map == NUL) |
| 6933 | { |
| 6934 | lp->sl_has_map = FALSE; |
| 6935 | return; |
| 6936 | } |
| 6937 | lp->sl_has_map = TRUE; |
| 6938 | |
| 6939 | /* Init the array and hash table empty. */ |
| 6940 | for (i = 0; i < 256; ++i) |
| 6941 | lp->sl_map_array[i] = 0; |
| 6942 | #ifdef FEAT_MBYTE |
| 6943 | hash_init(&lp->sl_map_hash); |
| 6944 | #endif |
| 6945 | |
| 6946 | /* |
| 6947 | * The similar characters are stored separated with slashes: |
| 6948 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 6949 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 6950 | */ |
| 6951 | for (p = map; *p != NUL; ) |
| 6952 | { |
| 6953 | #ifdef FEAT_MBYTE |
| 6954 | c = mb_ptr2char_adv(&p); |
| 6955 | #else |
| 6956 | c = *p++; |
| 6957 | #endif |
| 6958 | if (c == '/') |
| 6959 | headc = 0; |
| 6960 | else |
| 6961 | { |
| 6962 | if (headc == 0) |
| 6963 | headc = c; |
| 6964 | |
| 6965 | #ifdef FEAT_MBYTE |
| 6966 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 6967 | * the hash table. Each entry is the char, a NUL the headchar and |
| 6968 | * a NUL. */ |
| 6969 | if (c >= 256) |
| 6970 | { |
| 6971 | int cl = mb_char2len(c); |
| 6972 | int headcl = mb_char2len(headc); |
| 6973 | char_u *b; |
| 6974 | hash_T hash; |
| 6975 | hashitem_T *hi; |
| 6976 | |
| 6977 | b = alloc((unsigned)(cl + headcl + 2)); |
| 6978 | if (b == NULL) |
| 6979 | return; |
| 6980 | mb_char2bytes(c, b); |
| 6981 | b[cl] = NUL; |
| 6982 | mb_char2bytes(headc, b + cl + 1); |
| 6983 | b[cl + 1 + headcl] = NUL; |
| 6984 | hash = hash_hash(b); |
| 6985 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 6986 | if (HASHITEM_EMPTY(hi)) |
| 6987 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 6988 | else |
| 6989 | { |
| 6990 | /* This should have been checked when generating the .spl |
| 6991 | * file. */ |
| 6992 | EMSG(_("E999: duplicate char in MAP entry")); |
| 6993 | vim_free(b); |
| 6994 | } |
| 6995 | } |
| 6996 | else |
| 6997 | #endif |
| 6998 | lp->sl_map_array[c] = headc; |
| 6999 | } |
| 7000 | } |
| 7001 | } |
| 7002 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7003 | /* |
| 7004 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 7005 | * lines in the .aff file. |
| 7006 | */ |
| 7007 | static int |
| 7008 | similar_chars(slang, c1, c2) |
| 7009 | slang_T *slang; |
| 7010 | int c1; |
| 7011 | int c2; |
| 7012 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7013 | int m1, m2; |
| 7014 | #ifdef FEAT_MBYTE |
| 7015 | char_u buf[MB_MAXBYTES]; |
| 7016 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7017 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7018 | if (c1 >= 256) |
| 7019 | { |
| 7020 | buf[mb_char2bytes(c1, buf)] = 0; |
| 7021 | hi = hash_find(&slang->sl_map_hash, buf); |
| 7022 | if (HASHITEM_EMPTY(hi)) |
| 7023 | m1 = 0; |
| 7024 | else |
| 7025 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 7026 | } |
| 7027 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7028 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7029 | m1 = slang->sl_map_array[c1]; |
| 7030 | if (m1 == 0) |
| 7031 | return FALSE; |
| 7032 | |
| 7033 | |
| 7034 | #ifdef FEAT_MBYTE |
| 7035 | if (c2 >= 256) |
| 7036 | { |
| 7037 | buf[mb_char2bytes(c2, buf)] = 0; |
| 7038 | hi = hash_find(&slang->sl_map_hash, buf); |
| 7039 | if (HASHITEM_EMPTY(hi)) |
| 7040 | m2 = 0; |
| 7041 | else |
| 7042 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 7043 | } |
| 7044 | else |
| 7045 | #endif |
| 7046 | m2 = slang->sl_map_array[c2]; |
| 7047 | |
| 7048 | return m1 == m2; |
| 7049 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7050 | |
| 7051 | /* |
| 7052 | * Add a suggestion to the list of suggestions. |
| 7053 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 7054 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 7055 | * with spell_edit_score(). |
| 7056 | */ |
| 7057 | static void |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7058 | add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7059 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7060 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7061 | char_u *goodword; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7062 | int badlen; /* length of bad word used */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7063 | int score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7064 | int altscore; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7065 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7066 | { |
| 7067 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7068 | int i; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7069 | char_u *p = NULL; |
| 7070 | int c = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7071 | |
| 7072 | /* Check that the word wasn't banned. */ |
| 7073 | if (was_banned(su, goodword)) |
| 7074 | return; |
| 7075 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7076 | /* If past "su_badlen" and the rest is identical stop at "su_badlen". |
| 7077 | * Remove the common part from "goodword". */ |
| 7078 | i = badlen - su->su_badlen; |
| 7079 | if (i > 0) |
| 7080 | { |
| 7081 | /* This assumes there was no case folding or it didn't change the |
| 7082 | * length... */ |
| 7083 | p = goodword + STRLEN(goodword) - i; |
| 7084 | if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0) |
| 7085 | { |
| 7086 | badlen = su->su_badlen; |
| 7087 | c = *p; |
| 7088 | *p = NUL; |
| 7089 | } |
| 7090 | else |
| 7091 | p = NULL; |
| 7092 | } |
| 7093 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7094 | if (score <= su->su_maxscore) |
| 7095 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7096 | /* Check if the word is already there. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7097 | stp = &SUG(*gap, 0); |
| 7098 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7099 | if (STRCMP(stp[i].st_word, goodword) == 0) |
| 7100 | { |
| 7101 | /* Found it. Remember the lowest score. */ |
| 7102 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7103 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7104 | stp[i].st_score = score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7105 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7106 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7107 | break; |
| 7108 | } |
| 7109 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7110 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7111 | { |
| 7112 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7113 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7114 | stp->st_word = vim_strsave(goodword); |
| 7115 | if (stp->st_word != NULL) |
| 7116 | { |
| 7117 | stp->st_score = score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7118 | stp->st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7119 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7120 | stp->st_orglen = badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7121 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7122 | |
| 7123 | /* If we have too many suggestions now, sort the list and keep |
| 7124 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7125 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 7126 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 7127 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7128 | } |
| 7129 | } |
| 7130 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7131 | |
| 7132 | if (p != NULL) |
| 7133 | *p = c; /* restore "goodword" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | /* |
| 7137 | * Add a word to be banned. |
| 7138 | */ |
| 7139 | static void |
| 7140 | add_banned(su, word) |
| 7141 | suginfo_T *su; |
| 7142 | char_u *word; |
| 7143 | { |
| 7144 | char_u *s = vim_strsave(word); |
| 7145 | hash_T hash; |
| 7146 | hashitem_T *hi; |
| 7147 | |
| 7148 | if (s != NULL) |
| 7149 | { |
| 7150 | hash = hash_hash(s); |
| 7151 | hi = hash_lookup(&su->su_banned, s, hash); |
| 7152 | if (HASHITEM_EMPTY(hi)) |
| 7153 | hash_add_item(&su->su_banned, hi, s, hash); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 7154 | else |
| 7155 | vim_free(s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | /* |
| 7160 | * Return TRUE if a word appears in the list of banned words. |
| 7161 | */ |
| 7162 | static int |
| 7163 | was_banned(su, word) |
| 7164 | suginfo_T *su; |
| 7165 | char_u *word; |
| 7166 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7167 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 7168 | |
| 7169 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7170 | } |
| 7171 | |
| 7172 | /* |
| 7173 | * Free the banned words in "su". |
| 7174 | */ |
| 7175 | static void |
| 7176 | free_banned(su) |
| 7177 | suginfo_T *su; |
| 7178 | { |
| 7179 | int todo; |
| 7180 | hashitem_T *hi; |
| 7181 | |
| 7182 | todo = su->su_banned.ht_used; |
| 7183 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 7184 | { |
| 7185 | if (!HASHITEM_EMPTY(hi)) |
| 7186 | { |
| 7187 | vim_free(hi->hi_key); |
| 7188 | --todo; |
| 7189 | } |
| 7190 | } |
| 7191 | hash_clear(&su->su_banned); |
| 7192 | } |
| 7193 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7194 | /* |
| 7195 | * Recompute the score if sound-folding is possible. This is slow, |
| 7196 | * thus only done for the final results. |
| 7197 | */ |
| 7198 | static void |
| 7199 | rescore_suggestions(su) |
| 7200 | suginfo_T *su; |
| 7201 | { |
| 7202 | langp_T *lp; |
| 7203 | suggest_T *stp; |
| 7204 | char_u sal_badword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7205 | int i; |
| 7206 | |
| 7207 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7208 | lp->lp_slang != NULL; ++lp) |
| 7209 | { |
| 7210 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7211 | { |
| 7212 | /* soundfold the bad word */ |
| 7213 | spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword); |
| 7214 | |
| 7215 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7216 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7217 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7218 | if (!stp->st_had_bonus) |
| 7219 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7220 | stp->st_altscore = stp_sal_score(stp, su, |
| 7221 | lp->lp_slang, sal_badword); |
| 7222 | if (stp->st_altscore == SCORE_MAXMAX) |
| 7223 | stp->st_altscore = SCORE_BIG; |
| 7224 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7225 | } |
| 7226 | } |
| 7227 | break; |
| 7228 | } |
| 7229 | } |
| 7230 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7231 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7232 | static int |
| 7233 | #ifdef __BORLANDC__ |
| 7234 | _RTLENTRYF |
| 7235 | #endif |
| 7236 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 7237 | |
| 7238 | /* |
| 7239 | * Function given to qsort() to sort the suggestions on st_score. |
| 7240 | */ |
| 7241 | static int |
| 7242 | #ifdef __BORLANDC__ |
| 7243 | _RTLENTRYF |
| 7244 | #endif |
| 7245 | sug_compare(s1, s2) |
| 7246 | const void *s1; |
| 7247 | const void *s2; |
| 7248 | { |
| 7249 | suggest_T *p1 = (suggest_T *)s1; |
| 7250 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7251 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7252 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7253 | if (n == 0) |
| 7254 | return p1->st_altscore - p2->st_altscore; |
| 7255 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | /* |
| 7259 | * Cleanup the suggestions: |
| 7260 | * - Sort on score. |
| 7261 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7262 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7263 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7264 | static int |
| 7265 | cleanup_suggestions(gap, maxscore, keep) |
| 7266 | garray_T *gap; |
| 7267 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7268 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7269 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7270 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7271 | int i; |
| 7272 | |
| 7273 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7274 | qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7275 | |
| 7276 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7277 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7278 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7279 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7280 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7281 | gap->ga_len = keep; |
| 7282 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7283 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7284 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7285 | } |
| 7286 | |
| 7287 | /* |
| 7288 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 7289 | */ |
| 7290 | static void |
| 7291 | spell_soundfold(slang, inword, res) |
| 7292 | slang_T *slang; |
| 7293 | char_u *inword; |
| 7294 | char_u *res; |
| 7295 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7296 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7297 | char_u word[MAXWLEN]; |
| 7298 | #ifdef FEAT_MBYTE |
| 7299 | int l; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7300 | int found_mbyte = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7301 | #endif |
| 7302 | char_u *s; |
| 7303 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7304 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7305 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7306 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7307 | int n, k = 0; |
| 7308 | int z0; |
| 7309 | int k0; |
| 7310 | int n0; |
| 7311 | int c; |
| 7312 | int pri; |
| 7313 | int p0 = -333; |
| 7314 | int c0; |
| 7315 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7316 | /* Remove accents, if wanted. We actually remove all non-word characters. |
| 7317 | * But keep white space. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7318 | if (slang->sl_rem_accents) |
| 7319 | { |
| 7320 | t = word; |
| 7321 | for (s = inword; *s != NUL; ) |
| 7322 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7323 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7324 | { |
| 7325 | *t++ = ' '; |
| 7326 | s = skipwhite(s); |
| 7327 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7328 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7329 | else if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7330 | { |
| 7331 | l = mb_ptr2len_check(s); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7332 | if (spell_iswordp(s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7333 | { |
| 7334 | mch_memmove(t, s, l); |
| 7335 | t += l; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7336 | if (l > 1) |
| 7337 | found_mbyte = TRUE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7338 | } |
| 7339 | s += l; |
| 7340 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7341 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7342 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7343 | { |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7344 | if (spell_iswordp(s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7345 | *t++ = *s; |
| 7346 | ++s; |
| 7347 | } |
| 7348 | } |
| 7349 | *t = NUL; |
| 7350 | } |
| 7351 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7352 | { |
| 7353 | #ifdef FEAT_MBYTE |
| 7354 | if (has_mbyte) |
| 7355 | for (s = inword; *s != NUL; s += l) |
| 7356 | if ((l = mb_ptr2len_check(s)) > 1) |
| 7357 | { |
| 7358 | found_mbyte = TRUE; |
| 7359 | break; |
| 7360 | } |
| 7361 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7362 | STRCPY(word, inword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| 7365 | #ifdef FEAT_MBYTE |
| 7366 | /* If there are multi-byte characters in the word return it as-is, because |
| 7367 | * the following won't work. */ |
| 7368 | if (found_mbyte) |
| 7369 | { |
| 7370 | STRCPY(res, word); |
| 7371 | return; |
| 7372 | } |
| 7373 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7374 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7375 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7376 | |
| 7377 | /* |
| 7378 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7379 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7380 | * TODO: support for multi-byte chars. |
| 7381 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7382 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7383 | while ((c = word[i]) != NUL) |
| 7384 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7385 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7386 | n = slang->sl_sal_first[c]; |
| 7387 | z0 = 0; |
| 7388 | |
| 7389 | if (n >= 0) |
| 7390 | { |
| 7391 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7392 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7393 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7394 | /* Quickly skip entries that don't match the word. Most |
| 7395 | * entries are less then three chars, optimize for that. */ |
| 7396 | k = smp[n].sm_leadlen; |
| 7397 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7398 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7399 | if (word[i + 1] != s[1]) |
| 7400 | continue; |
| 7401 | if (k > 2) |
| 7402 | { |
| 7403 | for (j = 2; j < k; ++j) |
| 7404 | if (word[i + j] != s[j]) |
| 7405 | break; |
| 7406 | if (j < k) |
| 7407 | continue; |
| 7408 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7411 | if ((pf = smp[n].sm_oneoff) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7412 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7413 | /* Check for match with one of the chars in "sm_oneoff". */ |
| 7414 | while (*pf != NUL && *pf != word[i + k]) |
| 7415 | ++pf; |
| 7416 | if (*pf == NUL) |
| 7417 | continue; |
| 7418 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7419 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7420 | s = smp[n].sm_rules; |
| 7421 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7422 | |
| 7423 | p0 = *s; |
| 7424 | k0 = k; |
| 7425 | while (*s == '-' && k > 1) |
| 7426 | { |
| 7427 | k--; |
| 7428 | s++; |
| 7429 | } |
| 7430 | if (*s == '<') |
| 7431 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7432 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7433 | { |
| 7434 | /* determine priority */ |
| 7435 | pri = *s - '0'; |
| 7436 | s++; |
| 7437 | } |
| 7438 | if (*s == '^' && *(s + 1) == '^') |
| 7439 | s++; |
| 7440 | |
| 7441 | if (*s == NUL |
| 7442 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7443 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7444 | || spell_iswordp(word + i - 1))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7445 | && (*(s + 1) != '$' |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7446 | || (!spell_iswordp(word + i + k0)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7447 | || (*s == '$' && i > 0 |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7448 | && spell_iswordp(word + i - 1) |
| 7449 | && (!spell_iswordp(word + i + k0)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7450 | { |
| 7451 | /* search for followup rules, if: */ |
| 7452 | /* followup and k > 1 and NO '-' in searchstring */ |
| 7453 | c0 = word[i + k - 1]; |
| 7454 | n0 = slang->sl_sal_first[c0]; |
| 7455 | |
| 7456 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7457 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7458 | { |
| 7459 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7460 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7461 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7462 | /* Quickly skip entries that don't match the word. |
| 7463 | * */ |
| 7464 | k0 = smp[n0].sm_leadlen; |
| 7465 | if (k0 > 1) |
| 7466 | { |
| 7467 | if (word[i + k] != s[1]) |
| 7468 | continue; |
| 7469 | if (k0 > 2) |
| 7470 | { |
| 7471 | pf = word + i + k + 1; |
| 7472 | for (j = 2; j < k0; ++j) |
| 7473 | if (*pf++ != s[j]) |
| 7474 | break; |
| 7475 | if (j < k0) |
| 7476 | continue; |
| 7477 | } |
| 7478 | } |
| 7479 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7480 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7481 | if ((pf = smp[n0].sm_oneoff) != NULL) |
| 7482 | { |
| 7483 | /* Check for match with one of the chars in |
| 7484 | * "sm_oneoff". */ |
| 7485 | while (*pf != NUL && *pf != word[i + k0]) |
| 7486 | ++pf; |
| 7487 | if (*pf == NUL) |
| 7488 | continue; |
| 7489 | ++k0; |
| 7490 | } |
| 7491 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7492 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7493 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7494 | while (*s == '-') |
| 7495 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7496 | /* "k0" gets NOT reduced because |
| 7497 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7498 | s++; |
| 7499 | } |
| 7500 | if (*s == '<') |
| 7501 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7502 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7503 | { |
| 7504 | p0 = *s - '0'; |
| 7505 | s++; |
| 7506 | } |
| 7507 | |
| 7508 | if (*s == NUL |
| 7509 | /* *s == '^' cuts */ |
| 7510 | || (*s == '$' |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7511 | && !spell_iswordp(word + i + k0))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7512 | { |
| 7513 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7514 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7515 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7516 | |
| 7517 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7518 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7519 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7520 | /* rule fits; stop search */ |
| 7521 | break; |
| 7522 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7523 | } |
| 7524 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7525 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7526 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
| 7529 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7530 | s = smp[n].sm_to; |
| 7531 | pf = smp[n].sm_rules; |
| 7532 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7533 | if (p0 == 1 && z == 0) |
| 7534 | { |
| 7535 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7536 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 7537 | || res[reslen - 1] == *s)) |
| 7538 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7539 | z0 = 1; |
| 7540 | z = 1; |
| 7541 | k0 = 0; |
| 7542 | while (*s != NUL && word[i+k0] != NUL) |
| 7543 | { |
| 7544 | word[i + k0] = *s; |
| 7545 | k0++; |
| 7546 | s++; |
| 7547 | } |
| 7548 | if (k > k0) |
| 7549 | mch_memmove(word + i + k0, word + i + k, |
| 7550 | STRLEN(word + i + k) + 1); |
| 7551 | |
| 7552 | /* new "actual letter" */ |
| 7553 | c = word[i]; |
| 7554 | } |
| 7555 | else |
| 7556 | { |
| 7557 | /* no '<' rule used */ |
| 7558 | i += k - 1; |
| 7559 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7560 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7561 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7562 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7563 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7564 | res[reslen] = *s; |
| 7565 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7566 | } |
| 7567 | s++; |
| 7568 | } |
| 7569 | /* new "actual letter" */ |
| 7570 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7571 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7572 | { |
| 7573 | if (c != NUL) |
| 7574 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7575 | res[reslen] = c; |
| 7576 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7577 | } |
| 7578 | mch_memmove(word, word + i + 1, |
| 7579 | STRLEN(word + i + 1) + 1); |
| 7580 | i = 0; |
| 7581 | z0 = 1; |
| 7582 | } |
| 7583 | } |
| 7584 | break; |
| 7585 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7586 | } |
| 7587 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7588 | else if (vim_iswhite(c)) |
| 7589 | { |
| 7590 | c = ' '; |
| 7591 | k = 1; |
| 7592 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7593 | |
| 7594 | if (z0 == 0) |
| 7595 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7596 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 7597 | && (!slang->sl_collapse || reslen == 0 |
| 7598 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7599 | { |
| 7600 | /* condense only double letters */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7601 | res[reslen] = c; |
| 7602 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7603 | } |
| 7604 | |
| 7605 | i++; |
| 7606 | z = 0; |
| 7607 | k = 0; |
| 7608 | } |
| 7609 | } |
| 7610 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7611 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7612 | } |
| 7613 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7614 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7615 | * Compute a score for two sound-a-like words. |
| 7616 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 7617 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 7618 | * avoiding checks that will not be possible. |
| 7619 | */ |
| 7620 | static int |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7621 | soundalike_score(goodstart, badstart) |
| 7622 | char_u *goodstart; /* sound-folded good word */ |
| 7623 | char_u *badstart; /* sound-folded bad word */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7624 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7625 | char_u *goodsound = goodstart; |
| 7626 | char_u *badsound = badstart; |
| 7627 | int goodlen; |
| 7628 | int badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7629 | int n; |
| 7630 | char_u *pl, *ps; |
| 7631 | char_u *pl2, *ps2; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7632 | int score = 0; |
| 7633 | |
| 7634 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 7635 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 7636 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 7637 | { |
| 7638 | score = SCORE_DEL / 2; |
| 7639 | if (*badsound == '*') |
| 7640 | ++badsound; |
| 7641 | else |
| 7642 | ++goodsound; |
| 7643 | } |
| 7644 | |
| 7645 | goodlen = STRLEN(goodsound); |
| 7646 | badlen = STRLEN(badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7647 | |
| 7648 | /* Return quickly if the lenghts are too different to be fixed by two |
| 7649 | * changes. */ |
| 7650 | n = goodlen - badlen; |
| 7651 | if (n < -2 || n > 2) |
| 7652 | return SCORE_MAXMAX; |
| 7653 | |
| 7654 | if (n > 0) |
| 7655 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7656 | pl = goodsound; /* goodsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7657 | ps = badsound; |
| 7658 | } |
| 7659 | else |
| 7660 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7661 | pl = badsound; /* badsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7662 | ps = goodsound; |
| 7663 | } |
| 7664 | |
| 7665 | /* Skip over the identical part. */ |
| 7666 | while (*pl == *ps && *pl != NUL) |
| 7667 | { |
| 7668 | ++pl; |
| 7669 | ++ps; |
| 7670 | } |
| 7671 | |
| 7672 | switch (n) |
| 7673 | { |
| 7674 | case -2: |
| 7675 | case 2: |
| 7676 | /* |
| 7677 | * Must delete two characters from "pl". |
| 7678 | */ |
| 7679 | ++pl; /* first delete */ |
| 7680 | while (*pl == *ps) |
| 7681 | { |
| 7682 | ++pl; |
| 7683 | ++ps; |
| 7684 | } |
| 7685 | /* strings must be equal after second delete */ |
| 7686 | if (STRCMP(pl + 1, ps) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7687 | return score + SCORE_DEL * 2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7688 | |
| 7689 | /* Failed to compare. */ |
| 7690 | break; |
| 7691 | |
| 7692 | case -1: |
| 7693 | case 1: |
| 7694 | /* |
| 7695 | * Minimal one delete from "pl" required. |
| 7696 | */ |
| 7697 | |
| 7698 | /* 1: delete */ |
| 7699 | pl2 = pl + 1; |
| 7700 | ps2 = ps; |
| 7701 | while (*pl2 == *ps2) |
| 7702 | { |
| 7703 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7704 | return score + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7705 | ++pl2; |
| 7706 | ++ps2; |
| 7707 | } |
| 7708 | |
| 7709 | /* 2: delete then swap, then rest must be equal */ |
| 7710 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7711 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7712 | return score + SCORE_DEL + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7713 | |
| 7714 | /* 3: delete then substitute, then the rest must be equal */ |
| 7715 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7716 | return score + SCORE_DEL + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7717 | |
| 7718 | /* 4: first swap then delete */ |
| 7719 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 7720 | { |
| 7721 | pl2 = pl + 2; /* swap, skip two chars */ |
| 7722 | ps2 = ps + 2; |
| 7723 | while (*pl2 == *ps2) |
| 7724 | { |
| 7725 | ++pl2; |
| 7726 | ++ps2; |
| 7727 | } |
| 7728 | /* delete a char and then strings must be equal */ |
| 7729 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7730 | return score + SCORE_SWAP + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
| 7733 | /* 5: first substitute then delete */ |
| 7734 | pl2 = pl + 1; /* substitute, skip one char */ |
| 7735 | ps2 = ps + 1; |
| 7736 | while (*pl2 == *ps2) |
| 7737 | { |
| 7738 | ++pl2; |
| 7739 | ++ps2; |
| 7740 | } |
| 7741 | /* delete a char and then strings must be equal */ |
| 7742 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7743 | return score + SCORE_SUBST + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7744 | |
| 7745 | /* Failed to compare. */ |
| 7746 | break; |
| 7747 | |
| 7748 | case 0: |
| 7749 | /* |
| 7750 | * Lenghts are equal, thus changes must result in same length: An |
| 7751 | * insert is only possible in combination with a delete. |
| 7752 | * 1: check if for identical strings |
| 7753 | */ |
| 7754 | if (*pl == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7755 | return score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7756 | |
| 7757 | /* 2: swap */ |
| 7758 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 7759 | { |
| 7760 | pl2 = pl + 2; /* swap, skip two chars */ |
| 7761 | ps2 = ps + 2; |
| 7762 | while (*pl2 == *ps2) |
| 7763 | { |
| 7764 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7765 | return score + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7766 | ++pl2; |
| 7767 | ++ps2; |
| 7768 | } |
| 7769 | /* 3: swap and swap again */ |
| 7770 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7771 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7772 | return score + SCORE_SWAP + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7773 | |
| 7774 | /* 4: swap and substitute */ |
| 7775 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7776 | return score + SCORE_SWAP + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | /* 5: substitute */ |
| 7780 | pl2 = pl + 1; |
| 7781 | ps2 = ps + 1; |
| 7782 | while (*pl2 == *ps2) |
| 7783 | { |
| 7784 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7785 | return score + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7786 | ++pl2; |
| 7787 | ++ps2; |
| 7788 | } |
| 7789 | |
| 7790 | /* 6: substitute and swap */ |
| 7791 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7792 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7793 | return score + SCORE_SUBST + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7794 | |
| 7795 | /* 7: substitute and substitute */ |
| 7796 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7797 | return score + SCORE_SUBST + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7798 | |
| 7799 | /* 8: insert then delete */ |
| 7800 | pl2 = pl; |
| 7801 | ps2 = ps + 1; |
| 7802 | while (*pl2 == *ps2) |
| 7803 | { |
| 7804 | ++pl2; |
| 7805 | ++ps2; |
| 7806 | } |
| 7807 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7808 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7809 | |
| 7810 | /* 9: delete then insert */ |
| 7811 | pl2 = pl + 1; |
| 7812 | ps2 = ps; |
| 7813 | while (*pl2 == *ps2) |
| 7814 | { |
| 7815 | ++pl2; |
| 7816 | ++ps2; |
| 7817 | } |
| 7818 | if (STRCMP(pl2, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7819 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7820 | |
| 7821 | /* Failed to compare. */ |
| 7822 | break; |
| 7823 | } |
| 7824 | |
| 7825 | return SCORE_MAXMAX; |
| 7826 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7827 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7828 | /* |
| 7829 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7830 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7831 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7832 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7833 | * It has been converted from C++ to C and modified to support multi-byte |
| 7834 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7835 | */ |
| 7836 | static int |
| 7837 | spell_edit_score(badword, goodword) |
| 7838 | char_u *badword; |
| 7839 | char_u *goodword; |
| 7840 | { |
| 7841 | int *cnt; |
| 7842 | int badlen, goodlen; |
| 7843 | int j, i; |
| 7844 | int t; |
| 7845 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7846 | int pbc, pgc; |
| 7847 | #ifdef FEAT_MBYTE |
| 7848 | char_u *p; |
| 7849 | int wbadword[MAXWLEN]; |
| 7850 | int wgoodword[MAXWLEN]; |
| 7851 | |
| 7852 | if (has_mbyte) |
| 7853 | { |
| 7854 | /* Get the characters from the multi-byte strings and put them in an |
| 7855 | * int array for easy access. */ |
| 7856 | for (p = badword, badlen = 0; *p != NUL; ) |
| 7857 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
| 7858 | ++badlen; |
| 7859 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 7860 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
| 7861 | ++goodlen; |
| 7862 | } |
| 7863 | else |
| 7864 | #endif |
| 7865 | { |
| 7866 | badlen = STRLEN(badword) + 1; |
| 7867 | goodlen = STRLEN(goodword) + 1; |
| 7868 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7869 | |
| 7870 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 7871 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7872 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 7873 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7874 | if (cnt == NULL) |
| 7875 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7876 | |
| 7877 | CNT(0, 0) = 0; |
| 7878 | for (j = 1; j <= goodlen; ++j) |
| 7879 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 7880 | |
| 7881 | for (i = 1; i <= badlen; ++i) |
| 7882 | { |
| 7883 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 7884 | for (j = 1; j <= goodlen; ++j) |
| 7885 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7886 | #ifdef FEAT_MBYTE |
| 7887 | if (has_mbyte) |
| 7888 | { |
| 7889 | bc = wbadword[i - 1]; |
| 7890 | gc = wgoodword[j - 1]; |
| 7891 | } |
| 7892 | else |
| 7893 | #endif |
| 7894 | { |
| 7895 | bc = badword[i - 1]; |
| 7896 | gc = goodword[j - 1]; |
| 7897 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7898 | if (bc == gc) |
| 7899 | CNT(i, j) = CNT(i - 1, j - 1); |
| 7900 | else |
| 7901 | { |
| 7902 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7903 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7904 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 7905 | else |
| 7906 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 7907 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7908 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7909 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7910 | #ifdef FEAT_MBYTE |
| 7911 | if (has_mbyte) |
| 7912 | { |
| 7913 | pbc = wbadword[i - 2]; |
| 7914 | pgc = wgoodword[j - 2]; |
| 7915 | } |
| 7916 | else |
| 7917 | #endif |
| 7918 | { |
| 7919 | pbc = badword[i - 2]; |
| 7920 | pgc = goodword[j - 2]; |
| 7921 | } |
| 7922 | if (bc == pgc && pbc == gc) |
| 7923 | { |
| 7924 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 7925 | if (t < CNT(i, j)) |
| 7926 | CNT(i, j) = t; |
| 7927 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7928 | } |
| 7929 | t = SCORE_DEL + CNT(i - 1, j); |
| 7930 | if (t < CNT(i, j)) |
| 7931 | CNT(i, j) = t; |
| 7932 | t = SCORE_INS + CNT(i, j - 1); |
| 7933 | if (t < CNT(i, j)) |
| 7934 | CNT(i, j) = t; |
| 7935 | } |
| 7936 | } |
| 7937 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7938 | |
| 7939 | i = CNT(badlen - 1, goodlen - 1); |
| 7940 | vim_free(cnt); |
| 7941 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7942 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7943 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7944 | /* |
| 7945 | * ":spelldump" |
| 7946 | */ |
| 7947 | /*ARGSUSED*/ |
| 7948 | void |
| 7949 | ex_spelldump(eap) |
| 7950 | exarg_T *eap; |
| 7951 | { |
| 7952 | buf_T *buf = curbuf; |
| 7953 | langp_T *lp; |
| 7954 | slang_T *slang; |
| 7955 | idx_T arridx[MAXWLEN]; |
| 7956 | int curi[MAXWLEN]; |
| 7957 | char_u word[MAXWLEN]; |
| 7958 | int c; |
| 7959 | char_u *byts; |
| 7960 | idx_T *idxs; |
| 7961 | linenr_T lnum = 0; |
| 7962 | int round; |
| 7963 | int depth; |
| 7964 | int n; |
| 7965 | int flags; |
| 7966 | |
| 7967 | if (no_spell_checking()) |
| 7968 | return; |
| 7969 | |
| 7970 | /* Create a new empty buffer by splitting the window. */ |
| 7971 | do_cmdline_cmd((char_u *)"new"); |
| 7972 | if (!bufempty() || !buf_valid(buf)) |
| 7973 | return; |
| 7974 | |
| 7975 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 7976 | { |
| 7977 | slang = lp->lp_slang; |
| 7978 | |
| 7979 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 7980 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 7981 | |
| 7982 | /* round 1: case-folded tree |
| 7983 | * round 2: keep-case tree */ |
| 7984 | for (round = 1; round <= 2; ++round) |
| 7985 | { |
| 7986 | if (round == 1) |
| 7987 | { |
| 7988 | byts = slang->sl_fbyts; |
| 7989 | idxs = slang->sl_fidxs; |
| 7990 | } |
| 7991 | else |
| 7992 | { |
| 7993 | byts = slang->sl_kbyts; |
| 7994 | idxs = slang->sl_kidxs; |
| 7995 | } |
| 7996 | if (byts == NULL) |
| 7997 | continue; /* array is empty */ |
| 7998 | |
| 7999 | depth = 0; |
| 8000 | arridx[0] = 0; |
| 8001 | curi[0] = 1; |
| 8002 | while (depth >= 0 && !got_int) |
| 8003 | { |
| 8004 | if (curi[depth] > byts[arridx[depth]]) |
| 8005 | { |
| 8006 | /* Done all bytes at this node, go up one level. */ |
| 8007 | --depth; |
| 8008 | line_breakcheck(); |
| 8009 | } |
| 8010 | else |
| 8011 | { |
| 8012 | /* Do one more byte at this node. */ |
| 8013 | n = arridx[depth] + curi[depth]; |
| 8014 | ++curi[depth]; |
| 8015 | c = byts[n]; |
| 8016 | if (c == 0) |
| 8017 | { |
| 8018 | /* End of word, deal with the word. |
| 8019 | * Don't use keep-case words in the fold-case tree, |
| 8020 | * they will appear in the keep-case tree. |
| 8021 | * Only use the word when the region matches. */ |
| 8022 | flags = (int)idxs[n]; |
| 8023 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
| 8024 | && ((flags & WF_REGION) == 0 |
| 8025 | || (((unsigned)flags >> 8) |
| 8026 | & lp->lp_region) != 0)) |
| 8027 | { |
| 8028 | word[depth] = NUL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8029 | |
| 8030 | /* Dump the basic word if there is no prefix or |
| 8031 | * when it's the first one. */ |
| 8032 | c = (unsigned)flags >> 16; |
| 8033 | if (c == 0 || curi[depth] == 2) |
| 8034 | dump_word(word, round, flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8035 | |
| 8036 | /* Apply the prefix, if there is one. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8037 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8038 | lnum = apply_prefixes(slang, word, round, |
| 8039 | flags, lnum); |
| 8040 | } |
| 8041 | } |
| 8042 | else |
| 8043 | { |
| 8044 | /* Normal char, go one level deeper. */ |
| 8045 | word[depth++] = c; |
| 8046 | arridx[depth] = idxs[n]; |
| 8047 | curi[depth] = 1; |
| 8048 | } |
| 8049 | } |
| 8050 | } |
| 8051 | } |
| 8052 | } |
| 8053 | |
| 8054 | /* Delete the empty line that we started with. */ |
| 8055 | if (curbuf->b_ml.ml_line_count > 1) |
| 8056 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 8057 | |
| 8058 | redraw_later(NOT_VALID); |
| 8059 | } |
| 8060 | |
| 8061 | /* |
| 8062 | * Dump one word: apply case modifications and append a line to the buffer. |
| 8063 | */ |
| 8064 | static void |
| 8065 | dump_word(word, round, flags, lnum) |
| 8066 | char_u *word; |
| 8067 | int round; |
| 8068 | int flags; |
| 8069 | linenr_T lnum; |
| 8070 | { |
| 8071 | int keepcap = FALSE; |
| 8072 | char_u *p; |
| 8073 | char_u cword[MAXWLEN]; |
| 8074 | char_u badword[MAXWLEN + 3]; |
| 8075 | |
| 8076 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
| 8077 | { |
| 8078 | /* Need to fix case according to "flags". */ |
| 8079 | make_case_word(word, cword, flags); |
| 8080 | p = cword; |
| 8081 | } |
| 8082 | else |
| 8083 | { |
| 8084 | p = word; |
| 8085 | if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0) |
| 8086 | keepcap = TRUE; |
| 8087 | } |
| 8088 | |
| 8089 | /* Bad word is preceded by "/!" and some other |
| 8090 | * flags. */ |
| 8091 | if ((flags & (WF_BANNED | WF_RARE)) || keepcap) |
| 8092 | { |
| 8093 | STRCPY(badword, "/"); |
| 8094 | if (keepcap) |
| 8095 | STRCAT(badword, "="); |
| 8096 | if (flags & WF_BANNED) |
| 8097 | STRCAT(badword, "!"); |
| 8098 | else if (flags & WF_RARE) |
| 8099 | STRCAT(badword, "?"); |
| 8100 | STRCAT(badword, p); |
| 8101 | p = badword; |
| 8102 | } |
| 8103 | |
| 8104 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 8105 | } |
| 8106 | |
| 8107 | /* |
| 8108 | * Find matching prefixes for "word". Prepend each to "word" and append |
| 8109 | * a line to the buffer. |
| 8110 | * Return the updated line number. |
| 8111 | */ |
| 8112 | static linenr_T |
| 8113 | apply_prefixes(slang, word, round, flags, startlnum) |
| 8114 | slang_T *slang; |
| 8115 | char_u *word; /* case-folded word */ |
| 8116 | int round; |
| 8117 | int flags; /* flags with prefix ID */ |
| 8118 | linenr_T startlnum; |
| 8119 | { |
| 8120 | idx_T arridx[MAXWLEN]; |
| 8121 | int curi[MAXWLEN]; |
| 8122 | char_u prefix[MAXWLEN]; |
| 8123 | int c; |
| 8124 | char_u *byts; |
| 8125 | idx_T *idxs; |
| 8126 | linenr_T lnum = startlnum; |
| 8127 | int depth; |
| 8128 | int n; |
| 8129 | int len; |
| 8130 | int prefid = (unsigned)flags >> 16; |
| 8131 | int i; |
| 8132 | |
| 8133 | byts = slang->sl_pbyts; |
| 8134 | idxs = slang->sl_pidxs; |
| 8135 | if (byts != NULL) /* array not is empty */ |
| 8136 | { |
| 8137 | /* |
| 8138 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
| 8139 | * When at the end of a prefix check that it supports "prefid". |
| 8140 | */ |
| 8141 | depth = 0; |
| 8142 | arridx[0] = 0; |
| 8143 | curi[0] = 1; |
| 8144 | while (depth >= 0 && !got_int) |
| 8145 | { |
| 8146 | len = arridx[depth]; |
| 8147 | if (curi[depth] > byts[len]) |
| 8148 | { |
| 8149 | /* Done all bytes at this node, go up one level. */ |
| 8150 | --depth; |
| 8151 | line_breakcheck(); |
| 8152 | } |
| 8153 | else |
| 8154 | { |
| 8155 | /* Do one more byte at this node. */ |
| 8156 | n = len + curi[depth]; |
| 8157 | ++curi[depth]; |
| 8158 | c = byts[n]; |
| 8159 | if (c == 0) |
| 8160 | { |
| 8161 | /* End of prefix, find out how many IDs there are. */ |
| 8162 | for (i = 1; i < len; ++i) |
| 8163 | if (byts[n + i] != 0) |
| 8164 | break; |
| 8165 | curi[depth] += i - 1; |
| 8166 | |
| 8167 | if (valid_word_prefix(i, n, prefid, word, slang)) |
| 8168 | { |
| 8169 | vim_strncpy(prefix + depth, word, MAXWLEN - depth); |
| 8170 | dump_word(prefix, round, flags, lnum++); |
| 8171 | } |
| 8172 | } |
| 8173 | else |
| 8174 | { |
| 8175 | /* Normal char, go one level deeper. */ |
| 8176 | prefix[depth++] = c; |
| 8177 | arridx[depth] = idxs[n]; |
| 8178 | curi[depth] = 1; |
| 8179 | } |
| 8180 | } |
| 8181 | } |
| 8182 | } |
| 8183 | |
| 8184 | return lnum; |
| 8185 | } |
| 8186 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8187 | #endif /* FEAT_SYN_HL */ |