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 */ |
| 399 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 400 | |
| 401 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 402 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 403 | |
| 404 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 405 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 406 | |
| 407 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 408 | * Structure to store info for word matching. |
| 409 | */ |
| 410 | typedef struct matchinf_S |
| 411 | { |
| 412 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 413 | |
| 414 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 415 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 416 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 417 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 418 | char_u *mi_cend; /* char after what was used for |
| 419 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 420 | |
| 421 | /* case-folded text */ |
| 422 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 423 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 424 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 425 | /* for when checking word after a prefix */ |
| 426 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 427 | prefixID/condition */ |
| 428 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 429 | int mi_prefixlen; /* byte length of prefix */ |
| 430 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 431 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 432 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 433 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 434 | } matchinf_T; |
| 435 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 436 | /* |
| 437 | * The tables used for recognizing word characters according to spelling. |
| 438 | * These are only used for the first 256 characters of 'encoding'. |
| 439 | */ |
| 440 | typedef struct spelltab_S |
| 441 | { |
| 442 | char_u st_isw[256]; /* flags: is word char */ |
| 443 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 444 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 445 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 446 | } spelltab_T; |
| 447 | |
| 448 | static spelltab_T spelltab; |
| 449 | static int did_set_spelltab; |
| 450 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 451 | #define CF_WORD 0x01 |
| 452 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 453 | |
| 454 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 455 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 456 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * Return TRUE if "p" points to a word character or "c" is a word character |
| 460 | * for spelling. |
| 461 | * Checking for a word character is done very often, avoid the function call |
| 462 | * overhead. |
| 463 | */ |
| 464 | #ifdef FEAT_MBYTE |
| 465 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 466 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 467 | #else |
| 468 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 469 | #endif |
| 470 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 471 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 472 | * 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] | 473 | */ |
| 474 | typedef enum |
| 475 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 476 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 477 | * ends); if badword ends there is a match, otherwise |
| 478 | * try splitting word. */ |
| 479 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 480 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 481 | STATE_PLAIN, /* Use each byte of the node. */ |
| 482 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 483 | STATE_INS, /* Insert a byte in the bad word. */ |
| 484 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 485 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 486 | STATE_SWAP3, /* Swap two characters over three. */ |
| 487 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 488 | STATE_UNROT3L, /* Undo rotate three characters left */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 489 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 490 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 491 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 492 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 493 | STATE_FINAL /* End of this node. */ |
| 494 | } state_T; |
| 495 | |
| 496 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 497 | * Struct to keep the state at each level in suggest_try_change(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 498 | */ |
| 499 | typedef struct trystate_S |
| 500 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 501 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 502 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 503 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 504 | short ts_curi; /* index in list of child nodes */ |
| 505 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 506 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 507 | char_u ts_twordlen; /* valid length of tword[] */ |
| 508 | #ifdef FEAT_MBYTE |
| 509 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 510 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 511 | char_u ts_isdiff; /* DIFF_ values */ |
| 512 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 513 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 514 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 515 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 516 | char_u ts_save_badflags; /* su_badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 517 | } trystate_T; |
| 518 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 519 | /* values for ts_isdiff */ |
| 520 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 521 | #define DIFF_YES 1 /* different byte found */ |
| 522 | #define DIFF_INSERT 2 /* inserting character */ |
| 523 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 524 | /* mode values for find_word */ |
| 525 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 526 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 527 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 528 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 529 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 530 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 531 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 532 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 533 | 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] | 534 | static void find_prefix __ARGS((matchinf_T *mip)); |
| 535 | static int fold_more __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 536 | static int spell_valid_case __ARGS((int origflags, int treeflags)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 537 | static int no_spell_checking __ARGS((void)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 538 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 539 | static char_u *spell_enc __ARGS((void)); |
| 540 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 541 | 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] | 542 | 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] | 543 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 544 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 545 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 546 | 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] | 547 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 548 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 549 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 550 | static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount)); |
| 551 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 552 | 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] | 553 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 554 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 555 | static void suggest_try_change __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 556 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
| 557 | 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] | 558 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 559 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 560 | 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] | 561 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 562 | 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] | 563 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 564 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 565 | 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] | 566 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 567 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 568 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 569 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 570 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 571 | 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] | 572 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 573 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 574 | static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum)); |
| 575 | 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] | 576 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 577 | /* |
| 578 | * Use our own character-case definitions, because the current locale may |
| 579 | * differ from what the .spl file uses. |
| 580 | * These must not be called with negative number! |
| 581 | */ |
| 582 | #ifndef FEAT_MBYTE |
| 583 | /* Non-multi-byte implementation. */ |
| 584 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 585 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 586 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 587 | #else |
| 588 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 589 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 590 | * the "w" library function for characters above 255 if available. */ |
| 591 | # ifdef HAVE_TOWLOWER |
| 592 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 593 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 594 | # else |
| 595 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 596 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 597 | # endif |
| 598 | |
| 599 | # ifdef HAVE_TOWUPPER |
| 600 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 601 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 602 | # else |
| 603 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 604 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 605 | # endif |
| 606 | |
| 607 | # ifdef HAVE_ISWUPPER |
| 608 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 609 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 610 | # else |
| 611 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 612 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 613 | # endif |
| 614 | #endif |
| 615 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 616 | |
| 617 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 618 | |
| 619 | /* |
| 620 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 621 | * "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] | 622 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 623 | * or when it's OK it remains unchanged. |
| 624 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 625 | * |
| 626 | * "sug" is normally NULL. When looking for suggestions it points to |
| 627 | * suginfo_T. It's passed as a void pointer to keep the struct local. |
| 628 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 629 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 630 | * caller can skip over the word. |
| 631 | */ |
| 632 | int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 633 | spell_check(wp, ptr, attrp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 634 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 635 | char_u *ptr; |
| 636 | int *attrp; |
| 637 | { |
| 638 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 639 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 640 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 641 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 642 | /* A word never starts at a space or a control character. Return quickly |
| 643 | * then, skipping over the character. */ |
| 644 | if (*ptr <= ' ') |
| 645 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 646 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 647 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 648 | * 0X99FF. But when a word character follows do check spelling to find |
| 649 | * "3GPP". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 650 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 651 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 652 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 653 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 654 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 655 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 656 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 657 | nrlen = mi.mi_end - ptr; |
| 658 | } |
| 659 | if (!SPELL_ISWORDP(mi.mi_end)) |
| 660 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 661 | |
| 662 | /* Try including the digits in the word. */ |
| 663 | mi.mi_fend = ptr + nrlen; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 664 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 665 | else |
| 666 | mi.mi_fend = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 667 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 668 | /* 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] | 669 | mi.mi_word = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 670 | if (SPELL_ISWORDP(mi.mi_fend)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 671 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 672 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 673 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 674 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 675 | } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend)); |
| 676 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 677 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 678 | /* We always use the characters up to the next non-word character, |
| 679 | * also for bad words. */ |
| 680 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 681 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 682 | /* Check caps type later. */ |
| 683 | mi.mi_capflags = 0; |
| 684 | mi.mi_cend = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 685 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 686 | /* Include one non-word character so that we can check for the |
| 687 | * word end. */ |
| 688 | if (*mi.mi_fend != NUL) |
| 689 | mb_ptr_adv(mi.mi_fend); |
| 690 | |
| 691 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 692 | MAXWLEN + 1); |
| 693 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 694 | |
| 695 | /* The word is bad unless we recognize it. */ |
| 696 | mi.mi_result = SP_BAD; |
| 697 | |
| 698 | /* |
| 699 | * Loop over the languages specified in 'spelllang'. |
| 700 | * We check them all, because a matching word may be longer than an |
| 701 | * already found matching word. |
| 702 | */ |
| 703 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 704 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 705 | { |
| 706 | /* Check for a matching word in case-folded words. */ |
| 707 | find_word(&mi, FIND_FOLDWORD); |
| 708 | |
| 709 | /* Check for a matching word in keep-case words. */ |
| 710 | find_word(&mi, FIND_KEEPWORD); |
| 711 | |
| 712 | /* Check for matching prefixes. */ |
| 713 | find_prefix(&mi); |
| 714 | } |
| 715 | |
| 716 | if (mi.mi_result != SP_OK) |
| 717 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 718 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 719 | * rare and local words, e.g., "3GPP". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 720 | if (nrlen > 0) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 721 | { |
| 722 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 723 | return nrlen; |
| 724 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 725 | |
| 726 | /* When we are at a non-word character there is no error, just |
| 727 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 728 | else if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 729 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 730 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 731 | if (has_mbyte) |
| 732 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 733 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 734 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 735 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 736 | |
| 737 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 738 | *attrp = highlight_attr[HLF_SPB]; |
| 739 | else if (mi.mi_result == SP_RARE) |
| 740 | *attrp = highlight_attr[HLF_SPR]; |
| 741 | else |
| 742 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 745 | return (int)(mi.mi_end - ptr); |
| 746 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 747 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 748 | /* |
| 749 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 750 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 751 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 752 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 753 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 754 | * |
| 755 | * For a match mip->mi_result is updated. |
| 756 | */ |
| 757 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 758 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 759 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 760 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 761 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 762 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 763 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 764 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 765 | int endidxcnt = 0; |
| 766 | int len; |
| 767 | int wlen = 0; |
| 768 | int flen; |
| 769 | int c; |
| 770 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 771 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 772 | #ifdef FEAT_MBYTE |
| 773 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 774 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 775 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 776 | int res = SP_BAD; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 777 | slang_T *slang = mip->mi_lp->lp_slang; |
| 778 | unsigned flags; |
| 779 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 780 | idx_T *idxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 781 | int prefid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 782 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 783 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 784 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 785 | /* Check for word with matching case in keep-case tree. */ |
| 786 | ptr = mip->mi_word; |
| 787 | flen = 9999; /* no case folding, always enough bytes */ |
| 788 | byts = slang->sl_kbyts; |
| 789 | idxs = slang->sl_kidxs; |
| 790 | } |
| 791 | else |
| 792 | { |
| 793 | /* Check for case-folded in case-folded tree. */ |
| 794 | ptr = mip->mi_fword; |
| 795 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 796 | byts = slang->sl_fbyts; |
| 797 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 798 | |
| 799 | if (mode == FIND_PREFIX) |
| 800 | { |
| 801 | /* Skip over the prefix. */ |
| 802 | wlen = mip->mi_prefixlen; |
| 803 | flen -= mip->mi_prefixlen; |
| 804 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 807 | if (byts == NULL) |
| 808 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 809 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 810 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 811 | * Repeat advancing in the tree until: |
| 812 | * - there is a byte that doesn't match, |
| 813 | * - we reach the end of the tree, |
| 814 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 815 | */ |
| 816 | for (;;) |
| 817 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 818 | if (flen <= 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 819 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 820 | |
| 821 | len = byts[arridx++]; |
| 822 | |
| 823 | /* If the first possible byte is a zero the word could end here. |
| 824 | * Remember this index, we first check for the longest word. */ |
| 825 | if (byts[arridx] == 0) |
| 826 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 827 | if (endidxcnt == MAXWLEN) |
| 828 | { |
| 829 | /* Must be a corrupted spell file. */ |
| 830 | EMSG(_(e_format)); |
| 831 | return; |
| 832 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 833 | endlen[endidxcnt] = wlen; |
| 834 | endidx[endidxcnt++] = arridx++; |
| 835 | --len; |
| 836 | |
| 837 | /* Skip over the zeros, there can be several flag/region |
| 838 | * combinations. */ |
| 839 | while (len > 0 && byts[arridx] == 0) |
| 840 | { |
| 841 | ++arridx; |
| 842 | --len; |
| 843 | } |
| 844 | if (len == 0) |
| 845 | break; /* no children, word must end here */ |
| 846 | } |
| 847 | |
| 848 | /* Stop looking at end of the line. */ |
| 849 | if (ptr[wlen] == NUL) |
| 850 | break; |
| 851 | |
| 852 | /* Perform a binary search in the list of accepted bytes. */ |
| 853 | c = ptr[wlen]; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 854 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 855 | c = ' '; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 856 | lo = arridx; |
| 857 | hi = arridx + len - 1; |
| 858 | while (lo < hi) |
| 859 | { |
| 860 | m = (lo + hi) / 2; |
| 861 | if (byts[m] > c) |
| 862 | hi = m - 1; |
| 863 | else if (byts[m] < c) |
| 864 | lo = m + 1; |
| 865 | else |
| 866 | { |
| 867 | lo = hi = m; |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* Stop if there is no matching byte. */ |
| 873 | if (hi < lo || byts[lo] != c) |
| 874 | break; |
| 875 | |
| 876 | /* Continue at the child (if there is one). */ |
| 877 | arridx = idxs[lo]; |
| 878 | ++wlen; |
| 879 | --flen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 880 | |
| 881 | /* One space in the good word may stand for several spaces in the |
| 882 | * checked word. */ |
| 883 | if (c == ' ') |
| 884 | { |
| 885 | for (;;) |
| 886 | { |
| 887 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 888 | flen = fold_more(mip); |
| 889 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 890 | break; |
| 891 | ++wlen; |
| 892 | --flen; |
| 893 | } |
| 894 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Verify that one of the possible endings is valid. Try the longest |
| 899 | * first. |
| 900 | */ |
| 901 | while (endidxcnt > 0) |
| 902 | { |
| 903 | --endidxcnt; |
| 904 | arridx = endidx[endidxcnt]; |
| 905 | wlen = endlen[endidxcnt]; |
| 906 | |
| 907 | #ifdef FEAT_MBYTE |
| 908 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 909 | continue; /* not at first byte of character */ |
| 910 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 911 | if (SPELL_ISWORDP(ptr + wlen)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 912 | continue; /* next char is a word character */ |
| 913 | |
| 914 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 915 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 916 | { |
| 917 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 918 | * when folding case. This can be slow, take a shortcut when the |
| 919 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 920 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 921 | if (STRNCMP(ptr, p, wlen) != 0) |
| 922 | { |
| 923 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 924 | mb_ptr_adv(p); |
| 925 | wlen = p - mip->mi_word; |
| 926 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 927 | } |
| 928 | #endif |
| 929 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 930 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 931 | * prefix ID. |
| 932 | * Repeat this if there are more flags/region alternatives until there |
| 933 | * is a match. */ |
| 934 | res = SP_BAD; |
| 935 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 936 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 937 | { |
| 938 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 939 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 940 | /* For the fold-case tree check that the case of the checked word |
| 941 | * matches with what the word in the tree requires. |
| 942 | * For keep-case tree the case is always right. For prefixes we |
| 943 | * don't bother to check. */ |
| 944 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 945 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 946 | if (mip->mi_cend != mip->mi_word + wlen) |
| 947 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 948 | /* mi_capflags was set for a different word length, need |
| 949 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 950 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 951 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 954 | if (mip->mi_capflags == WF_KEEPCAP |
| 955 | || !spell_valid_case(mip->mi_capflags, flags)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 956 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 959 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 960 | * check the prefix ID and the condition. Do that for the list at |
| 961 | * mip->mi_prefarridx. */ |
| 962 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 963 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 964 | /* The prefix ID is stored two bytes above the flags. */ |
| 965 | prefid = (unsigned)flags >> 16; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 966 | if (!valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
| 967 | prefid, mip->mi_fword + mip->mi_prefixlen, |
| 968 | slang)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 969 | continue; |
| 970 | } |
| 971 | |
| 972 | if (flags & WF_BANNED) |
| 973 | res = SP_BANNED; |
| 974 | else if (flags & WF_REGION) |
| 975 | { |
| 976 | /* Check region. */ |
| 977 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 978 | res = SP_OK; |
| 979 | else |
| 980 | res = SP_LOCAL; |
| 981 | } |
| 982 | else if (flags & WF_RARE) |
| 983 | res = SP_RARE; |
| 984 | else |
| 985 | res = SP_OK; |
| 986 | |
| 987 | /* Always use the longest match and the best result. */ |
| 988 | if (mip->mi_result > res) |
| 989 | { |
| 990 | mip->mi_result = res; |
| 991 | mip->mi_end = mip->mi_word + wlen; |
| 992 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 993 | 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] | 994 | mip->mi_end = mip->mi_word + wlen; |
| 995 | |
| 996 | if (res == SP_OK) |
| 997 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1000 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1001 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1002 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1005 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1006 | * Return TRUE if the prefix indicated by "mip->mi_prefarridx" matches with |
| 1007 | * the prefix ID "prefid" for the word "word". |
| 1008 | */ |
| 1009 | static int |
| 1010 | valid_word_prefix(totprefcnt, arridx, prefid, word, slang) |
| 1011 | int totprefcnt; /* nr of prefix IDs */ |
| 1012 | int arridx; /* idx in sl_pidxs[] */ |
| 1013 | int prefid; |
| 1014 | char_u *word; |
| 1015 | slang_T *slang; |
| 1016 | { |
| 1017 | int prefcnt; |
| 1018 | int pidx; |
| 1019 | regprog_T *rp; |
| 1020 | regmatch_T regmatch; |
| 1021 | |
| 1022 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1023 | { |
| 1024 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1025 | |
| 1026 | /* Check the prefix ID. */ |
| 1027 | if (prefid != (pidx & 0xff)) |
| 1028 | continue; |
| 1029 | |
| 1030 | /* Check the condition, if there is one. The condition index is |
| 1031 | * stored above the prefix ID byte. */ |
| 1032 | rp = slang->sl_prefprog[(unsigned)pidx >> 8]; |
| 1033 | if (rp != NULL) |
| 1034 | { |
| 1035 | regmatch.regprog = rp; |
| 1036 | regmatch.rm_ic = FALSE; |
| 1037 | if (!vim_regexec(®match, word, 0)) |
| 1038 | continue; |
| 1039 | } |
| 1040 | |
| 1041 | /* It's a match! */ |
| 1042 | return TRUE; |
| 1043 | } |
| 1044 | return FALSE; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1048 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1049 | * If it does, then check the following word. |
| 1050 | * |
| 1051 | * For a match mip->mi_result is updated. |
| 1052 | */ |
| 1053 | static void |
| 1054 | find_prefix(mip) |
| 1055 | matchinf_T *mip; |
| 1056 | { |
| 1057 | idx_T arridx = 0; |
| 1058 | int len; |
| 1059 | int wlen = 0; |
| 1060 | int flen; |
| 1061 | int c; |
| 1062 | char_u *ptr; |
| 1063 | idx_T lo, hi, m; |
| 1064 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1065 | char_u *byts; |
| 1066 | idx_T *idxs; |
| 1067 | |
| 1068 | /* We use the case-folded word here, since prefixes are always |
| 1069 | * case-folded. */ |
| 1070 | ptr = mip->mi_fword; |
| 1071 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 1072 | byts = slang->sl_pbyts; |
| 1073 | idxs = slang->sl_pidxs; |
| 1074 | |
| 1075 | if (byts == NULL) |
| 1076 | return; /* array is empty */ |
| 1077 | |
| 1078 | /* |
| 1079 | * Repeat advancing in the tree until: |
| 1080 | * - there is a byte that doesn't match, |
| 1081 | * - we reach the end of the tree, |
| 1082 | * - or we reach the end of the line. |
| 1083 | */ |
| 1084 | for (;;) |
| 1085 | { |
| 1086 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1087 | flen = fold_more(mip); |
| 1088 | |
| 1089 | len = byts[arridx++]; |
| 1090 | |
| 1091 | /* If the first possible byte is a zero the prefix could end here. |
| 1092 | * Check if the following word matches and supports the prefix. */ |
| 1093 | if (byts[arridx] == 0) |
| 1094 | { |
| 1095 | /* There can be several prefixes with different conditions. We |
| 1096 | * try them all, since we don't know which one will give the |
| 1097 | * longest match. The word is the same each time, pass the list |
| 1098 | * of possible prefixes to find_word(). */ |
| 1099 | mip->mi_prefarridx = arridx; |
| 1100 | mip->mi_prefcnt = len; |
| 1101 | while (len > 0 && byts[arridx] == 0) |
| 1102 | { |
| 1103 | ++arridx; |
| 1104 | --len; |
| 1105 | } |
| 1106 | mip->mi_prefcnt -= len; |
| 1107 | |
| 1108 | /* Find the word that comes after the prefix. */ |
| 1109 | mip->mi_prefixlen = wlen; |
| 1110 | find_word(mip, FIND_PREFIX); |
| 1111 | |
| 1112 | |
| 1113 | if (len == 0) |
| 1114 | break; /* no children, word must end here */ |
| 1115 | } |
| 1116 | |
| 1117 | /* Stop looking at end of the line. */ |
| 1118 | if (ptr[wlen] == NUL) |
| 1119 | break; |
| 1120 | |
| 1121 | /* Perform a binary search in the list of accepted bytes. */ |
| 1122 | c = ptr[wlen]; |
| 1123 | lo = arridx; |
| 1124 | hi = arridx + len - 1; |
| 1125 | while (lo < hi) |
| 1126 | { |
| 1127 | m = (lo + hi) / 2; |
| 1128 | if (byts[m] > c) |
| 1129 | hi = m - 1; |
| 1130 | else if (byts[m] < c) |
| 1131 | lo = m + 1; |
| 1132 | else |
| 1133 | { |
| 1134 | lo = hi = m; |
| 1135 | break; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* Stop if there is no matching byte. */ |
| 1140 | if (hi < lo || byts[lo] != c) |
| 1141 | break; |
| 1142 | |
| 1143 | /* Continue at the child (if there is one). */ |
| 1144 | arridx = idxs[lo]; |
| 1145 | ++wlen; |
| 1146 | --flen; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Need to fold at least one more character. Do until next non-word character |
| 1152 | * for efficiency. |
| 1153 | * Return the length of the folded chars in bytes. |
| 1154 | */ |
| 1155 | static int |
| 1156 | fold_more(mip) |
| 1157 | matchinf_T *mip; |
| 1158 | { |
| 1159 | int flen; |
| 1160 | char_u *p; |
| 1161 | |
| 1162 | p = mip->mi_fend; |
| 1163 | do |
| 1164 | { |
| 1165 | mb_ptr_adv(mip->mi_fend); |
| 1166 | } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend)); |
| 1167 | |
| 1168 | /* Include the non-word character so that we can check for the |
| 1169 | * word end. */ |
| 1170 | if (*mip->mi_fend != NUL) |
| 1171 | mb_ptr_adv(mip->mi_fend); |
| 1172 | |
| 1173 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1174 | mip->mi_fword + mip->mi_fwordlen, |
| 1175 | MAXWLEN - mip->mi_fwordlen); |
| 1176 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1177 | mip->mi_fwordlen += flen; |
| 1178 | return flen; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1182 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1183 | * case. |
| 1184 | */ |
| 1185 | static int |
| 1186 | spell_valid_case(origflags, treeflags) |
| 1187 | int origflags; /* flags for the checked word. */ |
| 1188 | int treeflags; /* flags for the word in the spell tree */ |
| 1189 | { |
| 1190 | return (origflags == WF_ALLCAP |
| 1191 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
| 1192 | && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP))); |
| 1193 | } |
| 1194 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1195 | /* |
| 1196 | * Return TRUE if spell checking is not enabled. |
| 1197 | */ |
| 1198 | static int |
| 1199 | no_spell_checking() |
| 1200 | { |
| 1201 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 1202 | { |
| 1203 | EMSG(_("E756: Spell checking is not enabled")); |
| 1204 | return TRUE; |
| 1205 | } |
| 1206 | return FALSE; |
| 1207 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1208 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1209 | /* |
| 1210 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1211 | * "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] | 1212 | * Return OK if found, FAIL otherwise. |
| 1213 | */ |
| 1214 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1215 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1216 | int dir; /* FORWARD or BACKWARD */ |
| 1217 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1218 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1219 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1220 | linenr_T lnum; |
| 1221 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1222 | char_u *line; |
| 1223 | char_u *p; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1224 | char_u *endp; |
| 1225 | int attr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1226 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1227 | int has_syntax = syntax_present(curbuf); |
| 1228 | int col; |
| 1229 | int can_spell; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1230 | char_u *buf = NULL; |
| 1231 | int buflen = 0; |
| 1232 | int skip = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1233 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1234 | if (no_spell_checking()) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1235 | return FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1236 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1237 | /* |
| 1238 | * 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] | 1239 | * 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] | 1240 | * |
| 1241 | * When searching backwards, we continue in the line to find the last |
| 1242 | * bad word (in the cursor line: before the cursor). |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1243 | * |
| 1244 | * We concatenate the start of the next line, so that wrapped words work |
| 1245 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 1246 | * though... |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1247 | */ |
| 1248 | lnum = curwin->w_cursor.lnum; |
| 1249 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1250 | |
| 1251 | while (!got_int) |
| 1252 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1253 | line = ml_get(lnum); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1254 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1255 | len = STRLEN(line); |
| 1256 | if (buflen < len + MAXWLEN + 2) |
| 1257 | { |
| 1258 | vim_free(buf); |
| 1259 | buflen = len + MAXWLEN + 2; |
| 1260 | buf = alloc(buflen); |
| 1261 | if (buf == NULL) |
| 1262 | break; |
| 1263 | } |
| 1264 | |
| 1265 | /* Copy the line into "buf" and append the start of the next line if |
| 1266 | * possible. */ |
| 1267 | STRCPY(buf, line); |
| 1268 | if (lnum < curbuf->b_ml.ml_line_count) |
| 1269 | spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN); |
| 1270 | |
| 1271 | p = buf + skip; |
| 1272 | endp = buf + len; |
| 1273 | while (p < endp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1274 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1275 | /* When searching backward don't search after the cursor. */ |
| 1276 | if (dir == BACKWARD |
| 1277 | && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1278 | && (colnr_T)(p - buf) >= curwin->w_cursor.col) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | |
| 1281 | /* start of word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1282 | attr = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1283 | len = spell_check(curwin, p, &attr); |
| 1284 | |
| 1285 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1286 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1287 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1288 | if (allwords || attr == highlight_attr[HLF_SPB]) |
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 | /* When searching forward only accept a bad word after |
| 1291 | * the cursor. */ |
| 1292 | if (dir == BACKWARD |
| 1293 | || lnum > curwin->w_cursor.lnum |
| 1294 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1295 | && (colnr_T)(curline ? p - buf + len |
| 1296 | : p - buf) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1297 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1298 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1299 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1300 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1301 | col = p - buf; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1302 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1303 | FALSE, &can_spell); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1304 | } |
| 1305 | else |
| 1306 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1307 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1308 | if (can_spell) |
| 1309 | { |
| 1310 | found_pos.lnum = lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1311 | found_pos.col = p - buf; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1312 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1313 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1314 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1315 | if (dir == FORWARD) |
| 1316 | { |
| 1317 | /* No need to search further. */ |
| 1318 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1319 | vim_free(buf); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1320 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1323 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1324 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1327 | /* advance to character after the word */ |
| 1328 | p += len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1331 | if (curline) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1332 | break; /* only check cursor line */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1333 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1334 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1335 | if (dir == BACKWARD) |
| 1336 | { |
| 1337 | if (found_pos.lnum != 0) |
| 1338 | { |
| 1339 | /* Use the last match in the line. */ |
| 1340 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1341 | vim_free(buf); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1342 | return OK; |
| 1343 | } |
| 1344 | if (lnum == 1) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1345 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1346 | --lnum; |
| 1347 | } |
| 1348 | else |
| 1349 | { |
| 1350 | if (lnum == curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1351 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1352 | ++lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1353 | |
| 1354 | /* Skip the characters at the start of the next line that were |
| 1355 | * included in a match crossing line boundaries. */ |
| 1356 | if (attr == 0) |
| 1357 | skip = p - endp; |
| 1358 | else |
| 1359 | skip = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1360 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1361 | |
| 1362 | line_breakcheck(); |
| 1363 | } |
| 1364 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1365 | vim_free(buf); |
| 1366 | return FAIL; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * For spell checking: concatenate the start of the following line "line" into |
| 1371 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| 1372 | */ |
| 1373 | void |
| 1374 | spell_cat_line(buf, line, maxlen) |
| 1375 | char_u *buf; |
| 1376 | char_u *line; |
| 1377 | int maxlen; |
| 1378 | { |
| 1379 | char_u *p; |
| 1380 | int n; |
| 1381 | |
| 1382 | p = skipwhite(line); |
| 1383 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 1384 | p = skipwhite(p + 1); |
| 1385 | |
| 1386 | if (*p != NUL) |
| 1387 | { |
| 1388 | *buf = ' '; |
| 1389 | vim_strncpy(buf + 1, line, maxlen - 1); |
| 1390 | n = p - line; |
| 1391 | if (n >= maxlen) |
| 1392 | n = maxlen - 1; |
| 1393 | vim_memset(buf + 1, ' ', n); |
| 1394 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1398 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1399 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1400 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1401 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1402 | spell_load_lang(lang) |
| 1403 | char_u *lang; |
| 1404 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1405 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1406 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1407 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1408 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1409 | /* 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] | 1410 | * It's truncated when an error is detected. */ |
| 1411 | STRCPY(langcp, lang); |
| 1412 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1413 | /* |
| 1414 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1415 | */ |
| 1416 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1417 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1418 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1419 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1420 | if (r == FAIL && *langcp != NUL) |
| 1421 | { |
| 1422 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1423 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1424 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1425 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1428 | if (r == FAIL) |
| 1429 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1430 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1431 | else if (*langcp != NUL) |
| 1432 | { |
| 1433 | /* Load all the additions. */ |
| 1434 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1435 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1441 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1442 | */ |
| 1443 | static char_u * |
| 1444 | spell_enc() |
| 1445 | { |
| 1446 | |
| 1447 | #ifdef FEAT_MBYTE |
| 1448 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1449 | return p_enc; |
| 1450 | #endif |
| 1451 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /* |
| 1455 | * Allocate a new slang_T. |
| 1456 | * Caller must fill "sl_next". |
| 1457 | */ |
| 1458 | static slang_T * |
| 1459 | slang_alloc(lang) |
| 1460 | char_u *lang; |
| 1461 | { |
| 1462 | slang_T *lp; |
| 1463 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1464 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1465 | if (lp != NULL) |
| 1466 | { |
| 1467 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1468 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1469 | ga_init2(&lp->sl_sal, sizeof(salitem_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1470 | } |
| 1471 | return lp; |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Free the contents of an slang_T and the structure itself. |
| 1476 | */ |
| 1477 | static void |
| 1478 | slang_free(lp) |
| 1479 | slang_T *lp; |
| 1480 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1481 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1482 | vim_free(lp->sl_fname); |
| 1483 | slang_clear(lp); |
| 1484 | vim_free(lp); |
| 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * Clear an slang_T so that the file can be reloaded. |
| 1489 | */ |
| 1490 | static void |
| 1491 | slang_clear(lp) |
| 1492 | slang_T *lp; |
| 1493 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1494 | garray_T *gap; |
| 1495 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1496 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1497 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1498 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1499 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1500 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1501 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1502 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1503 | vim_free(lp->sl_pbyts); |
| 1504 | lp->sl_pbyts = NULL; |
| 1505 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1506 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1507 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1508 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1509 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1510 | vim_free(lp->sl_pidxs); |
| 1511 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1512 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1513 | gap = &lp->sl_rep; |
| 1514 | while (gap->ga_len > 0) |
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 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1517 | vim_free(ftp->ft_from); |
| 1518 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1519 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1520 | ga_clear(gap); |
| 1521 | |
| 1522 | gap = &lp->sl_sal; |
| 1523 | while (gap->ga_len > 0) |
| 1524 | { |
| 1525 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1526 | vim_free(smp->sm_lead); |
| 1527 | vim_free(smp->sm_to); |
| 1528 | } |
| 1529 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1530 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1531 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1532 | vim_free(lp->sl_prefprog[i]); |
| 1533 | vim_free(lp->sl_prefprog); |
| 1534 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1535 | #ifdef FEAT_MBYTE |
| 1536 | { |
| 1537 | int todo = lp->sl_map_hash.ht_used; |
| 1538 | hashitem_T *hi; |
| 1539 | |
| 1540 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1541 | if (!HASHITEM_EMPTY(hi)) |
| 1542 | { |
| 1543 | --todo; |
| 1544 | vim_free(hi->hi_key); |
| 1545 | } |
| 1546 | } |
| 1547 | hash_clear(&lp->sl_map_hash); |
| 1548 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1552 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1553 | * Invoked through do_in_runtimepath(). |
| 1554 | */ |
| 1555 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1556 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1557 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1558 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1559 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1560 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * Load one spell file and store the info into a slang_T. |
| 1565 | * |
| 1566 | * This is invoked in two ways: |
| 1567 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1568 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1569 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1570 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1571 | * 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] | 1572 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1573 | static slang_T * |
| 1574 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1575 | char_u *fname; |
| 1576 | char_u *lang; |
| 1577 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1578 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1579 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1580 | FILE *fd; |
| 1581 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1582 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1583 | char_u *bp; |
| 1584 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1585 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1586 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1587 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1588 | int round; |
| 1589 | char_u *save_sourcing_name = sourcing_name; |
| 1590 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1591 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1592 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1593 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1594 | garray_T *gap; |
| 1595 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1596 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1597 | int rr; |
| 1598 | short *first; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1599 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1600 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1601 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1602 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1603 | if (fd == NULL) |
| 1604 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1605 | if (!silent) |
| 1606 | EMSG2(_(e_notopen), fname); |
| 1607 | else if (p_verbose > 2) |
| 1608 | { |
| 1609 | verbose_enter(); |
| 1610 | smsg((char_u *)e_notopen, fname); |
| 1611 | verbose_leave(); |
| 1612 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1613 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1614 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1615 | if (p_verbose > 2) |
| 1616 | { |
| 1617 | verbose_enter(); |
| 1618 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1619 | verbose_leave(); |
| 1620 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1621 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1622 | if (old_lp == NULL) |
| 1623 | { |
| 1624 | lp = slang_alloc(lang); |
| 1625 | if (lp == NULL) |
| 1626 | goto endFAIL; |
| 1627 | |
| 1628 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1629 | lp->sl_fname = vim_strsave(fname); |
| 1630 | if (lp->sl_fname == NULL) |
| 1631 | goto endFAIL; |
| 1632 | |
| 1633 | /* Check for .add.spl. */ |
| 1634 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1635 | } |
| 1636 | else |
| 1637 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1638 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1639 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1640 | sourcing_name = fname; |
| 1641 | sourcing_lnum = 0; |
| 1642 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1643 | /* <HEADER>: <fileID> |
| 1644 | * <regioncnt> <regionname> ... |
| 1645 | * <charflagslen> <charflags> |
| 1646 | * <fcharslen> <fchars> |
| 1647 | * <prefcondcnt> <prefcond> ... |
| 1648 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1649 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1650 | buf[i] = getc(fd); /* <fileID> */ |
| 1651 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1652 | { |
| 1653 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1654 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1658 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1659 | { |
| 1660 | truncerr: |
| 1661 | EMSG(_("E758: Truncated spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1662 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1663 | } |
| 1664 | if (cnt > 8) |
| 1665 | { |
| 1666 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1667 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1668 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1669 | } |
| 1670 | for (i = 0; i < cnt; ++i) |
| 1671 | { |
| 1672 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1673 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1674 | } |
| 1675 | lp->sl_regions[cnt * 2] = NUL; |
| 1676 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1677 | cnt = getc(fd); /* <charflagslen> */ |
| 1678 | if (cnt > 0) |
| 1679 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1680 | p = alloc((unsigned)cnt); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1681 | if (p == NULL) |
| 1682 | goto endFAIL; |
| 1683 | for (i = 0; i < cnt; ++i) |
| 1684 | p[i] = getc(fd); /* <charflags> */ |
| 1685 | |
| 1686 | ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */ |
| 1687 | if (ccnt <= 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1688 | { |
| 1689 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1690 | goto formerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1691 | } |
| 1692 | fol = alloc((unsigned)ccnt + 1); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1693 | if (fol == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1694 | { |
| 1695 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1696 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1697 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1698 | for (i = 0; i < ccnt; ++i) |
| 1699 | fol[i] = getc(fd); /* <fchars> */ |
| 1700 | fol[i] = NUL; |
| 1701 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1702 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1703 | i = set_spell_charflags(p, cnt, fol); |
| 1704 | vim_free(p); |
| 1705 | vim_free(fol); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1706 | #if 0 /* tolerate the differences */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1707 | if (i == FAIL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1708 | goto formerr; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1709 | #endif |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1710 | } |
| 1711 | else |
| 1712 | { |
| 1713 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1714 | cnt = (getc(fd) << 8) + getc(fd); |
| 1715 | if (cnt != 0) |
| 1716 | goto formerr; |
| 1717 | } |
| 1718 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1719 | /* <prefcondcnt> <prefcond> ... */ |
| 1720 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1721 | if (cnt > 0) |
| 1722 | { |
| 1723 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1724 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1725 | if (lp->sl_prefprog == NULL) |
| 1726 | goto endFAIL; |
| 1727 | lp->sl_prefixcnt = cnt; |
| 1728 | |
| 1729 | for (i = 0; i < cnt; ++i) |
| 1730 | { |
| 1731 | /* <prefcond> : <condlen> <condstr> */ |
| 1732 | n = getc(fd); /* <condlen> */ |
| 1733 | if (n < 0) |
| 1734 | goto formerr; |
| 1735 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1736 | * compile the regexp program used to check for the condition. */ |
| 1737 | if (n > 0) |
| 1738 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1739 | buf[0] = '^'; /* always match at one position only */ |
| 1740 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1741 | while (n-- > 0) |
| 1742 | *p++ = getc(fd); /* <condstr> */ |
| 1743 | *p = NUL; |
| 1744 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1750 | /* <SUGGEST> : <repcount> <rep> ... |
| 1751 | * <salflags> <salcount> <sal> ... |
| 1752 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1753 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1754 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1755 | if (cnt < 0) |
| 1756 | goto formerr; |
| 1757 | |
| 1758 | gap = &lp->sl_rep; |
| 1759 | if (ga_grow(gap, cnt) == FAIL) |
| 1760 | goto endFAIL; |
| 1761 | |
| 1762 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1763 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1764 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1765 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
| 1766 | for (rr = 1; rr <= 2; ++rr) |
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 | ccnt = getc(fd); |
| 1769 | if (ccnt < 0) |
| 1770 | { |
| 1771 | if (rr == 2) |
| 1772 | vim_free(ftp->ft_from); |
| 1773 | goto formerr; |
| 1774 | } |
| 1775 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1776 | { |
| 1777 | if (rr == 2) |
| 1778 | vim_free(ftp->ft_from); |
| 1779 | goto endFAIL; |
| 1780 | } |
| 1781 | for (i = 0; i < ccnt; ++i) |
| 1782 | p[i] = getc(fd); /* <repfrom> or <repto> */ |
| 1783 | p[i] = NUL; |
| 1784 | if (rr == 1) |
| 1785 | ftp->ft_from = p; |
| 1786 | else |
| 1787 | ftp->ft_to = p; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /* Fill the first-index table. */ |
| 1792 | first = lp->sl_rep_first; |
| 1793 | for (i = 0; i < 256; ++i) |
| 1794 | first[i] = -1; |
| 1795 | for (i = 0; i < gap->ga_len; ++i) |
| 1796 | { |
| 1797 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1798 | if (first[*ftp->ft_from] == -1) |
| 1799 | first[*ftp->ft_from] = i; |
| 1800 | } |
| 1801 | |
| 1802 | i = getc(fd); /* <salflags> */ |
| 1803 | if (i & SAL_F0LLOWUP) |
| 1804 | lp->sl_followup = TRUE; |
| 1805 | if (i & SAL_COLLAPSE) |
| 1806 | lp->sl_collapse = TRUE; |
| 1807 | if (i & SAL_REM_ACCENTS) |
| 1808 | lp->sl_rem_accents = TRUE; |
| 1809 | |
| 1810 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1811 | if (cnt < 0) |
| 1812 | goto formerr; |
| 1813 | |
| 1814 | gap = &lp->sl_sal; |
| 1815 | if (ga_grow(gap, cnt) == FAIL) |
| 1816 | goto endFAIL; |
| 1817 | |
| 1818 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 1819 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 1820 | { |
| 1821 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 1822 | ccnt = getc(fd); /* <salfromlen> */ |
| 1823 | if (ccnt < 0) |
| 1824 | goto formerr; |
| 1825 | if ((p = alloc(ccnt + 2)) == NULL) |
| 1826 | goto endFAIL; |
| 1827 | smp->sm_lead = p; |
| 1828 | |
| 1829 | /* Read up to the first special char into sm_lead. */ |
| 1830 | for (i = 0; i < ccnt; ++i) |
| 1831 | { |
| 1832 | c = getc(fd); /* <salfrom> */ |
| 1833 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
| 1834 | break; |
| 1835 | *p++ = c; |
| 1836 | } |
| 1837 | smp->sm_leadlen = p - smp->sm_lead; |
| 1838 | *p++ = NUL; |
| 1839 | |
| 1840 | /* Put optional chars in sm_oneoff, if any. */ |
| 1841 | if (c == '(') |
| 1842 | { |
| 1843 | smp->sm_oneoff = p; |
| 1844 | for (++i; i < ccnt; ++i) |
| 1845 | { |
| 1846 | c = getc(fd); /* <salfrom> */ |
| 1847 | if (c == ')') |
| 1848 | break; |
| 1849 | *p++ = c; |
| 1850 | } |
| 1851 | *p++ = NUL; |
| 1852 | if (++i < ccnt) |
| 1853 | c = getc(fd); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1854 | } |
| 1855 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1856 | smp->sm_oneoff = NULL; |
| 1857 | |
| 1858 | /* Any following chars go in sm_rules. */ |
| 1859 | smp->sm_rules = p; |
| 1860 | if (i < ccnt) |
| 1861 | *p++ = c; |
| 1862 | for (++i; i < ccnt; ++i) |
| 1863 | *p++ = getc(fd); /* <salfrom> */ |
| 1864 | *p++ = NUL; |
| 1865 | |
| 1866 | ccnt = getc(fd); /* <saltolen> */ |
| 1867 | if (ccnt < 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1868 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1869 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1870 | goto formerr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1871 | } |
| 1872 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1873 | { |
| 1874 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1875 | goto endFAIL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1876 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1877 | smp->sm_to = p; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1878 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1879 | for (i = 0; i < ccnt; ++i) |
| 1880 | *p++ = getc(fd); /* <salto> */ |
| 1881 | *p++ = NUL; |
| 1882 | } |
| 1883 | |
| 1884 | /* Fill the first-index table. */ |
| 1885 | first = lp->sl_sal_first; |
| 1886 | for (i = 0; i < 256; ++i) |
| 1887 | first[i] = -1; |
| 1888 | for (i = 0; i < gap->ga_len; ++i) |
| 1889 | { |
| 1890 | smp = &((salitem_T *)gap->ga_data)[i]; |
| 1891 | if (first[*smp->sm_lead] == -1) |
| 1892 | first[*smp->sm_lead] = i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */ |
| 1896 | if (cnt < 0) |
| 1897 | goto formerr; |
| 1898 | p = alloc(cnt + 1); |
| 1899 | if (p == NULL) |
| 1900 | goto endFAIL; |
| 1901 | for (i = 0; i < cnt; ++i) |
| 1902 | p[i] = getc(fd); /* <mapstr> */ |
| 1903 | p[i] = NUL; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1904 | set_map_str(lp, p); |
| 1905 | vim_free(p); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1906 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1907 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1908 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1909 | * round 2: <KWORDTREE> |
| 1910 | * round 3: <PREFIXTREE> */ |
| 1911 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1912 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1913 | /* The tree size was computed when writing the file, so that we can |
| 1914 | * allocate it as one long block. <nodecount> */ |
| 1915 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 1916 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1917 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1918 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1919 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1920 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1921 | bp = lalloc((long_u)len, TRUE); |
| 1922 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1923 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1924 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1925 | lp->sl_fbyts = bp; |
| 1926 | else if (round == 2) |
| 1927 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1928 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1929 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1930 | |
| 1931 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1932 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 1933 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1934 | goto endFAIL; |
| 1935 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1936 | lp->sl_fidxs = ip; |
| 1937 | else if (round == 2) |
| 1938 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1939 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1940 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1941 | |
| 1942 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1943 | 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] | 1944 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1945 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1946 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1947 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1948 | } |
| 1949 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1950 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1951 | /* For a new file link it in the list of spell files. */ |
| 1952 | if (old_lp == NULL) |
| 1953 | { |
| 1954 | lp->sl_next = first_lang; |
| 1955 | first_lang = lp; |
| 1956 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1957 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1958 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1959 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1960 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1961 | if (lang != NULL) |
| 1962 | /* truncating the name signals the error to spell_load_lang() */ |
| 1963 | *lang = NUL; |
| 1964 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1965 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1966 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1967 | lp = NULL; |
| 1968 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1969 | |
| 1970 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1971 | if (fd != NULL) |
| 1972 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1973 | sourcing_name = save_sourcing_name; |
| 1974 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1975 | |
| 1976 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1980 | * Read one row of siblings from the spell file and store it in the byte array |
| 1981 | * "byts" and index array "idxs". Recursively read the children. |
| 1982 | * |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1983 | * NOTE: The code here must match put_node(). |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1984 | * |
| 1985 | * Returns the index follosing the siblings. |
| 1986 | * Returns -1 if the file is shorter than expected. |
| 1987 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1988 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1989 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1990 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1991 | FILE *fd; |
| 1992 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1993 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1994 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1995 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1996 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 1997 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1998 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1999 | int len; |
| 2000 | int i; |
| 2001 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2002 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2003 | int c; |
| 2004 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2005 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2006 | len = getc(fd); /* <siblingcount> */ |
| 2007 | if (len <= 0) |
| 2008 | return -1; |
| 2009 | |
| 2010 | if (startidx + len >= maxidx) |
| 2011 | return -2; |
| 2012 | byts[idx++] = len; |
| 2013 | |
| 2014 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 2015 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2016 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2017 | c = getc(fd); /* <byte> */ |
| 2018 | if (c < 0) |
| 2019 | return -1; |
| 2020 | if (c <= BY_SPECIAL) |
| 2021 | { |
| 2022 | if (c == BY_NOFLAGS) |
| 2023 | { |
| 2024 | /* No flags, all regions. */ |
| 2025 | idxs[idx] = 0; |
| 2026 | c = 0; |
| 2027 | } |
| 2028 | else if (c == BY_FLAGS) |
| 2029 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2030 | if (prefixtree) |
| 2031 | { |
| 2032 | /* Read the prefix ID and the condition nr. In idxs[] |
| 2033 | * store the prefix ID in the low byte, the condition |
| 2034 | * index shifted up 8 bits. */ |
| 2035 | c = getc(fd); /* <prefixID> */ |
| 2036 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 2037 | if (n >= maxprefcondnr) |
| 2038 | return -2; |
| 2039 | c = (n << 8) + c; |
| 2040 | } |
| 2041 | else |
| 2042 | { |
| 2043 | /* Read flags and optional region and prefix ID. In |
| 2044 | * idxs[] the flags go in the low byte, region above that |
| 2045 | * and prefix ID above the region. */ |
| 2046 | c = getc(fd); /* <flags> */ |
| 2047 | if (c & WF_REGION) |
| 2048 | c = (getc(fd) << 8) + c; /* <region> */ |
| 2049 | if (c & WF_PFX) |
| 2050 | c = (getc(fd) << 16) + c; /* <prefixID> */ |
| 2051 | } |
| 2052 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2053 | idxs[idx] = c; |
| 2054 | c = 0; |
| 2055 | } |
| 2056 | else /* c == BY_INDEX */ |
| 2057 | { |
| 2058 | /* <nodeidx> */ |
| 2059 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2060 | if (n < 0 || n >= maxidx) |
| 2061 | return -2; |
| 2062 | idxs[idx] = n + SHARED_MASK; |
| 2063 | c = getc(fd); /* <xbyte> */ |
| 2064 | } |
| 2065 | } |
| 2066 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2069 | /* Recursively read the children for non-shared siblings. |
| 2070 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 2071 | * remove SHARED_MASK) */ |
| 2072 | for (i = 1; i <= len; ++i) |
| 2073 | if (byts[startidx + i] != 0) |
| 2074 | { |
| 2075 | if (idxs[startidx + i] & SHARED_MASK) |
| 2076 | idxs[startidx + i] &= ~SHARED_MASK; |
| 2077 | else |
| 2078 | { |
| 2079 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2080 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 2081 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2082 | if (idx < 0) |
| 2083 | break; |
| 2084 | } |
| 2085 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2086 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2087 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Parse 'spelllang' and set buf->b_langp accordingly. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2092 | * Returns NULL if it's OK, an error message otherwise. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2093 | */ |
| 2094 | char_u * |
| 2095 | did_set_spelllang(buf) |
| 2096 | buf_T *buf; |
| 2097 | { |
| 2098 | garray_T ga; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2099 | char_u *splp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2100 | char_u *region; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2101 | int filename; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2102 | int region_mask; |
| 2103 | slang_T *lp; |
| 2104 | int c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2105 | char_u lang[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2106 | char_u spf_name[MAXPATHL]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2107 | int load_spf; |
| 2108 | int len; |
| 2109 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2110 | |
| 2111 | ga_init2(&ga, sizeof(langp_T), 2); |
| 2112 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2113 | /* Make the name of the .spl file associated with 'spellfile'. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2114 | if (*buf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2115 | load_spf = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2116 | else |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2117 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2118 | vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl", |
| 2119 | buf->b_p_spf); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2120 | load_spf = TRUE; |
| 2121 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2122 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2123 | /* loop over comma separated language names. */ |
| 2124 | for (splp = buf->b_p_spl; *splp != NUL; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2125 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2126 | /* Get one language name. */ |
| 2127 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 2128 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2129 | region = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2130 | len = STRLEN(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2131 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2132 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 2133 | * If there is a region name let "region" point to it and remove it |
| 2134 | * from the name. */ |
| 2135 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 2136 | { |
| 2137 | filename = TRUE; |
| 2138 | |
| 2139 | /* Check if we loaded this language before. */ |
| 2140 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2141 | if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME) |
| 2142 | break; |
| 2143 | } |
| 2144 | else |
| 2145 | { |
| 2146 | filename = FALSE; |
| 2147 | if (len > 3 && lang[len - 3] == '_') |
| 2148 | { |
| 2149 | region = lang + len - 2; |
| 2150 | len -= 3; |
| 2151 | lang[len] = NUL; |
| 2152 | } |
| 2153 | |
| 2154 | /* Check if we loaded this language before. */ |
| 2155 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2156 | if (STRICMP(lang, lp->sl_name) == 0) |
| 2157 | break; |
| 2158 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2159 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2160 | /* If not found try loading the language now. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2161 | if (lp == NULL) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2162 | { |
| 2163 | if (filename) |
| 2164 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 2165 | else |
| 2166 | spell_load_lang(lang); |
| 2167 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2168 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2169 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2170 | * Loop over the languages, there can be several files for "lang". |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2171 | */ |
| 2172 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2173 | if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME |
| 2174 | : STRICMP(lang, lp->sl_name) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2175 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2176 | region_mask = REGION_ALL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2177 | if (!filename && region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2178 | { |
| 2179 | /* find region in sl_regions */ |
| 2180 | c = find_region(lp->sl_regions, region); |
| 2181 | if (c == REGION_ALL) |
| 2182 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2183 | if (!lp->sl_add) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2184 | smsg((char_u *) |
| 2185 | _("Warning: region %s not supported"), |
| 2186 | region); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2187 | } |
| 2188 | else |
| 2189 | region_mask = 1 << c; |
| 2190 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2191 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2192 | if (ga_grow(&ga, 1) == FAIL) |
| 2193 | { |
| 2194 | ga_clear(&ga); |
| 2195 | return e_outofmem; |
| 2196 | } |
| 2197 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2198 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2199 | ++ga.ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2200 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2201 | /* Check if this is the spell file related to 'spellfile'. */ |
| 2202 | if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE) |
| 2203 | == FPC_SAME) |
| 2204 | load_spf = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2205 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2208 | /* |
| 2209 | * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath', |
| 2210 | * then it's probably loaded above already. Otherwise load it here. |
| 2211 | */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2212 | if (load_spf) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2213 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2214 | /* Check if it was loaded already. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2215 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2216 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2217 | break; |
| 2218 | if (lp == NULL) |
| 2219 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2220 | /* Not loaded, try loading it now. The language name includes the |
| 2221 | * region name, the region is ignored otherwise. */ |
| 2222 | vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN); |
| 2223 | p = vim_strchr(lang, '.'); |
| 2224 | if (p != NULL) |
| 2225 | *p = NUL; /* truncate at ".encoding.add" */ |
| 2226 | lp = spell_load_file(spf_name, lang, NULL, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2227 | } |
| 2228 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2229 | { |
| 2230 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2231 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL; |
| 2232 | ++ga.ga_len; |
| 2233 | } |
| 2234 | } |
| 2235 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2236 | /* Add a NULL entry to mark the end of the list. */ |
| 2237 | if (ga_grow(&ga, 1) == FAIL) |
| 2238 | { |
| 2239 | ga_clear(&ga); |
| 2240 | return e_outofmem; |
| 2241 | } |
| 2242 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2243 | ++ga.ga_len; |
| 2244 | |
| 2245 | /* Everything is fine, store the new b_langp value. */ |
| 2246 | ga_clear(&buf->b_langp); |
| 2247 | buf->b_langp = ga; |
| 2248 | |
| 2249 | return NULL; |
| 2250 | } |
| 2251 | |
| 2252 | /* |
| 2253 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2254 | * Each region is simply stored as the two characters of it's name. |
| 2255 | * Returns the index if found, REGION_ALL if not found. |
| 2256 | */ |
| 2257 | static int |
| 2258 | find_region(rp, region) |
| 2259 | char_u *rp; |
| 2260 | char_u *region; |
| 2261 | { |
| 2262 | int i; |
| 2263 | |
| 2264 | for (i = 0; ; i += 2) |
| 2265 | { |
| 2266 | if (rp[i] == NUL) |
| 2267 | return REGION_ALL; |
| 2268 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2269 | break; |
| 2270 | } |
| 2271 | return i / 2; |
| 2272 | } |
| 2273 | |
| 2274 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2275 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2276 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2277 | * Word WF_ONECAP |
| 2278 | * W WORD WF_ALLCAP |
| 2279 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2280 | */ |
| 2281 | static int |
| 2282 | captype(word, end) |
| 2283 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2284 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2285 | { |
| 2286 | char_u *p; |
| 2287 | int c; |
| 2288 | int firstcap; |
| 2289 | int allcap; |
| 2290 | int past_second = FALSE; /* past second word char */ |
| 2291 | |
| 2292 | /* find first letter */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2293 | for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2294 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2295 | return 0; /* only non-word characters, illegal word */ |
| 2296 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2297 | if (has_mbyte) |
| 2298 | c = mb_ptr2char_adv(&p); |
| 2299 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2300 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2301 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2302 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2303 | |
| 2304 | /* |
| 2305 | * Need to check all letters to find a word with mixed upper/lower. |
| 2306 | * But a word with an upper char only at start is a ONECAP. |
| 2307 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2308 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2309 | if (SPELL_ISWORDP(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2310 | { |
| 2311 | #ifdef FEAT_MBYTE |
| 2312 | c = mb_ptr2char(p); |
| 2313 | #else |
| 2314 | c = *p; |
| 2315 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2316 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2317 | { |
| 2318 | /* UUl -> KEEPCAP */ |
| 2319 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2320 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2321 | allcap = FALSE; |
| 2322 | } |
| 2323 | else if (!allcap) |
| 2324 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2325 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2326 | past_second = TRUE; |
| 2327 | } |
| 2328 | |
| 2329 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2330 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2331 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2332 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2333 | return 0; |
| 2334 | } |
| 2335 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2336 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 2337 | /* |
| 2338 | * Free all languages. |
| 2339 | */ |
| 2340 | void |
| 2341 | spell_free_all() |
| 2342 | { |
| 2343 | slang_T *lp; |
| 2344 | buf_T *buf; |
| 2345 | |
| 2346 | /* Go through all buffers and handle 'spelllang'. */ |
| 2347 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2348 | ga_clear(&buf->b_langp); |
| 2349 | |
| 2350 | while (first_lang != NULL) |
| 2351 | { |
| 2352 | lp = first_lang; |
| 2353 | first_lang = lp->sl_next; |
| 2354 | slang_free(lp); |
| 2355 | } |
| 2356 | } |
| 2357 | # endif |
| 2358 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2359 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2360 | /* |
| 2361 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2362 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2363 | */ |
| 2364 | void |
| 2365 | spell_reload() |
| 2366 | { |
| 2367 | buf_T *buf; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2368 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2369 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2370 | /* Initialize the table for SPELL_ISWORDP(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2371 | init_spell_chartab(); |
| 2372 | |
| 2373 | /* Unload all allocated memory. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2374 | spell_free_all(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2375 | |
| 2376 | /* Go through all buffers and handle 'spelllang'. */ |
| 2377 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2378 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2379 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2380 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2381 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2382 | { |
| 2383 | FOR_ALL_WINDOWS(wp) |
| 2384 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2385 | { |
| 2386 | (void)did_set_spelllang(buf); |
| 2387 | # ifdef FEAT_WINDOWS |
| 2388 | break; |
| 2389 | # endif |
| 2390 | } |
| 2391 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | # endif |
| 2395 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2396 | /* |
| 2397 | * Reload the spell file "fname" if it's loaded. |
| 2398 | */ |
| 2399 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2400 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2401 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2402 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2403 | { |
| 2404 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2405 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2406 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2407 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2408 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 2409 | { |
| 2410 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2411 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2412 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2413 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2414 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2415 | |
| 2416 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 2417 | * 'spelllang' to get it loaded. */ |
| 2418 | if (added_word && !didit) |
| 2419 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2423 | /* |
| 2424 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2425 | */ |
| 2426 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2427 | #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] | 2428 | and .dic file. */ |
| 2429 | /* |
| 2430 | * Main structure to store the contents of a ".aff" file. |
| 2431 | */ |
| 2432 | typedef struct afffile_S |
| 2433 | { |
| 2434 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2435 | int af_rar; /* RAR ID for rare word */ |
| 2436 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2437 | int af_bad; /* BAD ID for banned word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2438 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2439 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2440 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2441 | } afffile_T; |
| 2442 | |
| 2443 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2444 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2445 | struct affentry_S |
| 2446 | { |
| 2447 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2448 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 2449 | 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] | 2450 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 2451 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2452 | }; |
| 2453 | |
| 2454 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 2455 | typedef struct affheader_S |
| 2456 | { |
| 2457 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2458 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2459 | int ah_combine; /* suffix may combine with prefix */ |
| 2460 | affentry_T *ah_first; /* first affix entry */ |
| 2461 | } affheader_T; |
| 2462 | |
| 2463 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 2464 | |
| 2465 | /* |
| 2466 | * Structure that is used to store the items in the word tree. This avoids |
| 2467 | * the need to keep track of each allocated thing, it's freed all at once |
| 2468 | * after ":mkspell" is done. |
| 2469 | */ |
| 2470 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 2471 | typedef struct sblock_S sblock_T; |
| 2472 | struct sblock_S |
| 2473 | { |
| 2474 | sblock_T *sb_next; /* next block in list */ |
| 2475 | int sb_used; /* nr of bytes already in use */ |
| 2476 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2477 | }; |
| 2478 | |
| 2479 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2480 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2481 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2482 | typedef struct wordnode_S wordnode_T; |
| 2483 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2484 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2485 | union /* shared to save space */ |
| 2486 | { |
| 2487 | char_u hashkey[6]; /* room for the hash key */ |
| 2488 | int index; /* index in written nodes (valid after first |
| 2489 | round) */ |
| 2490 | } wn_u1; |
| 2491 | union /* shared to save space */ |
| 2492 | { |
| 2493 | wordnode_T *next; /* next node with same hash key */ |
| 2494 | wordnode_T *wnode; /* parent node that will write this node */ |
| 2495 | } wn_u2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2496 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 2497 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 2498 | always sorted) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2499 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 2500 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2501 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 2502 | PREFIXTREE it's the prefcondnr */ |
| 2503 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2504 | }; |
| 2505 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2506 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2507 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2508 | /* |
| 2509 | * Info used while reading the spell files. |
| 2510 | */ |
| 2511 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2512 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2513 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2514 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2515 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2516 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2517 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2518 | sblock_T *si_blocks; /* memory blocks used */ |
| 2519 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2520 | int si_add; /* addition file */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2521 | int si_clear_chartab; /* when TRUE clear char tables */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2522 | int si_region; /* region mask */ |
| 2523 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2524 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2525 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2526 | int si_region_count; /* number of regions supported (1 when there |
| 2527 | are no regions) */ |
| 2528 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2529 | |
| 2530 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 2531 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
| 2532 | int si_followup; /* soundsalike: ? */ |
| 2533 | int si_collapse; /* soundsalike: ? */ |
| 2534 | int si_rem_accents; /* soundsalike: remove accents */ |
| 2535 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2536 | garray_T si_prefcond; /* table with conditions for postponed |
| 2537 | * prefixes, each stored as a string */ |
| 2538 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2539 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2540 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2541 | 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] | 2542 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2543 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 2544 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2545 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2546 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 2547 | 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] | 2548 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 2549 | 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] | 2550 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 2551 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 2552 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 2553 | static void free_blocks __ARGS((sblock_T *bl)); |
| 2554 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2555 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 2556 | 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] | 2557 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2558 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 2559 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2560 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2561 | static void clear_node __ARGS((wordnode_T *node)); |
| 2562 | 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] | 2563 | 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] | 2564 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2565 | |
| 2566 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2567 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2568 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2569 | */ |
| 2570 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2571 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2572 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2573 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2574 | { |
| 2575 | FILE *fd; |
| 2576 | afffile_T *aff; |
| 2577 | char_u rline[MAXLINELEN]; |
| 2578 | char_u *line; |
| 2579 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2580 | #define MAXITEMCNT 7 |
| 2581 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2582 | int itemcnt; |
| 2583 | char_u *p; |
| 2584 | int lnum = 0; |
| 2585 | affheader_T *cur_aff = NULL; |
| 2586 | int aff_todo = 0; |
| 2587 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2588 | char_u *low = NULL; |
| 2589 | char_u *fol = NULL; |
| 2590 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2591 | 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] | 2592 | int do_rep; |
| 2593 | int do_sal; |
| 2594 | int do_map; |
| 2595 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2596 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2597 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2598 | /* |
| 2599 | * Open the file. |
| 2600 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2601 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2602 | if (fd == NULL) |
| 2603 | { |
| 2604 | EMSG2(_(e_notopen), fname); |
| 2605 | return NULL; |
| 2606 | } |
| 2607 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2608 | if (spin->si_verbose || p_verbose > 2) |
| 2609 | { |
| 2610 | if (!spin->si_verbose) |
| 2611 | verbose_enter(); |
| 2612 | smsg((char_u *)_("Reading affix file %s..."), fname); |
| 2613 | out_flush(); |
| 2614 | if (!spin->si_verbose) |
| 2615 | verbose_leave(); |
| 2616 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2617 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2618 | /* Only do REP lines when not done in another .aff file already. */ |
| 2619 | do_rep = spin->si_rep.ga_len == 0; |
| 2620 | |
| 2621 | /* Only do SAL lines when not done in another .aff file already. */ |
| 2622 | do_sal = spin->si_sal.ga_len == 0; |
| 2623 | |
| 2624 | /* Only do MAP lines when not done in another .aff file already. */ |
| 2625 | do_map = spin->si_map.ga_len == 0; |
| 2626 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2627 | /* |
| 2628 | * Allocate and init the afffile_T structure. |
| 2629 | */ |
| 2630 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2631 | if (aff == NULL) |
| 2632 | return NULL; |
| 2633 | hash_init(&aff->af_pref); |
| 2634 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2635 | |
| 2636 | /* |
| 2637 | * Read all the lines in the file one by one. |
| 2638 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2639 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2640 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2641 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2642 | ++lnum; |
| 2643 | |
| 2644 | /* Skip comment lines. */ |
| 2645 | if (*rline == '#') |
| 2646 | continue; |
| 2647 | |
| 2648 | /* Convert from "SET" to 'encoding' when needed. */ |
| 2649 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2650 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2651 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2652 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2653 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2654 | if (pc == NULL) |
| 2655 | { |
| 2656 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 2657 | fname, lnum, rline); |
| 2658 | continue; |
| 2659 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2660 | line = pc; |
| 2661 | } |
| 2662 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2663 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2664 | { |
| 2665 | pc = NULL; |
| 2666 | line = rline; |
| 2667 | } |
| 2668 | |
| 2669 | /* Split the line up in white separated items. Put a NUL after each |
| 2670 | * item. */ |
| 2671 | itemcnt = 0; |
| 2672 | for (p = line; ; ) |
| 2673 | { |
| 2674 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 2675 | ++p; |
| 2676 | if (*p == NUL) |
| 2677 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2678 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2679 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2680 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2681 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2682 | ++p; |
| 2683 | if (*p == NUL) |
| 2684 | break; |
| 2685 | *p++ = NUL; |
| 2686 | } |
| 2687 | |
| 2688 | /* Handle non-empty lines. */ |
| 2689 | if (itemcnt > 0) |
| 2690 | { |
| 2691 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 2692 | && aff->af_enc == NULL) |
| 2693 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2694 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2695 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 2696 | aff->af_enc = enc_canonize(items[1]); |
| 2697 | if (aff->af_enc != NULL && !spin->si_ascii |
| 2698 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 2699 | p_enc) == FAIL) |
| 2700 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 2701 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2702 | #else |
| 2703 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 2704 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2705 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2706 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 2707 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2708 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2709 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2710 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2711 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2712 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2713 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2714 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 2715 | && aff->af_rar == 0) |
| 2716 | { |
| 2717 | aff->af_rar = items[1][0]; |
| 2718 | if (items[1][1] != NUL) |
| 2719 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2720 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2721 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 2722 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2723 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2724 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2725 | if (items[1][1] != NUL) |
| 2726 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2727 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2728 | else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2 |
| 2729 | && aff->af_bad == 0) |
| 2730 | { |
| 2731 | aff->af_bad = items[1][0]; |
| 2732 | if (items[1][1] != NUL) |
| 2733 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2734 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2735 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 2736 | { |
| 2737 | aff->af_pfxpostpone = TRUE; |
| 2738 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2739 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2740 | || STRCMP(items[0], "SFX") == 0) |
| 2741 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2742 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2743 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2744 | /* Myspell allows extra text after the item, but that might |
| 2745 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2746 | if (itemcnt > 4 && *items[4] != '#') |
| 2747 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2748 | fname, lnum, items[4]); |
| 2749 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2750 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2751 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 2752 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2753 | if (cur_aff == NULL) |
| 2754 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2755 | cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2756 | cur_aff->ah_key[1] = NUL; |
| 2757 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2758 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2759 | if (*items[2] == 'Y') |
| 2760 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2761 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2762 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 2763 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2764 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2765 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2766 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2767 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2768 | /* Use a new number in the .spl file later, to be able to |
| 2769 | * handle multiple .aff files. */ |
| 2770 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2771 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2772 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2773 | else |
| 2774 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2775 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2776 | hi = hash_find(tp, cur_aff->ah_key); |
| 2777 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2778 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2779 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 2780 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2781 | aff_todo = 0; |
| 2782 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2783 | else |
| 2784 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2785 | } |
| 2786 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2787 | || STRCMP(items[0], "SFX") == 0) |
| 2788 | && aff_todo > 0 |
| 2789 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2790 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2791 | { |
| 2792 | affentry_T *aff_entry; |
| 2793 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2794 | /* Myspell allows extra text after the item, but that might |
| 2795 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2796 | if (itemcnt > 5 && *items[5] != '#') |
| 2797 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2798 | fname, lnum, items[5]); |
| 2799 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2800 | /* New item for an affix letter. */ |
| 2801 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2802 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 2803 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2804 | if (aff_entry == NULL) |
| 2805 | break; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2806 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2807 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2808 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 2809 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2810 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2811 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 2812 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2813 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2814 | /* Don't use an affix entry with non-ASCII characters when |
| 2815 | * "spin->si_ascii" is TRUE. */ |
| 2816 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2817 | || has_non_ascii(aff_entry->ae_add))) |
| 2818 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2819 | aff_entry->ae_next = cur_aff->ah_first; |
| 2820 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2821 | |
| 2822 | if (STRCMP(items[4], ".") != 0) |
| 2823 | { |
| 2824 | char_u buf[MAXLINELEN]; |
| 2825 | |
| 2826 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 2827 | items[4]); |
| 2828 | if (*items[0] == 'P') |
| 2829 | sprintf((char *)buf, "^%s", items[4]); |
| 2830 | else |
| 2831 | sprintf((char *)buf, "%s$", items[4]); |
| 2832 | aff_entry->ae_prog = vim_regcomp(buf, |
| 2833 | RE_MAGIC + RE_STRING); |
| 2834 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2835 | |
| 2836 | /* For postponed prefixes we need an entry in si_prefcond |
| 2837 | * for the condition. Use an existing one if possible. */ |
| 2838 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 2839 | && aff_entry->ae_chop == NULL) |
| 2840 | { |
| 2841 | int idx; |
| 2842 | char_u **pp; |
| 2843 | |
| 2844 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 2845 | --idx) |
| 2846 | { |
| 2847 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 2848 | if (str_equal(p, aff_entry->ae_cond)) |
| 2849 | break; |
| 2850 | } |
| 2851 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 2852 | { |
| 2853 | /* Not found, add a new condition. */ |
| 2854 | idx = spin->si_prefcond.ga_len++; |
| 2855 | pp = ((char_u **)spin->si_prefcond.ga_data) + idx; |
| 2856 | if (aff_entry->ae_cond == NULL) |
| 2857 | *pp = NULL; |
| 2858 | else |
| 2859 | *pp = getroom_save(&spin->si_blocks, |
| 2860 | aff_entry->ae_cond); |
| 2861 | } |
| 2862 | |
| 2863 | /* Add the prefix to the prefix tree. */ |
| 2864 | if (aff_entry->ae_add == NULL) |
| 2865 | p = (char_u *)""; |
| 2866 | else |
| 2867 | p = aff_entry->ae_add; |
| 2868 | tree_add_word(p, spin->si_prefroot, -1, idx, |
| 2869 | cur_aff->ah_newID, &spin->si_blocks); |
| 2870 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2871 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2872 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2873 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 2874 | { |
| 2875 | if (fol != NULL) |
| 2876 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 2877 | fname, lnum); |
| 2878 | else |
| 2879 | fol = vim_strsave(items[1]); |
| 2880 | } |
| 2881 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 2882 | { |
| 2883 | if (low != NULL) |
| 2884 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 2885 | fname, lnum); |
| 2886 | else |
| 2887 | low = vim_strsave(items[1]); |
| 2888 | } |
| 2889 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 2890 | { |
| 2891 | if (upp != NULL) |
| 2892 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 2893 | fname, lnum); |
| 2894 | else |
| 2895 | upp = vim_strsave(items[1]); |
| 2896 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2897 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2898 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2899 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2900 | if (!isdigit(*items[1])) |
| 2901 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 2902 | fname, lnum); |
| 2903 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2904 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 2905 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2906 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2907 | if (do_rep) |
| 2908 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 2909 | } |
| 2910 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 2911 | { |
| 2912 | /* MAP item or count */ |
| 2913 | if (!found_map) |
| 2914 | { |
| 2915 | /* First line contains the count. */ |
| 2916 | found_map = TRUE; |
| 2917 | if (!isdigit(*items[1])) |
| 2918 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 2919 | fname, lnum); |
| 2920 | } |
| 2921 | else if (do_map) |
| 2922 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2923 | int c; |
| 2924 | |
| 2925 | /* Check that every character appears only once. */ |
| 2926 | for (p = items[1]; *p != NUL; ) |
| 2927 | { |
| 2928 | #ifdef FEAT_MBYTE |
| 2929 | c = mb_ptr2char_adv(&p); |
| 2930 | #else |
| 2931 | c = *p++; |
| 2932 | #endif |
| 2933 | if ((spin->si_map.ga_len > 0 |
| 2934 | && vim_strchr(spin->si_map.ga_data, c) |
| 2935 | != NULL) |
| 2936 | || vim_strchr(p, c) != NULL) |
| 2937 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 2938 | fname, lnum); |
| 2939 | } |
| 2940 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2941 | /* We simply concatenate all the MAP strings, separated by |
| 2942 | * slashes. */ |
| 2943 | ga_concat(&spin->si_map, items[1]); |
| 2944 | ga_append(&spin->si_map, '/'); |
| 2945 | } |
| 2946 | } |
| 2947 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 2948 | { |
| 2949 | if (do_sal) |
| 2950 | { |
| 2951 | /* SAL item (sounds-a-like) |
| 2952 | * Either one of the known keys or a from-to pair. */ |
| 2953 | if (STRCMP(items[1], "followup") == 0) |
| 2954 | spin->si_followup = sal_to_bool(items[2]); |
| 2955 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 2956 | spin->si_collapse = sal_to_bool(items[2]); |
| 2957 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 2958 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 2959 | else |
| 2960 | /* when "to" is "_" it means empty */ |
| 2961 | add_fromto(spin, &spin->si_sal, items[1], |
| 2962 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 2963 | : items[2]); |
| 2964 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2965 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2966 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2967 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 2968 | fname, lnum, items[0]); |
| 2969 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2972 | if (fol != NULL || low != NULL || upp != NULL) |
| 2973 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2974 | if (spin->si_clear_chartab) |
| 2975 | { |
| 2976 | /* Clear the char type tables, don't want to use any of the |
| 2977 | * currently used spell properties. */ |
| 2978 | init_spell_chartab(); |
| 2979 | spin->si_clear_chartab = FALSE; |
| 2980 | } |
| 2981 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2982 | /* |
| 2983 | * Don't write a word table for an ASCII file, so that we don't check |
| 2984 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2985 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2986 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 2987 | */ |
| 2988 | if (!spin->si_ascii |
| 2989 | #ifdef FEAT_MBYTE |
| 2990 | && !enc_utf8 |
| 2991 | #endif |
| 2992 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2993 | { |
| 2994 | if (fol == NULL || low == NULL || upp == NULL) |
| 2995 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 2996 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2997 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2998 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2999 | |
| 3000 | vim_free(fol); |
| 3001 | vim_free(low); |
| 3002 | vim_free(upp); |
| 3003 | } |
| 3004 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3005 | vim_free(pc); |
| 3006 | fclose(fd); |
| 3007 | return aff; |
| 3008 | } |
| 3009 | |
| 3010 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3011 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 3012 | * NULL as equal. |
| 3013 | */ |
| 3014 | static int |
| 3015 | str_equal(s1, s2) |
| 3016 | char_u *s1; |
| 3017 | char_u *s2; |
| 3018 | { |
| 3019 | if (s1 == NULL || s2 == NULL) |
| 3020 | return s1 == s2; |
| 3021 | return STRCMP(s1, s2) == 0; |
| 3022 | } |
| 3023 | |
| 3024 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3025 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 3026 | * They are stored case-folded. |
| 3027 | */ |
| 3028 | static void |
| 3029 | add_fromto(spin, gap, from, to) |
| 3030 | spellinfo_T *spin; |
| 3031 | garray_T *gap; |
| 3032 | char_u *from; |
| 3033 | char_u *to; |
| 3034 | { |
| 3035 | fromto_T *ftp; |
| 3036 | char_u word[MAXWLEN]; |
| 3037 | |
| 3038 | if (ga_grow(gap, 1) == OK) |
| 3039 | { |
| 3040 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 3041 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 3042 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 3043 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 3044 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 3045 | ++gap->ga_len; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 3051 | */ |
| 3052 | static int |
| 3053 | sal_to_bool(s) |
| 3054 | char_u *s; |
| 3055 | { |
| 3056 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 3057 | } |
| 3058 | |
| 3059 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3060 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 3061 | * When "s" is NULL FALSE is returned. |
| 3062 | */ |
| 3063 | static int |
| 3064 | has_non_ascii(s) |
| 3065 | char_u *s; |
| 3066 | { |
| 3067 | char_u *p; |
| 3068 | |
| 3069 | if (s != NULL) |
| 3070 | for (p = s; *p != NUL; ++p) |
| 3071 | if (*p >= 128) |
| 3072 | return TRUE; |
| 3073 | return FALSE; |
| 3074 | } |
| 3075 | |
| 3076 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3077 | * Free the structure filled by spell_read_aff(). |
| 3078 | */ |
| 3079 | static void |
| 3080 | spell_free_aff(aff) |
| 3081 | afffile_T *aff; |
| 3082 | { |
| 3083 | hashtab_T *ht; |
| 3084 | hashitem_T *hi; |
| 3085 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3086 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3087 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3088 | |
| 3089 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3090 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3091 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3092 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 3093 | { |
| 3094 | todo = ht->ht_used; |
| 3095 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3096 | { |
| 3097 | if (!HASHITEM_EMPTY(hi)) |
| 3098 | { |
| 3099 | --todo; |
| 3100 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3101 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3102 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3103 | } |
| 3104 | } |
| 3105 | if (ht == &aff->af_suff) |
| 3106 | break; |
| 3107 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3108 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3109 | hash_clear(&aff->af_pref); |
| 3110 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3114 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3115 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3116 | */ |
| 3117 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3118 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3119 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3120 | spellinfo_T *spin; |
| 3121 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3122 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3123 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3124 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3125 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3126 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3127 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3128 | char_u *pc; |
| 3129 | char_u *w; |
| 3130 | int l; |
| 3131 | hash_T hash; |
| 3132 | hashitem_T *hi; |
| 3133 | FILE *fd; |
| 3134 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3135 | int non_ascii = 0; |
| 3136 | int retval = OK; |
| 3137 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3138 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3139 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3140 | /* |
| 3141 | * Open the file. |
| 3142 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3143 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3144 | if (fd == NULL) |
| 3145 | { |
| 3146 | EMSG2(_(e_notopen), fname); |
| 3147 | return FAIL; |
| 3148 | } |
| 3149 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3150 | /* The hashtable is only used to detect duplicated words. */ |
| 3151 | hash_init(&ht); |
| 3152 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3153 | spin->si_foldwcount = 0; |
| 3154 | spin->si_keepwcount = 0; |
| 3155 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3156 | if (spin->si_verbose || p_verbose > 2) |
| 3157 | { |
| 3158 | if (!spin->si_verbose) |
| 3159 | verbose_enter(); |
| 3160 | smsg((char_u *)_("Reading dictionary file %s..."), fname); |
| 3161 | out_flush(); |
| 3162 | if (!spin->si_verbose) |
| 3163 | verbose_leave(); |
| 3164 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3165 | |
| 3166 | /* Read and ignore the first line: word count. */ |
| 3167 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3168 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3169 | EMSG2(_("E760: No word count in %s"), fname); |
| 3170 | |
| 3171 | /* |
| 3172 | * Read all the lines in the file one by one. |
| 3173 | * The words are converted to 'encoding' here, before being added to |
| 3174 | * the hashtable. |
| 3175 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3176 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3177 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3178 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3179 | ++lnum; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3180 | if (line[0] == '#') |
| 3181 | continue; /* comment line */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3182 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3183 | /* Remove CR, LF and white space from the end. White space halfway |
| 3184 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3185 | l = STRLEN(line); |
| 3186 | while (l > 0 && line[l - 1] <= ' ') |
| 3187 | --l; |
| 3188 | if (l == 0) |
| 3189 | continue; /* empty line */ |
| 3190 | line[l] = NUL; |
| 3191 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3192 | /* Find the optional affix names. */ |
| 3193 | afflist = vim_strchr(line, '/'); |
| 3194 | if (afflist != NULL) |
| 3195 | *afflist++ = NUL; |
| 3196 | |
| 3197 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3198 | if (spin->si_ascii && has_non_ascii(line)) |
| 3199 | { |
| 3200 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3201 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3202 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3203 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3204 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3205 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3206 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3207 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3208 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3209 | if (pc == NULL) |
| 3210 | { |
| 3211 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3212 | fname, lnum, line); |
| 3213 | continue; |
| 3214 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3215 | w = pc; |
| 3216 | } |
| 3217 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3218 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3219 | { |
| 3220 | pc = NULL; |
| 3221 | w = line; |
| 3222 | } |
| 3223 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3224 | /* This takes time, print a message now and then. */ |
| 3225 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3226 | { |
| 3227 | vim_snprintf((char *)message, sizeof(message), |
| 3228 | _("line %6d, word %6d - %s"), |
| 3229 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3230 | msg_start(); |
| 3231 | msg_puts_long_attr(message, 0); |
| 3232 | msg_clr_eos(); |
| 3233 | msg_didout = FALSE; |
| 3234 | msg_col = 0; |
| 3235 | out_flush(); |
| 3236 | } |
| 3237 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3238 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 3239 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3240 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3241 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3242 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3243 | if (retval == FAIL) |
| 3244 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3245 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3246 | hash = hash_hash(dw); |
| 3247 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3248 | if (!HASHITEM_EMPTY(hi)) |
| 3249 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3250 | fname, lnum, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3251 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3252 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3253 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3254 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3255 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3256 | if (afflist != NULL) |
| 3257 | { |
| 3258 | /* Check for affix name that stands for keep-case word and stands |
| 3259 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3260 | if (affile->af_kep != NUL |
| 3261 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3262 | flags |= WF_KEEPCAP; |
| 3263 | if (affile->af_rar != NUL |
| 3264 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 3265 | flags |= WF_RARE; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3266 | if (affile->af_bad != NUL |
| 3267 | && vim_strchr(afflist, affile->af_bad) != NULL) |
| 3268 | flags |= WF_BANNED; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3269 | |
| 3270 | if (affile->af_pfxpostpone) |
| 3271 | /* Need to store the list of prefix IDs with the word. */ |
| 3272 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3275 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3276 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3277 | retval = FAIL; |
| 3278 | |
| 3279 | if (afflist != NULL) |
| 3280 | { |
| 3281 | /* Find all matching suffixes and add the resulting words. |
| 3282 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3283 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3284 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3285 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3286 | retval = FAIL; |
| 3287 | |
| 3288 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3289 | if (store_aff_word(dw, spin, afflist, affile, |
| 3290 | &affile->af_pref, NULL, |
| 3291 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3292 | retval = FAIL; |
| 3293 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3296 | if (spin->si_ascii && non_ascii > 0) |
| 3297 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3298 | non_ascii); |
| 3299 | hash_clear(&ht); |
| 3300 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3301 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3302 | return retval; |
| 3303 | } |
| 3304 | |
| 3305 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3306 | * Get the list of prefix IDs from the affix list "afflist". |
| 3307 | * Used for PFXPOSTPONE. |
| 3308 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 3309 | * or when out of memory. |
| 3310 | */ |
| 3311 | static char_u * |
| 3312 | get_pfxlist(affile, afflist, blp) |
| 3313 | afffile_T *affile; |
| 3314 | char_u *afflist; |
| 3315 | sblock_T **blp; |
| 3316 | { |
| 3317 | char_u *p; |
| 3318 | int cnt; |
| 3319 | int round; |
| 3320 | char_u *res = NULL; |
| 3321 | char_u key[2]; |
| 3322 | hashitem_T *hi; |
| 3323 | |
| 3324 | key[1] = NUL; |
| 3325 | |
| 3326 | /* round 1: count the number of prefix IDs. |
| 3327 | * round 2: move prefix IDs to "res" */ |
| 3328 | for (round = 1; round <= 2; ++round) |
| 3329 | { |
| 3330 | cnt = 0; |
| 3331 | for (p = afflist; *p != NUL; ++p) |
| 3332 | { |
| 3333 | key[0] = *p; |
| 3334 | hi = hash_find(&affile->af_pref, key); |
| 3335 | if (!HASHITEM_EMPTY(hi)) |
| 3336 | { |
| 3337 | /* This is a prefix ID, use the new number. */ |
| 3338 | if (round == 2) |
| 3339 | res[cnt] = HI2AH(hi)->ah_newID; |
| 3340 | ++cnt; |
| 3341 | } |
| 3342 | } |
| 3343 | if (round == 1 && cnt > 0) |
| 3344 | res = getroom(blp, cnt + 1); |
| 3345 | if (res == NULL) |
| 3346 | break; |
| 3347 | } |
| 3348 | |
| 3349 | if (res != NULL) |
| 3350 | res[cnt] = NUL; |
| 3351 | return res; |
| 3352 | } |
| 3353 | |
| 3354 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3355 | * Apply affixes to a word and store the resulting words. |
| 3356 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 3357 | * prefixes or suffixes. |
| 3358 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 3359 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3360 | * |
| 3361 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3362 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3363 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3364 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3365 | char_u *word; /* basic word start */ |
| 3366 | spellinfo_T *spin; /* spell info */ |
| 3367 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3368 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3369 | hashtab_T *ht; |
| 3370 | hashtab_T *xht; |
| 3371 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3372 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3373 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3374 | { |
| 3375 | int todo; |
| 3376 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3377 | affheader_T *ah; |
| 3378 | affentry_T *ae; |
| 3379 | regmatch_T regmatch; |
| 3380 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3381 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3382 | int i; |
| 3383 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3384 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3385 | todo = ht->ht_used; |
| 3386 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3387 | { |
| 3388 | if (!HASHITEM_EMPTY(hi)) |
| 3389 | { |
| 3390 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3391 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3392 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3393 | /* Check that the affix combines, if required, and that the word |
| 3394 | * supports this affix. */ |
| 3395 | if ((!comb || ah->ah_combine) |
| 3396 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3397 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3398 | /* Loop over all affix entries with this name. */ |
| 3399 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3400 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3401 | /* Check the condition. It's not logical to match case |
| 3402 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3403 | * Myspell. |
| 3404 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 3405 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3406 | regmatch.regprog = ae->ae_prog; |
| 3407 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3408 | if ((xht != NULL || !affile->af_pfxpostpone |
| 3409 | || ae->ae_chop != NULL) |
| 3410 | && (ae->ae_prog == NULL |
| 3411 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3412 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3413 | /* Match. Remove the chop and add the affix. */ |
| 3414 | if (xht == NULL) |
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 | /* prefix: chop/add at the start of the word */ |
| 3417 | if (ae->ae_add == NULL) |
| 3418 | *newword = NUL; |
| 3419 | else |
| 3420 | STRCPY(newword, ae->ae_add); |
| 3421 | p = word; |
| 3422 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3423 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3424 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3425 | #ifdef FEAT_MBYTE |
| 3426 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3427 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3428 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3429 | for ( ; i > 0; --i) |
| 3430 | mb_ptr_adv(p); |
| 3431 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3432 | else |
| 3433 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3434 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3435 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3436 | STRCAT(newword, p); |
| 3437 | } |
| 3438 | else |
| 3439 | { |
| 3440 | /* suffix: chop/add at the end of the word */ |
| 3441 | STRCPY(newword, word); |
| 3442 | if (ae->ae_chop != NULL) |
| 3443 | { |
| 3444 | /* Remove chop string. */ |
| 3445 | p = newword + STRLEN(newword); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3446 | #ifdef FEAT_MBYTE |
| 3447 | if (has_mbyte) |
| 3448 | i = mb_charlen(ae->ae_chop); |
| 3449 | else |
| 3450 | #endif |
| 3451 | i = STRLEN(ae->ae_chop); |
| 3452 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3453 | mb_ptr_back(newword, p); |
| 3454 | *p = NUL; |
| 3455 | } |
| 3456 | if (ae->ae_add != NULL) |
| 3457 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3460 | /* Store the modified word. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3461 | if (store_word(newword, spin, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3462 | flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3463 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3464 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3465 | /* When added a suffix and combining is allowed also |
| 3466 | * try adding prefixes additionally. */ |
| 3467 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3468 | if (store_aff_word(newword, spin, afflist, affile, |
| 3469 | xht, NULL, TRUE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3470 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3477 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3481 | * Read a file with a list of words. |
| 3482 | */ |
| 3483 | static int |
| 3484 | spell_read_wordfile(fname, spin) |
| 3485 | char_u *fname; |
| 3486 | spellinfo_T *spin; |
| 3487 | { |
| 3488 | FILE *fd; |
| 3489 | long lnum = 0; |
| 3490 | char_u rline[MAXLINELEN]; |
| 3491 | char_u *line; |
| 3492 | char_u *pc = NULL; |
| 3493 | int l; |
| 3494 | int retval = OK; |
| 3495 | int did_word = FALSE; |
| 3496 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3497 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3498 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3499 | |
| 3500 | /* |
| 3501 | * Open the file. |
| 3502 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3503 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3504 | if (fd == NULL) |
| 3505 | { |
| 3506 | EMSG2(_(e_notopen), fname); |
| 3507 | return FAIL; |
| 3508 | } |
| 3509 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3510 | if (spin->si_verbose || p_verbose > 2) |
| 3511 | { |
| 3512 | if (!spin->si_verbose) |
| 3513 | verbose_enter(); |
| 3514 | smsg((char_u *)_("Reading word file %s..."), fname); |
| 3515 | out_flush(); |
| 3516 | if (!spin->si_verbose) |
| 3517 | verbose_leave(); |
| 3518 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3519 | |
| 3520 | /* |
| 3521 | * Read all the lines in the file one by one. |
| 3522 | */ |
| 3523 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 3524 | { |
| 3525 | line_breakcheck(); |
| 3526 | ++lnum; |
| 3527 | |
| 3528 | /* Skip comment lines. */ |
| 3529 | if (*rline == '#') |
| 3530 | continue; |
| 3531 | |
| 3532 | /* Remove CR, LF and white space from the end. */ |
| 3533 | l = STRLEN(rline); |
| 3534 | while (l > 0 && rline[l - 1] <= ' ') |
| 3535 | --l; |
| 3536 | if (l == 0) |
| 3537 | continue; /* empty or blank line */ |
| 3538 | rline[l] = NUL; |
| 3539 | |
| 3540 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 3541 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3542 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3543 | if (spin->si_conv.vc_type != CONV_NONE) |
| 3544 | { |
| 3545 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 3546 | if (pc == NULL) |
| 3547 | { |
| 3548 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3549 | fname, lnum, rline); |
| 3550 | continue; |
| 3551 | } |
| 3552 | line = pc; |
| 3553 | } |
| 3554 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3555 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3556 | { |
| 3557 | pc = NULL; |
| 3558 | line = rline; |
| 3559 | } |
| 3560 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3561 | flags = 0; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3562 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3563 | |
| 3564 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3565 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3566 | ++line; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3567 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3568 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3569 | { |
| 3570 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3571 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 3572 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3573 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3574 | smsg((char_u *)_("/encoding= line after word 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 |
| 3577 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3578 | #ifdef FEAT_MBYTE |
| 3579 | char_u *enc; |
| 3580 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3581 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3582 | line += 10; |
| 3583 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3584 | if (enc != NULL && !spin->si_ascii |
| 3585 | && convert_setup(&spin->si_conv, enc, |
| 3586 | p_enc) == FAIL) |
| 3587 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3588 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3589 | vim_free(enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3590 | #else |
| 3591 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3592 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3593 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3594 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3595 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3596 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3597 | if (STRNCMP(line, "regions=", 8) == 0) |
| 3598 | { |
| 3599 | if (spin->si_region_count > 1) |
| 3600 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 3601 | fname, lnum, line); |
| 3602 | else |
| 3603 | { |
| 3604 | line += 8; |
| 3605 | if (STRLEN(line) > 16) |
| 3606 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 3607 | fname, lnum, line); |
| 3608 | else |
| 3609 | { |
| 3610 | spin->si_region_count = STRLEN(line) / 2; |
| 3611 | STRCPY(spin->si_region_name, line); |
| 3612 | } |
| 3613 | } |
| 3614 | continue; |
| 3615 | } |
| 3616 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3617 | if (*line == '=') |
| 3618 | { |
| 3619 | /* keep-case word */ |
| 3620 | flags |= WF_KEEPCAP; |
| 3621 | ++line; |
| 3622 | } |
| 3623 | |
| 3624 | if (*line == '!') |
| 3625 | { |
| 3626 | /* Bad, bad, wicked word. */ |
| 3627 | flags |= WF_BANNED; |
| 3628 | ++line; |
| 3629 | } |
| 3630 | else if (*line == '?') |
| 3631 | { |
| 3632 | /* Rare word. */ |
| 3633 | flags |= WF_RARE; |
| 3634 | ++line; |
| 3635 | } |
| 3636 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3637 | if (VIM_ISDIGIT(*line)) |
| 3638 | { |
| 3639 | /* region number(s) */ |
| 3640 | regionmask = 0; |
| 3641 | while (VIM_ISDIGIT(*line)) |
| 3642 | { |
| 3643 | l = *line - '0'; |
| 3644 | if (l > spin->si_region_count) |
| 3645 | { |
| 3646 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
| 3647 | fname, lnum, line); |
| 3648 | break; |
| 3649 | } |
| 3650 | regionmask |= 1 << (l - 1); |
| 3651 | ++line; |
| 3652 | } |
| 3653 | flags |= WF_REGION; |
| 3654 | } |
| 3655 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3656 | if (flags == 0) |
| 3657 | { |
| 3658 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3659 | fname, lnum, line); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3660 | continue; |
| 3661 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3665 | if (spin->si_ascii && has_non_ascii(line)) |
| 3666 | { |
| 3667 | ++non_ascii; |
| 3668 | continue; |
| 3669 | } |
| 3670 | |
| 3671 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3672 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3673 | { |
| 3674 | retval = FAIL; |
| 3675 | break; |
| 3676 | } |
| 3677 | did_word = TRUE; |
| 3678 | } |
| 3679 | |
| 3680 | vim_free(pc); |
| 3681 | fclose(fd); |
| 3682 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3683 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 3684 | { |
| 3685 | if (p_verbose > 2) |
| 3686 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3687 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3688 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3689 | if (p_verbose > 2) |
| 3690 | verbose_leave(); |
| 3691 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3692 | return retval; |
| 3693 | } |
| 3694 | |
| 3695 | /* |
| 3696 | * Get part of an sblock_T, "len" bytes long. |
| 3697 | * This avoids calling free() for every little struct we use. |
| 3698 | * The memory is cleared to all zeros. |
| 3699 | * Returns NULL when out of memory. |
| 3700 | */ |
| 3701 | static void * |
| 3702 | getroom(blp, len) |
| 3703 | sblock_T **blp; |
| 3704 | size_t len; /* length needed */ |
| 3705 | { |
| 3706 | char_u *p; |
| 3707 | sblock_T *bl = *blp; |
| 3708 | |
| 3709 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 3710 | { |
| 3711 | /* Allocate a block of memory. This is not freed until much later. */ |
| 3712 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 3713 | if (bl == NULL) |
| 3714 | return NULL; |
| 3715 | bl->sb_next = *blp; |
| 3716 | *blp = bl; |
| 3717 | bl->sb_used = 0; |
| 3718 | } |
| 3719 | |
| 3720 | p = bl->sb_data + bl->sb_used; |
| 3721 | bl->sb_used += len; |
| 3722 | |
| 3723 | return p; |
| 3724 | } |
| 3725 | |
| 3726 | /* |
| 3727 | * Make a copy of a string into memory allocated with getroom(). |
| 3728 | */ |
| 3729 | static char_u * |
| 3730 | getroom_save(blp, s) |
| 3731 | sblock_T **blp; |
| 3732 | char_u *s; |
| 3733 | { |
| 3734 | char_u *sc; |
| 3735 | |
| 3736 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 3737 | if (sc != NULL) |
| 3738 | STRCPY(sc, s); |
| 3739 | return sc; |
| 3740 | } |
| 3741 | |
| 3742 | |
| 3743 | /* |
| 3744 | * Free the list of allocated sblock_T. |
| 3745 | */ |
| 3746 | static void |
| 3747 | free_blocks(bl) |
| 3748 | sblock_T *bl; |
| 3749 | { |
| 3750 | sblock_T *next; |
| 3751 | |
| 3752 | while (bl != NULL) |
| 3753 | { |
| 3754 | next = bl->sb_next; |
| 3755 | vim_free(bl); |
| 3756 | bl = next; |
| 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | /* |
| 3761 | * Allocate the root of a word tree. |
| 3762 | */ |
| 3763 | static wordnode_T * |
| 3764 | wordtree_alloc(blp) |
| 3765 | sblock_T **blp; |
| 3766 | { |
| 3767 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3768 | } |
| 3769 | |
| 3770 | /* |
| 3771 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3772 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 3773 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3774 | * 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] | 3775 | * When "pfxlist" is not NULL store the word for each prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3776 | */ |
| 3777 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3778 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3779 | char_u *word; |
| 3780 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3781 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3782 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3783 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3784 | { |
| 3785 | int len = STRLEN(word); |
| 3786 | int ct = captype(word, word + len); |
| 3787 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3788 | int res = OK; |
| 3789 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3790 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3791 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3792 | for (p = pfxlist; res == OK; ++p) |
| 3793 | { |
| 3794 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 3795 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 3796 | if (p == NULL || *p == NUL) |
| 3797 | break; |
| 3798 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3799 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3800 | |
| 3801 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3802 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3803 | for (p = pfxlist; res == OK; ++p) |
| 3804 | { |
| 3805 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 3806 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 3807 | if (p == NULL || *p == NUL) |
| 3808 | break; |
| 3809 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3810 | ++spin->si_keepwcount; |
| 3811 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3812 | return res; |
| 3813 | } |
| 3814 | |
| 3815 | /* |
| 3816 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3817 | * When "flags" is -1 we are adding to the prefix tree where flags don't |
| 3818 | * matter and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3819 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3820 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3821 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3822 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3823 | char_u *word; |
| 3824 | wordnode_T *root; |
| 3825 | int flags; |
| 3826 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3827 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3828 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3829 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3830 | wordnode_T *node = root; |
| 3831 | wordnode_T *np; |
| 3832 | wordnode_T **prev = NULL; |
| 3833 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3834 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3835 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 3836 | for (i = 0; ; ++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 | /* Look for the sibling that has the same character. They are sorted |
| 3839 | * 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] | 3840 | * higher byte value. For zero bytes (end of word) the sorting is |
| 3841 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3842 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3843 | while (node != NULL |
| 3844 | && (node->wn_byte < word[i] |
| 3845 | || (node->wn_byte == NUL |
| 3846 | && (flags < 0 |
| 3847 | ? node->wn_prefixID < prefixID |
| 3848 | : node->wn_flags < (flags & 0xff) |
| 3849 | || (node->wn_flags == (flags & 0xff) |
| 3850 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3851 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3852 | prev = &node->wn_sibling; |
| 3853 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3854 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3855 | if (node == NULL |
| 3856 | || node->wn_byte != word[i] |
| 3857 | || (word[i] == NUL |
| 3858 | && (flags < 0 |
| 3859 | || node->wn_flags != (flags & 0xff) |
| 3860 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3861 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3862 | /* Allocate a new node. */ |
| 3863 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3864 | if (np == NULL) |
| 3865 | return FAIL; |
| 3866 | np->wn_byte = word[i]; |
| 3867 | *prev = np; |
| 3868 | np->wn_sibling = node; |
| 3869 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3870 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3871 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3872 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3873 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3874 | node->wn_flags = flags; |
| 3875 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3876 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3877 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3878 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3879 | prev = &node->wn_child; |
| 3880 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3887 | * Compress a tree: find tails that are identical and can be shared. |
| 3888 | */ |
| 3889 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3890 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3891 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3892 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3893 | { |
| 3894 | hashtab_T ht; |
| 3895 | int n; |
| 3896 | int tot = 0; |
| 3897 | |
| 3898 | if (root != NULL) |
| 3899 | { |
| 3900 | hash_init(&ht); |
| 3901 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3902 | if (spin->si_verbose || p_verbose > 2) |
| 3903 | { |
| 3904 | if (!spin->si_verbose) |
| 3905 | verbose_enter(); |
| 3906 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3907 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3908 | if (p_verbose > 2) |
| 3909 | verbose_leave(); |
| 3910 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3911 | hash_clear(&ht); |
| 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | /* |
| 3916 | * Compress a node, its siblings and its children, depth first. |
| 3917 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3918 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3919 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3920 | node_compress(node, ht, tot) |
| 3921 | wordnode_T *node; |
| 3922 | hashtab_T *ht; |
| 3923 | int *tot; /* total count of nodes before compressing, |
| 3924 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3925 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3926 | wordnode_T *np; |
| 3927 | wordnode_T *tp; |
| 3928 | wordnode_T *child; |
| 3929 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3930 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3931 | int len = 0; |
| 3932 | unsigned nr, n; |
| 3933 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3934 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3935 | /* |
| 3936 | * Go through the list of siblings. Compress each child and then try |
| 3937 | * finding an identical child to replace it. |
| 3938 | * Note that with "child" we mean not just the node that is pointed to, |
| 3939 | * but the whole list of siblings, of which the node is the first. |
| 3940 | */ |
| 3941 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3942 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3943 | ++len; |
| 3944 | if ((child = np->wn_child) != NULL) |
| 3945 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3946 | /* Compress the child. This fills hashkey. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3947 | compressed += node_compress(child, ht, tot); |
| 3948 | |
| 3949 | /* Try to find an identical child. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3950 | hash = hash_hash(child->wn_u1.hashkey); |
| 3951 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3952 | tp = NULL; |
| 3953 | if (!HASHITEM_EMPTY(hi)) |
| 3954 | { |
| 3955 | /* There are children with an identical hash value. Now check |
| 3956 | * if there is one that is really identical. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3957 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3958 | if (node_equal(child, tp)) |
| 3959 | { |
| 3960 | /* Found one! Now use that child in place of the |
| 3961 | * current one. This means the current child is |
| 3962 | * dropped from the tree. */ |
| 3963 | np->wn_child = tp; |
| 3964 | ++compressed; |
| 3965 | break; |
| 3966 | } |
| 3967 | if (tp == NULL) |
| 3968 | { |
| 3969 | /* No other child with this hash value equals the child of |
| 3970 | * the node, add it to the linked list after the first |
| 3971 | * item. */ |
| 3972 | tp = HI2WN(hi); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3973 | child->wn_u2.next = tp->wn_u2.next; |
| 3974 | tp->wn_u2.next = child; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3975 | } |
| 3976 | } |
| 3977 | else |
| 3978 | /* No other child has this hash value, add it to the |
| 3979 | * hashtable. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3980 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3981 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3982 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3983 | *tot += len; |
| 3984 | |
| 3985 | /* |
| 3986 | * Make a hash key for the node and its siblings, so that we can quickly |
| 3987 | * find a lookalike node. This must be done after compressing the sibling |
| 3988 | * list, otherwise the hash key would become invalid by the compression. |
| 3989 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3990 | node->wn_u1.hashkey[0] = len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3991 | nr = 0; |
| 3992 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3993 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3994 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3995 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 3996 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3997 | else |
| 3998 | /* byte node: use the byte value and the child pointer */ |
| 3999 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 4000 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4001 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4002 | |
| 4003 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 4004 | n = nr & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4005 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4006 | n = (nr >> 8) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4007 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4008 | n = (nr >> 16) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4009 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4010 | n = (nr >> 24) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4011 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 4012 | node->wn_u1.hashkey[5] = NUL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4013 | |
| 4014 | return compressed; |
| 4015 | } |
| 4016 | |
| 4017 | /* |
| 4018 | * Return TRUE when two nodes have identical siblings and children. |
| 4019 | */ |
| 4020 | static int |
| 4021 | node_equal(n1, n2) |
| 4022 | wordnode_T *n1; |
| 4023 | wordnode_T *n2; |
| 4024 | { |
| 4025 | wordnode_T *p1; |
| 4026 | wordnode_T *p2; |
| 4027 | |
| 4028 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 4029 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 4030 | if (p1->wn_byte != p2->wn_byte |
| 4031 | || (p1->wn_byte == NUL |
| 4032 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4033 | || p1->wn_region != p2->wn_region |
| 4034 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4035 | : (p1->wn_child != p2->wn_child))) |
| 4036 | break; |
| 4037 | |
| 4038 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | /* |
| 4042 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 4043 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4044 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4045 | put_bytes(fd, nr, len) |
| 4046 | FILE *fd; |
| 4047 | long_u nr; |
| 4048 | int len; |
| 4049 | { |
| 4050 | int i; |
| 4051 | |
| 4052 | for (i = len - 1; i >= 0; --i) |
| 4053 | putc((int)(nr >> (i * 8)), fd); |
| 4054 | } |
| 4055 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4056 | static int |
| 4057 | #ifdef __BORLANDC__ |
| 4058 | _RTLENTRYF |
| 4059 | #endif |
| 4060 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 4061 | |
| 4062 | /* |
| 4063 | * Function given to qsort() to sort the REP items on "from" string. |
| 4064 | */ |
| 4065 | static int |
| 4066 | #ifdef __BORLANDC__ |
| 4067 | _RTLENTRYF |
| 4068 | #endif |
| 4069 | rep_compare(s1, s2) |
| 4070 | const void *s1; |
| 4071 | const void *s2; |
| 4072 | { |
| 4073 | fromto_T *p1 = (fromto_T *)s1; |
| 4074 | fromto_T *p2 = (fromto_T *)s2; |
| 4075 | |
| 4076 | return STRCMP(p1->ft_from, p2->ft_from); |
| 4077 | } |
| 4078 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4079 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4080 | * Write the Vim spell file "fname". |
| 4081 | */ |
| 4082 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4083 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4084 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4085 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4086 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4087 | FILE *fd; |
| 4088 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4089 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4090 | wordnode_T *tree; |
| 4091 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4092 | int i; |
| 4093 | int l; |
| 4094 | garray_T *gap; |
| 4095 | fromto_T *ftp; |
| 4096 | char_u *p; |
| 4097 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4098 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4099 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4100 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4101 | { |
| 4102 | EMSG2(_(e_notopen), fname); |
| 4103 | return; |
| 4104 | } |
| 4105 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4106 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4107 | * <charflagslen> <charflags> |
| 4108 | * <fcharslen> <fchars> |
| 4109 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4110 | |
| 4111 | /* <fileID> */ |
| 4112 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 4113 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4114 | |
| 4115 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4116 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4117 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4118 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 4119 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 4120 | (size_t)1, fd); |
| 4121 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4122 | } |
| 4123 | else |
| 4124 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4125 | putc(0, fd); |
| 4126 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4129 | /* |
| 4130 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4131 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 4132 | * 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] | 4133 | * 'encoding'. |
| 4134 | * Also skip this for an .add.spl file, the main spell file must contain |
| 4135 | * the table (avoids that it conflicts). File is shorter too. |
| 4136 | */ |
| 4137 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4138 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4139 | putc(0, fd); |
| 4140 | putc(0, fd); |
| 4141 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4142 | } |
| 4143 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4144 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4145 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4146 | /* Write the prefix conditions. */ |
| 4147 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 4148 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4149 | /* Sort the REP items. */ |
| 4150 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 4151 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4152 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4153 | /* <SUGGEST> : <repcount> <rep> ... |
| 4154 | * <salflags> <salcount> <sal> ... |
| 4155 | * <maplen> <mapstr> */ |
| 4156 | for (round = 1; round <= 2; ++round) |
| 4157 | { |
| 4158 | if (round == 1) |
| 4159 | gap = &spin->si_rep; |
| 4160 | else |
| 4161 | { |
| 4162 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4163 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4164 | i = 0; |
| 4165 | if (spin->si_followup) |
| 4166 | i |= SAL_F0LLOWUP; |
| 4167 | if (spin->si_collapse) |
| 4168 | i |= SAL_COLLAPSE; |
| 4169 | if (spin->si_rem_accents) |
| 4170 | i |= SAL_REM_ACCENTS; |
| 4171 | putc(i, fd); /* <salflags> */ |
| 4172 | } |
| 4173 | |
| 4174 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 4175 | for (i = 0; i < gap->ga_len; ++i) |
| 4176 | { |
| 4177 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 4178 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 4179 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 4180 | for (rr = 1; rr <= 2; ++rr) |
| 4181 | { |
| 4182 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 4183 | l = STRLEN(p); |
| 4184 | putc(l, fd); |
| 4185 | fwrite(p, l, (size_t)1, fd); |
| 4186 | } |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 4191 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 4192 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 4193 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4194 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4195 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4196 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4197 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4198 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4199 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4200 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4201 | if (round == 1) |
| 4202 | tree = spin->si_foldroot; |
| 4203 | else if (round == 2) |
| 4204 | tree = spin->si_keeproot; |
| 4205 | else |
| 4206 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4207 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4208 | /* Clear the index and wnode fields in the tree. */ |
| 4209 | clear_node(tree); |
| 4210 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4211 | /* Count the number of nodes. Needed to be able to allocate the |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4212 | * memory when reading the nodes. Also fills in index for shared |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4213 | * nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4214 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4215 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4216 | /* number of nodes in 4 bytes */ |
| 4217 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4218 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4219 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4220 | /* Write the nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4221 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4224 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4228 | * Clear the index and wnode fields of "node", it siblings and its |
| 4229 | * children. This is needed because they are a union with other items to save |
| 4230 | * space. |
| 4231 | */ |
| 4232 | static void |
| 4233 | clear_node(node) |
| 4234 | wordnode_T *node; |
| 4235 | { |
| 4236 | wordnode_T *np; |
| 4237 | |
| 4238 | if (node != NULL) |
| 4239 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4240 | { |
| 4241 | np->wn_u1.index = 0; |
| 4242 | np->wn_u2.wnode = NULL; |
| 4243 | |
| 4244 | if (np->wn_byte != NUL) |
| 4245 | clear_node(np->wn_child); |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | |
| 4250 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4251 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4252 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4253 | * This first writes the list of possible bytes (siblings). Then for each |
| 4254 | * byte recursively write the children. |
| 4255 | * |
| 4256 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 4257 | * are made about the indexes (so that we don't have to write them in the |
| 4258 | * file). |
| 4259 | * |
| 4260 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4261 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4262 | static int |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4263 | put_node(fd, node, index, regionmask, prefixtree) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4264 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4265 | wordnode_T *node; |
| 4266 | int index; |
| 4267 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4268 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4269 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4270 | int newindex = index; |
| 4271 | int siblingcount = 0; |
| 4272 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4273 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4274 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4275 | /* If "node" is zero the tree is empty. */ |
| 4276 | if (node == NULL) |
| 4277 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4278 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4279 | /* Store the index where this node is written. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4280 | node->wn_u1.index = index; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4281 | |
| 4282 | /* Count the number of siblings. */ |
| 4283 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4284 | ++siblingcount; |
| 4285 | |
| 4286 | /* Write the sibling count. */ |
| 4287 | if (fd != NULL) |
| 4288 | putc(siblingcount, fd); /* <siblingcount> */ |
| 4289 | |
| 4290 | /* Write each sibling byte and optionally extra info. */ |
| 4291 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4292 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4293 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4294 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4295 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4296 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4297 | /* For a NUL byte (end of word) write the flags etc. */ |
| 4298 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4299 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4300 | /* In PREFIXTREE write the required prefixID and the |
| 4301 | * associated condition nr (stored in wn_region). */ |
| 4302 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4303 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4304 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4305 | } |
| 4306 | else |
| 4307 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4308 | /* For word trees we write the flag/region items. */ |
| 4309 | flags = np->wn_flags; |
| 4310 | if (regionmask != 0 && np->wn_region != regionmask) |
| 4311 | flags |= WF_REGION; |
| 4312 | if (np->wn_prefixID != 0) |
| 4313 | flags |= WF_PFX; |
| 4314 | if (flags == 0) |
| 4315 | { |
| 4316 | /* word without flags or region */ |
| 4317 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 4318 | } |
| 4319 | else |
| 4320 | { |
| 4321 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4322 | putc(flags, fd); /* <flags> */ |
| 4323 | if (flags & WF_REGION) |
| 4324 | putc(np->wn_region, fd); /* <region> */ |
| 4325 | if (flags & WF_PFX) |
| 4326 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4327 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4328 | } |
| 4329 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4330 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4331 | else |
| 4332 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4333 | if (np->wn_child->wn_u1.index != 0 |
| 4334 | && np->wn_child->wn_u2.wnode != node) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4335 | { |
| 4336 | /* The child is written elsewhere, write the reference. */ |
| 4337 | if (fd != NULL) |
| 4338 | { |
| 4339 | putc(BY_INDEX, fd); /* <byte> */ |
| 4340 | /* <nodeidx> */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4341 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4342 | } |
| 4343 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4344 | else if (np->wn_child->wn_u2.wnode == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4345 | /* We will write the child below and give it an index. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4346 | np->wn_child->wn_u2.wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4347 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4348 | if (fd != NULL) |
| 4349 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 4350 | { |
| 4351 | EMSG(_(e_write)); |
| 4352 | return 0; |
| 4353 | } |
| 4354 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4355 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4356 | |
| 4357 | /* Space used in the array when reading: one for each sibling and one for |
| 4358 | * the count. */ |
| 4359 | newindex += siblingcount + 1; |
| 4360 | |
| 4361 | /* Recursively dump the children of each sibling. */ |
| 4362 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4363 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 4364 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4365 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4366 | |
| 4367 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | |
| 4371 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4372 | * ":mkspell [-ascii] outfile infile ..." |
| 4373 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4374 | */ |
| 4375 | void |
| 4376 | ex_mkspell(eap) |
| 4377 | exarg_T *eap; |
| 4378 | { |
| 4379 | int fcount; |
| 4380 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4381 | char_u *arg = eap->arg; |
| 4382 | int ascii = FALSE; |
| 4383 | |
| 4384 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 4385 | { |
| 4386 | ascii = TRUE; |
| 4387 | arg = skipwhite(arg + 6); |
| 4388 | } |
| 4389 | |
| 4390 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 4391 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 4392 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4393 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4394 | FreeWild(fcount, fnames); |
| 4395 | } |
| 4396 | } |
| 4397 | |
| 4398 | /* |
| 4399 | * Create a Vim spell file from one or more word lists. |
| 4400 | * "fnames[0]" is the output file name. |
| 4401 | * "fnames[fcount - 1]" is the last input file name. |
| 4402 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 4403 | * and ".spl" is appended to make the output file name. |
| 4404 | */ |
| 4405 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4406 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4407 | int fcount; |
| 4408 | char_u **fnames; |
| 4409 | int ascii; /* -ascii argument given */ |
| 4410 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4411 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4412 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4413 | char_u fname[MAXPATHL]; |
| 4414 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4415 | char_u **innames; |
| 4416 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4417 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4418 | int i; |
| 4419 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4420 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4421 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4422 | spellinfo_T spin; |
| 4423 | |
| 4424 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4425 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4426 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4427 | spin.si_followup = TRUE; |
| 4428 | spin.si_rem_accents = TRUE; |
| 4429 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 4430 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 4431 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4432 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4433 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4434 | /* default: fnames[0] is output file, following are input files */ |
| 4435 | innames = &fnames[1]; |
| 4436 | incount = fcount - 1; |
| 4437 | |
| 4438 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4439 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4440 | len = STRLEN(fnames[0]); |
| 4441 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 4442 | { |
| 4443 | /* For ":mkspell path/en.latin1.add" output file is |
| 4444 | * "path/en.latin1.add.spl". */ |
| 4445 | innames = &fnames[0]; |
| 4446 | incount = 1; |
| 4447 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 4448 | } |
| 4449 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 4450 | { |
| 4451 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4452 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4453 | } |
| 4454 | else |
| 4455 | /* Name should be language, make the file name from it. */ |
| 4456 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4457 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4458 | |
| 4459 | /* Check for .ascii.spl. */ |
| 4460 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 4461 | spin.si_ascii = TRUE; |
| 4462 | |
| 4463 | /* Check for .add.spl. */ |
| 4464 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 4465 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4468 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4469 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4470 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 4471 | EMSG(_("E751: Output file name must not have region name")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4472 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4473 | EMSG(_("E754: Only up to 8 regions supported")); |
| 4474 | else |
| 4475 | { |
| 4476 | /* Check for overwriting before doing things that may take a lot of |
| 4477 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4478 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4479 | { |
| 4480 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4481 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4482 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4483 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4484 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4485 | EMSG2(_(e_isadir2), wfname); |
| 4486 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * Init the aff and dic pointers. |
| 4491 | * Get the region names if there are more than 2 arguments. |
| 4492 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4493 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4494 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4495 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4496 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4497 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4498 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4499 | len = STRLEN(innames[i]); |
| 4500 | if (STRLEN(gettail(innames[i])) < 5 |
| 4501 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4502 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4503 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 4504 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4505 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4506 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 4507 | spin.si_region_name[i * 2 + 1] = |
| 4508 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4509 | } |
| 4510 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4511 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4512 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4513 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 4514 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4515 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 4516 | if (spin.si_foldroot == NULL |
| 4517 | || spin.si_keeproot == NULL |
| 4518 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4519 | { |
| 4520 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4521 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4524 | /* When not producing a .add.spl file clear the character table when |
| 4525 | * we encounter one in the .aff file. This means we dump the current |
| 4526 | * one in the .spl file if the .aff file doesn't define one. That's |
| 4527 | * better than guessing the contents, the table will match a |
| 4528 | * previously loaded spell file. */ |
| 4529 | if (!spin.si_add) |
| 4530 | spin.si_clear_chartab = TRUE; |
| 4531 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4532 | /* |
| 4533 | * Read all the .aff and .dic files. |
| 4534 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4535 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4536 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4537 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4538 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4539 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4540 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4541 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4542 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4543 | if (mch_stat((char *)fname, &st) >= 0) |
| 4544 | { |
| 4545 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 4546 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4547 | afile[i] = spell_read_aff(fname, &spin); |
| 4548 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4549 | error = TRUE; |
| 4550 | else |
| 4551 | { |
| 4552 | /* Read the .dic file and store the words in the trees. */ |
| 4553 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4554 | innames[i]); |
| 4555 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4556 | error = TRUE; |
| 4557 | } |
| 4558 | } |
| 4559 | else |
| 4560 | { |
| 4561 | /* No .aff file, try reading the file as a word list. Store |
| 4562 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4563 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4564 | error = TRUE; |
| 4565 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4566 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4567 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4568 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4569 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4570 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4573 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4574 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4575 | /* |
| 4576 | * Remove the dummy NUL from the start of the tree root. |
| 4577 | */ |
| 4578 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 4579 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4580 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4581 | |
| 4582 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4583 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4584 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4585 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4586 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4587 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4588 | verbose_enter(); |
| 4589 | MSG(_("Compressing word tree...")); |
| 4590 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4591 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4592 | verbose_leave(); |
| 4593 | } |
| 4594 | wordtree_compress(spin.si_foldroot, &spin); |
| 4595 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4596 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4599 | if (!error) |
| 4600 | { |
| 4601 | /* |
| 4602 | * Write the info in the spell file. |
| 4603 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4604 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4605 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4606 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4607 | verbose_enter(); |
| 4608 | smsg((char_u *)_("Writing spell file %s..."), wfname); |
| 4609 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4610 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4611 | verbose_leave(); |
| 4612 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4613 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4614 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4615 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4616 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4617 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4618 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4619 | verbose_enter(); |
| 4620 | MSG(_("Done!")); |
| 4621 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4622 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4623 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4624 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4625 | verbose_leave(); |
| 4626 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4627 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4628 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4629 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4633 | ga_clear(&spin.si_rep); |
| 4634 | ga_clear(&spin.si_sal); |
| 4635 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4636 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4637 | |
| 4638 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4639 | for (i = 0; i < incount; ++i) |
| 4640 | if (afile[i] != NULL) |
| 4641 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4642 | |
| 4643 | /* Free all the bits and pieces at once. */ |
| 4644 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4645 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4648 | |
| 4649 | /* |
| 4650 | * ":spellgood {word}" |
| 4651 | * ":spellwrong {word}" |
| 4652 | */ |
| 4653 | void |
| 4654 | ex_spell(eap) |
| 4655 | exarg_T *eap; |
| 4656 | { |
| 4657 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong); |
| 4658 | } |
| 4659 | |
| 4660 | /* |
| 4661 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 4662 | */ |
| 4663 | void |
| 4664 | spell_add_word(word, len, bad) |
| 4665 | char_u *word; |
| 4666 | int len; |
| 4667 | int bad; |
| 4668 | { |
| 4669 | FILE *fd; |
| 4670 | buf_T *buf; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4671 | int new_spf = FALSE; |
| 4672 | struct stat st; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4673 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4674 | /* If 'spellfile' isn't set figure out a good default value. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4675 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4676 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4677 | init_spellfile(); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4678 | new_spf = TRUE; |
| 4679 | } |
| 4680 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4681 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4682 | EMSG(_("E764: 'spellfile' is not set")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4683 | else |
| 4684 | { |
| 4685 | /* Check that the user isn't editing the .add file somewhere. */ |
| 4686 | buf = buflist_findname_exp(curbuf->b_p_spf); |
| 4687 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 4688 | buf = NULL; |
| 4689 | if (buf != NULL && bufIsChanged(buf)) |
| 4690 | EMSG(_(e_bufloaded)); |
| 4691 | else |
| 4692 | { |
| 4693 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4694 | if (fd == NULL && new_spf) |
| 4695 | { |
| 4696 | /* We just initialized the 'spellfile' option and can't open |
| 4697 | * the file. We may need to create the "spell" directory |
| 4698 | * first. We already checked the runtime directory is |
| 4699 | * writable in init_spellfile(). */ |
| 4700 | STRCPY(NameBuff, curbuf->b_p_spf); |
| 4701 | *gettail_sep(NameBuff) = NUL; |
| 4702 | if (mch_stat((char *)NameBuff, &st) < 0) |
| 4703 | { |
| 4704 | /* The directory doesn't exist. Try creating it and |
| 4705 | * opening the file again. */ |
| 4706 | vim_mkdir(NameBuff, 0755); |
| 4707 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
| 4708 | } |
| 4709 | } |
| 4710 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4711 | if (fd == NULL) |
| 4712 | EMSG2(_(e_notopen), curbuf->b_p_spf); |
| 4713 | else |
| 4714 | { |
| 4715 | if (bad) |
| 4716 | fprintf(fd, "/!%.*s\n", len, word); |
| 4717 | else |
| 4718 | fprintf(fd, "%.*s\n", len, word); |
| 4719 | fclose(fd); |
| 4720 | |
| 4721 | /* Update the .add.spl file. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4722 | mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4723 | |
| 4724 | /* If the .add file is edited somewhere, reload it. */ |
| 4725 | if (buf != NULL) |
| 4726 | buf_reload(buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4727 | |
| 4728 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4729 | } |
| 4730 | } |
| 4731 | } |
| 4732 | } |
| 4733 | |
| 4734 | /* |
| 4735 | * Initialize 'spellfile' for the current buffer. |
| 4736 | */ |
| 4737 | static void |
| 4738 | init_spellfile() |
| 4739 | { |
| 4740 | char_u buf[MAXPATHL]; |
| 4741 | int l; |
| 4742 | slang_T *sl; |
| 4743 | char_u *rtp; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4744 | char_u *lend; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4745 | |
| 4746 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 4747 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4748 | /* Find the end of the language name. Exclude the region. */ |
| 4749 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 4750 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| 4751 | ; |
| 4752 | |
| 4753 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 4754 | * are allowed to write. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4755 | rtp = p_rtp; |
| 4756 | while (*rtp != NUL) |
| 4757 | { |
| 4758 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 4759 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 4760 | if (filewritable(buf) == 2) |
| 4761 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4762 | /* Use the first language name from 'spelllang' and the |
| 4763 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4764 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 4765 | l = STRLEN(buf); |
| 4766 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4767 | "/spell/%.*s.%s.add", |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4768 | (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4769 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 4770 | ? (char_u *)"ascii" : spell_enc()); |
| 4771 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 4772 | break; |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4777 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4778 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4779 | /* |
| 4780 | * Init the chartab used for spelling for ASCII. |
| 4781 | * EBCDIC is not supported! |
| 4782 | */ |
| 4783 | static void |
| 4784 | clear_spell_chartab(sp) |
| 4785 | spelltab_T *sp; |
| 4786 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4787 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4788 | |
| 4789 | /* Init everything to FALSE. */ |
| 4790 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 4791 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 4792 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4793 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4794 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4795 | sp->st_upper[i] = i; |
| 4796 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4797 | |
| 4798 | /* We include digits. A word shouldn't start with a digit, but handling |
| 4799 | * that is done separately. */ |
| 4800 | for (i = '0'; i <= '9'; ++i) |
| 4801 | sp->st_isw[i] = TRUE; |
| 4802 | for (i = 'A'; i <= 'Z'; ++i) |
| 4803 | { |
| 4804 | sp->st_isw[i] = TRUE; |
| 4805 | sp->st_isu[i] = TRUE; |
| 4806 | sp->st_fold[i] = i + 0x20; |
| 4807 | } |
| 4808 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4809 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4810 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4811 | sp->st_upper[i] = i - 0x20; |
| 4812 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
| 4815 | /* |
| 4816 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 4817 | * Called once while starting up and when 'encoding' changes. |
| 4818 | * The default is to use isalpha(), but the spell file should define the word |
| 4819 | * characters to make it possible that 'encoding' differs from the current |
| 4820 | * locale. |
| 4821 | */ |
| 4822 | void |
| 4823 | init_spell_chartab() |
| 4824 | { |
| 4825 | int i; |
| 4826 | |
| 4827 | did_set_spelltab = FALSE; |
| 4828 | clear_spell_chartab(&spelltab); |
| 4829 | |
| 4830 | #ifdef FEAT_MBYTE |
| 4831 | if (enc_dbcs) |
| 4832 | { |
| 4833 | /* DBCS: assume double-wide characters are word characters. */ |
| 4834 | for (i = 128; i <= 255; ++i) |
| 4835 | if (MB_BYTE2LEN(i) == 2) |
| 4836 | spelltab.st_isw[i] = TRUE; |
| 4837 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4838 | else if (enc_utf8) |
| 4839 | { |
| 4840 | for (i = 128; i < 256; ++i) |
| 4841 | { |
| 4842 | spelltab.st_isu[i] = utf_isupper(i); |
| 4843 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 4844 | spelltab.st_fold[i] = utf_fold(i); |
| 4845 | spelltab.st_upper[i] = utf_toupper(i); |
| 4846 | } |
| 4847 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4848 | else |
| 4849 | #endif |
| 4850 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4851 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4852 | for (i = 128; i < 256; ++i) |
| 4853 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4854 | if (MB_ISUPPER(i)) |
| 4855 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4856 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4857 | spelltab.st_isu[i] = TRUE; |
| 4858 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 4859 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4860 | else if (MB_ISLOWER(i)) |
| 4861 | { |
| 4862 | spelltab.st_isw[i] = TRUE; |
| 4863 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 4864 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4869 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 4870 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 4871 | |
| 4872 | /* |
| 4873 | * Set the spell character tables from strings in the affix file. |
| 4874 | */ |
| 4875 | static int |
| 4876 | set_spell_chartab(fol, low, upp) |
| 4877 | char_u *fol; |
| 4878 | char_u *low; |
| 4879 | char_u *upp; |
| 4880 | { |
| 4881 | /* We build the new tables here first, so that we can compare with the |
| 4882 | * previous one. */ |
| 4883 | spelltab_T new_st; |
| 4884 | char_u *pf = fol, *pl = low, *pu = upp; |
| 4885 | int f, l, u; |
| 4886 | |
| 4887 | clear_spell_chartab(&new_st); |
| 4888 | |
| 4889 | while (*pf != NUL) |
| 4890 | { |
| 4891 | if (*pl == NUL || *pu == NUL) |
| 4892 | { |
| 4893 | EMSG(_(e_affform)); |
| 4894 | return FAIL; |
| 4895 | } |
| 4896 | #ifdef FEAT_MBYTE |
| 4897 | f = mb_ptr2char_adv(&pf); |
| 4898 | l = mb_ptr2char_adv(&pl); |
| 4899 | u = mb_ptr2char_adv(&pu); |
| 4900 | #else |
| 4901 | f = *pf++; |
| 4902 | l = *pl++; |
| 4903 | u = *pu++; |
| 4904 | #endif |
| 4905 | /* Every character that appears is a word character. */ |
| 4906 | if (f < 256) |
| 4907 | new_st.st_isw[f] = TRUE; |
| 4908 | if (l < 256) |
| 4909 | new_st.st_isw[l] = TRUE; |
| 4910 | if (u < 256) |
| 4911 | new_st.st_isw[u] = TRUE; |
| 4912 | |
| 4913 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 4914 | * case-folding */ |
| 4915 | if (l < 256 && l != f) |
| 4916 | { |
| 4917 | if (f >= 256) |
| 4918 | { |
| 4919 | EMSG(_(e_affrange)); |
| 4920 | return FAIL; |
| 4921 | } |
| 4922 | new_st.st_fold[l] = f; |
| 4923 | } |
| 4924 | |
| 4925 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4926 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 4927 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4928 | if (u < 256 && u != f) |
| 4929 | { |
| 4930 | if (f >= 256) |
| 4931 | { |
| 4932 | EMSG(_(e_affrange)); |
| 4933 | return FAIL; |
| 4934 | } |
| 4935 | new_st.st_fold[u] = f; |
| 4936 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4937 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | |
| 4941 | if (*pl != NUL || *pu != NUL) |
| 4942 | { |
| 4943 | EMSG(_(e_affform)); |
| 4944 | return FAIL; |
| 4945 | } |
| 4946 | |
| 4947 | return set_spell_finish(&new_st); |
| 4948 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4949 | |
| 4950 | /* |
| 4951 | * Set the spell character tables from strings in the .spl file. |
| 4952 | */ |
| 4953 | static int |
| 4954 | set_spell_charflags(flags, cnt, upp) |
| 4955 | char_u *flags; |
| 4956 | int cnt; |
| 4957 | char_u *upp; |
| 4958 | { |
| 4959 | /* We build the new tables here first, so that we can compare with the |
| 4960 | * previous one. */ |
| 4961 | spelltab_T new_st; |
| 4962 | int i; |
| 4963 | char_u *p = upp; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4964 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4965 | |
| 4966 | clear_spell_chartab(&new_st); |
| 4967 | |
| 4968 | for (i = 0; i < cnt; ++i) |
| 4969 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4970 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 4971 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4972 | |
| 4973 | if (*p == NUL) |
| 4974 | return FAIL; |
| 4975 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4976 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4977 | #else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4978 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4979 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4980 | new_st.st_fold[i + 128] = c; |
| 4981 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 4982 | new_st.st_upper[c] = i + 128; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | return set_spell_finish(&new_st); |
| 4986 | } |
| 4987 | |
| 4988 | static int |
| 4989 | set_spell_finish(new_st) |
| 4990 | spelltab_T *new_st; |
| 4991 | { |
| 4992 | int i; |
| 4993 | |
| 4994 | if (did_set_spelltab) |
| 4995 | { |
| 4996 | /* check that it's the same table */ |
| 4997 | for (i = 0; i < 256; ++i) |
| 4998 | { |
| 4999 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 5000 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5001 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 5002 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5003 | { |
| 5004 | EMSG(_("E763: Word characters differ between spell files")); |
| 5005 | return FAIL; |
| 5006 | } |
| 5007 | } |
| 5008 | } |
| 5009 | else |
| 5010 | { |
| 5011 | /* copy the new spelltab into the one being used */ |
| 5012 | spelltab = *new_st; |
| 5013 | did_set_spelltab = TRUE; |
| 5014 | } |
| 5015 | |
| 5016 | return OK; |
| 5017 | } |
| 5018 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5019 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5020 | * Write the table with prefix conditions to the .spl file. |
| 5021 | */ |
| 5022 | static void |
| 5023 | write_spell_prefcond(fd, gap) |
| 5024 | FILE *fd; |
| 5025 | garray_T *gap; |
| 5026 | { |
| 5027 | int i; |
| 5028 | char_u *p; |
| 5029 | int len; |
| 5030 | |
| 5031 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 5032 | |
| 5033 | for (i = 0; i < gap->ga_len; ++i) |
| 5034 | { |
| 5035 | /* <prefcond> : <condlen> <condstr> */ |
| 5036 | p = ((char_u **)gap->ga_data)[i]; |
| 5037 | if (p == NULL) |
| 5038 | fputc(0, fd); |
| 5039 | else |
| 5040 | { |
| 5041 | len = STRLEN(p); |
| 5042 | fputc(len, fd); |
| 5043 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 5044 | } |
| 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5049 | * Write the current tables into the .spl file. |
| 5050 | * This makes sure the same characters are recognized as word characters when |
| 5051 | * generating an when using a spell file. |
| 5052 | */ |
| 5053 | static void |
| 5054 | write_spell_chartab(fd) |
| 5055 | FILE *fd; |
| 5056 | { |
| 5057 | char_u charbuf[256 * 4]; |
| 5058 | int len = 0; |
| 5059 | int flags; |
| 5060 | int i; |
| 5061 | |
| 5062 | fputc(128, fd); /* <charflagslen> */ |
| 5063 | for (i = 128; i < 256; ++i) |
| 5064 | { |
| 5065 | flags = 0; |
| 5066 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5067 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5068 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5069 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5070 | fputc(flags, fd); /* <charflags> */ |
| 5071 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5072 | #ifdef FEAT_MBYTE |
| 5073 | if (has_mbyte) |
| 5074 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 5075 | else |
| 5076 | #endif |
| 5077 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 5081 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 5082 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5083 | |
| 5084 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5085 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 5086 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5087 | * When using a multi-byte 'encoding' the length may change! |
| 5088 | * Returns FAIL when something wrong. |
| 5089 | */ |
| 5090 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5091 | spell_casefold(str, len, buf, buflen) |
| 5092 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5093 | int len; |
| 5094 | char_u *buf; |
| 5095 | int buflen; |
| 5096 | { |
| 5097 | int i; |
| 5098 | |
| 5099 | if (len >= buflen) |
| 5100 | { |
| 5101 | buf[0] = NUL; |
| 5102 | return FAIL; /* result will not fit */ |
| 5103 | } |
| 5104 | |
| 5105 | #ifdef FEAT_MBYTE |
| 5106 | if (has_mbyte) |
| 5107 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5108 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5109 | char_u *p; |
| 5110 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5111 | |
| 5112 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5113 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5114 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5115 | if (outi + MB_MAXBYTES > buflen) |
| 5116 | { |
| 5117 | buf[outi] = NUL; |
| 5118 | return FAIL; |
| 5119 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5120 | c = mb_ptr2char_adv(&p); |
| 5121 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5122 | } |
| 5123 | buf[outi] = NUL; |
| 5124 | } |
| 5125 | else |
| 5126 | #endif |
| 5127 | { |
| 5128 | /* Be quick for non-multibyte encodings. */ |
| 5129 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5130 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5131 | buf[i] = NUL; |
| 5132 | } |
| 5133 | |
| 5134 | return OK; |
| 5135 | } |
| 5136 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5137 | /* |
| 5138 | * "z?": Find badly spelled word under or after the cursor. |
| 5139 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5140 | */ |
| 5141 | void |
| 5142 | spell_suggest() |
| 5143 | { |
| 5144 | char_u *line; |
| 5145 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5146 | char_u wcopy[MAXWLEN + 2]; |
| 5147 | char_u *p; |
| 5148 | int i; |
| 5149 | int c; |
| 5150 | suginfo_T sug; |
| 5151 | suggest_T *stp; |
| 5152 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5153 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5154 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL |
| 5155 | || curwin->w_cursor.col > prev_cursor.col) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5156 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5157 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 5158 | return; |
| 5159 | |
| 5160 | /* No bad word or it starts after the cursor: use the word under the |
| 5161 | * cursor. */ |
| 5162 | curwin->w_cursor = prev_cursor; |
| 5163 | line = ml_get_curline(); |
| 5164 | p = line + curwin->w_cursor.col; |
| 5165 | /* Backup to before start of word. */ |
| 5166 | while (p > line && SPELL_ISWORDP(p)) |
| 5167 | mb_ptr_back(line, p); |
| 5168 | /* Forward to start of word. */ |
| 5169 | while (!SPELL_ISWORDP(p)) |
| 5170 | mb_ptr_adv(p); |
| 5171 | |
| 5172 | if (!SPELL_ISWORDP(p)) /* No word found. */ |
| 5173 | { |
| 5174 | beep_flush(); |
| 5175 | return; |
| 5176 | } |
| 5177 | curwin->w_cursor.col = p - line; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5180 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5181 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5182 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5183 | /* Get the list of suggestions */ |
| 5184 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5185 | |
| 5186 | if (sug.su_ga.ga_len == 0) |
| 5187 | MSG(_("Sorry, no suggestions")); |
| 5188 | else |
| 5189 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5190 | /* List the suggestions. */ |
| 5191 | msg_start(); |
| 5192 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 5193 | sug.su_badlen, sug.su_badptr); |
| 5194 | msg_puts(IObuff); |
| 5195 | msg_clr_eos(); |
| 5196 | msg_putchar('\n'); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5197 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5198 | msg_scroll = TRUE; |
| 5199 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 5200 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5201 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5202 | |
| 5203 | /* The suggested word may replace only part of the bad word, add |
| 5204 | * the not replaced part. */ |
| 5205 | STRCPY(wcopy, stp->st_word); |
| 5206 | if (sug.su_badlen > stp->st_orglen) |
| 5207 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 5208 | sug.su_badptr + stp->st_orglen, |
| 5209 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5210 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy); |
| 5211 | msg_puts(IObuff); |
| 5212 | |
| 5213 | /* The word may replace more than "su_badlen". */ |
| 5214 | if (sug.su_badlen < stp->st_orglen) |
| 5215 | { |
| 5216 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 5217 | stp->st_orglen, sug.su_badptr); |
| 5218 | msg_puts(IObuff); |
| 5219 | } |
| 5220 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5221 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5222 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5223 | /* Add the score. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5224 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5225 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"), |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5226 | stp->st_salscore ? "s " : "", |
| 5227 | stp->st_score, stp->st_altscore); |
| 5228 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5229 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"), |
| 5230 | stp->st_score); |
| 5231 | msg_advance(30); |
| 5232 | msg_puts(IObuff); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5233 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5234 | lines_left = 3; /* avoid more prompt */ |
| 5235 | msg_putchar('\n'); |
| 5236 | } |
| 5237 | |
| 5238 | /* Ask for choice. */ |
| 5239 | i = prompt_for_number(); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5240 | 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] | 5241 | { |
| 5242 | /* Replace the word. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5243 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5244 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 5245 | if (p != NULL) |
| 5246 | { |
| 5247 | c = sug.su_badptr - line; |
| 5248 | mch_memmove(p, line, c); |
| 5249 | STRCPY(p + c, stp->st_word); |
| 5250 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 5251 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 5252 | curwin->w_cursor.col = c; |
| 5253 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5254 | |
| 5255 | /* For redo we use a change-word command. */ |
| 5256 | ResetRedobuff(); |
| 5257 | AppendToRedobuff((char_u *)"ciw"); |
| 5258 | AppendToRedobuff(stp->st_word); |
| 5259 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5260 | } |
| 5261 | } |
| 5262 | else |
| 5263 | curwin->w_cursor = prev_cursor; |
| 5264 | } |
| 5265 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5266 | spell_find_cleanup(&sug); |
| 5267 | } |
| 5268 | |
| 5269 | /* |
| 5270 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 5271 | * a list of allocated strings. |
| 5272 | */ |
| 5273 | void |
| 5274 | spell_suggest_list(gap, word, maxcount) |
| 5275 | garray_T *gap; |
| 5276 | char_u *word; |
| 5277 | int maxcount; /* maximum nr of suggestions */ |
| 5278 | { |
| 5279 | suginfo_T sug; |
| 5280 | int i; |
| 5281 | suggest_T *stp; |
| 5282 | char_u *wcopy; |
| 5283 | |
| 5284 | spell_find_suggest(word, &sug, maxcount); |
| 5285 | |
| 5286 | /* Make room in "gap". */ |
| 5287 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 5288 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 5289 | return; |
| 5290 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5291 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5292 | { |
| 5293 | stp = &SUG(sug.su_ga, i); |
| 5294 | |
| 5295 | /* The suggested word may replace only part of "word", add the not |
| 5296 | * replaced part. */ |
| 5297 | wcopy = alloc(STRLEN(stp->st_word) |
| 5298 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 5299 | if (wcopy == NULL) |
| 5300 | break; |
| 5301 | STRCPY(wcopy, stp->st_word); |
| 5302 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 5303 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 5304 | } |
| 5305 | |
| 5306 | spell_find_cleanup(&sug); |
| 5307 | } |
| 5308 | |
| 5309 | /* |
| 5310 | * Find spell suggestions for the word at the start of "badptr". |
| 5311 | * Return the suggestions in "su->su_ga". |
| 5312 | * The maximum number of suggestions is "maxcount". |
| 5313 | * Note: does use info for the current window. |
| 5314 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 5315 | */ |
| 5316 | static void |
| 5317 | spell_find_suggest(badptr, su, maxcount) |
| 5318 | char_u *badptr; |
| 5319 | suginfo_T *su; |
| 5320 | int maxcount; |
| 5321 | { |
| 5322 | int attr; |
| 5323 | |
| 5324 | /* |
| 5325 | * Set the info in "*su". |
| 5326 | */ |
| 5327 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 5328 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 5329 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 5330 | if (*badptr == NUL) |
| 5331 | return; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5332 | hash_init(&su->su_banned); |
| 5333 | |
| 5334 | su->su_badptr = badptr; |
| 5335 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr); |
| 5336 | su->su_maxcount = maxcount; |
| 5337 | |
| 5338 | if (su->su_badlen >= MAXWLEN) |
| 5339 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 5340 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 5341 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 5342 | su->su_fbadword, MAXWLEN); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5343 | /* get caps flags for bad word */ |
| 5344 | 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] | 5345 | |
| 5346 | /* Ban the bad word itself. It may appear in another region. */ |
| 5347 | add_banned(su, su->su_badword); |
| 5348 | |
| 5349 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5350 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5351 | * |
| 5352 | * Set a maximum score to limit the combination of operations that is |
| 5353 | * tried. |
| 5354 | */ |
| 5355 | su->su_maxscore = SCORE_MAXINIT; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5356 | suggest_try_special(su); |
| 5357 | |
| 5358 | /* |
| 5359 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 5360 | * from the .aff file and inserting a space (split the word). |
| 5361 | */ |
| 5362 | suggest_try_change(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5363 | |
| 5364 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 5365 | if (sps_flags & SPS_DOUBLE) |
| 5366 | score_comp_sal(su); |
| 5367 | |
| 5368 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5369 | * 3. Try finding sound-a-like words. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5370 | * |
| 5371 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 5372 | * very slow and often doesn't find new suggestions. |
| 5373 | */ |
| 5374 | if ((sps_flags & SPS_DOUBLE) |
| 5375 | || (!(sps_flags & SPS_FAST) |
| 5376 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 5377 | { |
| 5378 | /* Allow a higher score now. */ |
| 5379 | su->su_maxscore = SCORE_MAXMAX; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5380 | suggest_try_soundalike(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | /* When CTRL-C was hit while searching do show the results. */ |
| 5384 | ui_breakcheck(); |
| 5385 | if (got_int) |
| 5386 | { |
| 5387 | (void)vgetc(); |
| 5388 | got_int = FALSE; |
| 5389 | } |
| 5390 | |
| 5391 | if (sps_flags & SPS_DOUBLE) |
| 5392 | { |
| 5393 | /* Combine the two list of suggestions. */ |
| 5394 | score_combine(su); |
| 5395 | } |
| 5396 | else if (su->su_ga.ga_len != 0) |
| 5397 | { |
| 5398 | if (sps_flags & SPS_BEST) |
| 5399 | /* Adjust the word score for how it sounds like. */ |
| 5400 | rescore_suggestions(su); |
| 5401 | |
| 5402 | /* Sort the suggestions and truncate at "maxcount". */ |
| 5403 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount); |
| 5404 | } |
| 5405 | } |
| 5406 | |
| 5407 | /* |
| 5408 | * Free the info put in "*su" by spell_find_suggest(). |
| 5409 | */ |
| 5410 | static void |
| 5411 | spell_find_cleanup(su) |
| 5412 | suginfo_T *su; |
| 5413 | { |
| 5414 | int i; |
| 5415 | |
| 5416 | /* Free the suggestions. */ |
| 5417 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 5418 | vim_free(SUG(su->su_ga, i).st_word); |
| 5419 | ga_clear(&su->su_ga); |
| 5420 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 5421 | vim_free(SUG(su->su_sga, i).st_word); |
| 5422 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5423 | |
| 5424 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5425 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5429 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 5430 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 5431 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5432 | */ |
| 5433 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5434 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5435 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5436 | char_u *wcopy; |
| 5437 | int upper; /* TRUE: first letter made upper case */ |
| 5438 | { |
| 5439 | char_u *p; |
| 5440 | int c; |
| 5441 | int l; |
| 5442 | |
| 5443 | p = word; |
| 5444 | #ifdef FEAT_MBYTE |
| 5445 | if (has_mbyte) |
| 5446 | c = mb_ptr2char_adv(&p); |
| 5447 | else |
| 5448 | #endif |
| 5449 | c = *p++; |
| 5450 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5451 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5452 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5453 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5454 | #ifdef FEAT_MBYTE |
| 5455 | if (has_mbyte) |
| 5456 | l = mb_char2bytes(c, wcopy); |
| 5457 | else |
| 5458 | #endif |
| 5459 | { |
| 5460 | l = 1; |
| 5461 | wcopy[0] = c; |
| 5462 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5463 | vim_strncpy(wcopy + l, p, MAXWLEN - l); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5467 | * Make a copy of "word" with all the letters upper cased into |
| 5468 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5469 | */ |
| 5470 | static void |
| 5471 | allcap_copy(word, wcopy) |
| 5472 | char_u *word; |
| 5473 | char_u *wcopy; |
| 5474 | { |
| 5475 | char_u *s; |
| 5476 | char_u *d; |
| 5477 | int c; |
| 5478 | |
| 5479 | d = wcopy; |
| 5480 | for (s = word; *s != NUL; ) |
| 5481 | { |
| 5482 | #ifdef FEAT_MBYTE |
| 5483 | if (has_mbyte) |
| 5484 | c = mb_ptr2char_adv(&s); |
| 5485 | else |
| 5486 | #endif |
| 5487 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5488 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5489 | |
| 5490 | #ifdef FEAT_MBYTE |
| 5491 | if (has_mbyte) |
| 5492 | { |
| 5493 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 5494 | break; |
| 5495 | d += mb_char2bytes(c, d); |
| 5496 | } |
| 5497 | else |
| 5498 | #endif |
| 5499 | { |
| 5500 | if (d - wcopy >= MAXWLEN - 1) |
| 5501 | break; |
| 5502 | *d++ = c; |
| 5503 | } |
| 5504 | } |
| 5505 | *d = NUL; |
| 5506 | } |
| 5507 | |
| 5508 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5509 | * Try finding suggestions by recognizing specific situations. |
| 5510 | */ |
| 5511 | static void |
| 5512 | suggest_try_special(su) |
| 5513 | suginfo_T *su; |
| 5514 | { |
| 5515 | char_u *p; |
| 5516 | int len; |
| 5517 | int c; |
| 5518 | char_u word[MAXWLEN]; |
| 5519 | |
| 5520 | /* |
| 5521 | * Recognize a word that is repeated: "the the". |
| 5522 | */ |
| 5523 | p = skiptowhite(su->su_fbadword); |
| 5524 | len = p - su->su_fbadword; |
| 5525 | p = skipwhite(p); |
| 5526 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 5527 | { |
| 5528 | /* Include badflags: if the badword is onecap or allcap |
| 5529 | * use that for the goodword too: "The the" -> "The". */ |
| 5530 | c = su->su_fbadword[len]; |
| 5531 | su->su_fbadword[len] = NUL; |
| 5532 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 5533 | su->su_fbadword[len] = c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5534 | 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] | 5535 | } |
| 5536 | } |
| 5537 | |
| 5538 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5539 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5540 | * |
| 5541 | * This uses a state machine. At each node in the tree we try various |
| 5542 | * operations. When trying if an operation work "depth" is increased and the |
| 5543 | * stack[] is used to store info. This allows combinations, thus insert one |
| 5544 | * character, replace one and delete another. The number of changes is |
| 5545 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5546 | */ |
| 5547 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5548 | suggest_try_change(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5549 | suginfo_T *su; |
| 5550 | { |
| 5551 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 5552 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 5553 | trystate_T stack[MAXWLEN]; |
| 5554 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 5555 | * to for word split) */ |
| 5556 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 5557 | int splitoff = 0; /* index in tword after last split */ |
| 5558 | trystate_T *sp; |
| 5559 | int newscore; |
| 5560 | langp_T *lp; |
| 5561 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5562 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5563 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5564 | int c, c2, c3; |
| 5565 | int n = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5566 | int flags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5567 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5568 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5569 | int len; |
| 5570 | char_u *p; |
| 5571 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5572 | int fl = 0, tl; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5573 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5574 | |
| 5575 | /* 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] | 5576 | * to find matches (esp. REP items). Append some more text, changing |
| 5577 | * chars after the bad word may help. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5578 | STRCPY(fword, su->su_fbadword); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5579 | n = STRLEN(fword); |
| 5580 | p = su->su_badptr + su->su_badlen; |
| 5581 | (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5582 | |
| 5583 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 5584 | lp->lp_slang != NULL; ++lp) |
| 5585 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5586 | /* |
| 5587 | * Go through the whole case-fold tree, try changes at each node. |
| 5588 | * "tword[]" contains the word collected from nodes in the tree. |
| 5589 | * "fword[]" the word we are trying to match with (initially the bad |
| 5590 | * word). |
| 5591 | */ |
| 5592 | byts = lp->lp_slang->sl_fbyts; |
| 5593 | idxs = lp->lp_slang->sl_fidxs; |
| 5594 | |
| 5595 | depth = 0; |
| 5596 | stack[0].ts_state = STATE_START; |
| 5597 | stack[0].ts_score = 0; |
| 5598 | stack[0].ts_curi = 1; |
| 5599 | stack[0].ts_fidx = 0; |
| 5600 | stack[0].ts_fidxtry = 0; |
| 5601 | stack[0].ts_twordlen = 0; |
| 5602 | stack[0].ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5603 | #ifdef FEAT_MBYTE |
| 5604 | stack[0].ts_tcharlen = 0; |
| 5605 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5606 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5607 | /* |
| 5608 | * Loop to find all suggestions. At each round we either: |
| 5609 | * - For the current state try one operation, advance "ts_curi", |
| 5610 | * increase "depth". |
| 5611 | * - When a state is done go to the next, set "ts_state". |
| 5612 | * - When all states are tried decrease "depth". |
| 5613 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5614 | while (depth >= 0 && !got_int) |
| 5615 | { |
| 5616 | sp = &stack[depth]; |
| 5617 | switch (sp->ts_state) |
| 5618 | { |
| 5619 | case STATE_START: |
| 5620 | /* |
| 5621 | * Start of node: Deal with NUL bytes, which means |
| 5622 | * tword[] may end here. |
| 5623 | */ |
| 5624 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 5625 | len = byts[arridx]; /* bytes in this node */ |
| 5626 | arridx += sp->ts_curi; /* index of current byte */ |
| 5627 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5628 | if (sp->ts_curi > len || byts[arridx] != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5629 | { |
| 5630 | /* Past bytes in node and/or past NUL bytes. */ |
| 5631 | sp->ts_state = STATE_ENDNUL; |
| 5632 | break; |
| 5633 | } |
| 5634 | |
| 5635 | /* |
| 5636 | * End of word in tree. |
| 5637 | */ |
| 5638 | ++sp->ts_curi; /* eat one NUL byte */ |
| 5639 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5640 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5641 | |
| 5642 | /* |
| 5643 | * Form the word with proper case in preword. |
| 5644 | * If there is a word from a previous split, append. |
| 5645 | */ |
| 5646 | tword[sp->ts_twordlen] = NUL; |
| 5647 | if (flags & WF_KEEPCAP) |
| 5648 | /* Must find the word in the keep-case tree. */ |
| 5649 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 5650 | preword + prewordlen); |
| 5651 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5652 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5653 | /* Include badflags: if the badword is onecap or allcap |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5654 | * use that for the goodword too. But if the badword is |
| 5655 | * allcap and it's only one char long use onecap. */ |
| 5656 | c = su->su_badflags; |
| 5657 | if ((c & WF_ALLCAP) |
| 5658 | #ifdef FEAT_MBYTE |
| 5659 | && su->su_badlen == mb_ptr2len_check(su->su_badptr) |
| 5660 | #else |
| 5661 | && su->su_badlen == 1 |
| 5662 | #endif |
| 5663 | ) |
| 5664 | c = WF_ONECAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5665 | make_case_word(tword + splitoff, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5666 | preword + prewordlen, flags | c); |
| 5667 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5668 | |
| 5669 | /* Don't use a banned word. It may appear again as a good |
| 5670 | * word, thus remember it. */ |
| 5671 | if (flags & WF_BANNED) |
| 5672 | { |
| 5673 | add_banned(su, preword + prewordlen); |
| 5674 | break; |
| 5675 | } |
| 5676 | if (was_banned(su, preword + prewordlen)) |
| 5677 | break; |
| 5678 | |
| 5679 | newscore = 0; |
| 5680 | if ((flags & WF_REGION) |
| 5681 | && (((unsigned)flags >> 8) & lp->lp_region) == 0) |
| 5682 | newscore += SCORE_REGION; |
| 5683 | if (flags & WF_RARE) |
| 5684 | newscore += SCORE_RARE; |
| 5685 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5686 | if (!spell_valid_case(su->su_badflags, |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5687 | captype(preword + prewordlen, NULL))) |
| 5688 | newscore += SCORE_ICASE; |
| 5689 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5690 | if ((fword[sp->ts_fidx] == NUL |
| 5691 | || !SPELL_ISWORDP(fword + sp->ts_fidx)) |
| 5692 | && sp->ts_fidx >= sp->ts_fidxtry) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5693 | { |
| 5694 | /* The badword also ends: add suggestions, */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5695 | add_suggestion(su, &su->su_ga, preword, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5696 | sp->ts_fidx - repextra, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5697 | sp->ts_score + newscore, 0, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5698 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5699 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 5700 | #ifdef FEAT_MBYTE |
| 5701 | /* Don't split halfway a character. */ |
| 5702 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 5703 | #endif |
| 5704 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5705 | { |
| 5706 | /* The word in the tree ends but the badword |
| 5707 | * continues: try inserting a space and check that a valid |
| 5708 | * words starts at fword[sp->ts_fidx]. */ |
| 5709 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 5710 | { |
| 5711 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 5712 | sp->ts_save_prewordlen = prewordlen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5713 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5714 | sp->ts_save_splitoff = splitoff; |
| 5715 | |
| 5716 | /* Append a space to preword. */ |
| 5717 | STRCAT(preword, " "); |
| 5718 | prewordlen = STRLEN(preword); |
| 5719 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5720 | #ifdef FEAT_MBYTE |
| 5721 | if (has_mbyte) |
| 5722 | { |
| 5723 | int i = 0; |
| 5724 | |
| 5725 | /* Case-folding may change the number of bytes: |
| 5726 | * Count nr of chars in fword[sp->ts_fidx] and |
| 5727 | * advance that many chars in su->su_badptr. */ |
| 5728 | for (p = fword; p < fword + sp->ts_fidx; |
| 5729 | mb_ptr_adv(p)) |
| 5730 | ++i; |
| 5731 | for (p = su->su_badptr; i > 0; mb_ptr_adv(p)) |
| 5732 | --i; |
| 5733 | } |
| 5734 | else |
| 5735 | #endif |
| 5736 | p = su->su_badptr + sp->ts_fidx; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5737 | su->su_badflags = captype(p, su->su_badptr |
| 5738 | + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5739 | |
| 5740 | sp->ts_state = STATE_SPLITUNDO; |
| 5741 | ++depth; |
| 5742 | /* Restart at top of the tree. */ |
| 5743 | stack[depth].ts_arridx = 0; |
| 5744 | } |
| 5745 | } |
| 5746 | break; |
| 5747 | |
| 5748 | case STATE_SPLITUNDO: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5749 | /* Undo the changes done for word split. */ |
| 5750 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5751 | splitoff = sp->ts_save_splitoff; |
| 5752 | prewordlen = sp->ts_save_prewordlen; |
| 5753 | |
| 5754 | /* Continue looking for NUL bytes. */ |
| 5755 | sp->ts_state = STATE_START; |
| 5756 | break; |
| 5757 | |
| 5758 | case STATE_ENDNUL: |
| 5759 | /* Past the NUL bytes in the node. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5760 | if (fword[sp->ts_fidx] == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5761 | { |
| 5762 | /* The badword ends, can't use the bytes in this node. */ |
| 5763 | sp->ts_state = STATE_DEL; |
| 5764 | break; |
| 5765 | } |
| 5766 | sp->ts_state = STATE_PLAIN; |
| 5767 | /*FALLTHROUGH*/ |
| 5768 | |
| 5769 | case STATE_PLAIN: |
| 5770 | /* |
| 5771 | * Go over all possible bytes at this node, add each to |
| 5772 | * tword[] and use child node. "ts_curi" is the index. |
| 5773 | */ |
| 5774 | arridx = sp->ts_arridx; |
| 5775 | if (sp->ts_curi > byts[arridx]) |
| 5776 | { |
| 5777 | /* Done all bytes at this node, do next state. When still |
| 5778 | * at already changed bytes skip the other tricks. */ |
| 5779 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 5780 | sp->ts_state = STATE_DEL; |
| 5781 | else |
| 5782 | sp->ts_state = STATE_FINAL; |
| 5783 | } |
| 5784 | else |
| 5785 | { |
| 5786 | arridx += sp->ts_curi++; |
| 5787 | c = byts[arridx]; |
| 5788 | |
| 5789 | /* Normal byte, go one level deeper. If it's not equal to |
| 5790 | * the byte in the bad word adjust the score. But don't |
| 5791 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5792 | if (c == fword[sp->ts_fidx] |
| 5793 | #ifdef FEAT_MBYTE |
| 5794 | || (sp->ts_tcharlen > 0 |
| 5795 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5796 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5797 | ) |
| 5798 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5799 | else |
| 5800 | newscore = SCORE_SUBST; |
| 5801 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 5802 | && try_deeper(su, stack, depth, newscore)) |
| 5803 | { |
| 5804 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5805 | sp = &stack[depth]; |
| 5806 | ++sp->ts_fidx; |
| 5807 | tword[sp->ts_twordlen++] = c; |
| 5808 | sp->ts_arridx = idxs[arridx]; |
| 5809 | #ifdef FEAT_MBYTE |
| 5810 | if (newscore == SCORE_SUBST) |
| 5811 | sp->ts_isdiff = DIFF_YES; |
| 5812 | if (has_mbyte) |
| 5813 | { |
| 5814 | /* Multi-byte characters are a bit complicated to |
| 5815 | * handle: They differ when any of the bytes |
| 5816 | * differ and then their length may also differ. */ |
| 5817 | if (sp->ts_tcharlen == 0) |
| 5818 | { |
| 5819 | /* First byte. */ |
| 5820 | sp->ts_tcharidx = 0; |
| 5821 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 5822 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 5823 | sp->ts_isdiff = (newscore != 0) |
| 5824 | ? DIFF_YES : DIFF_NONE; |
| 5825 | } |
| 5826 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 5827 | /* When inserting trail bytes don't advance in |
| 5828 | * the bad word. */ |
| 5829 | --sp->ts_fidx; |
| 5830 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 5831 | { |
| 5832 | /* Last byte of character. */ |
| 5833 | if (sp->ts_isdiff == DIFF_YES) |
| 5834 | { |
| 5835 | /* Correct ts_fidx for the byte length of |
| 5836 | * the character (we didn't check that |
| 5837 | * before). */ |
| 5838 | sp->ts_fidx = sp->ts_fcharstart |
| 5839 | + MB_BYTE2LEN( |
| 5840 | fword[sp->ts_fcharstart]); |
| 5841 | |
| 5842 | /* For a similar character adjust score |
| 5843 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 5844 | if (lp->lp_slang->sl_has_map |
| 5845 | && similar_chars(lp->lp_slang, |
| 5846 | mb_ptr2char(tword |
| 5847 | + sp->ts_twordlen |
| 5848 | - sp->ts_tcharlen), |
| 5849 | mb_ptr2char(fword |
| 5850 | + sp->ts_fcharstart))) |
| 5851 | sp->ts_score -= |
| 5852 | SCORE_SUBST - SCORE_SIMILAR; |
| 5853 | } |
| 5854 | |
| 5855 | /* Starting a new char, reset the length. */ |
| 5856 | sp->ts_tcharlen = 0; |
| 5857 | } |
| 5858 | } |
| 5859 | else |
| 5860 | #endif |
| 5861 | { |
| 5862 | /* If we found a similar char adjust the score. |
| 5863 | * We do this after calling try_deeper() because |
| 5864 | * it's slow. */ |
| 5865 | if (newscore != 0 |
| 5866 | && lp->lp_slang->sl_has_map |
| 5867 | && similar_chars(lp->lp_slang, |
| 5868 | c, fword[sp->ts_fidx - 1])) |
| 5869 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 5870 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5871 | } |
| 5872 | } |
| 5873 | break; |
| 5874 | |
| 5875 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5876 | #ifdef FEAT_MBYTE |
| 5877 | /* When past the first byte of a multi-byte char don't try |
| 5878 | * delete/insert/swap a character. */ |
| 5879 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 5880 | { |
| 5881 | sp->ts_state = STATE_FINAL; |
| 5882 | break; |
| 5883 | } |
| 5884 | #endif |
| 5885 | /* |
| 5886 | * Try skipping one character in the bad word (delete it). |
| 5887 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5888 | sp->ts_state = STATE_INS; |
| 5889 | sp->ts_curi = 1; |
| 5890 | if (fword[sp->ts_fidx] != NUL |
| 5891 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 5892 | { |
| 5893 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5894 | #ifdef FEAT_MBYTE |
| 5895 | if (has_mbyte) |
| 5896 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
| 5897 | else |
| 5898 | #endif |
| 5899 | ++stack[depth].ts_fidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5900 | break; |
| 5901 | } |
| 5902 | /*FALLTHROUGH*/ |
| 5903 | |
| 5904 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5905 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5906 | * node. */ |
| 5907 | n = sp->ts_arridx; |
| 5908 | if (sp->ts_curi > byts[n]) |
| 5909 | { |
| 5910 | /* Done all bytes at this node, do next state. */ |
| 5911 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5912 | } |
| 5913 | else |
| 5914 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5915 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5916 | n += sp->ts_curi++; |
| 5917 | c = byts[n]; |
| 5918 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 5919 | { |
| 5920 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5921 | sp = &stack[depth]; |
| 5922 | tword[sp->ts_twordlen++] = c; |
| 5923 | sp->ts_arridx = idxs[n]; |
| 5924 | #ifdef FEAT_MBYTE |
| 5925 | if (has_mbyte) |
| 5926 | { |
| 5927 | fl = MB_BYTE2LEN(c); |
| 5928 | if (fl > 1) |
| 5929 | { |
| 5930 | /* There are following bytes for the same |
| 5931 | * character. We must find all bytes before |
| 5932 | * trying delete/insert/swap/etc. */ |
| 5933 | sp->ts_tcharlen = fl; |
| 5934 | sp->ts_tcharidx = 1; |
| 5935 | sp->ts_isdiff = DIFF_INSERT; |
| 5936 | } |
| 5937 | } |
| 5938 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5939 | } |
| 5940 | } |
| 5941 | break; |
| 5942 | |
| 5943 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5944 | /* |
| 5945 | * Swap two bytes in the bad word: "12" -> "21". |
| 5946 | * We change "fword" here, it's changed back afterwards. |
| 5947 | */ |
| 5948 | p = fword + sp->ts_fidx; |
| 5949 | c = *p; |
| 5950 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5951 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5952 | /* End of word, can't swap or replace. */ |
| 5953 | sp->ts_state = STATE_FINAL; |
| 5954 | break; |
| 5955 | } |
| 5956 | #ifdef FEAT_MBYTE |
| 5957 | if (has_mbyte) |
| 5958 | { |
| 5959 | n = mb_ptr2len_check(p); |
| 5960 | c = mb_ptr2char(p); |
| 5961 | c2 = mb_ptr2char(p + n); |
| 5962 | } |
| 5963 | else |
| 5964 | #endif |
| 5965 | c2 = p[1]; |
| 5966 | if (c == c2) |
| 5967 | { |
| 5968 | /* Characters are identical, swap won't do anything. */ |
| 5969 | sp->ts_state = STATE_SWAP3; |
| 5970 | break; |
| 5971 | } |
| 5972 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 5973 | { |
| 5974 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5975 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5976 | #ifdef FEAT_MBYTE |
| 5977 | if (has_mbyte) |
| 5978 | { |
| 5979 | fl = mb_char2len(c2); |
| 5980 | mch_memmove(p, p + n, fl); |
| 5981 | mb_char2bytes(c, p + fl); |
| 5982 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 5983 | } |
| 5984 | else |
| 5985 | #endif |
| 5986 | { |
| 5987 | p[0] = c2; |
| 5988 | p[1] = c; |
| 5989 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 5990 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5991 | } |
| 5992 | else |
| 5993 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 5994 | sp->ts_state = STATE_REP_INI; |
| 5995 | break; |
| 5996 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5997 | case STATE_UNSWAP: |
| 5998 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 5999 | p = fword + sp->ts_fidx; |
| 6000 | #ifdef FEAT_MBYTE |
| 6001 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6002 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6003 | n = MB_BYTE2LEN(*p); |
| 6004 | c = mb_ptr2char(p + n); |
| 6005 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 6006 | mb_char2bytes(c, p); |
| 6007 | } |
| 6008 | else |
| 6009 | #endif |
| 6010 | { |
| 6011 | c = *p; |
| 6012 | *p = p[1]; |
| 6013 | p[1] = c; |
| 6014 | } |
| 6015 | /*FALLTHROUGH*/ |
| 6016 | |
| 6017 | case STATE_SWAP3: |
| 6018 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 6019 | * "fword" here, it's changed back afterwards. */ |
| 6020 | p = fword + sp->ts_fidx; |
| 6021 | #ifdef FEAT_MBYTE |
| 6022 | if (has_mbyte) |
| 6023 | { |
| 6024 | n = mb_ptr2len_check(p); |
| 6025 | c = mb_ptr2char(p); |
| 6026 | fl = mb_ptr2len_check(p + n); |
| 6027 | c2 = mb_ptr2char(p + n); |
| 6028 | c3 = mb_ptr2char(p + n + fl); |
| 6029 | } |
| 6030 | else |
| 6031 | #endif |
| 6032 | { |
| 6033 | c = *p; |
| 6034 | c2 = p[1]; |
| 6035 | c3 = p[2]; |
| 6036 | } |
| 6037 | |
| 6038 | /* When characters are identical: "121" then SWAP3 result is |
| 6039 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 6040 | * result is same as SWAP on next char: "112". Thus skip all |
| 6041 | * swapping. Also skip when c3 is NUL. */ |
| 6042 | if (c == c3 || c3 == NUL) |
| 6043 | { |
| 6044 | sp->ts_state = STATE_REP_INI; |
| 6045 | break; |
| 6046 | } |
| 6047 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6048 | { |
| 6049 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6050 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6051 | #ifdef FEAT_MBYTE |
| 6052 | if (has_mbyte) |
| 6053 | { |
| 6054 | tl = mb_char2len(c3); |
| 6055 | mch_memmove(p, p + n + fl, tl); |
| 6056 | mb_char2bytes(c2, p + tl); |
| 6057 | mb_char2bytes(c, p + fl + tl); |
| 6058 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 6059 | } |
| 6060 | else |
| 6061 | #endif |
| 6062 | { |
| 6063 | p[0] = p[2]; |
| 6064 | p[2] = c; |
| 6065 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6066 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6067 | } |
| 6068 | else |
| 6069 | sp->ts_state = STATE_REP_INI; |
| 6070 | break; |
| 6071 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6072 | case STATE_UNSWAP3: |
| 6073 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 6074 | p = fword + sp->ts_fidx; |
| 6075 | #ifdef FEAT_MBYTE |
| 6076 | if (has_mbyte) |
| 6077 | { |
| 6078 | n = MB_BYTE2LEN(*p); |
| 6079 | c2 = mb_ptr2char(p + n); |
| 6080 | fl = MB_BYTE2LEN(p[n]); |
| 6081 | c = mb_ptr2char(p + n + fl); |
| 6082 | tl = MB_BYTE2LEN(p[n + fl]); |
| 6083 | mch_memmove(p + fl + tl, p, n); |
| 6084 | mb_char2bytes(c, p); |
| 6085 | mb_char2bytes(c2, p + tl); |
| 6086 | } |
| 6087 | else |
| 6088 | #endif |
| 6089 | { |
| 6090 | c = *p; |
| 6091 | *p = p[2]; |
| 6092 | p[2] = c; |
| 6093 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6094 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6095 | /* Rotate three characters left: "123" -> "231". We change |
| 6096 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6097 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6098 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6099 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6100 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6101 | p = fword + sp->ts_fidx; |
| 6102 | #ifdef FEAT_MBYTE |
| 6103 | if (has_mbyte) |
| 6104 | { |
| 6105 | n = mb_ptr2len_check(p); |
| 6106 | c = mb_ptr2char(p); |
| 6107 | fl = mb_ptr2len_check(p + n); |
| 6108 | fl += mb_ptr2len_check(p + n + fl); |
| 6109 | mch_memmove(p, p + n, fl); |
| 6110 | mb_char2bytes(c, p + fl); |
| 6111 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 6112 | } |
| 6113 | else |
| 6114 | #endif |
| 6115 | { |
| 6116 | c = *p; |
| 6117 | *p = p[1]; |
| 6118 | p[1] = p[2]; |
| 6119 | p[2] = c; |
| 6120 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6121 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6122 | } |
| 6123 | else |
| 6124 | sp->ts_state = STATE_REP_INI; |
| 6125 | break; |
| 6126 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6127 | case STATE_UNROT3L: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6128 | /* Undo ROT3L: "231" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6129 | p = fword + sp->ts_fidx; |
| 6130 | #ifdef FEAT_MBYTE |
| 6131 | if (has_mbyte) |
| 6132 | { |
| 6133 | n = MB_BYTE2LEN(*p); |
| 6134 | n += MB_BYTE2LEN(p[n]); |
| 6135 | c = mb_ptr2char(p + n); |
| 6136 | tl = MB_BYTE2LEN(p[n]); |
| 6137 | mch_memmove(p + tl, p, n); |
| 6138 | mb_char2bytes(c, p); |
| 6139 | } |
| 6140 | else |
| 6141 | #endif |
| 6142 | { |
| 6143 | c = p[2]; |
| 6144 | p[2] = p[1]; |
| 6145 | p[1] = *p; |
| 6146 | *p = c; |
| 6147 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6148 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6149 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6150 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6151 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6152 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6153 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6154 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6155 | p = fword + sp->ts_fidx; |
| 6156 | #ifdef FEAT_MBYTE |
| 6157 | if (has_mbyte) |
| 6158 | { |
| 6159 | n = mb_ptr2len_check(p); |
| 6160 | n += mb_ptr2len_check(p + n); |
| 6161 | c = mb_ptr2char(p + n); |
| 6162 | tl = mb_ptr2len_check(p + n); |
| 6163 | mch_memmove(p + tl, p, n); |
| 6164 | mb_char2bytes(c, p); |
| 6165 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 6166 | } |
| 6167 | else |
| 6168 | #endif |
| 6169 | { |
| 6170 | c = p[2]; |
| 6171 | p[2] = p[1]; |
| 6172 | p[1] = *p; |
| 6173 | *p = c; |
| 6174 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6175 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6176 | } |
| 6177 | else |
| 6178 | sp->ts_state = STATE_REP_INI; |
| 6179 | break; |
| 6180 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6181 | case STATE_UNROT3R: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6182 | /* Undo ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6183 | p = fword + sp->ts_fidx; |
| 6184 | #ifdef FEAT_MBYTE |
| 6185 | if (has_mbyte) |
| 6186 | { |
| 6187 | c = mb_ptr2char(p); |
| 6188 | tl = MB_BYTE2LEN(*p); |
| 6189 | n = MB_BYTE2LEN(p[tl]); |
| 6190 | n += MB_BYTE2LEN(p[tl + n]); |
| 6191 | mch_memmove(p, p + tl, n); |
| 6192 | mb_char2bytes(c, p + n); |
| 6193 | } |
| 6194 | else |
| 6195 | #endif |
| 6196 | { |
| 6197 | c = *p; |
| 6198 | *p = p[1]; |
| 6199 | p[1] = p[2]; |
| 6200 | p[2] = c; |
| 6201 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6202 | /*FALLTHROUGH*/ |
| 6203 | |
| 6204 | case STATE_REP_INI: |
| 6205 | /* Check if matching with REP items from the .aff file would |
| 6206 | * work. Quickly skip if there are no REP items or the score |
| 6207 | * is going to be too high anyway. */ |
| 6208 | gap = &lp->lp_slang->sl_rep; |
| 6209 | if (gap->ga_len == 0 |
| 6210 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 6211 | { |
| 6212 | sp->ts_state = STATE_FINAL; |
| 6213 | break; |
| 6214 | } |
| 6215 | |
| 6216 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6217 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6218 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 6219 | if (sp->ts_curi < 0) |
| 6220 | { |
| 6221 | sp->ts_state = STATE_FINAL; |
| 6222 | break; |
| 6223 | } |
| 6224 | |
| 6225 | sp->ts_state = STATE_REP; |
| 6226 | /*FALLTHROUGH*/ |
| 6227 | |
| 6228 | case STATE_REP: |
| 6229 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6230 | * match replace the characters and check if the resulting |
| 6231 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6232 | p = fword + sp->ts_fidx; |
| 6233 | |
| 6234 | gap = &lp->lp_slang->sl_rep; |
| 6235 | while (sp->ts_curi < gap->ga_len) |
| 6236 | { |
| 6237 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 6238 | if (*ftp->ft_from != *p) |
| 6239 | { |
| 6240 | /* past possible matching entries */ |
| 6241 | sp->ts_curi = gap->ga_len; |
| 6242 | break; |
| 6243 | } |
| 6244 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 6245 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 6246 | { |
| 6247 | /* Need to undo this afterwards. */ |
| 6248 | sp->ts_state = STATE_REP_UNDO; |
| 6249 | |
| 6250 | /* Change the "from" to the "to" string. */ |
| 6251 | ++depth; |
| 6252 | fl = STRLEN(ftp->ft_from); |
| 6253 | tl = STRLEN(ftp->ft_to); |
| 6254 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6255 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6256 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6257 | repextra += tl - fl; |
| 6258 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6259 | mch_memmove(p, ftp->ft_to, tl); |
| 6260 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6261 | #ifdef FEAT_MBYTE |
| 6262 | stack[depth].ts_tcharlen = 0; |
| 6263 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6264 | break; |
| 6265 | } |
| 6266 | } |
| 6267 | |
| 6268 | if (sp->ts_curi >= gap->ga_len) |
| 6269 | /* No (more) matches. */ |
| 6270 | sp->ts_state = STATE_FINAL; |
| 6271 | |
| 6272 | break; |
| 6273 | |
| 6274 | case STATE_REP_UNDO: |
| 6275 | /* Undo a REP replacement and continue with the next one. */ |
| 6276 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 6277 | + sp->ts_curi - 1; |
| 6278 | fl = STRLEN(ftp->ft_from); |
| 6279 | tl = STRLEN(ftp->ft_to); |
| 6280 | p = fword + sp->ts_fidx; |
| 6281 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6282 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6283 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6284 | repextra -= tl - fl; |
| 6285 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6286 | mch_memmove(p, ftp->ft_from, fl); |
| 6287 | sp->ts_state = STATE_REP; |
| 6288 | break; |
| 6289 | |
| 6290 | default: |
| 6291 | /* Did all possible states at this level, go up one level. */ |
| 6292 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6293 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6294 | /* Don't check for CTRL-C too often, it takes time. */ |
| 6295 | line_breakcheck(); |
| 6296 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6297 | } |
| 6298 | } |
| 6299 | } |
| 6300 | |
| 6301 | /* |
| 6302 | * Try going one level deeper in the tree. |
| 6303 | */ |
| 6304 | static int |
| 6305 | try_deeper(su, stack, depth, score_add) |
| 6306 | suginfo_T *su; |
| 6307 | trystate_T *stack; |
| 6308 | int depth; |
| 6309 | int score_add; |
| 6310 | { |
| 6311 | int newscore; |
| 6312 | |
| 6313 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 6314 | newscore = stack[depth].ts_score + score_add; |
| 6315 | if (newscore >= su->su_maxscore) |
| 6316 | return FALSE; |
| 6317 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6318 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6319 | stack[depth + 1].ts_state = STATE_START; |
| 6320 | stack[depth + 1].ts_score = newscore; |
| 6321 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6322 | return TRUE; |
| 6323 | } |
| 6324 | |
| 6325 | /* |
| 6326 | * "fword" is a good word with case folded. Find the matching keep-case |
| 6327 | * words and put it in "kword". |
| 6328 | * Theoretically there could be several keep-case words that result in the |
| 6329 | * same case-folded word, but we only find one... |
| 6330 | */ |
| 6331 | static void |
| 6332 | find_keepcap_word(slang, fword, kword) |
| 6333 | slang_T *slang; |
| 6334 | char_u *fword; |
| 6335 | char_u *kword; |
| 6336 | { |
| 6337 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 6338 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6339 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6340 | |
| 6341 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6342 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6343 | int round[MAXWLEN]; |
| 6344 | int fwordidx[MAXWLEN]; |
| 6345 | int uwordidx[MAXWLEN]; |
| 6346 | int kwordlen[MAXWLEN]; |
| 6347 | |
| 6348 | int flen, ulen; |
| 6349 | int l; |
| 6350 | int len; |
| 6351 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6352 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6353 | char_u *p; |
| 6354 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6355 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6356 | |
| 6357 | if (byts == NULL) |
| 6358 | { |
| 6359 | /* array is empty: "cannot happen" */ |
| 6360 | *kword = NUL; |
| 6361 | return; |
| 6362 | } |
| 6363 | |
| 6364 | /* Make an all-cap version of "fword". */ |
| 6365 | allcap_copy(fword, uword); |
| 6366 | |
| 6367 | /* |
| 6368 | * Each character needs to be tried both case-folded and upper-case. |
| 6369 | * All this gets very complicated if we keep in mind that changing case |
| 6370 | * may change the byte length of a multi-byte character... |
| 6371 | */ |
| 6372 | depth = 0; |
| 6373 | arridx[0] = 0; |
| 6374 | round[0] = 0; |
| 6375 | fwordidx[0] = 0; |
| 6376 | uwordidx[0] = 0; |
| 6377 | kwordlen[0] = 0; |
| 6378 | while (depth >= 0) |
| 6379 | { |
| 6380 | if (fword[fwordidx[depth]] == NUL) |
| 6381 | { |
| 6382 | /* We are at the end of "fword". If the tree allows a word to end |
| 6383 | * here we have found a match. */ |
| 6384 | if (byts[arridx[depth] + 1] == 0) |
| 6385 | { |
| 6386 | kword[kwordlen[depth]] = NUL; |
| 6387 | return; |
| 6388 | } |
| 6389 | |
| 6390 | /* kword is getting too long, continue one level up */ |
| 6391 | --depth; |
| 6392 | } |
| 6393 | else if (++round[depth] > 2) |
| 6394 | { |
| 6395 | /* tried both fold-case and upper-case character, continue one |
| 6396 | * level up */ |
| 6397 | --depth; |
| 6398 | } |
| 6399 | else |
| 6400 | { |
| 6401 | /* |
| 6402 | * round[depth] == 1: Try using the folded-case character. |
| 6403 | * round[depth] == 2: Try using the upper-case character. |
| 6404 | */ |
| 6405 | #ifdef FEAT_MBYTE |
| 6406 | if (has_mbyte) |
| 6407 | { |
| 6408 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 6409 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 6410 | } |
| 6411 | else |
| 6412 | #endif |
| 6413 | ulen = flen = 1; |
| 6414 | if (round[depth] == 1) |
| 6415 | { |
| 6416 | p = fword + fwordidx[depth]; |
| 6417 | l = flen; |
| 6418 | } |
| 6419 | else |
| 6420 | { |
| 6421 | p = uword + uwordidx[depth]; |
| 6422 | l = ulen; |
| 6423 | } |
| 6424 | |
| 6425 | for (tryidx = arridx[depth]; l > 0; --l) |
| 6426 | { |
| 6427 | /* Perform a binary search in the list of accepted bytes. */ |
| 6428 | len = byts[tryidx++]; |
| 6429 | c = *p++; |
| 6430 | lo = tryidx; |
| 6431 | hi = tryidx + len - 1; |
| 6432 | while (lo < hi) |
| 6433 | { |
| 6434 | m = (lo + hi) / 2; |
| 6435 | if (byts[m] > c) |
| 6436 | hi = m - 1; |
| 6437 | else if (byts[m] < c) |
| 6438 | lo = m + 1; |
| 6439 | else |
| 6440 | { |
| 6441 | lo = hi = m; |
| 6442 | break; |
| 6443 | } |
| 6444 | } |
| 6445 | |
| 6446 | /* Stop if there is no matching byte. */ |
| 6447 | if (hi < lo || byts[lo] != c) |
| 6448 | break; |
| 6449 | |
| 6450 | /* Continue at the child (if there is one). */ |
| 6451 | tryidx = idxs[lo]; |
| 6452 | } |
| 6453 | |
| 6454 | if (l == 0) |
| 6455 | { |
| 6456 | /* |
| 6457 | * Found the matching char. Copy it to "kword" and go a |
| 6458 | * level deeper. |
| 6459 | */ |
| 6460 | if (round[depth] == 1) |
| 6461 | { |
| 6462 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 6463 | flen); |
| 6464 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 6465 | } |
| 6466 | else |
| 6467 | { |
| 6468 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 6469 | ulen); |
| 6470 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 6471 | } |
| 6472 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 6473 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 6474 | |
| 6475 | ++depth; |
| 6476 | arridx[depth] = tryidx; |
| 6477 | round[depth] = 0; |
| 6478 | } |
| 6479 | } |
| 6480 | } |
| 6481 | |
| 6482 | /* Didn't find it: "cannot happen". */ |
| 6483 | *kword = NUL; |
| 6484 | } |
| 6485 | |
| 6486 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6487 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 6488 | * su->su_sga. |
| 6489 | */ |
| 6490 | static void |
| 6491 | score_comp_sal(su) |
| 6492 | suginfo_T *su; |
| 6493 | { |
| 6494 | langp_T *lp; |
| 6495 | char_u badsound[MAXWLEN]; |
| 6496 | int i; |
| 6497 | suggest_T *stp; |
| 6498 | suggest_T *sstp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6499 | int score; |
| 6500 | |
| 6501 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 6502 | return; |
| 6503 | |
| 6504 | /* Use the sound-folding of the first language that supports it. */ |
| 6505 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6506 | lp->lp_slang != NULL; ++lp) |
| 6507 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6508 | { |
| 6509 | /* soundfold the bad word */ |
| 6510 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 6511 | |
| 6512 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6513 | { |
| 6514 | stp = &SUG(su->su_ga, i); |
| 6515 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6516 | /* Case-fold the suggested word, sound-fold it and compute the |
| 6517 | * sound-a-like score. */ |
| 6518 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6519 | if (score < SCORE_MAXMAX) |
| 6520 | { |
| 6521 | /* Add the suggestion. */ |
| 6522 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 6523 | sstp->st_word = vim_strsave(stp->st_word); |
| 6524 | if (sstp->st_word != NULL) |
| 6525 | { |
| 6526 | sstp->st_score = score; |
| 6527 | sstp->st_altscore = 0; |
| 6528 | sstp->st_orglen = stp->st_orglen; |
| 6529 | ++su->su_sga.ga_len; |
| 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | break; |
| 6534 | } |
| 6535 | } |
| 6536 | |
| 6537 | /* |
| 6538 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 6539 | * They are intwined. |
| 6540 | */ |
| 6541 | static void |
| 6542 | score_combine(su) |
| 6543 | suginfo_T *su; |
| 6544 | { |
| 6545 | int i; |
| 6546 | int j; |
| 6547 | garray_T ga; |
| 6548 | garray_T *gap; |
| 6549 | langp_T *lp; |
| 6550 | suggest_T *stp; |
| 6551 | char_u *p; |
| 6552 | char_u badsound[MAXWLEN]; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6553 | int round; |
| 6554 | |
| 6555 | /* Add the alternate score to su_ga. */ |
| 6556 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6557 | lp->lp_slang != NULL; ++lp) |
| 6558 | { |
| 6559 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6560 | { |
| 6561 | /* soundfold the bad word */ |
| 6562 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 6563 | |
| 6564 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6565 | { |
| 6566 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6567 | stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang, |
| 6568 | badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6569 | if (stp->st_altscore == SCORE_MAXMAX) |
| 6570 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 6571 | else |
| 6572 | stp->st_score = (stp->st_score * 3 |
| 6573 | + stp->st_altscore) / 4; |
| 6574 | stp->st_salscore = FALSE; |
| 6575 | } |
| 6576 | break; |
| 6577 | } |
| 6578 | } |
| 6579 | |
| 6580 | /* Add the alternate score to su_sga. */ |
| 6581 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6582 | { |
| 6583 | stp = &SUG(su->su_sga, i); |
| 6584 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 6585 | if (stp->st_score == SCORE_MAXMAX) |
| 6586 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 6587 | else |
| 6588 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 6589 | stp->st_salscore = TRUE; |
| 6590 | } |
| 6591 | |
| 6592 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 6593 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6594 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 6595 | |
| 6596 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 6597 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 6598 | return; |
| 6599 | |
| 6600 | stp = &SUG(ga, 0); |
| 6601 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 6602 | { |
| 6603 | /* round 1: get a suggestion from su_ga |
| 6604 | * round 2: get a suggestion from su_sga */ |
| 6605 | for (round = 1; round <= 2; ++round) |
| 6606 | { |
| 6607 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 6608 | if (i < gap->ga_len) |
| 6609 | { |
| 6610 | /* Don't add a word if it's already there. */ |
| 6611 | p = SUG(*gap, i).st_word; |
| 6612 | for (j = 0; j < ga.ga_len; ++j) |
| 6613 | if (STRCMP(stp[j].st_word, p) == 0) |
| 6614 | break; |
| 6615 | if (j == ga.ga_len) |
| 6616 | stp[ga.ga_len++] = SUG(*gap, i); |
| 6617 | else |
| 6618 | vim_free(p); |
| 6619 | } |
| 6620 | } |
| 6621 | } |
| 6622 | |
| 6623 | ga_clear(&su->su_ga); |
| 6624 | ga_clear(&su->su_sga); |
| 6625 | |
| 6626 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 6627 | if (ga.ga_len > su->su_maxcount) |
| 6628 | { |
| 6629 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 6630 | vim_free(stp[i].st_word); |
| 6631 | ga.ga_len = su->su_maxcount; |
| 6632 | } |
| 6633 | |
| 6634 | su->su_ga = ga; |
| 6635 | } |
| 6636 | |
| 6637 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6638 | * For the goodword in "stp" compute the soundalike score compared to the |
| 6639 | * badword. |
| 6640 | */ |
| 6641 | static int |
| 6642 | stp_sal_score(stp, su, slang, badsound) |
| 6643 | suggest_T *stp; |
| 6644 | suginfo_T *su; |
| 6645 | slang_T *slang; |
| 6646 | char_u *badsound; /* sound-folded badword */ |
| 6647 | { |
| 6648 | char_u *p; |
| 6649 | char_u badsound2[MAXWLEN]; |
| 6650 | char_u fword[MAXWLEN]; |
| 6651 | char_u goodsound[MAXWLEN]; |
| 6652 | |
| 6653 | if (stp->st_orglen <= su->su_badlen) |
| 6654 | p = badsound; |
| 6655 | else |
| 6656 | { |
| 6657 | /* soundfold the bad word with more characters following */ |
| 6658 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 6659 | |
| 6660 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 6661 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 6662 | * removing the space. Don't do it when the good word also contains a |
| 6663 | * space. */ |
| 6664 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 6665 | && *skiptowhite(stp->st_word) == NUL) |
| 6666 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| 6667 | mch_memmove(p, p + 1, STRLEN(p)); |
| 6668 | |
| 6669 | spell_soundfold(slang, fword, badsound2); |
| 6670 | p = badsound2; |
| 6671 | } |
| 6672 | |
| 6673 | /* Case-fold the word, sound-fold the word and compute the score for the |
| 6674 | * difference. */ |
| 6675 | (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), fword, MAXWLEN); |
| 6676 | spell_soundfold(slang, fword, goodsound); |
| 6677 | |
| 6678 | return soundalike_score(goodsound, p); |
| 6679 | } |
| 6680 | |
| 6681 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6682 | * Find suggestions by comparing the word in a sound-a-like form. |
| 6683 | */ |
| 6684 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6685 | suggest_try_soundalike(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6686 | suginfo_T *su; |
| 6687 | { |
| 6688 | char_u salword[MAXWLEN]; |
| 6689 | char_u tword[MAXWLEN]; |
| 6690 | char_u tfword[MAXWLEN]; |
| 6691 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6692 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6693 | int curi[MAXWLEN]; |
| 6694 | langp_T *lp; |
| 6695 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6696 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6697 | int depth; |
| 6698 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6699 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6700 | int round; |
| 6701 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6702 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6703 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6704 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6705 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6706 | lp->lp_slang != NULL; ++lp) |
| 6707 | { |
| 6708 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6709 | { |
| 6710 | /* soundfold the bad word */ |
| 6711 | spell_soundfold(lp->lp_slang, su->su_fbadword, salword); |
| 6712 | |
| 6713 | /* |
| 6714 | * Go through the whole tree, soundfold each word and compare. |
| 6715 | * round 1: use the case-folded tree. |
| 6716 | * round 2: use the keep-case tree. |
| 6717 | */ |
| 6718 | for (round = 1; round <= 2; ++round) |
| 6719 | { |
| 6720 | if (round == 1) |
| 6721 | { |
| 6722 | byts = lp->lp_slang->sl_fbyts; |
| 6723 | idxs = lp->lp_slang->sl_fidxs; |
| 6724 | } |
| 6725 | else |
| 6726 | { |
| 6727 | byts = lp->lp_slang->sl_kbyts; |
| 6728 | idxs = lp->lp_slang->sl_kidxs; |
| 6729 | } |
| 6730 | |
| 6731 | depth = 0; |
| 6732 | arridx[0] = 0; |
| 6733 | curi[0] = 1; |
| 6734 | while (depth >= 0 && !got_int) |
| 6735 | { |
| 6736 | if (curi[depth] > byts[arridx[depth]]) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6737 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6738 | /* Done all bytes at this node, go up one level. */ |
| 6739 | --depth; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6740 | line_breakcheck(); |
| 6741 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6742 | else |
| 6743 | { |
| 6744 | /* Do one more byte at this node. */ |
| 6745 | n = arridx[depth] + curi[depth]; |
| 6746 | ++curi[depth]; |
| 6747 | c = byts[n]; |
| 6748 | if (c == 0) |
| 6749 | { |
| 6750 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6751 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6752 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 6753 | { |
| 6754 | tword[depth] = NUL; |
| 6755 | if (round == 1) |
| 6756 | spell_soundfold(lp->lp_slang, |
| 6757 | tword, tsalword); |
| 6758 | else |
| 6759 | { |
| 6760 | /* In keep-case tree need to case-fold the |
| 6761 | * word. */ |
| 6762 | (void)spell_casefold(tword, depth, |
| 6763 | tfword, MAXWLEN); |
| 6764 | spell_soundfold(lp->lp_slang, |
| 6765 | tfword, tsalword); |
| 6766 | } |
| 6767 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6768 | /* Compute the edit distance between the |
| 6769 | * sound-a-like words. */ |
| 6770 | sound_score = soundalike_score(salword, |
| 6771 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6772 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6773 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6774 | char_u cword[MAXWLEN]; |
| 6775 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6776 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6777 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6778 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6779 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6780 | /* Need to fix case according to |
| 6781 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6782 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6783 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6784 | } |
| 6785 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6786 | p = tword; |
| 6787 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6788 | if (sps_flags & SPS_DOUBLE) |
| 6789 | add_suggestion(su, &su->su_sga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6790 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6791 | sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6792 | else |
| 6793 | { |
| 6794 | /* Compute the score. */ |
| 6795 | score = spell_edit_score( |
| 6796 | su->su_badword, p); |
| 6797 | if (sps_flags & SPS_BEST) |
| 6798 | /* give a bonus for the good word |
| 6799 | * sounding the same as the bad |
| 6800 | * word */ |
| 6801 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6802 | su->su_badlen, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6803 | RESCORE(score, sound_score), |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6804 | sound_score, TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6805 | else |
| 6806 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6807 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6808 | score + sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6809 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6810 | } |
| 6811 | } |
| 6812 | |
| 6813 | /* Skip over other NUL bytes. */ |
| 6814 | while (byts[n + 1] == 0) |
| 6815 | { |
| 6816 | ++n; |
| 6817 | ++curi[depth]; |
| 6818 | } |
| 6819 | } |
| 6820 | else |
| 6821 | { |
| 6822 | /* Normal char, go one level deeper. */ |
| 6823 | tword[depth++] = c; |
| 6824 | arridx[depth] = idxs[n]; |
| 6825 | curi[depth] = 1; |
| 6826 | } |
| 6827 | } |
| 6828 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6829 | } |
| 6830 | } |
| 6831 | } |
| 6832 | } |
| 6833 | |
| 6834 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6835 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6836 | */ |
| 6837 | static void |
| 6838 | make_case_word(fword, cword, flags) |
| 6839 | char_u *fword; |
| 6840 | char_u *cword; |
| 6841 | int flags; |
| 6842 | { |
| 6843 | if (flags & WF_ALLCAP) |
| 6844 | /* Make it all upper-case */ |
| 6845 | allcap_copy(fword, cword); |
| 6846 | else if (flags & WF_ONECAP) |
| 6847 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6848 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6849 | else |
| 6850 | /* Use goodword as-is. */ |
| 6851 | STRCPY(cword, fword); |
| 6852 | } |
| 6853 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6854 | /* |
| 6855 | * Use map string "map" for languages "lp". |
| 6856 | */ |
| 6857 | static void |
| 6858 | set_map_str(lp, map) |
| 6859 | slang_T *lp; |
| 6860 | char_u *map; |
| 6861 | { |
| 6862 | char_u *p; |
| 6863 | int headc = 0; |
| 6864 | int c; |
| 6865 | int i; |
| 6866 | |
| 6867 | if (*map == NUL) |
| 6868 | { |
| 6869 | lp->sl_has_map = FALSE; |
| 6870 | return; |
| 6871 | } |
| 6872 | lp->sl_has_map = TRUE; |
| 6873 | |
| 6874 | /* Init the array and hash table empty. */ |
| 6875 | for (i = 0; i < 256; ++i) |
| 6876 | lp->sl_map_array[i] = 0; |
| 6877 | #ifdef FEAT_MBYTE |
| 6878 | hash_init(&lp->sl_map_hash); |
| 6879 | #endif |
| 6880 | |
| 6881 | /* |
| 6882 | * The similar characters are stored separated with slashes: |
| 6883 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 6884 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 6885 | */ |
| 6886 | for (p = map; *p != NUL; ) |
| 6887 | { |
| 6888 | #ifdef FEAT_MBYTE |
| 6889 | c = mb_ptr2char_adv(&p); |
| 6890 | #else |
| 6891 | c = *p++; |
| 6892 | #endif |
| 6893 | if (c == '/') |
| 6894 | headc = 0; |
| 6895 | else |
| 6896 | { |
| 6897 | if (headc == 0) |
| 6898 | headc = c; |
| 6899 | |
| 6900 | #ifdef FEAT_MBYTE |
| 6901 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 6902 | * the hash table. Each entry is the char, a NUL the headchar and |
| 6903 | * a NUL. */ |
| 6904 | if (c >= 256) |
| 6905 | { |
| 6906 | int cl = mb_char2len(c); |
| 6907 | int headcl = mb_char2len(headc); |
| 6908 | char_u *b; |
| 6909 | hash_T hash; |
| 6910 | hashitem_T *hi; |
| 6911 | |
| 6912 | b = alloc((unsigned)(cl + headcl + 2)); |
| 6913 | if (b == NULL) |
| 6914 | return; |
| 6915 | mb_char2bytes(c, b); |
| 6916 | b[cl] = NUL; |
| 6917 | mb_char2bytes(headc, b + cl + 1); |
| 6918 | b[cl + 1 + headcl] = NUL; |
| 6919 | hash = hash_hash(b); |
| 6920 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 6921 | if (HASHITEM_EMPTY(hi)) |
| 6922 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 6923 | else |
| 6924 | { |
| 6925 | /* This should have been checked when generating the .spl |
| 6926 | * file. */ |
| 6927 | EMSG(_("E999: duplicate char in MAP entry")); |
| 6928 | vim_free(b); |
| 6929 | } |
| 6930 | } |
| 6931 | else |
| 6932 | #endif |
| 6933 | lp->sl_map_array[c] = headc; |
| 6934 | } |
| 6935 | } |
| 6936 | } |
| 6937 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6938 | /* |
| 6939 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 6940 | * lines in the .aff file. |
| 6941 | */ |
| 6942 | static int |
| 6943 | similar_chars(slang, c1, c2) |
| 6944 | slang_T *slang; |
| 6945 | int c1; |
| 6946 | int c2; |
| 6947 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6948 | int m1, m2; |
| 6949 | #ifdef FEAT_MBYTE |
| 6950 | char_u buf[MB_MAXBYTES]; |
| 6951 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6952 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6953 | if (c1 >= 256) |
| 6954 | { |
| 6955 | buf[mb_char2bytes(c1, buf)] = 0; |
| 6956 | hi = hash_find(&slang->sl_map_hash, buf); |
| 6957 | if (HASHITEM_EMPTY(hi)) |
| 6958 | m1 = 0; |
| 6959 | else |
| 6960 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 6961 | } |
| 6962 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6963 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6964 | m1 = slang->sl_map_array[c1]; |
| 6965 | if (m1 == 0) |
| 6966 | return FALSE; |
| 6967 | |
| 6968 | |
| 6969 | #ifdef FEAT_MBYTE |
| 6970 | if (c2 >= 256) |
| 6971 | { |
| 6972 | buf[mb_char2bytes(c2, buf)] = 0; |
| 6973 | hi = hash_find(&slang->sl_map_hash, buf); |
| 6974 | if (HASHITEM_EMPTY(hi)) |
| 6975 | m2 = 0; |
| 6976 | else |
| 6977 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 6978 | } |
| 6979 | else |
| 6980 | #endif |
| 6981 | m2 = slang->sl_map_array[c2]; |
| 6982 | |
| 6983 | return m1 == m2; |
| 6984 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6985 | |
| 6986 | /* |
| 6987 | * Add a suggestion to the list of suggestions. |
| 6988 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 6989 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 6990 | * with spell_edit_score(). |
| 6991 | */ |
| 6992 | static void |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6993 | add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6994 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6995 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6996 | char_u *goodword; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6997 | int badlen; /* length of bad word used */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6998 | int score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6999 | int altscore; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7000 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7001 | { |
| 7002 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7003 | int i; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7004 | char_u *p = NULL; |
| 7005 | int c = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7006 | |
| 7007 | /* Check that the word wasn't banned. */ |
| 7008 | if (was_banned(su, goodword)) |
| 7009 | return; |
| 7010 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7011 | /* If past "su_badlen" and the rest is identical stop at "su_badlen". |
| 7012 | * Remove the common part from "goodword". */ |
| 7013 | i = badlen - su->su_badlen; |
| 7014 | if (i > 0) |
| 7015 | { |
| 7016 | /* This assumes there was no case folding or it didn't change the |
| 7017 | * length... */ |
| 7018 | p = goodword + STRLEN(goodword) - i; |
| 7019 | if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0) |
| 7020 | { |
| 7021 | badlen = su->su_badlen; |
| 7022 | c = *p; |
| 7023 | *p = NUL; |
| 7024 | } |
| 7025 | else |
| 7026 | p = NULL; |
| 7027 | } |
| 7028 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7029 | if (score <= su->su_maxscore) |
| 7030 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7031 | /* Check if the word is already there. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7032 | stp = &SUG(*gap, 0); |
| 7033 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7034 | if (STRCMP(stp[i].st_word, goodword) == 0) |
| 7035 | { |
| 7036 | /* Found it. Remember the lowest score. */ |
| 7037 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7038 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7039 | stp[i].st_score = score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7040 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7041 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7042 | break; |
| 7043 | } |
| 7044 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7045 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7046 | { |
| 7047 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7048 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7049 | stp->st_word = vim_strsave(goodword); |
| 7050 | if (stp->st_word != NULL) |
| 7051 | { |
| 7052 | stp->st_score = score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7053 | stp->st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7054 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7055 | stp->st_orglen = badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7056 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7057 | |
| 7058 | /* If we have too many suggestions now, sort the list and keep |
| 7059 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7060 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 7061 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 7062 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7063 | } |
| 7064 | } |
| 7065 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7066 | |
| 7067 | if (p != NULL) |
| 7068 | *p = c; /* restore "goodword" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | /* |
| 7072 | * Add a word to be banned. |
| 7073 | */ |
| 7074 | static void |
| 7075 | add_banned(su, word) |
| 7076 | suginfo_T *su; |
| 7077 | char_u *word; |
| 7078 | { |
| 7079 | char_u *s = vim_strsave(word); |
| 7080 | hash_T hash; |
| 7081 | hashitem_T *hi; |
| 7082 | |
| 7083 | if (s != NULL) |
| 7084 | { |
| 7085 | hash = hash_hash(s); |
| 7086 | hi = hash_lookup(&su->su_banned, s, hash); |
| 7087 | if (HASHITEM_EMPTY(hi)) |
| 7088 | hash_add_item(&su->su_banned, hi, s, hash); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 7089 | else |
| 7090 | vim_free(s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7091 | } |
| 7092 | } |
| 7093 | |
| 7094 | /* |
| 7095 | * Return TRUE if a word appears in the list of banned words. |
| 7096 | */ |
| 7097 | static int |
| 7098 | was_banned(su, word) |
| 7099 | suginfo_T *su; |
| 7100 | char_u *word; |
| 7101 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7102 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 7103 | |
| 7104 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7105 | } |
| 7106 | |
| 7107 | /* |
| 7108 | * Free the banned words in "su". |
| 7109 | */ |
| 7110 | static void |
| 7111 | free_banned(su) |
| 7112 | suginfo_T *su; |
| 7113 | { |
| 7114 | int todo; |
| 7115 | hashitem_T *hi; |
| 7116 | |
| 7117 | todo = su->su_banned.ht_used; |
| 7118 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 7119 | { |
| 7120 | if (!HASHITEM_EMPTY(hi)) |
| 7121 | { |
| 7122 | vim_free(hi->hi_key); |
| 7123 | --todo; |
| 7124 | } |
| 7125 | } |
| 7126 | hash_clear(&su->su_banned); |
| 7127 | } |
| 7128 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7129 | /* |
| 7130 | * Recompute the score if sound-folding is possible. This is slow, |
| 7131 | * thus only done for the final results. |
| 7132 | */ |
| 7133 | static void |
| 7134 | rescore_suggestions(su) |
| 7135 | suginfo_T *su; |
| 7136 | { |
| 7137 | langp_T *lp; |
| 7138 | suggest_T *stp; |
| 7139 | char_u sal_badword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7140 | int i; |
| 7141 | |
| 7142 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7143 | lp->lp_slang != NULL; ++lp) |
| 7144 | { |
| 7145 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7146 | { |
| 7147 | /* soundfold the bad word */ |
| 7148 | spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword); |
| 7149 | |
| 7150 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7151 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7152 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7153 | if (!stp->st_had_bonus) |
| 7154 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7155 | stp->st_altscore = stp_sal_score(stp, su, |
| 7156 | lp->lp_slang, sal_badword); |
| 7157 | if (stp->st_altscore == SCORE_MAXMAX) |
| 7158 | stp->st_altscore = SCORE_BIG; |
| 7159 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7160 | } |
| 7161 | } |
| 7162 | break; |
| 7163 | } |
| 7164 | } |
| 7165 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7166 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7167 | static int |
| 7168 | #ifdef __BORLANDC__ |
| 7169 | _RTLENTRYF |
| 7170 | #endif |
| 7171 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 7172 | |
| 7173 | /* |
| 7174 | * Function given to qsort() to sort the suggestions on st_score. |
| 7175 | */ |
| 7176 | static int |
| 7177 | #ifdef __BORLANDC__ |
| 7178 | _RTLENTRYF |
| 7179 | #endif |
| 7180 | sug_compare(s1, s2) |
| 7181 | const void *s1; |
| 7182 | const void *s2; |
| 7183 | { |
| 7184 | suggest_T *p1 = (suggest_T *)s1; |
| 7185 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7186 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7187 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7188 | if (n == 0) |
| 7189 | return p1->st_altscore - p2->st_altscore; |
| 7190 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7191 | } |
| 7192 | |
| 7193 | /* |
| 7194 | * Cleanup the suggestions: |
| 7195 | * - Sort on score. |
| 7196 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7197 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7198 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7199 | static int |
| 7200 | cleanup_suggestions(gap, maxscore, keep) |
| 7201 | garray_T *gap; |
| 7202 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7203 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7204 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7205 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7206 | int i; |
| 7207 | |
| 7208 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7209 | 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] | 7210 | |
| 7211 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7212 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7213 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7214 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7215 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7216 | gap->ga_len = keep; |
| 7217 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7218 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7219 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
| 7222 | /* |
| 7223 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 7224 | */ |
| 7225 | static void |
| 7226 | spell_soundfold(slang, inword, res) |
| 7227 | slang_T *slang; |
| 7228 | char_u *inword; |
| 7229 | char_u *res; |
| 7230 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7231 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7232 | char_u word[MAXWLEN]; |
| 7233 | #ifdef FEAT_MBYTE |
| 7234 | int l; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7235 | int found_mbyte = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7236 | #endif |
| 7237 | char_u *s; |
| 7238 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7239 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7240 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7241 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7242 | int n, k = 0; |
| 7243 | int z0; |
| 7244 | int k0; |
| 7245 | int n0; |
| 7246 | int c; |
| 7247 | int pri; |
| 7248 | int p0 = -333; |
| 7249 | int c0; |
| 7250 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7251 | /* Remove accents, if wanted. We actually remove all non-word characters. |
| 7252 | * But keep white space. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7253 | if (slang->sl_rem_accents) |
| 7254 | { |
| 7255 | t = word; |
| 7256 | for (s = inword; *s != NUL; ) |
| 7257 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7258 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7259 | { |
| 7260 | *t++ = ' '; |
| 7261 | s = skipwhite(s); |
| 7262 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7263 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7264 | else if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7265 | { |
| 7266 | l = mb_ptr2len_check(s); |
| 7267 | if (SPELL_ISWORDP(s)) |
| 7268 | { |
| 7269 | mch_memmove(t, s, l); |
| 7270 | t += l; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7271 | if (l > 1) |
| 7272 | found_mbyte = TRUE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7273 | } |
| 7274 | s += l; |
| 7275 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7276 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7277 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7278 | { |
| 7279 | if (SPELL_ISWORDP(s)) |
| 7280 | *t++ = *s; |
| 7281 | ++s; |
| 7282 | } |
| 7283 | } |
| 7284 | *t = NUL; |
| 7285 | } |
| 7286 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7287 | { |
| 7288 | #ifdef FEAT_MBYTE |
| 7289 | if (has_mbyte) |
| 7290 | for (s = inword; *s != NUL; s += l) |
| 7291 | if ((l = mb_ptr2len_check(s)) > 1) |
| 7292 | { |
| 7293 | found_mbyte = TRUE; |
| 7294 | break; |
| 7295 | } |
| 7296 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7297 | STRCPY(word, inword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7298 | } |
| 7299 | |
| 7300 | #ifdef FEAT_MBYTE |
| 7301 | /* If there are multi-byte characters in the word return it as-is, because |
| 7302 | * the following won't work. */ |
| 7303 | if (found_mbyte) |
| 7304 | { |
| 7305 | STRCPY(res, word); |
| 7306 | return; |
| 7307 | } |
| 7308 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7309 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7310 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7311 | |
| 7312 | /* |
| 7313 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7314 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7315 | * TODO: support for multi-byte chars. |
| 7316 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7317 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7318 | while ((c = word[i]) != NUL) |
| 7319 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7320 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7321 | n = slang->sl_sal_first[c]; |
| 7322 | z0 = 0; |
| 7323 | |
| 7324 | if (n >= 0) |
| 7325 | { |
| 7326 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7327 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7328 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7329 | /* Quickly skip entries that don't match the word. Most |
| 7330 | * entries are less then three chars, optimize for that. */ |
| 7331 | k = smp[n].sm_leadlen; |
| 7332 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7333 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7334 | if (word[i + 1] != s[1]) |
| 7335 | continue; |
| 7336 | if (k > 2) |
| 7337 | { |
| 7338 | for (j = 2; j < k; ++j) |
| 7339 | if (word[i + j] != s[j]) |
| 7340 | break; |
| 7341 | if (j < k) |
| 7342 | continue; |
| 7343 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7344 | } |
| 7345 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7346 | if ((pf = smp[n].sm_oneoff) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7347 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7348 | /* Check for match with one of the chars in "sm_oneoff". */ |
| 7349 | while (*pf != NUL && *pf != word[i + k]) |
| 7350 | ++pf; |
| 7351 | if (*pf == NUL) |
| 7352 | continue; |
| 7353 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7354 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7355 | s = smp[n].sm_rules; |
| 7356 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7357 | |
| 7358 | p0 = *s; |
| 7359 | k0 = k; |
| 7360 | while (*s == '-' && k > 1) |
| 7361 | { |
| 7362 | k--; |
| 7363 | s++; |
| 7364 | } |
| 7365 | if (*s == '<') |
| 7366 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7367 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7368 | { |
| 7369 | /* determine priority */ |
| 7370 | pri = *s - '0'; |
| 7371 | s++; |
| 7372 | } |
| 7373 | if (*s == '^' && *(s + 1) == '^') |
| 7374 | s++; |
| 7375 | |
| 7376 | if (*s == NUL |
| 7377 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7378 | && (i == 0 || !(word[i - 1] == ' ' |
| 7379 | || SPELL_ISWORDP(word + i - 1))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7380 | && (*(s + 1) != '$' |
| 7381 | || (!SPELL_ISWORDP(word + i + k0)))) |
| 7382 | || (*s == '$' && i > 0 |
| 7383 | && SPELL_ISWORDP(word + i - 1) |
| 7384 | && (!SPELL_ISWORDP(word + i + k0)))) |
| 7385 | { |
| 7386 | /* search for followup rules, if: */ |
| 7387 | /* followup and k > 1 and NO '-' in searchstring */ |
| 7388 | c0 = word[i + k - 1]; |
| 7389 | n0 = slang->sl_sal_first[c0]; |
| 7390 | |
| 7391 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7392 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7393 | { |
| 7394 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7395 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7396 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7397 | /* Quickly skip entries that don't match the word. |
| 7398 | * */ |
| 7399 | k0 = smp[n0].sm_leadlen; |
| 7400 | if (k0 > 1) |
| 7401 | { |
| 7402 | if (word[i + k] != s[1]) |
| 7403 | continue; |
| 7404 | if (k0 > 2) |
| 7405 | { |
| 7406 | pf = word + i + k + 1; |
| 7407 | for (j = 2; j < k0; ++j) |
| 7408 | if (*pf++ != s[j]) |
| 7409 | break; |
| 7410 | if (j < k0) |
| 7411 | continue; |
| 7412 | } |
| 7413 | } |
| 7414 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7415 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7416 | if ((pf = smp[n0].sm_oneoff) != NULL) |
| 7417 | { |
| 7418 | /* Check for match with one of the chars in |
| 7419 | * "sm_oneoff". */ |
| 7420 | while (*pf != NUL && *pf != word[i + k0]) |
| 7421 | ++pf; |
| 7422 | if (*pf == NUL) |
| 7423 | continue; |
| 7424 | ++k0; |
| 7425 | } |
| 7426 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7427 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7428 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7429 | while (*s == '-') |
| 7430 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7431 | /* "k0" gets NOT reduced because |
| 7432 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7433 | s++; |
| 7434 | } |
| 7435 | if (*s == '<') |
| 7436 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7437 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7438 | { |
| 7439 | p0 = *s - '0'; |
| 7440 | s++; |
| 7441 | } |
| 7442 | |
| 7443 | if (*s == NUL |
| 7444 | /* *s == '^' cuts */ |
| 7445 | || (*s == '$' |
| 7446 | && !SPELL_ISWORDP(word + i + k0))) |
| 7447 | { |
| 7448 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7449 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7450 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7451 | |
| 7452 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7453 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7454 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7455 | /* rule fits; stop search */ |
| 7456 | break; |
| 7457 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7460 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7461 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7462 | } |
| 7463 | |
| 7464 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7465 | s = smp[n].sm_to; |
| 7466 | pf = smp[n].sm_rules; |
| 7467 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7468 | if (p0 == 1 && z == 0) |
| 7469 | { |
| 7470 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7471 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 7472 | || res[reslen - 1] == *s)) |
| 7473 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7474 | z0 = 1; |
| 7475 | z = 1; |
| 7476 | k0 = 0; |
| 7477 | while (*s != NUL && word[i+k0] != NUL) |
| 7478 | { |
| 7479 | word[i + k0] = *s; |
| 7480 | k0++; |
| 7481 | s++; |
| 7482 | } |
| 7483 | if (k > k0) |
| 7484 | mch_memmove(word + i + k0, word + i + k, |
| 7485 | STRLEN(word + i + k) + 1); |
| 7486 | |
| 7487 | /* new "actual letter" */ |
| 7488 | c = word[i]; |
| 7489 | } |
| 7490 | else |
| 7491 | { |
| 7492 | /* no '<' rule used */ |
| 7493 | i += k - 1; |
| 7494 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7495 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7496 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7497 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7498 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7499 | res[reslen] = *s; |
| 7500 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7501 | } |
| 7502 | s++; |
| 7503 | } |
| 7504 | /* new "actual letter" */ |
| 7505 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7506 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7507 | { |
| 7508 | if (c != NUL) |
| 7509 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7510 | res[reslen] = c; |
| 7511 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7512 | } |
| 7513 | mch_memmove(word, word + i + 1, |
| 7514 | STRLEN(word + i + 1) + 1); |
| 7515 | i = 0; |
| 7516 | z0 = 1; |
| 7517 | } |
| 7518 | } |
| 7519 | break; |
| 7520 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7521 | } |
| 7522 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7523 | else if (vim_iswhite(c)) |
| 7524 | { |
| 7525 | c = ' '; |
| 7526 | k = 1; |
| 7527 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7528 | |
| 7529 | if (z0 == 0) |
| 7530 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7531 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 7532 | && (!slang->sl_collapse || reslen == 0 |
| 7533 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7534 | { |
| 7535 | /* condense only double letters */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7536 | res[reslen] = c; |
| 7537 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | i++; |
| 7541 | z = 0; |
| 7542 | k = 0; |
| 7543 | } |
| 7544 | } |
| 7545 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7546 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7547 | } |
| 7548 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7549 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7550 | * Compute a score for two sound-a-like words. |
| 7551 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 7552 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 7553 | * avoiding checks that will not be possible. |
| 7554 | */ |
| 7555 | static int |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7556 | soundalike_score(goodstart, badstart) |
| 7557 | char_u *goodstart; /* sound-folded good word */ |
| 7558 | char_u *badstart; /* sound-folded bad word */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7559 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7560 | char_u *goodsound = goodstart; |
| 7561 | char_u *badsound = badstart; |
| 7562 | int goodlen; |
| 7563 | int badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7564 | int n; |
| 7565 | char_u *pl, *ps; |
| 7566 | char_u *pl2, *ps2; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7567 | int score = 0; |
| 7568 | |
| 7569 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 7570 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 7571 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 7572 | { |
| 7573 | score = SCORE_DEL / 2; |
| 7574 | if (*badsound == '*') |
| 7575 | ++badsound; |
| 7576 | else |
| 7577 | ++goodsound; |
| 7578 | } |
| 7579 | |
| 7580 | goodlen = STRLEN(goodsound); |
| 7581 | badlen = STRLEN(badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7582 | |
| 7583 | /* Return quickly if the lenghts are too different to be fixed by two |
| 7584 | * changes. */ |
| 7585 | n = goodlen - badlen; |
| 7586 | if (n < -2 || n > 2) |
| 7587 | return SCORE_MAXMAX; |
| 7588 | |
| 7589 | if (n > 0) |
| 7590 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7591 | pl = goodsound; /* goodsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7592 | ps = badsound; |
| 7593 | } |
| 7594 | else |
| 7595 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7596 | pl = badsound; /* badsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7597 | ps = goodsound; |
| 7598 | } |
| 7599 | |
| 7600 | /* Skip over the identical part. */ |
| 7601 | while (*pl == *ps && *pl != NUL) |
| 7602 | { |
| 7603 | ++pl; |
| 7604 | ++ps; |
| 7605 | } |
| 7606 | |
| 7607 | switch (n) |
| 7608 | { |
| 7609 | case -2: |
| 7610 | case 2: |
| 7611 | /* |
| 7612 | * Must delete two characters from "pl". |
| 7613 | */ |
| 7614 | ++pl; /* first delete */ |
| 7615 | while (*pl == *ps) |
| 7616 | { |
| 7617 | ++pl; |
| 7618 | ++ps; |
| 7619 | } |
| 7620 | /* strings must be equal after second delete */ |
| 7621 | if (STRCMP(pl + 1, ps) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7622 | return score + SCORE_DEL * 2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7623 | |
| 7624 | /* Failed to compare. */ |
| 7625 | break; |
| 7626 | |
| 7627 | case -1: |
| 7628 | case 1: |
| 7629 | /* |
| 7630 | * Minimal one delete from "pl" required. |
| 7631 | */ |
| 7632 | |
| 7633 | /* 1: delete */ |
| 7634 | pl2 = pl + 1; |
| 7635 | ps2 = ps; |
| 7636 | while (*pl2 == *ps2) |
| 7637 | { |
| 7638 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7639 | return score + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7640 | ++pl2; |
| 7641 | ++ps2; |
| 7642 | } |
| 7643 | |
| 7644 | /* 2: delete then swap, then rest must be equal */ |
| 7645 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7646 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7647 | return score + SCORE_DEL + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7648 | |
| 7649 | /* 3: delete then substitute, then the rest must be equal */ |
| 7650 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7651 | return score + SCORE_DEL + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7652 | |
| 7653 | /* 4: first swap then delete */ |
| 7654 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 7655 | { |
| 7656 | pl2 = pl + 2; /* swap, skip two chars */ |
| 7657 | ps2 = ps + 2; |
| 7658 | while (*pl2 == *ps2) |
| 7659 | { |
| 7660 | ++pl2; |
| 7661 | ++ps2; |
| 7662 | } |
| 7663 | /* delete a char and then strings must be equal */ |
| 7664 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7665 | return score + SCORE_SWAP + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
| 7668 | /* 5: first substitute then delete */ |
| 7669 | pl2 = pl + 1; /* substitute, skip one char */ |
| 7670 | ps2 = ps + 1; |
| 7671 | while (*pl2 == *ps2) |
| 7672 | { |
| 7673 | ++pl2; |
| 7674 | ++ps2; |
| 7675 | } |
| 7676 | /* delete a char and then strings must be equal */ |
| 7677 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7678 | return score + SCORE_SUBST + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7679 | |
| 7680 | /* Failed to compare. */ |
| 7681 | break; |
| 7682 | |
| 7683 | case 0: |
| 7684 | /* |
| 7685 | * Lenghts are equal, thus changes must result in same length: An |
| 7686 | * insert is only possible in combination with a delete. |
| 7687 | * 1: check if for identical strings |
| 7688 | */ |
| 7689 | if (*pl == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7690 | return score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7691 | |
| 7692 | /* 2: swap */ |
| 7693 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 7694 | { |
| 7695 | pl2 = pl + 2; /* swap, skip two chars */ |
| 7696 | ps2 = ps + 2; |
| 7697 | while (*pl2 == *ps2) |
| 7698 | { |
| 7699 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7700 | return score + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7701 | ++pl2; |
| 7702 | ++ps2; |
| 7703 | } |
| 7704 | /* 3: swap and swap again */ |
| 7705 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7706 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7707 | return score + SCORE_SWAP + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7708 | |
| 7709 | /* 4: swap and substitute */ |
| 7710 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7711 | return score + SCORE_SWAP + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
| 7714 | /* 5: substitute */ |
| 7715 | pl2 = pl + 1; |
| 7716 | ps2 = ps + 1; |
| 7717 | while (*pl2 == *ps2) |
| 7718 | { |
| 7719 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7720 | return score + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7721 | ++pl2; |
| 7722 | ++ps2; |
| 7723 | } |
| 7724 | |
| 7725 | /* 6: substitute and swap */ |
| 7726 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7727 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7728 | return score + SCORE_SUBST + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7729 | |
| 7730 | /* 7: substitute and substitute */ |
| 7731 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7732 | return score + SCORE_SUBST + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7733 | |
| 7734 | /* 8: insert then delete */ |
| 7735 | pl2 = pl; |
| 7736 | ps2 = ps + 1; |
| 7737 | while (*pl2 == *ps2) |
| 7738 | { |
| 7739 | ++pl2; |
| 7740 | ++ps2; |
| 7741 | } |
| 7742 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7743 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7744 | |
| 7745 | /* 9: delete then insert */ |
| 7746 | pl2 = pl + 1; |
| 7747 | ps2 = ps; |
| 7748 | while (*pl2 == *ps2) |
| 7749 | { |
| 7750 | ++pl2; |
| 7751 | ++ps2; |
| 7752 | } |
| 7753 | if (STRCMP(pl2, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7754 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7755 | |
| 7756 | /* Failed to compare. */ |
| 7757 | break; |
| 7758 | } |
| 7759 | |
| 7760 | return SCORE_MAXMAX; |
| 7761 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7762 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7763 | /* |
| 7764 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7765 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7766 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7767 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7768 | * It has been converted from C++ to C and modified to support multi-byte |
| 7769 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7770 | */ |
| 7771 | static int |
| 7772 | spell_edit_score(badword, goodword) |
| 7773 | char_u *badword; |
| 7774 | char_u *goodword; |
| 7775 | { |
| 7776 | int *cnt; |
| 7777 | int badlen, goodlen; |
| 7778 | int j, i; |
| 7779 | int t; |
| 7780 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7781 | int pbc, pgc; |
| 7782 | #ifdef FEAT_MBYTE |
| 7783 | char_u *p; |
| 7784 | int wbadword[MAXWLEN]; |
| 7785 | int wgoodword[MAXWLEN]; |
| 7786 | |
| 7787 | if (has_mbyte) |
| 7788 | { |
| 7789 | /* Get the characters from the multi-byte strings and put them in an |
| 7790 | * int array for easy access. */ |
| 7791 | for (p = badword, badlen = 0; *p != NUL; ) |
| 7792 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
| 7793 | ++badlen; |
| 7794 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 7795 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
| 7796 | ++goodlen; |
| 7797 | } |
| 7798 | else |
| 7799 | #endif |
| 7800 | { |
| 7801 | badlen = STRLEN(badword) + 1; |
| 7802 | goodlen = STRLEN(goodword) + 1; |
| 7803 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7804 | |
| 7805 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 7806 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7807 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 7808 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7809 | if (cnt == NULL) |
| 7810 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7811 | |
| 7812 | CNT(0, 0) = 0; |
| 7813 | for (j = 1; j <= goodlen; ++j) |
| 7814 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 7815 | |
| 7816 | for (i = 1; i <= badlen; ++i) |
| 7817 | { |
| 7818 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 7819 | for (j = 1; j <= goodlen; ++j) |
| 7820 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7821 | #ifdef FEAT_MBYTE |
| 7822 | if (has_mbyte) |
| 7823 | { |
| 7824 | bc = wbadword[i - 1]; |
| 7825 | gc = wgoodword[j - 1]; |
| 7826 | } |
| 7827 | else |
| 7828 | #endif |
| 7829 | { |
| 7830 | bc = badword[i - 1]; |
| 7831 | gc = goodword[j - 1]; |
| 7832 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7833 | if (bc == gc) |
| 7834 | CNT(i, j) = CNT(i - 1, j - 1); |
| 7835 | else |
| 7836 | { |
| 7837 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7838 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7839 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 7840 | else |
| 7841 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 7842 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7843 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7844 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7845 | #ifdef FEAT_MBYTE |
| 7846 | if (has_mbyte) |
| 7847 | { |
| 7848 | pbc = wbadword[i - 2]; |
| 7849 | pgc = wgoodword[j - 2]; |
| 7850 | } |
| 7851 | else |
| 7852 | #endif |
| 7853 | { |
| 7854 | pbc = badword[i - 2]; |
| 7855 | pgc = goodword[j - 2]; |
| 7856 | } |
| 7857 | if (bc == pgc && pbc == gc) |
| 7858 | { |
| 7859 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 7860 | if (t < CNT(i, j)) |
| 7861 | CNT(i, j) = t; |
| 7862 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7863 | } |
| 7864 | t = SCORE_DEL + CNT(i - 1, j); |
| 7865 | if (t < CNT(i, j)) |
| 7866 | CNT(i, j) = t; |
| 7867 | t = SCORE_INS + CNT(i, j - 1); |
| 7868 | if (t < CNT(i, j)) |
| 7869 | CNT(i, j) = t; |
| 7870 | } |
| 7871 | } |
| 7872 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7873 | |
| 7874 | i = CNT(badlen - 1, goodlen - 1); |
| 7875 | vim_free(cnt); |
| 7876 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7877 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7878 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7879 | /* |
| 7880 | * ":spelldump" |
| 7881 | */ |
| 7882 | /*ARGSUSED*/ |
| 7883 | void |
| 7884 | ex_spelldump(eap) |
| 7885 | exarg_T *eap; |
| 7886 | { |
| 7887 | buf_T *buf = curbuf; |
| 7888 | langp_T *lp; |
| 7889 | slang_T *slang; |
| 7890 | idx_T arridx[MAXWLEN]; |
| 7891 | int curi[MAXWLEN]; |
| 7892 | char_u word[MAXWLEN]; |
| 7893 | int c; |
| 7894 | char_u *byts; |
| 7895 | idx_T *idxs; |
| 7896 | linenr_T lnum = 0; |
| 7897 | int round; |
| 7898 | int depth; |
| 7899 | int n; |
| 7900 | int flags; |
| 7901 | |
| 7902 | if (no_spell_checking()) |
| 7903 | return; |
| 7904 | |
| 7905 | /* Create a new empty buffer by splitting the window. */ |
| 7906 | do_cmdline_cmd((char_u *)"new"); |
| 7907 | if (!bufempty() || !buf_valid(buf)) |
| 7908 | return; |
| 7909 | |
| 7910 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 7911 | { |
| 7912 | slang = lp->lp_slang; |
| 7913 | |
| 7914 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 7915 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 7916 | |
| 7917 | /* round 1: case-folded tree |
| 7918 | * round 2: keep-case tree */ |
| 7919 | for (round = 1; round <= 2; ++round) |
| 7920 | { |
| 7921 | if (round == 1) |
| 7922 | { |
| 7923 | byts = slang->sl_fbyts; |
| 7924 | idxs = slang->sl_fidxs; |
| 7925 | } |
| 7926 | else |
| 7927 | { |
| 7928 | byts = slang->sl_kbyts; |
| 7929 | idxs = slang->sl_kidxs; |
| 7930 | } |
| 7931 | if (byts == NULL) |
| 7932 | continue; /* array is empty */ |
| 7933 | |
| 7934 | depth = 0; |
| 7935 | arridx[0] = 0; |
| 7936 | curi[0] = 1; |
| 7937 | while (depth >= 0 && !got_int) |
| 7938 | { |
| 7939 | if (curi[depth] > byts[arridx[depth]]) |
| 7940 | { |
| 7941 | /* Done all bytes at this node, go up one level. */ |
| 7942 | --depth; |
| 7943 | line_breakcheck(); |
| 7944 | } |
| 7945 | else |
| 7946 | { |
| 7947 | /* Do one more byte at this node. */ |
| 7948 | n = arridx[depth] + curi[depth]; |
| 7949 | ++curi[depth]; |
| 7950 | c = byts[n]; |
| 7951 | if (c == 0) |
| 7952 | { |
| 7953 | /* End of word, deal with the word. |
| 7954 | * Don't use keep-case words in the fold-case tree, |
| 7955 | * they will appear in the keep-case tree. |
| 7956 | * Only use the word when the region matches. */ |
| 7957 | flags = (int)idxs[n]; |
| 7958 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
| 7959 | && ((flags & WF_REGION) == 0 |
| 7960 | || (((unsigned)flags >> 8) |
| 7961 | & lp->lp_region) != 0)) |
| 7962 | { |
| 7963 | word[depth] = NUL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 7964 | |
| 7965 | /* Dump the basic word if there is no prefix or |
| 7966 | * when it's the first one. */ |
| 7967 | c = (unsigned)flags >> 16; |
| 7968 | if (c == 0 || curi[depth] == 2) |
| 7969 | dump_word(word, round, flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7970 | |
| 7971 | /* Apply the prefix, if there is one. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 7972 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7973 | lnum = apply_prefixes(slang, word, round, |
| 7974 | flags, lnum); |
| 7975 | } |
| 7976 | } |
| 7977 | else |
| 7978 | { |
| 7979 | /* Normal char, go one level deeper. */ |
| 7980 | word[depth++] = c; |
| 7981 | arridx[depth] = idxs[n]; |
| 7982 | curi[depth] = 1; |
| 7983 | } |
| 7984 | } |
| 7985 | } |
| 7986 | } |
| 7987 | } |
| 7988 | |
| 7989 | /* Delete the empty line that we started with. */ |
| 7990 | if (curbuf->b_ml.ml_line_count > 1) |
| 7991 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 7992 | |
| 7993 | redraw_later(NOT_VALID); |
| 7994 | } |
| 7995 | |
| 7996 | /* |
| 7997 | * Dump one word: apply case modifications and append a line to the buffer. |
| 7998 | */ |
| 7999 | static void |
| 8000 | dump_word(word, round, flags, lnum) |
| 8001 | char_u *word; |
| 8002 | int round; |
| 8003 | int flags; |
| 8004 | linenr_T lnum; |
| 8005 | { |
| 8006 | int keepcap = FALSE; |
| 8007 | char_u *p; |
| 8008 | char_u cword[MAXWLEN]; |
| 8009 | char_u badword[MAXWLEN + 3]; |
| 8010 | |
| 8011 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
| 8012 | { |
| 8013 | /* Need to fix case according to "flags". */ |
| 8014 | make_case_word(word, cword, flags); |
| 8015 | p = cword; |
| 8016 | } |
| 8017 | else |
| 8018 | { |
| 8019 | p = word; |
| 8020 | if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0) |
| 8021 | keepcap = TRUE; |
| 8022 | } |
| 8023 | |
| 8024 | /* Bad word is preceded by "/!" and some other |
| 8025 | * flags. */ |
| 8026 | if ((flags & (WF_BANNED | WF_RARE)) || keepcap) |
| 8027 | { |
| 8028 | STRCPY(badword, "/"); |
| 8029 | if (keepcap) |
| 8030 | STRCAT(badword, "="); |
| 8031 | if (flags & WF_BANNED) |
| 8032 | STRCAT(badword, "!"); |
| 8033 | else if (flags & WF_RARE) |
| 8034 | STRCAT(badword, "?"); |
| 8035 | STRCAT(badword, p); |
| 8036 | p = badword; |
| 8037 | } |
| 8038 | |
| 8039 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 8040 | } |
| 8041 | |
| 8042 | /* |
| 8043 | * Find matching prefixes for "word". Prepend each to "word" and append |
| 8044 | * a line to the buffer. |
| 8045 | * Return the updated line number. |
| 8046 | */ |
| 8047 | static linenr_T |
| 8048 | apply_prefixes(slang, word, round, flags, startlnum) |
| 8049 | slang_T *slang; |
| 8050 | char_u *word; /* case-folded word */ |
| 8051 | int round; |
| 8052 | int flags; /* flags with prefix ID */ |
| 8053 | linenr_T startlnum; |
| 8054 | { |
| 8055 | idx_T arridx[MAXWLEN]; |
| 8056 | int curi[MAXWLEN]; |
| 8057 | char_u prefix[MAXWLEN]; |
| 8058 | int c; |
| 8059 | char_u *byts; |
| 8060 | idx_T *idxs; |
| 8061 | linenr_T lnum = startlnum; |
| 8062 | int depth; |
| 8063 | int n; |
| 8064 | int len; |
| 8065 | int prefid = (unsigned)flags >> 16; |
| 8066 | int i; |
| 8067 | |
| 8068 | byts = slang->sl_pbyts; |
| 8069 | idxs = slang->sl_pidxs; |
| 8070 | if (byts != NULL) /* array not is empty */ |
| 8071 | { |
| 8072 | /* |
| 8073 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
| 8074 | * When at the end of a prefix check that it supports "prefid". |
| 8075 | */ |
| 8076 | depth = 0; |
| 8077 | arridx[0] = 0; |
| 8078 | curi[0] = 1; |
| 8079 | while (depth >= 0 && !got_int) |
| 8080 | { |
| 8081 | len = arridx[depth]; |
| 8082 | if (curi[depth] > byts[len]) |
| 8083 | { |
| 8084 | /* Done all bytes at this node, go up one level. */ |
| 8085 | --depth; |
| 8086 | line_breakcheck(); |
| 8087 | } |
| 8088 | else |
| 8089 | { |
| 8090 | /* Do one more byte at this node. */ |
| 8091 | n = len + curi[depth]; |
| 8092 | ++curi[depth]; |
| 8093 | c = byts[n]; |
| 8094 | if (c == 0) |
| 8095 | { |
| 8096 | /* End of prefix, find out how many IDs there are. */ |
| 8097 | for (i = 1; i < len; ++i) |
| 8098 | if (byts[n + i] != 0) |
| 8099 | break; |
| 8100 | curi[depth] += i - 1; |
| 8101 | |
| 8102 | if (valid_word_prefix(i, n, prefid, word, slang)) |
| 8103 | { |
| 8104 | vim_strncpy(prefix + depth, word, MAXWLEN - depth); |
| 8105 | dump_word(prefix, round, flags, lnum++); |
| 8106 | } |
| 8107 | } |
| 8108 | else |
| 8109 | { |
| 8110 | /* Normal char, go one level deeper. */ |
| 8111 | prefix[depth++] = c; |
| 8112 | arridx[depth] = idxs[n]; |
| 8113 | curi[depth] = 1; |
| 8114 | } |
| 8115 | } |
| 8116 | } |
| 8117 | } |
| 8118 | |
| 8119 | return lnum; |
| 8120 | } |
| 8121 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8122 | #endif /* FEAT_SYN_HL */ |