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 */ |
| 360 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 361 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| 362 | hashtab_T su_banned; /* table with banned words */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 363 | } suginfo_T; |
| 364 | |
| 365 | /* One word suggestion. Used in "si_ga". */ |
| 366 | typedef struct suggest_S |
| 367 | { |
| 368 | char_u *st_word; /* suggested word, allocated string */ |
| 369 | int st_orglen; /* length of replaced text */ |
| 370 | int st_score; /* lower is better */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 371 | int st_altscore; /* used when st_score compares equal */ |
| 372 | int st_salscore; /* st_score is for soundalike */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 373 | int st_had_bonus; /* bonus already included in score */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 374 | } suggest_T; |
| 375 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 376 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 377 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 378 | /* Number of suggestions kept when cleaning up. When rescore_suggestions() is |
| 379 | * called the score may change, thus we need to keep more than what is |
| 380 | * displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 381 | #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 25 ? 25 : (su)->su_maxcount) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 382 | |
| 383 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 384 | * of suggestions that are not going to be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 385 | #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 386 | |
| 387 | /* score for various changes */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 388 | #define SCORE_SPLIT 149 /* split bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 389 | #define SCORE_ICASE 52 /* slightly different case */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 390 | #define SCORE_REGION 70 /* word is for different region */ |
| 391 | #define SCORE_RARE 180 /* rare word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 392 | #define SCORE_SWAP 90 /* swap two characters */ |
| 393 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| 394 | #define SCORE_REP 87 /* REP replacement */ |
| 395 | #define SCORE_SUBST 93 /* substitute a character */ |
| 396 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 397 | #define SCORE_DEL 94 /* delete a character */ |
| 398 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 399 | |
| 400 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 401 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 402 | |
| 403 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 404 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 405 | |
| 406 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 407 | * Structure to store info for word matching. |
| 408 | */ |
| 409 | typedef struct matchinf_S |
| 410 | { |
| 411 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 412 | |
| 413 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 414 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 415 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 416 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 417 | char_u *mi_cend; /* char after what was used for |
| 418 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 419 | |
| 420 | /* case-folded text */ |
| 421 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 422 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 423 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 424 | /* for when checking word after a prefix */ |
| 425 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 426 | prefixID/condition */ |
| 427 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 428 | int mi_prefixlen; /* byte length of prefix */ |
| 429 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 430 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 431 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 432 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 433 | } matchinf_T; |
| 434 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 435 | /* |
| 436 | * The tables used for recognizing word characters according to spelling. |
| 437 | * These are only used for the first 256 characters of 'encoding'. |
| 438 | */ |
| 439 | typedef struct spelltab_S |
| 440 | { |
| 441 | char_u st_isw[256]; /* flags: is word char */ |
| 442 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 443 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 444 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 445 | } spelltab_T; |
| 446 | |
| 447 | static spelltab_T spelltab; |
| 448 | static int did_set_spelltab; |
| 449 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 450 | #define CF_WORD 0x01 |
| 451 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 452 | |
| 453 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 454 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 455 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * Return TRUE if "p" points to a word character or "c" is a word character |
| 459 | * for spelling. |
| 460 | * Checking for a word character is done very often, avoid the function call |
| 461 | * overhead. |
| 462 | */ |
| 463 | #ifdef FEAT_MBYTE |
| 464 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 465 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 466 | #else |
| 467 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 468 | #endif |
| 469 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 470 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 471 | * 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] | 472 | */ |
| 473 | typedef enum |
| 474 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 475 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 476 | * ends); if badword ends there is a match, otherwise |
| 477 | * try splitting word. */ |
| 478 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 479 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 480 | STATE_PLAIN, /* Use each byte of the node. */ |
| 481 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 482 | STATE_INS, /* Insert a byte in the bad word. */ |
| 483 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 484 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 485 | STATE_SWAP3, /* Swap two characters over three. */ |
| 486 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
| 487 | STATE_ROT3L, /* Rotate three characters left */ |
| 488 | STATE_UNROT3L, /* Undo rotate three characters left */ |
| 489 | STATE_ROT3R, /* Rotate three characters right */ |
| 490 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 491 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 492 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 493 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 494 | STATE_FINAL /* End of this node. */ |
| 495 | } state_T; |
| 496 | |
| 497 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 498 | * Struct to keep the state at each level in spell_try_change(). |
| 499 | */ |
| 500 | typedef struct trystate_S |
| 501 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 502 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 503 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 504 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 505 | short ts_curi; /* index in list of child nodes */ |
| 506 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 507 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 508 | char_u ts_twordlen; /* valid length of tword[] */ |
| 509 | #ifdef FEAT_MBYTE |
| 510 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 511 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 512 | char_u ts_isdiff; /* DIFF_ values */ |
| 513 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 514 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 515 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 516 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
| 517 | char_u ts_save_badflags; /* badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 518 | } trystate_T; |
| 519 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 520 | /* values for ts_isdiff */ |
| 521 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 522 | #define DIFF_YES 1 /* different byte found */ |
| 523 | #define DIFF_INSERT 2 /* inserting character */ |
| 524 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 525 | /* mode values for find_word */ |
| 526 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 527 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 528 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 529 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 530 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 531 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 532 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 533 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
| 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 | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 537 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 538 | static char_u *spell_enc __ARGS((void)); |
| 539 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 540 | 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] | 541 | 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] | 542 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 543 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 544 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 545 | 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] | 546 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 547 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 548 | 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] | 549 | static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount)); |
| 550 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 551 | 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] | 552 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 553 | static void spell_try_change __ARGS((suginfo_T *su)); |
| 554 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
| 555 | 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] | 556 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 557 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 558 | static void spell_try_soundalike __ARGS((suginfo_T *su)); |
| 559 | 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] | 560 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 561 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 562 | static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int use_score, int had_bonus)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 563 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 564 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 565 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 566 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 567 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 568 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 569 | static int spell_sound_score __ARGS((slang_T *slang, char_u *goodword, char_u *badsound)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 570 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 571 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
| 572 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 573 | /* |
| 574 | * Use our own character-case definitions, because the current locale may |
| 575 | * differ from what the .spl file uses. |
| 576 | * These must not be called with negative number! |
| 577 | */ |
| 578 | #ifndef FEAT_MBYTE |
| 579 | /* Non-multi-byte implementation. */ |
| 580 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 581 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 582 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 583 | #else |
| 584 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 585 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 586 | * the "w" library function for characters above 255 if available. */ |
| 587 | # ifdef HAVE_TOWLOWER |
| 588 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 589 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 590 | # else |
| 591 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 592 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 593 | # endif |
| 594 | |
| 595 | # ifdef HAVE_TOWUPPER |
| 596 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 597 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 598 | # else |
| 599 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 600 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 601 | # endif |
| 602 | |
| 603 | # ifdef HAVE_ISWUPPER |
| 604 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 605 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 606 | # else |
| 607 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 608 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 609 | # endif |
| 610 | #endif |
| 611 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 612 | |
| 613 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 617 | * "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] | 618 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 619 | * or when it's OK it remains unchanged. |
| 620 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 621 | * |
| 622 | * "sug" is normally NULL. When looking for suggestions it points to |
| 623 | * suginfo_T. It's passed as a void pointer to keep the struct local. |
| 624 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 625 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 626 | * caller can skip over the word. |
| 627 | */ |
| 628 | int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 629 | spell_check(wp, ptr, attrp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 630 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 631 | char_u *ptr; |
| 632 | int *attrp; |
| 633 | { |
| 634 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 635 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 636 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 637 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 638 | /* A word never starts at a space or a control character. Return quickly |
| 639 | * then, skipping over the character. */ |
| 640 | if (*ptr <= ' ') |
| 641 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 642 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 643 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
| 644 | * 0X99FF. But when a word character follows do check spelling. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 645 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 646 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 647 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 648 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 649 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 650 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 651 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 652 | nrlen = mi.mi_end - ptr; |
| 653 | } |
| 654 | if (!SPELL_ISWORDP(mi.mi_end)) |
| 655 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 656 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 657 | |
| 658 | /* Find the end of the word. */ |
| 659 | mi.mi_word = ptr; |
| 660 | mi.mi_fend = ptr; |
| 661 | |
| 662 | if (SPELL_ISWORDP(mi.mi_fend)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 663 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 664 | /* Make case-folded copy of the characters until the next non-word |
| 665 | * character. */ |
| 666 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 667 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 668 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 669 | } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend)); |
| 670 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 671 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 672 | /* We always use the characters up to the next non-word character, |
| 673 | * also for bad words. */ |
| 674 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 675 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 676 | /* Check caps type later. */ |
| 677 | mi.mi_capflags = 0; |
| 678 | mi.mi_cend = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 679 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 680 | /* Include one non-word character so that we can check for the |
| 681 | * word end. */ |
| 682 | if (*mi.mi_fend != NUL) |
| 683 | mb_ptr_adv(mi.mi_fend); |
| 684 | |
| 685 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 686 | MAXWLEN + 1); |
| 687 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 688 | |
| 689 | /* The word is bad unless we recognize it. */ |
| 690 | mi.mi_result = SP_BAD; |
| 691 | |
| 692 | /* |
| 693 | * Loop over the languages specified in 'spelllang'. |
| 694 | * We check them all, because a matching word may be longer than an |
| 695 | * already found matching word. |
| 696 | */ |
| 697 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 698 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 699 | { |
| 700 | /* Check for a matching word in case-folded words. */ |
| 701 | find_word(&mi, FIND_FOLDWORD); |
| 702 | |
| 703 | /* Check for a matching word in keep-case words. */ |
| 704 | find_word(&mi, FIND_KEEPWORD); |
| 705 | |
| 706 | /* Check for matching prefixes. */ |
| 707 | find_prefix(&mi); |
| 708 | } |
| 709 | |
| 710 | if (mi.mi_result != SP_OK) |
| 711 | { |
| 712 | /* If we found a number skip over it. Allows for "42nd". */ |
| 713 | if (nrlen > 0) |
| 714 | return nrlen; |
| 715 | |
| 716 | /* When we are at a non-word character there is no error, just |
| 717 | * skip over the character (try looking for a word after it). */ |
| 718 | if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 719 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 720 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 721 | if (has_mbyte) |
| 722 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 723 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 724 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 725 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 726 | |
| 727 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 728 | *attrp = highlight_attr[HLF_SPB]; |
| 729 | else if (mi.mi_result == SP_RARE) |
| 730 | *attrp = highlight_attr[HLF_SPR]; |
| 731 | else |
| 732 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 735 | return (int)(mi.mi_end - ptr); |
| 736 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 737 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 738 | /* |
| 739 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 740 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 741 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 742 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 743 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 744 | * |
| 745 | * For a match mip->mi_result is updated. |
| 746 | */ |
| 747 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 748 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 749 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 750 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 751 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 752 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 753 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 754 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 755 | int endidxcnt = 0; |
| 756 | int len; |
| 757 | int wlen = 0; |
| 758 | int flen; |
| 759 | int c; |
| 760 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 761 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 762 | #ifdef FEAT_MBYTE |
| 763 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 764 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 765 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 766 | int res = SP_BAD; |
| 767 | int valid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 768 | slang_T *slang = mip->mi_lp->lp_slang; |
| 769 | unsigned flags; |
| 770 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 771 | idx_T *idxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 772 | int prefcnt; |
| 773 | int pidx; |
| 774 | regmatch_T regmatch; |
| 775 | regprog_T *rp; |
| 776 | int prefid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 777 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 778 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 779 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 780 | /* Check for word with matching case in keep-case tree. */ |
| 781 | ptr = mip->mi_word; |
| 782 | flen = 9999; /* no case folding, always enough bytes */ |
| 783 | byts = slang->sl_kbyts; |
| 784 | idxs = slang->sl_kidxs; |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | /* Check for case-folded in case-folded tree. */ |
| 789 | ptr = mip->mi_fword; |
| 790 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 791 | byts = slang->sl_fbyts; |
| 792 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 793 | |
| 794 | if (mode == FIND_PREFIX) |
| 795 | { |
| 796 | /* Skip over the prefix. */ |
| 797 | wlen = mip->mi_prefixlen; |
| 798 | flen -= mip->mi_prefixlen; |
| 799 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 802 | if (byts == NULL) |
| 803 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 804 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 805 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 806 | * Repeat advancing in the tree until: |
| 807 | * - there is a byte that doesn't match, |
| 808 | * - we reach the end of the tree, |
| 809 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 810 | */ |
| 811 | for (;;) |
| 812 | { |
| 813 | if (flen == 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 814 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 815 | |
| 816 | len = byts[arridx++]; |
| 817 | |
| 818 | /* If the first possible byte is a zero the word could end here. |
| 819 | * Remember this index, we first check for the longest word. */ |
| 820 | if (byts[arridx] == 0) |
| 821 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 822 | if (endidxcnt == MAXWLEN) |
| 823 | { |
| 824 | /* Must be a corrupted spell file. */ |
| 825 | EMSG(_(e_format)); |
| 826 | return; |
| 827 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 828 | endlen[endidxcnt] = wlen; |
| 829 | endidx[endidxcnt++] = arridx++; |
| 830 | --len; |
| 831 | |
| 832 | /* Skip over the zeros, there can be several flag/region |
| 833 | * combinations. */ |
| 834 | while (len > 0 && byts[arridx] == 0) |
| 835 | { |
| 836 | ++arridx; |
| 837 | --len; |
| 838 | } |
| 839 | if (len == 0) |
| 840 | break; /* no children, word must end here */ |
| 841 | } |
| 842 | |
| 843 | /* Stop looking at end of the line. */ |
| 844 | if (ptr[wlen] == NUL) |
| 845 | break; |
| 846 | |
| 847 | /* Perform a binary search in the list of accepted bytes. */ |
| 848 | c = ptr[wlen]; |
| 849 | lo = arridx; |
| 850 | hi = arridx + len - 1; |
| 851 | while (lo < hi) |
| 852 | { |
| 853 | m = (lo + hi) / 2; |
| 854 | if (byts[m] > c) |
| 855 | hi = m - 1; |
| 856 | else if (byts[m] < c) |
| 857 | lo = m + 1; |
| 858 | else |
| 859 | { |
| 860 | lo = hi = m; |
| 861 | break; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /* Stop if there is no matching byte. */ |
| 866 | if (hi < lo || byts[lo] != c) |
| 867 | break; |
| 868 | |
| 869 | /* Continue at the child (if there is one). */ |
| 870 | arridx = idxs[lo]; |
| 871 | ++wlen; |
| 872 | --flen; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Verify that one of the possible endings is valid. Try the longest |
| 877 | * first. |
| 878 | */ |
| 879 | while (endidxcnt > 0) |
| 880 | { |
| 881 | --endidxcnt; |
| 882 | arridx = endidx[endidxcnt]; |
| 883 | wlen = endlen[endidxcnt]; |
| 884 | |
| 885 | #ifdef FEAT_MBYTE |
| 886 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 887 | continue; /* not at first byte of character */ |
| 888 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 889 | if (SPELL_ISWORDP(ptr + wlen)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 890 | continue; /* next char is a word character */ |
| 891 | |
| 892 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 893 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 894 | { |
| 895 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 896 | * when folding case. This can be slow, take a shortcut when the |
| 897 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 898 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 899 | if (STRNCMP(ptr, p, wlen) != 0) |
| 900 | { |
| 901 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 902 | mb_ptr_adv(p); |
| 903 | wlen = p - mip->mi_word; |
| 904 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 905 | } |
| 906 | #endif |
| 907 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 908 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 909 | * prefix ID. |
| 910 | * Repeat this if there are more flags/region alternatives until there |
| 911 | * is a match. */ |
| 912 | res = SP_BAD; |
| 913 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 914 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 915 | { |
| 916 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 917 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 918 | /* For the fold-case tree check that the case of the checked word |
| 919 | * matches with what the word in the tree requires. |
| 920 | * For keep-case tree the case is always right. For prefixes we |
| 921 | * don't bother to check. */ |
| 922 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 923 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 924 | if (mip->mi_cend != mip->mi_word + wlen) |
| 925 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 926 | /* mi_capflags was set for a different word length, need |
| 927 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 928 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 929 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 932 | if (!spell_valid_case(mip->mi_capflags, flags)) |
| 933 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 936 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 937 | * check the prefix ID and the condition. Do that for the list at |
| 938 | * mip->mi_prefarridx. */ |
| 939 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 940 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 941 | /* The prefix ID is stored two bytes above the flags. */ |
| 942 | prefid = (unsigned)flags >> 16; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 943 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 944 | valid = FALSE; |
| 945 | for (prefcnt = mip->mi_prefcnt - 1; prefcnt >= 0; --prefcnt) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 946 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 947 | pidx = slang->sl_pidxs[mip->mi_prefarridx + prefcnt]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 948 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 949 | /* Check the prefix ID. */ |
| 950 | if (prefid != (pidx & 0xff)) |
| 951 | continue; |
| 952 | |
| 953 | /* Check the condition, if there is one. The |
| 954 | * condition index is stored above the prefix ID byte. |
| 955 | */ |
| 956 | rp = slang->sl_prefprog[(unsigned)pidx >> 8]; |
| 957 | if (rp != NULL) |
| 958 | { |
| 959 | regmatch.regprog = rp; |
| 960 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 961 | if (!vim_regexec(®match, |
| 962 | mip->mi_fword + mip->mi_prefixlen, 0)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 963 | continue; |
| 964 | } |
| 965 | |
| 966 | /* It's a match, use it. */ |
| 967 | valid = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 968 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 969 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 970 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 971 | if (!valid) |
| 972 | continue; |
| 973 | } |
| 974 | |
| 975 | if (flags & WF_BANNED) |
| 976 | res = SP_BANNED; |
| 977 | else if (flags & WF_REGION) |
| 978 | { |
| 979 | /* Check region. */ |
| 980 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 981 | res = SP_OK; |
| 982 | else |
| 983 | res = SP_LOCAL; |
| 984 | } |
| 985 | else if (flags & WF_RARE) |
| 986 | res = SP_RARE; |
| 987 | else |
| 988 | res = SP_OK; |
| 989 | |
| 990 | /* Always use the longest match and the best result. */ |
| 991 | if (mip->mi_result > res) |
| 992 | { |
| 993 | mip->mi_result = res; |
| 994 | mip->mi_end = mip->mi_word + wlen; |
| 995 | } |
| 996 | else if (mip->mi_result == res |
| 997 | && mip->mi_end < mip->mi_word + wlen) |
| 998 | mip->mi_end = mip->mi_word + wlen; |
| 999 | |
| 1000 | if (res == SP_OK) |
| 1001 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1004 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1005 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1006 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1009 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1010 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1011 | * If it does, then check the following word. |
| 1012 | * |
| 1013 | * For a match mip->mi_result is updated. |
| 1014 | */ |
| 1015 | static void |
| 1016 | find_prefix(mip) |
| 1017 | matchinf_T *mip; |
| 1018 | { |
| 1019 | idx_T arridx = 0; |
| 1020 | int len; |
| 1021 | int wlen = 0; |
| 1022 | int flen; |
| 1023 | int c; |
| 1024 | char_u *ptr; |
| 1025 | idx_T lo, hi, m; |
| 1026 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1027 | char_u *byts; |
| 1028 | idx_T *idxs; |
| 1029 | |
| 1030 | /* We use the case-folded word here, since prefixes are always |
| 1031 | * case-folded. */ |
| 1032 | ptr = mip->mi_fword; |
| 1033 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 1034 | byts = slang->sl_pbyts; |
| 1035 | idxs = slang->sl_pidxs; |
| 1036 | |
| 1037 | if (byts == NULL) |
| 1038 | return; /* array is empty */ |
| 1039 | |
| 1040 | /* |
| 1041 | * Repeat advancing in the tree until: |
| 1042 | * - there is a byte that doesn't match, |
| 1043 | * - we reach the end of the tree, |
| 1044 | * - or we reach the end of the line. |
| 1045 | */ |
| 1046 | for (;;) |
| 1047 | { |
| 1048 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1049 | flen = fold_more(mip); |
| 1050 | |
| 1051 | len = byts[arridx++]; |
| 1052 | |
| 1053 | /* If the first possible byte is a zero the prefix could end here. |
| 1054 | * Check if the following word matches and supports the prefix. */ |
| 1055 | if (byts[arridx] == 0) |
| 1056 | { |
| 1057 | /* There can be several prefixes with different conditions. We |
| 1058 | * try them all, since we don't know which one will give the |
| 1059 | * longest match. The word is the same each time, pass the list |
| 1060 | * of possible prefixes to find_word(). */ |
| 1061 | mip->mi_prefarridx = arridx; |
| 1062 | mip->mi_prefcnt = len; |
| 1063 | while (len > 0 && byts[arridx] == 0) |
| 1064 | { |
| 1065 | ++arridx; |
| 1066 | --len; |
| 1067 | } |
| 1068 | mip->mi_prefcnt -= len; |
| 1069 | |
| 1070 | /* Find the word that comes after the prefix. */ |
| 1071 | mip->mi_prefixlen = wlen; |
| 1072 | find_word(mip, FIND_PREFIX); |
| 1073 | |
| 1074 | |
| 1075 | if (len == 0) |
| 1076 | break; /* no children, word must end here */ |
| 1077 | } |
| 1078 | |
| 1079 | /* Stop looking at end of the line. */ |
| 1080 | if (ptr[wlen] == NUL) |
| 1081 | break; |
| 1082 | |
| 1083 | /* Perform a binary search in the list of accepted bytes. */ |
| 1084 | c = ptr[wlen]; |
| 1085 | lo = arridx; |
| 1086 | hi = arridx + len - 1; |
| 1087 | while (lo < hi) |
| 1088 | { |
| 1089 | m = (lo + hi) / 2; |
| 1090 | if (byts[m] > c) |
| 1091 | hi = m - 1; |
| 1092 | else if (byts[m] < c) |
| 1093 | lo = m + 1; |
| 1094 | else |
| 1095 | { |
| 1096 | lo = hi = m; |
| 1097 | break; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /* Stop if there is no matching byte. */ |
| 1102 | if (hi < lo || byts[lo] != c) |
| 1103 | break; |
| 1104 | |
| 1105 | /* Continue at the child (if there is one). */ |
| 1106 | arridx = idxs[lo]; |
| 1107 | ++wlen; |
| 1108 | --flen; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Need to fold at least one more character. Do until next non-word character |
| 1114 | * for efficiency. |
| 1115 | * Return the length of the folded chars in bytes. |
| 1116 | */ |
| 1117 | static int |
| 1118 | fold_more(mip) |
| 1119 | matchinf_T *mip; |
| 1120 | { |
| 1121 | int flen; |
| 1122 | char_u *p; |
| 1123 | |
| 1124 | p = mip->mi_fend; |
| 1125 | do |
| 1126 | { |
| 1127 | mb_ptr_adv(mip->mi_fend); |
| 1128 | } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend)); |
| 1129 | |
| 1130 | /* Include the non-word character so that we can check for the |
| 1131 | * word end. */ |
| 1132 | if (*mip->mi_fend != NUL) |
| 1133 | mb_ptr_adv(mip->mi_fend); |
| 1134 | |
| 1135 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1136 | mip->mi_fword + mip->mi_fwordlen, |
| 1137 | MAXWLEN - mip->mi_fwordlen); |
| 1138 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1139 | mip->mi_fwordlen += flen; |
| 1140 | return flen; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1144 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1145 | * case. |
| 1146 | */ |
| 1147 | static int |
| 1148 | spell_valid_case(origflags, treeflags) |
| 1149 | int origflags; /* flags for the checked word. */ |
| 1150 | int treeflags; /* flags for the word in the spell tree */ |
| 1151 | { |
| 1152 | return (origflags == WF_ALLCAP |
| 1153 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
| 1154 | && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP))); |
| 1155 | } |
| 1156 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1157 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1158 | /* |
| 1159 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1160 | * "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] | 1161 | * Return OK if found, FAIL otherwise. |
| 1162 | */ |
| 1163 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1164 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1165 | int dir; /* FORWARD or BACKWARD */ |
| 1166 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1167 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1168 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1169 | linenr_T lnum; |
| 1170 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1171 | char_u *line; |
| 1172 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1173 | int attr = 0; |
| 1174 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1175 | int has_syntax = syntax_present(curbuf); |
| 1176 | int col; |
| 1177 | int can_spell; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1178 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1179 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1180 | { |
| 1181 | EMSG(_("E756: Spell checking not enabled")); |
| 1182 | return FAIL; |
| 1183 | } |
| 1184 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | * Start looking for bad word at the start of the line, because we can't |
| 1187 | * start halfway a word, we don't know where it starts or ends. |
| 1188 | * |
| 1189 | * When searching backwards, we continue in the line to find the last |
| 1190 | * bad word (in the cursor line: before the cursor). |
| 1191 | */ |
| 1192 | lnum = curwin->w_cursor.lnum; |
| 1193 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1194 | |
| 1195 | while (!got_int) |
| 1196 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1197 | line = ml_get(lnum); |
| 1198 | p = line; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1199 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1200 | while (*p != NUL) |
| 1201 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1202 | /* When searching backward don't search after the cursor. */ |
| 1203 | if (dir == BACKWARD |
| 1204 | && lnum == curwin->w_cursor.lnum |
| 1205 | && (colnr_T)(p - line) >= curwin->w_cursor.col) |
| 1206 | break; |
| 1207 | |
| 1208 | /* start of word */ |
| 1209 | len = spell_check(curwin, p, &attr); |
| 1210 | |
| 1211 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1212 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1213 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1214 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1215 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1216 | /* When searching forward only accept a bad word after |
| 1217 | * the cursor. */ |
| 1218 | if (dir == BACKWARD |
| 1219 | || lnum > curwin->w_cursor.lnum |
| 1220 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1221 | && (colnr_T)(curline ? p - line + len |
| 1222 | : p - line) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1223 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1224 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1225 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1226 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1227 | col = p - line; |
| 1228 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1229 | FALSE, &can_spell); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1230 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1231 | /* have to get the line again, a multi-line |
| 1232 | * regexp may make it invalid */ |
| 1233 | line = ml_get(lnum); |
| 1234 | p = line + col; |
| 1235 | } |
| 1236 | else |
| 1237 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1238 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1239 | if (can_spell) |
| 1240 | { |
| 1241 | found_pos.lnum = lnum; |
| 1242 | found_pos.col = p - line; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1243 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1244 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1245 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1246 | if (dir == FORWARD) |
| 1247 | { |
| 1248 | /* No need to search further. */ |
| 1249 | curwin->w_cursor = found_pos; |
| 1250 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1253 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1254 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1255 | attr = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1258 | /* advance to character after the word */ |
| 1259 | p += len; |
| 1260 | if (*p == NUL) |
| 1261 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1264 | if (curline) |
| 1265 | return FAIL; /* only check cursor line */ |
| 1266 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1267 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1268 | if (dir == BACKWARD) |
| 1269 | { |
| 1270 | if (found_pos.lnum != 0) |
| 1271 | { |
| 1272 | /* Use the last match in the line. */ |
| 1273 | curwin->w_cursor = found_pos; |
| 1274 | return OK; |
| 1275 | } |
| 1276 | if (lnum == 1) |
| 1277 | return FAIL; |
| 1278 | --lnum; |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | if (lnum == curbuf->b_ml.ml_line_count) |
| 1283 | return FAIL; |
| 1284 | ++lnum; |
| 1285 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1286 | |
| 1287 | line_breakcheck(); |
| 1288 | } |
| 1289 | |
| 1290 | return FAIL; /* interrupted */ |
| 1291 | } |
| 1292 | |
| 1293 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1294 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1295 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1296 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1297 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1298 | spell_load_lang(lang) |
| 1299 | char_u *lang; |
| 1300 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1301 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1302 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1303 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1304 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1305 | /* 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] | 1306 | * It's truncated when an error is detected. */ |
| 1307 | STRCPY(langcp, lang); |
| 1308 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1309 | /* |
| 1310 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1311 | */ |
| 1312 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1313 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1314 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1315 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1316 | if (r == FAIL && *langcp != NUL) |
| 1317 | { |
| 1318 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1319 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1320 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1321 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1324 | if (r == FAIL) |
| 1325 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1326 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1327 | else if (*langcp != NUL) |
| 1328 | { |
| 1329 | /* Load all the additions. */ |
| 1330 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1331 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1337 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1338 | */ |
| 1339 | static char_u * |
| 1340 | spell_enc() |
| 1341 | { |
| 1342 | |
| 1343 | #ifdef FEAT_MBYTE |
| 1344 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1345 | return p_enc; |
| 1346 | #endif |
| 1347 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * Allocate a new slang_T. |
| 1352 | * Caller must fill "sl_next". |
| 1353 | */ |
| 1354 | static slang_T * |
| 1355 | slang_alloc(lang) |
| 1356 | char_u *lang; |
| 1357 | { |
| 1358 | slang_T *lp; |
| 1359 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1360 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1361 | if (lp != NULL) |
| 1362 | { |
| 1363 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1364 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1365 | ga_init2(&lp->sl_sal, sizeof(salitem_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1366 | } |
| 1367 | return lp; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * Free the contents of an slang_T and the structure itself. |
| 1372 | */ |
| 1373 | static void |
| 1374 | slang_free(lp) |
| 1375 | slang_T *lp; |
| 1376 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1377 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1378 | vim_free(lp->sl_fname); |
| 1379 | slang_clear(lp); |
| 1380 | vim_free(lp); |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Clear an slang_T so that the file can be reloaded. |
| 1385 | */ |
| 1386 | static void |
| 1387 | slang_clear(lp) |
| 1388 | slang_T *lp; |
| 1389 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1390 | garray_T *gap; |
| 1391 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1392 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1393 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1394 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1395 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1396 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1397 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1398 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1399 | vim_free(lp->sl_pbyts); |
| 1400 | lp->sl_pbyts = NULL; |
| 1401 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1402 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1403 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1404 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1405 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1406 | vim_free(lp->sl_pidxs); |
| 1407 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1408 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1409 | gap = &lp->sl_rep; |
| 1410 | while (gap->ga_len > 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1411 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1412 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1413 | vim_free(ftp->ft_from); |
| 1414 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1415 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1416 | ga_clear(gap); |
| 1417 | |
| 1418 | gap = &lp->sl_sal; |
| 1419 | while (gap->ga_len > 0) |
| 1420 | { |
| 1421 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1422 | vim_free(smp->sm_lead); |
| 1423 | vim_free(smp->sm_to); |
| 1424 | } |
| 1425 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1426 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1427 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1428 | vim_free(lp->sl_prefprog[i]); |
| 1429 | vim_free(lp->sl_prefprog); |
| 1430 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1431 | #ifdef FEAT_MBYTE |
| 1432 | { |
| 1433 | int todo = lp->sl_map_hash.ht_used; |
| 1434 | hashitem_T *hi; |
| 1435 | |
| 1436 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1437 | if (!HASHITEM_EMPTY(hi)) |
| 1438 | { |
| 1439 | --todo; |
| 1440 | vim_free(hi->hi_key); |
| 1441 | } |
| 1442 | } |
| 1443 | hash_clear(&lp->sl_map_hash); |
| 1444 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1448 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1449 | * Invoked through do_in_runtimepath(). |
| 1450 | */ |
| 1451 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1452 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1453 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1454 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1455 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1456 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * Load one spell file and store the info into a slang_T. |
| 1461 | * |
| 1462 | * This is invoked in two ways: |
| 1463 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1464 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1465 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1466 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1467 | * 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] | 1468 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1469 | static slang_T * |
| 1470 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1471 | char_u *fname; |
| 1472 | char_u *lang; |
| 1473 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1474 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1475 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1476 | FILE *fd; |
| 1477 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1478 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1479 | char_u *bp; |
| 1480 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1481 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1482 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1483 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1484 | int round; |
| 1485 | char_u *save_sourcing_name = sourcing_name; |
| 1486 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1487 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1488 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1489 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1490 | garray_T *gap; |
| 1491 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1492 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1493 | int rr; |
| 1494 | short *first; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1495 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1496 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1497 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1498 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1499 | if (fd == NULL) |
| 1500 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1501 | if (!silent) |
| 1502 | EMSG2(_(e_notopen), fname); |
| 1503 | else if (p_verbose > 2) |
| 1504 | { |
| 1505 | verbose_enter(); |
| 1506 | smsg((char_u *)e_notopen, fname); |
| 1507 | verbose_leave(); |
| 1508 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1509 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1510 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1511 | if (p_verbose > 2) |
| 1512 | { |
| 1513 | verbose_enter(); |
| 1514 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1515 | verbose_leave(); |
| 1516 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1517 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1518 | if (old_lp == NULL) |
| 1519 | { |
| 1520 | lp = slang_alloc(lang); |
| 1521 | if (lp == NULL) |
| 1522 | goto endFAIL; |
| 1523 | |
| 1524 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1525 | lp->sl_fname = vim_strsave(fname); |
| 1526 | if (lp->sl_fname == NULL) |
| 1527 | goto endFAIL; |
| 1528 | |
| 1529 | /* Check for .add.spl. */ |
| 1530 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1531 | } |
| 1532 | else |
| 1533 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1534 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1535 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1536 | sourcing_name = fname; |
| 1537 | sourcing_lnum = 0; |
| 1538 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1539 | /* <HEADER>: <fileID> |
| 1540 | * <regioncnt> <regionname> ... |
| 1541 | * <charflagslen> <charflags> |
| 1542 | * <fcharslen> <fchars> |
| 1543 | * <prefcondcnt> <prefcond> ... |
| 1544 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1545 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1546 | buf[i] = getc(fd); /* <fileID> */ |
| 1547 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1548 | { |
| 1549 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1550 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1554 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1555 | { |
| 1556 | truncerr: |
| 1557 | EMSG(_("E758: Truncated spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1558 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1559 | } |
| 1560 | if (cnt > 8) |
| 1561 | { |
| 1562 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1563 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1564 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1565 | } |
| 1566 | for (i = 0; i < cnt; ++i) |
| 1567 | { |
| 1568 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1569 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1570 | } |
| 1571 | lp->sl_regions[cnt * 2] = NUL; |
| 1572 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1573 | cnt = getc(fd); /* <charflagslen> */ |
| 1574 | if (cnt > 0) |
| 1575 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1576 | p = alloc((unsigned)cnt); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1577 | if (p == NULL) |
| 1578 | goto endFAIL; |
| 1579 | for (i = 0; i < cnt; ++i) |
| 1580 | p[i] = getc(fd); /* <charflags> */ |
| 1581 | |
| 1582 | ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */ |
| 1583 | if (ccnt <= 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1584 | { |
| 1585 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1586 | goto formerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1587 | } |
| 1588 | fol = alloc((unsigned)ccnt + 1); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1589 | if (fol == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1590 | { |
| 1591 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1592 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1593 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1594 | for (i = 0; i < ccnt; ++i) |
| 1595 | fol[i] = getc(fd); /* <fchars> */ |
| 1596 | fol[i] = NUL; |
| 1597 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1598 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1599 | i = set_spell_charflags(p, cnt, fol); |
| 1600 | vim_free(p); |
| 1601 | vim_free(fol); |
| 1602 | if (i == FAIL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1603 | goto formerr; |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1608 | cnt = (getc(fd) << 8) + getc(fd); |
| 1609 | if (cnt != 0) |
| 1610 | goto formerr; |
| 1611 | } |
| 1612 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1613 | /* <prefcondcnt> <prefcond> ... */ |
| 1614 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1615 | if (cnt > 0) |
| 1616 | { |
| 1617 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1618 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1619 | if (lp->sl_prefprog == NULL) |
| 1620 | goto endFAIL; |
| 1621 | lp->sl_prefixcnt = cnt; |
| 1622 | |
| 1623 | for (i = 0; i < cnt; ++i) |
| 1624 | { |
| 1625 | /* <prefcond> : <condlen> <condstr> */ |
| 1626 | n = getc(fd); /* <condlen> */ |
| 1627 | if (n < 0) |
| 1628 | goto formerr; |
| 1629 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1630 | * compile the regexp program used to check for the condition. */ |
| 1631 | if (n > 0) |
| 1632 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1633 | buf[0] = '^'; /* always match at one position only */ |
| 1634 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1635 | while (n-- > 0) |
| 1636 | *p++ = getc(fd); /* <condstr> */ |
| 1637 | *p = NUL; |
| 1638 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1644 | /* <SUGGEST> : <repcount> <rep> ... |
| 1645 | * <salflags> <salcount> <sal> ... |
| 1646 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1647 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1648 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1649 | if (cnt < 0) |
| 1650 | goto formerr; |
| 1651 | |
| 1652 | gap = &lp->sl_rep; |
| 1653 | if (ga_grow(gap, cnt) == FAIL) |
| 1654 | goto endFAIL; |
| 1655 | |
| 1656 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1657 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1658 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1659 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
| 1660 | for (rr = 1; rr <= 2; ++rr) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1661 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1662 | ccnt = getc(fd); |
| 1663 | if (ccnt < 0) |
| 1664 | { |
| 1665 | if (rr == 2) |
| 1666 | vim_free(ftp->ft_from); |
| 1667 | goto formerr; |
| 1668 | } |
| 1669 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1670 | { |
| 1671 | if (rr == 2) |
| 1672 | vim_free(ftp->ft_from); |
| 1673 | goto endFAIL; |
| 1674 | } |
| 1675 | for (i = 0; i < ccnt; ++i) |
| 1676 | p[i] = getc(fd); /* <repfrom> or <repto> */ |
| 1677 | p[i] = NUL; |
| 1678 | if (rr == 1) |
| 1679 | ftp->ft_from = p; |
| 1680 | else |
| 1681 | ftp->ft_to = p; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /* Fill the first-index table. */ |
| 1686 | first = lp->sl_rep_first; |
| 1687 | for (i = 0; i < 256; ++i) |
| 1688 | first[i] = -1; |
| 1689 | for (i = 0; i < gap->ga_len; ++i) |
| 1690 | { |
| 1691 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1692 | if (first[*ftp->ft_from] == -1) |
| 1693 | first[*ftp->ft_from] = i; |
| 1694 | } |
| 1695 | |
| 1696 | i = getc(fd); /* <salflags> */ |
| 1697 | if (i & SAL_F0LLOWUP) |
| 1698 | lp->sl_followup = TRUE; |
| 1699 | if (i & SAL_COLLAPSE) |
| 1700 | lp->sl_collapse = TRUE; |
| 1701 | if (i & SAL_REM_ACCENTS) |
| 1702 | lp->sl_rem_accents = TRUE; |
| 1703 | |
| 1704 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1705 | if (cnt < 0) |
| 1706 | goto formerr; |
| 1707 | |
| 1708 | gap = &lp->sl_sal; |
| 1709 | if (ga_grow(gap, cnt) == FAIL) |
| 1710 | goto endFAIL; |
| 1711 | |
| 1712 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 1713 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 1714 | { |
| 1715 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 1716 | ccnt = getc(fd); /* <salfromlen> */ |
| 1717 | if (ccnt < 0) |
| 1718 | goto formerr; |
| 1719 | if ((p = alloc(ccnt + 2)) == NULL) |
| 1720 | goto endFAIL; |
| 1721 | smp->sm_lead = p; |
| 1722 | |
| 1723 | /* Read up to the first special char into sm_lead. */ |
| 1724 | for (i = 0; i < ccnt; ++i) |
| 1725 | { |
| 1726 | c = getc(fd); /* <salfrom> */ |
| 1727 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
| 1728 | break; |
| 1729 | *p++ = c; |
| 1730 | } |
| 1731 | smp->sm_leadlen = p - smp->sm_lead; |
| 1732 | *p++ = NUL; |
| 1733 | |
| 1734 | /* Put optional chars in sm_oneoff, if any. */ |
| 1735 | if (c == '(') |
| 1736 | { |
| 1737 | smp->sm_oneoff = p; |
| 1738 | for (++i; i < ccnt; ++i) |
| 1739 | { |
| 1740 | c = getc(fd); /* <salfrom> */ |
| 1741 | if (c == ')') |
| 1742 | break; |
| 1743 | *p++ = c; |
| 1744 | } |
| 1745 | *p++ = NUL; |
| 1746 | if (++i < ccnt) |
| 1747 | c = getc(fd); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1748 | } |
| 1749 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1750 | smp->sm_oneoff = NULL; |
| 1751 | |
| 1752 | /* Any following chars go in sm_rules. */ |
| 1753 | smp->sm_rules = p; |
| 1754 | if (i < ccnt) |
| 1755 | *p++ = c; |
| 1756 | for (++i; i < ccnt; ++i) |
| 1757 | *p++ = getc(fd); /* <salfrom> */ |
| 1758 | *p++ = NUL; |
| 1759 | |
| 1760 | ccnt = getc(fd); /* <saltolen> */ |
| 1761 | if (ccnt < 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1762 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1763 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1764 | goto formerr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1765 | } |
| 1766 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1767 | { |
| 1768 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1769 | goto endFAIL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1770 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1771 | smp->sm_to = p; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1772 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1773 | for (i = 0; i < ccnt; ++i) |
| 1774 | *p++ = getc(fd); /* <salto> */ |
| 1775 | *p++ = NUL; |
| 1776 | } |
| 1777 | |
| 1778 | /* Fill the first-index table. */ |
| 1779 | first = lp->sl_sal_first; |
| 1780 | for (i = 0; i < 256; ++i) |
| 1781 | first[i] = -1; |
| 1782 | for (i = 0; i < gap->ga_len; ++i) |
| 1783 | { |
| 1784 | smp = &((salitem_T *)gap->ga_data)[i]; |
| 1785 | if (first[*smp->sm_lead] == -1) |
| 1786 | first[*smp->sm_lead] = i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */ |
| 1790 | if (cnt < 0) |
| 1791 | goto formerr; |
| 1792 | p = alloc(cnt + 1); |
| 1793 | if (p == NULL) |
| 1794 | goto endFAIL; |
| 1795 | for (i = 0; i < cnt; ++i) |
| 1796 | p[i] = getc(fd); /* <mapstr> */ |
| 1797 | p[i] = NUL; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1798 | set_map_str(lp, p); |
| 1799 | vim_free(p); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1800 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1801 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1802 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1803 | * round 2: <KWORDTREE> |
| 1804 | * round 3: <PREFIXTREE> */ |
| 1805 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1806 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1807 | /* The tree size was computed when writing the file, so that we can |
| 1808 | * allocate it as one long block. <nodecount> */ |
| 1809 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 1810 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1811 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1812 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1813 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1814 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1815 | bp = lalloc((long_u)len, TRUE); |
| 1816 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1817 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1818 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1819 | lp->sl_fbyts = bp; |
| 1820 | else if (round == 2) |
| 1821 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1822 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1823 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1826 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 1827 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1828 | goto endFAIL; |
| 1829 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1830 | lp->sl_fidxs = ip; |
| 1831 | else if (round == 2) |
| 1832 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1833 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1834 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1835 | |
| 1836 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1837 | 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] | 1838 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1839 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1840 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1841 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1842 | } |
| 1843 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1844 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1845 | /* For a new file link it in the list of spell files. */ |
| 1846 | if (old_lp == NULL) |
| 1847 | { |
| 1848 | lp->sl_next = first_lang; |
| 1849 | first_lang = lp; |
| 1850 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1851 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1852 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1853 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1854 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1855 | if (lang != NULL) |
| 1856 | /* truncating the name signals the error to spell_load_lang() */ |
| 1857 | *lang = NUL; |
| 1858 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1859 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1860 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1861 | lp = NULL; |
| 1862 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1863 | |
| 1864 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1865 | if (fd != NULL) |
| 1866 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1867 | sourcing_name = save_sourcing_name; |
| 1868 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1869 | |
| 1870 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1874 | * Read one row of siblings from the spell file and store it in the byte array |
| 1875 | * "byts" and index array "idxs". Recursively read the children. |
| 1876 | * |
| 1877 | * NOTE: The code here must match put_tree(). |
| 1878 | * |
| 1879 | * Returns the index follosing the siblings. |
| 1880 | * Returns -1 if the file is shorter than expected. |
| 1881 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1882 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1883 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1884 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1885 | FILE *fd; |
| 1886 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1887 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1888 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1889 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1890 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 1891 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1892 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1893 | int len; |
| 1894 | int i; |
| 1895 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1896 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1897 | int c; |
| 1898 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1899 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1900 | len = getc(fd); /* <siblingcount> */ |
| 1901 | if (len <= 0) |
| 1902 | return -1; |
| 1903 | |
| 1904 | if (startidx + len >= maxidx) |
| 1905 | return -2; |
| 1906 | byts[idx++] = len; |
| 1907 | |
| 1908 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 1909 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1910 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1911 | c = getc(fd); /* <byte> */ |
| 1912 | if (c < 0) |
| 1913 | return -1; |
| 1914 | if (c <= BY_SPECIAL) |
| 1915 | { |
| 1916 | if (c == BY_NOFLAGS) |
| 1917 | { |
| 1918 | /* No flags, all regions. */ |
| 1919 | idxs[idx] = 0; |
| 1920 | c = 0; |
| 1921 | } |
| 1922 | else if (c == BY_FLAGS) |
| 1923 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1924 | if (prefixtree) |
| 1925 | { |
| 1926 | /* Read the prefix ID and the condition nr. In idxs[] |
| 1927 | * store the prefix ID in the low byte, the condition |
| 1928 | * index shifted up 8 bits. */ |
| 1929 | c = getc(fd); /* <prefixID> */ |
| 1930 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 1931 | if (n >= maxprefcondnr) |
| 1932 | return -2; |
| 1933 | c = (n << 8) + c; |
| 1934 | } |
| 1935 | else |
| 1936 | { |
| 1937 | /* Read flags and optional region and prefix ID. In |
| 1938 | * idxs[] the flags go in the low byte, region above that |
| 1939 | * and prefix ID above the region. */ |
| 1940 | c = getc(fd); /* <flags> */ |
| 1941 | if (c & WF_REGION) |
| 1942 | c = (getc(fd) << 8) + c; /* <region> */ |
| 1943 | if (c & WF_PFX) |
| 1944 | c = (getc(fd) << 16) + c; /* <prefixID> */ |
| 1945 | } |
| 1946 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1947 | idxs[idx] = c; |
| 1948 | c = 0; |
| 1949 | } |
| 1950 | else /* c == BY_INDEX */ |
| 1951 | { |
| 1952 | /* <nodeidx> */ |
| 1953 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 1954 | if (n < 0 || n >= maxidx) |
| 1955 | return -2; |
| 1956 | idxs[idx] = n + SHARED_MASK; |
| 1957 | c = getc(fd); /* <xbyte> */ |
| 1958 | } |
| 1959 | } |
| 1960 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1963 | /* Recursively read the children for non-shared siblings. |
| 1964 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 1965 | * remove SHARED_MASK) */ |
| 1966 | for (i = 1; i <= len; ++i) |
| 1967 | if (byts[startidx + i] != 0) |
| 1968 | { |
| 1969 | if (idxs[startidx + i] & SHARED_MASK) |
| 1970 | idxs[startidx + i] &= ~SHARED_MASK; |
| 1971 | else |
| 1972 | { |
| 1973 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1974 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 1975 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1976 | if (idx < 0) |
| 1977 | break; |
| 1978 | } |
| 1979 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1980 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1981 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * Parse 'spelllang' and set buf->b_langp accordingly. |
| 1986 | * Returns an error message or NULL. |
| 1987 | */ |
| 1988 | char_u * |
| 1989 | did_set_spelllang(buf) |
| 1990 | buf_T *buf; |
| 1991 | { |
| 1992 | garray_T ga; |
| 1993 | char_u *lang; |
| 1994 | char_u *e; |
| 1995 | char_u *region; |
| 1996 | int region_mask; |
| 1997 | slang_T *lp; |
| 1998 | int c; |
| 1999 | char_u lbuf[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2000 | char_u spf_name[MAXPATHL]; |
| 2001 | int did_spf = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2002 | |
| 2003 | ga_init2(&ga, sizeof(langp_T), 2); |
| 2004 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2005 | /* Get the name of the .spl file associated with 'spellfile'. */ |
| 2006 | if (*buf->b_p_spf == NUL) |
| 2007 | did_spf = TRUE; |
| 2008 | else |
| 2009 | vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl", |
| 2010 | buf->b_p_spf); |
| 2011 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2012 | /* loop over comma separated languages. */ |
| 2013 | for (lang = buf->b_p_spl; *lang != NUL; lang = e) |
| 2014 | { |
| 2015 | e = vim_strchr(lang, ','); |
| 2016 | if (e == NULL) |
| 2017 | e = lang + STRLEN(lang); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2018 | region = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2019 | if (e > lang + 2) |
| 2020 | { |
| 2021 | if (e - lang >= MAXWLEN) |
| 2022 | { |
| 2023 | ga_clear(&ga); |
| 2024 | return e_invarg; |
| 2025 | } |
| 2026 | if (lang[2] == '_') |
| 2027 | region = lang + 3; |
| 2028 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2029 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2030 | /* Check if we loaded this language before. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2031 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2032 | if (STRNICMP(lp->sl_name, lang, 2) == 0) |
| 2033 | break; |
| 2034 | |
| 2035 | if (lp == NULL) |
| 2036 | { |
| 2037 | /* Not found, load the language. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2038 | vim_strncpy(lbuf, lang, e - lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2039 | if (region != NULL) |
| 2040 | mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2041 | spell_load_lang(lbuf); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2044 | /* |
| 2045 | * Loop over the languages, there can be several files for each. |
| 2046 | */ |
| 2047 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2048 | if (STRNICMP(lp->sl_name, lang, 2) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2049 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2050 | region_mask = REGION_ALL; |
| 2051 | if (region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2052 | { |
| 2053 | /* find region in sl_regions */ |
| 2054 | c = find_region(lp->sl_regions, region); |
| 2055 | if (c == REGION_ALL) |
| 2056 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2057 | if (!lp->sl_add) |
| 2058 | { |
| 2059 | c = *e; |
| 2060 | *e = NUL; |
| 2061 | smsg((char_u *)_("Warning: region %s not supported"), |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2062 | lang); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2063 | *e = c; |
| 2064 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2065 | } |
| 2066 | else |
| 2067 | region_mask = 1 << c; |
| 2068 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2069 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2070 | if (ga_grow(&ga, 1) == FAIL) |
| 2071 | { |
| 2072 | ga_clear(&ga); |
| 2073 | return e_outofmem; |
| 2074 | } |
| 2075 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2076 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2077 | ++ga.ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2078 | |
| 2079 | /* Check if this is the 'spellfile' spell file. */ |
| 2080 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2081 | did_spf = TRUE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2082 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2083 | |
| 2084 | if (*e == ',') |
| 2085 | ++e; |
| 2086 | } |
| 2087 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2088 | /* |
| 2089 | * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath', |
| 2090 | * then it's probably loaded above already. Otherwise load it here. |
| 2091 | */ |
| 2092 | if (!did_spf) |
| 2093 | { |
| 2094 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2095 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2096 | break; |
| 2097 | if (lp == NULL) |
| 2098 | { |
| 2099 | vim_strncpy(lbuf, gettail(spf_name), 2); |
| 2100 | lp = spell_load_file(spf_name, lbuf, NULL, TRUE); |
| 2101 | } |
| 2102 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2103 | { |
| 2104 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2105 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL; |
| 2106 | ++ga.ga_len; |
| 2107 | } |
| 2108 | } |
| 2109 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2110 | /* Add a NULL entry to mark the end of the list. */ |
| 2111 | if (ga_grow(&ga, 1) == FAIL) |
| 2112 | { |
| 2113 | ga_clear(&ga); |
| 2114 | return e_outofmem; |
| 2115 | } |
| 2116 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2117 | ++ga.ga_len; |
| 2118 | |
| 2119 | /* Everything is fine, store the new b_langp value. */ |
| 2120 | ga_clear(&buf->b_langp); |
| 2121 | buf->b_langp = ga; |
| 2122 | |
| 2123 | return NULL; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2128 | * Each region is simply stored as the two characters of it's name. |
| 2129 | * Returns the index if found, REGION_ALL if not found. |
| 2130 | */ |
| 2131 | static int |
| 2132 | find_region(rp, region) |
| 2133 | char_u *rp; |
| 2134 | char_u *region; |
| 2135 | { |
| 2136 | int i; |
| 2137 | |
| 2138 | for (i = 0; ; i += 2) |
| 2139 | { |
| 2140 | if (rp[i] == NUL) |
| 2141 | return REGION_ALL; |
| 2142 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2143 | break; |
| 2144 | } |
| 2145 | return i / 2; |
| 2146 | } |
| 2147 | |
| 2148 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2149 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2150 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2151 | * Word WF_ONECAP |
| 2152 | * W WORD WF_ALLCAP |
| 2153 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2154 | */ |
| 2155 | static int |
| 2156 | captype(word, end) |
| 2157 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2158 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2159 | { |
| 2160 | char_u *p; |
| 2161 | int c; |
| 2162 | int firstcap; |
| 2163 | int allcap; |
| 2164 | int past_second = FALSE; /* past second word char */ |
| 2165 | |
| 2166 | /* find first letter */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2167 | for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2168 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2169 | return 0; /* only non-word characters, illegal word */ |
| 2170 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2171 | if (has_mbyte) |
| 2172 | c = mb_ptr2char_adv(&p); |
| 2173 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2174 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2175 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2176 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2177 | |
| 2178 | /* |
| 2179 | * Need to check all letters to find a word with mixed upper/lower. |
| 2180 | * But a word with an upper char only at start is a ONECAP. |
| 2181 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2182 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2183 | if (SPELL_ISWORDP(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2184 | { |
| 2185 | #ifdef FEAT_MBYTE |
| 2186 | c = mb_ptr2char(p); |
| 2187 | #else |
| 2188 | c = *p; |
| 2189 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2190 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2191 | { |
| 2192 | /* UUl -> KEEPCAP */ |
| 2193 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2194 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2195 | allcap = FALSE; |
| 2196 | } |
| 2197 | else if (!allcap) |
| 2198 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2199 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2200 | past_second = TRUE; |
| 2201 | } |
| 2202 | |
| 2203 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2204 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2205 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2206 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2207 | return 0; |
| 2208 | } |
| 2209 | |
| 2210 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2211 | /* |
| 2212 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2213 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2214 | */ |
| 2215 | void |
| 2216 | spell_reload() |
| 2217 | { |
| 2218 | buf_T *buf; |
| 2219 | slang_T *lp; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2220 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2221 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2222 | /* Initialize the table for SPELL_ISWORDP(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2223 | init_spell_chartab(); |
| 2224 | |
| 2225 | /* Unload all allocated memory. */ |
| 2226 | while (first_lang != NULL) |
| 2227 | { |
| 2228 | lp = first_lang; |
| 2229 | first_lang = lp->sl_next; |
| 2230 | slang_free(lp); |
| 2231 | } |
| 2232 | |
| 2233 | /* Go through all buffers and handle 'spelllang'. */ |
| 2234 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2235 | { |
| 2236 | ga_clear(&buf->b_langp); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2237 | |
| 2238 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2239 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2240 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2241 | { |
| 2242 | FOR_ALL_WINDOWS(wp) |
| 2243 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2244 | { |
| 2245 | (void)did_set_spelllang(buf); |
| 2246 | # ifdef FEAT_WINDOWS |
| 2247 | break; |
| 2248 | # endif |
| 2249 | } |
| 2250 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | # endif |
| 2254 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2255 | /* |
| 2256 | * Reload the spell file "fname" if it's loaded. |
| 2257 | */ |
| 2258 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2259 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2260 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2261 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2262 | { |
| 2263 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2264 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2265 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2266 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2267 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 2268 | { |
| 2269 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2270 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2271 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2272 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2273 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2274 | |
| 2275 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 2276 | * 'spelllang' to get it loaded. */ |
| 2277 | if (added_word && !didit) |
| 2278 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2282 | /* |
| 2283 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2284 | */ |
| 2285 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2286 | #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] | 2287 | and .dic file. */ |
| 2288 | /* |
| 2289 | * Main structure to store the contents of a ".aff" file. |
| 2290 | */ |
| 2291 | typedef struct afffile_S |
| 2292 | { |
| 2293 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2294 | int af_rar; /* RAR ID for rare word */ |
| 2295 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2296 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2297 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2298 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2299 | } afffile_T; |
| 2300 | |
| 2301 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2302 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2303 | struct affentry_S |
| 2304 | { |
| 2305 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2306 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 2307 | 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] | 2308 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 2309 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2310 | }; |
| 2311 | |
| 2312 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 2313 | typedef struct affheader_S |
| 2314 | { |
| 2315 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2316 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2317 | int ah_combine; /* suffix may combine with prefix */ |
| 2318 | affentry_T *ah_first; /* first affix entry */ |
| 2319 | } affheader_T; |
| 2320 | |
| 2321 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 2322 | |
| 2323 | /* |
| 2324 | * Structure that is used to store the items in the word tree. This avoids |
| 2325 | * the need to keep track of each allocated thing, it's freed all at once |
| 2326 | * after ":mkspell" is done. |
| 2327 | */ |
| 2328 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 2329 | typedef struct sblock_S sblock_T; |
| 2330 | struct sblock_S |
| 2331 | { |
| 2332 | sblock_T *sb_next; /* next block in list */ |
| 2333 | int sb_used; /* nr of bytes already in use */ |
| 2334 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2335 | }; |
| 2336 | |
| 2337 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2338 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2339 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2340 | typedef struct wordnode_S wordnode_T; |
| 2341 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2342 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2343 | char_u wn_hashkey[6]; /* room for the hash key */ |
| 2344 | wordnode_T *wn_next; /* next node with same hash key */ |
| 2345 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 2346 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 2347 | always sorted) */ |
| 2348 | wordnode_T *wn_wnode; /* parent node that will write this node */ |
| 2349 | int wn_index; /* index in written nodes (valid after first |
| 2350 | round) */ |
| 2351 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 2352 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2353 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 2354 | PREFIXTREE it's the prefcondnr */ |
| 2355 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2356 | }; |
| 2357 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2358 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2359 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2360 | /* |
| 2361 | * Info used while reading the spell files. |
| 2362 | */ |
| 2363 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2364 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2365 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2366 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2367 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2368 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2369 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2370 | sblock_T *si_blocks; /* memory blocks used */ |
| 2371 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2372 | int si_add; /* addition file */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2373 | int si_region; /* region mask */ |
| 2374 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2375 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2376 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2377 | int si_region_count; /* number of regions supported (1 when there |
| 2378 | are no regions) */ |
| 2379 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2380 | |
| 2381 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 2382 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
| 2383 | int si_followup; /* soundsalike: ? */ |
| 2384 | int si_collapse; /* soundsalike: ? */ |
| 2385 | int si_rem_accents; /* soundsalike: remove accents */ |
| 2386 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2387 | garray_T si_prefcond; /* table with conditions for postponed |
| 2388 | * prefixes, each stored as a string */ |
| 2389 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2390 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2391 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2392 | 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] | 2393 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2394 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 2395 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2396 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2397 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 2398 | 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] | 2399 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 2400 | 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] | 2401 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 2402 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 2403 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 2404 | static void free_blocks __ARGS((sblock_T *bl)); |
| 2405 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2406 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 2407 | 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] | 2408 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2409 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 2410 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2411 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2412 | static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2413 | 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] | 2414 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2415 | |
| 2416 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2417 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2418 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2419 | */ |
| 2420 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2421 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2422 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2423 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2424 | { |
| 2425 | FILE *fd; |
| 2426 | afffile_T *aff; |
| 2427 | char_u rline[MAXLINELEN]; |
| 2428 | char_u *line; |
| 2429 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2430 | #define MAXITEMCNT 7 |
| 2431 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2432 | int itemcnt; |
| 2433 | char_u *p; |
| 2434 | int lnum = 0; |
| 2435 | affheader_T *cur_aff = NULL; |
| 2436 | int aff_todo = 0; |
| 2437 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2438 | char_u *low = NULL; |
| 2439 | char_u *fol = NULL; |
| 2440 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2441 | 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] | 2442 | int do_rep; |
| 2443 | int do_sal; |
| 2444 | int do_map; |
| 2445 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2446 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2447 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2448 | /* |
| 2449 | * Open the file. |
| 2450 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2451 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2452 | if (fd == NULL) |
| 2453 | { |
| 2454 | EMSG2(_(e_notopen), fname); |
| 2455 | return NULL; |
| 2456 | } |
| 2457 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2458 | if (spin->si_verbose || p_verbose > 2) |
| 2459 | { |
| 2460 | if (!spin->si_verbose) |
| 2461 | verbose_enter(); |
| 2462 | smsg((char_u *)_("Reading affix file %s..."), fname); |
| 2463 | out_flush(); |
| 2464 | if (!spin->si_verbose) |
| 2465 | verbose_leave(); |
| 2466 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2467 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2468 | /* Only do REP lines when not done in another .aff file already. */ |
| 2469 | do_rep = spin->si_rep.ga_len == 0; |
| 2470 | |
| 2471 | /* Only do SAL lines when not done in another .aff file already. */ |
| 2472 | do_sal = spin->si_sal.ga_len == 0; |
| 2473 | |
| 2474 | /* Only do MAP lines when not done in another .aff file already. */ |
| 2475 | do_map = spin->si_map.ga_len == 0; |
| 2476 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2477 | /* |
| 2478 | * Allocate and init the afffile_T structure. |
| 2479 | */ |
| 2480 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2481 | if (aff == NULL) |
| 2482 | return NULL; |
| 2483 | hash_init(&aff->af_pref); |
| 2484 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2485 | |
| 2486 | /* |
| 2487 | * Read all the lines in the file one by one. |
| 2488 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2489 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2490 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2491 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2492 | ++lnum; |
| 2493 | |
| 2494 | /* Skip comment lines. */ |
| 2495 | if (*rline == '#') |
| 2496 | continue; |
| 2497 | |
| 2498 | /* Convert from "SET" to 'encoding' when needed. */ |
| 2499 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2500 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2501 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2502 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2503 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2504 | if (pc == NULL) |
| 2505 | { |
| 2506 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 2507 | fname, lnum, rline); |
| 2508 | continue; |
| 2509 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2510 | line = pc; |
| 2511 | } |
| 2512 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2513 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2514 | { |
| 2515 | pc = NULL; |
| 2516 | line = rline; |
| 2517 | } |
| 2518 | |
| 2519 | /* Split the line up in white separated items. Put a NUL after each |
| 2520 | * item. */ |
| 2521 | itemcnt = 0; |
| 2522 | for (p = line; ; ) |
| 2523 | { |
| 2524 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 2525 | ++p; |
| 2526 | if (*p == NUL) |
| 2527 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2528 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2529 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2530 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2531 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2532 | ++p; |
| 2533 | if (*p == NUL) |
| 2534 | break; |
| 2535 | *p++ = NUL; |
| 2536 | } |
| 2537 | |
| 2538 | /* Handle non-empty lines. */ |
| 2539 | if (itemcnt > 0) |
| 2540 | { |
| 2541 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 2542 | && aff->af_enc == NULL) |
| 2543 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2544 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2545 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 2546 | aff->af_enc = enc_canonize(items[1]); |
| 2547 | if (aff->af_enc != NULL && !spin->si_ascii |
| 2548 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 2549 | p_enc) == FAIL) |
| 2550 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 2551 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2552 | #else |
| 2553 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 2554 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2555 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2556 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 2557 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2558 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2559 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2560 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2561 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2562 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2563 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2564 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 2565 | && aff->af_rar == 0) |
| 2566 | { |
| 2567 | aff->af_rar = items[1][0]; |
| 2568 | if (items[1][1] != NUL) |
| 2569 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2570 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2571 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 2572 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2573 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2574 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2575 | if (items[1][1] != NUL) |
| 2576 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2577 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2578 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 2579 | { |
| 2580 | aff->af_pfxpostpone = TRUE; |
| 2581 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2582 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2583 | || STRCMP(items[0], "SFX") == 0) |
| 2584 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2585 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2586 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2587 | /* Myspell allows extra text after the item, but that might |
| 2588 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2589 | if (itemcnt > 4 && *items[4] != '#') |
| 2590 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2591 | fname, lnum, items[4]); |
| 2592 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2593 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2594 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 2595 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2596 | if (cur_aff == NULL) |
| 2597 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2598 | cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2599 | cur_aff->ah_key[1] = NUL; |
| 2600 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2601 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2602 | if (*items[2] == 'Y') |
| 2603 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2604 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2605 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 2606 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2607 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2608 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2609 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2610 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2611 | /* Use a new number in the .spl file later, to be able to |
| 2612 | * handle multiple .aff files. */ |
| 2613 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2614 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2615 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2616 | else |
| 2617 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2618 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2619 | hi = hash_find(tp, cur_aff->ah_key); |
| 2620 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2621 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2622 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 2623 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2624 | aff_todo = 0; |
| 2625 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2626 | else |
| 2627 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2628 | } |
| 2629 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2630 | || STRCMP(items[0], "SFX") == 0) |
| 2631 | && aff_todo > 0 |
| 2632 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2633 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2634 | { |
| 2635 | affentry_T *aff_entry; |
| 2636 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2637 | /* Myspell allows extra text after the item, but that might |
| 2638 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2639 | if (itemcnt > 5 && *items[5] != '#') |
| 2640 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2641 | fname, lnum, items[5]); |
| 2642 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2643 | /* New item for an affix letter. */ |
| 2644 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2645 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 2646 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2647 | if (aff_entry == NULL) |
| 2648 | break; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2649 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2650 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2651 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 2652 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2653 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2654 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 2655 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2656 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2657 | /* Don't use an affix entry with non-ASCII characters when |
| 2658 | * "spin->si_ascii" is TRUE. */ |
| 2659 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2660 | || has_non_ascii(aff_entry->ae_add))) |
| 2661 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2662 | aff_entry->ae_next = cur_aff->ah_first; |
| 2663 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2664 | |
| 2665 | if (STRCMP(items[4], ".") != 0) |
| 2666 | { |
| 2667 | char_u buf[MAXLINELEN]; |
| 2668 | |
| 2669 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 2670 | items[4]); |
| 2671 | if (*items[0] == 'P') |
| 2672 | sprintf((char *)buf, "^%s", items[4]); |
| 2673 | else |
| 2674 | sprintf((char *)buf, "%s$", items[4]); |
| 2675 | aff_entry->ae_prog = vim_regcomp(buf, |
| 2676 | RE_MAGIC + RE_STRING); |
| 2677 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2678 | |
| 2679 | /* For postponed prefixes we need an entry in si_prefcond |
| 2680 | * for the condition. Use an existing one if possible. */ |
| 2681 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 2682 | && aff_entry->ae_chop == NULL) |
| 2683 | { |
| 2684 | int idx; |
| 2685 | char_u **pp; |
| 2686 | |
| 2687 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 2688 | --idx) |
| 2689 | { |
| 2690 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 2691 | if (str_equal(p, aff_entry->ae_cond)) |
| 2692 | break; |
| 2693 | } |
| 2694 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 2695 | { |
| 2696 | /* Not found, add a new condition. */ |
| 2697 | idx = spin->si_prefcond.ga_len++; |
| 2698 | pp = ((char_u **)spin->si_prefcond.ga_data) + idx; |
| 2699 | if (aff_entry->ae_cond == NULL) |
| 2700 | *pp = NULL; |
| 2701 | else |
| 2702 | *pp = getroom_save(&spin->si_blocks, |
| 2703 | aff_entry->ae_cond); |
| 2704 | } |
| 2705 | |
| 2706 | /* Add the prefix to the prefix tree. */ |
| 2707 | if (aff_entry->ae_add == NULL) |
| 2708 | p = (char_u *)""; |
| 2709 | else |
| 2710 | p = aff_entry->ae_add; |
| 2711 | tree_add_word(p, spin->si_prefroot, -1, idx, |
| 2712 | cur_aff->ah_newID, &spin->si_blocks); |
| 2713 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2714 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2715 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2716 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 2717 | { |
| 2718 | if (fol != NULL) |
| 2719 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 2720 | fname, lnum); |
| 2721 | else |
| 2722 | fol = vim_strsave(items[1]); |
| 2723 | } |
| 2724 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 2725 | { |
| 2726 | if (low != NULL) |
| 2727 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 2728 | fname, lnum); |
| 2729 | else |
| 2730 | low = vim_strsave(items[1]); |
| 2731 | } |
| 2732 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 2733 | { |
| 2734 | if (upp != NULL) |
| 2735 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 2736 | fname, lnum); |
| 2737 | else |
| 2738 | upp = vim_strsave(items[1]); |
| 2739 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2740 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2741 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2742 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2743 | if (!isdigit(*items[1])) |
| 2744 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 2745 | fname, lnum); |
| 2746 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2747 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 2748 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2749 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2750 | if (do_rep) |
| 2751 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 2752 | } |
| 2753 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 2754 | { |
| 2755 | /* MAP item or count */ |
| 2756 | if (!found_map) |
| 2757 | { |
| 2758 | /* First line contains the count. */ |
| 2759 | found_map = TRUE; |
| 2760 | if (!isdigit(*items[1])) |
| 2761 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 2762 | fname, lnum); |
| 2763 | } |
| 2764 | else if (do_map) |
| 2765 | { |
| 2766 | /* We simply concatenate all the MAP strings, separated by |
| 2767 | * slashes. */ |
| 2768 | ga_concat(&spin->si_map, items[1]); |
| 2769 | ga_append(&spin->si_map, '/'); |
| 2770 | } |
| 2771 | } |
| 2772 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 2773 | { |
| 2774 | if (do_sal) |
| 2775 | { |
| 2776 | /* SAL item (sounds-a-like) |
| 2777 | * Either one of the known keys or a from-to pair. */ |
| 2778 | if (STRCMP(items[1], "followup") == 0) |
| 2779 | spin->si_followup = sal_to_bool(items[2]); |
| 2780 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 2781 | spin->si_collapse = sal_to_bool(items[2]); |
| 2782 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 2783 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 2784 | else |
| 2785 | /* when "to" is "_" it means empty */ |
| 2786 | add_fromto(spin, &spin->si_sal, items[1], |
| 2787 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 2788 | : items[2]); |
| 2789 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2790 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2791 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2792 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 2793 | fname, lnum, items[0]); |
| 2794 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2797 | if (fol != NULL || low != NULL || upp != NULL) |
| 2798 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2799 | /* |
| 2800 | * Don't write a word table for an ASCII file, so that we don't check |
| 2801 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2802 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2803 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 2804 | */ |
| 2805 | if (!spin->si_ascii |
| 2806 | #ifdef FEAT_MBYTE |
| 2807 | && !enc_utf8 |
| 2808 | #endif |
| 2809 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2810 | { |
| 2811 | if (fol == NULL || low == NULL || upp == NULL) |
| 2812 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 2813 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2814 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2815 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2816 | |
| 2817 | vim_free(fol); |
| 2818 | vim_free(low); |
| 2819 | vim_free(upp); |
| 2820 | } |
| 2821 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2822 | vim_free(pc); |
| 2823 | fclose(fd); |
| 2824 | return aff; |
| 2825 | } |
| 2826 | |
| 2827 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2828 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 2829 | * NULL as equal. |
| 2830 | */ |
| 2831 | static int |
| 2832 | str_equal(s1, s2) |
| 2833 | char_u *s1; |
| 2834 | char_u *s2; |
| 2835 | { |
| 2836 | if (s1 == NULL || s2 == NULL) |
| 2837 | return s1 == s2; |
| 2838 | return STRCMP(s1, s2) == 0; |
| 2839 | } |
| 2840 | |
| 2841 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2842 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 2843 | * They are stored case-folded. |
| 2844 | */ |
| 2845 | static void |
| 2846 | add_fromto(spin, gap, from, to) |
| 2847 | spellinfo_T *spin; |
| 2848 | garray_T *gap; |
| 2849 | char_u *from; |
| 2850 | char_u *to; |
| 2851 | { |
| 2852 | fromto_T *ftp; |
| 2853 | char_u word[MAXWLEN]; |
| 2854 | |
| 2855 | if (ga_grow(gap, 1) == OK) |
| 2856 | { |
| 2857 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 2858 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 2859 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 2860 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 2861 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 2862 | ++gap->ga_len; |
| 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | /* |
| 2867 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 2868 | */ |
| 2869 | static int |
| 2870 | sal_to_bool(s) |
| 2871 | char_u *s; |
| 2872 | { |
| 2873 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 2874 | } |
| 2875 | |
| 2876 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2877 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 2878 | * When "s" is NULL FALSE is returned. |
| 2879 | */ |
| 2880 | static int |
| 2881 | has_non_ascii(s) |
| 2882 | char_u *s; |
| 2883 | { |
| 2884 | char_u *p; |
| 2885 | |
| 2886 | if (s != NULL) |
| 2887 | for (p = s; *p != NUL; ++p) |
| 2888 | if (*p >= 128) |
| 2889 | return TRUE; |
| 2890 | return FALSE; |
| 2891 | } |
| 2892 | |
| 2893 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2894 | * Free the structure filled by spell_read_aff(). |
| 2895 | */ |
| 2896 | static void |
| 2897 | spell_free_aff(aff) |
| 2898 | afffile_T *aff; |
| 2899 | { |
| 2900 | hashtab_T *ht; |
| 2901 | hashitem_T *hi; |
| 2902 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2903 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2904 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2905 | |
| 2906 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2907 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2908 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2909 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 2910 | { |
| 2911 | todo = ht->ht_used; |
| 2912 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 2913 | { |
| 2914 | if (!HASHITEM_EMPTY(hi)) |
| 2915 | { |
| 2916 | --todo; |
| 2917 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2918 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 2919 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2920 | } |
| 2921 | } |
| 2922 | if (ht == &aff->af_suff) |
| 2923 | break; |
| 2924 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2925 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2926 | hash_clear(&aff->af_pref); |
| 2927 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2931 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2932 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2933 | */ |
| 2934 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2935 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2936 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2937 | spellinfo_T *spin; |
| 2938 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2939 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2940 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2941 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2942 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2943 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2944 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2945 | char_u *pc; |
| 2946 | char_u *w; |
| 2947 | int l; |
| 2948 | hash_T hash; |
| 2949 | hashitem_T *hi; |
| 2950 | FILE *fd; |
| 2951 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2952 | int non_ascii = 0; |
| 2953 | int retval = OK; |
| 2954 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2955 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2956 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2957 | /* |
| 2958 | * Open the file. |
| 2959 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2960 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2961 | if (fd == NULL) |
| 2962 | { |
| 2963 | EMSG2(_(e_notopen), fname); |
| 2964 | return FAIL; |
| 2965 | } |
| 2966 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2967 | /* The hashtable is only used to detect duplicated words. */ |
| 2968 | hash_init(&ht); |
| 2969 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2970 | spin->si_foldwcount = 0; |
| 2971 | spin->si_keepwcount = 0; |
| 2972 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2973 | if (spin->si_verbose || p_verbose > 2) |
| 2974 | { |
| 2975 | if (!spin->si_verbose) |
| 2976 | verbose_enter(); |
| 2977 | smsg((char_u *)_("Reading dictionary file %s..."), fname); |
| 2978 | out_flush(); |
| 2979 | if (!spin->si_verbose) |
| 2980 | verbose_leave(); |
| 2981 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2982 | |
| 2983 | /* Read and ignore the first line: word count. */ |
| 2984 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2985 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2986 | EMSG2(_("E760: No word count in %s"), fname); |
| 2987 | |
| 2988 | /* |
| 2989 | * Read all the lines in the file one by one. |
| 2990 | * The words are converted to 'encoding' here, before being added to |
| 2991 | * the hashtable. |
| 2992 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2993 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2994 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2995 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2996 | ++lnum; |
| 2997 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2998 | /* Remove CR, LF and white space from the end. White space halfway |
| 2999 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3000 | l = STRLEN(line); |
| 3001 | while (l > 0 && line[l - 1] <= ' ') |
| 3002 | --l; |
| 3003 | if (l == 0) |
| 3004 | continue; /* empty line */ |
| 3005 | line[l] = NUL; |
| 3006 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3007 | /* Find the optional affix names. */ |
| 3008 | afflist = vim_strchr(line, '/'); |
| 3009 | if (afflist != NULL) |
| 3010 | *afflist++ = NUL; |
| 3011 | |
| 3012 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3013 | if (spin->si_ascii && has_non_ascii(line)) |
| 3014 | { |
| 3015 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3016 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3017 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3018 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3019 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3020 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3021 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3022 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3023 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3024 | if (pc == NULL) |
| 3025 | { |
| 3026 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3027 | fname, lnum, line); |
| 3028 | continue; |
| 3029 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3030 | w = pc; |
| 3031 | } |
| 3032 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3033 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3034 | { |
| 3035 | pc = NULL; |
| 3036 | w = line; |
| 3037 | } |
| 3038 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3039 | /* This takes time, print a message now and then. */ |
| 3040 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3041 | { |
| 3042 | vim_snprintf((char *)message, sizeof(message), |
| 3043 | _("line %6d, word %6d - %s"), |
| 3044 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3045 | msg_start(); |
| 3046 | msg_puts_long_attr(message, 0); |
| 3047 | msg_clr_eos(); |
| 3048 | msg_didout = FALSE; |
| 3049 | msg_col = 0; |
| 3050 | out_flush(); |
| 3051 | } |
| 3052 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3053 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 3054 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3055 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3056 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3057 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3058 | if (retval == FAIL) |
| 3059 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3060 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3061 | hash = hash_hash(dw); |
| 3062 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3063 | if (!HASHITEM_EMPTY(hi)) |
| 3064 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3065 | fname, lnum, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3066 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3067 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3068 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3069 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3070 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3071 | if (afflist != NULL) |
| 3072 | { |
| 3073 | /* Check for affix name that stands for keep-case word and stands |
| 3074 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3075 | if (affile->af_kep != NUL |
| 3076 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3077 | flags |= WF_KEEPCAP; |
| 3078 | if (affile->af_rar != NUL |
| 3079 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 3080 | flags |= WF_RARE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3081 | |
| 3082 | if (affile->af_pfxpostpone) |
| 3083 | /* Need to store the list of prefix IDs with the word. */ |
| 3084 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3087 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3088 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3089 | retval = FAIL; |
| 3090 | |
| 3091 | if (afflist != NULL) |
| 3092 | { |
| 3093 | /* Find all matching suffixes and add the resulting words. |
| 3094 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3095 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3096 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3097 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3098 | retval = FAIL; |
| 3099 | |
| 3100 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3101 | if (store_aff_word(dw, spin, afflist, affile, |
| 3102 | &affile->af_pref, NULL, |
| 3103 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3104 | retval = FAIL; |
| 3105 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3108 | if (spin->si_ascii && non_ascii > 0) |
| 3109 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3110 | non_ascii); |
| 3111 | hash_clear(&ht); |
| 3112 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3113 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3114 | return retval; |
| 3115 | } |
| 3116 | |
| 3117 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3118 | * Get the list of prefix IDs from the affix list "afflist". |
| 3119 | * Used for PFXPOSTPONE. |
| 3120 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 3121 | * or when out of memory. |
| 3122 | */ |
| 3123 | static char_u * |
| 3124 | get_pfxlist(affile, afflist, blp) |
| 3125 | afffile_T *affile; |
| 3126 | char_u *afflist; |
| 3127 | sblock_T **blp; |
| 3128 | { |
| 3129 | char_u *p; |
| 3130 | int cnt; |
| 3131 | int round; |
| 3132 | char_u *res = NULL; |
| 3133 | char_u key[2]; |
| 3134 | hashitem_T *hi; |
| 3135 | |
| 3136 | key[1] = NUL; |
| 3137 | |
| 3138 | /* round 1: count the number of prefix IDs. |
| 3139 | * round 2: move prefix IDs to "res" */ |
| 3140 | for (round = 1; round <= 2; ++round) |
| 3141 | { |
| 3142 | cnt = 0; |
| 3143 | for (p = afflist; *p != NUL; ++p) |
| 3144 | { |
| 3145 | key[0] = *p; |
| 3146 | hi = hash_find(&affile->af_pref, key); |
| 3147 | if (!HASHITEM_EMPTY(hi)) |
| 3148 | { |
| 3149 | /* This is a prefix ID, use the new number. */ |
| 3150 | if (round == 2) |
| 3151 | res[cnt] = HI2AH(hi)->ah_newID; |
| 3152 | ++cnt; |
| 3153 | } |
| 3154 | } |
| 3155 | if (round == 1 && cnt > 0) |
| 3156 | res = getroom(blp, cnt + 1); |
| 3157 | if (res == NULL) |
| 3158 | break; |
| 3159 | } |
| 3160 | |
| 3161 | if (res != NULL) |
| 3162 | res[cnt] = NUL; |
| 3163 | return res; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3167 | * Apply affixes to a word and store the resulting words. |
| 3168 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 3169 | * prefixes or suffixes. |
| 3170 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 3171 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3172 | * |
| 3173 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3174 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3175 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3176 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3177 | char_u *word; /* basic word start */ |
| 3178 | spellinfo_T *spin; /* spell info */ |
| 3179 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3180 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3181 | hashtab_T *ht; |
| 3182 | hashtab_T *xht; |
| 3183 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3184 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3185 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3186 | { |
| 3187 | int todo; |
| 3188 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3189 | affheader_T *ah; |
| 3190 | affentry_T *ae; |
| 3191 | regmatch_T regmatch; |
| 3192 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3193 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3194 | int i; |
| 3195 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3196 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3197 | todo = ht->ht_used; |
| 3198 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3199 | { |
| 3200 | if (!HASHITEM_EMPTY(hi)) |
| 3201 | { |
| 3202 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3203 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3204 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3205 | /* Check that the affix combines, if required, and that the word |
| 3206 | * supports this affix. */ |
| 3207 | if ((!comb || ah->ah_combine) |
| 3208 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3209 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3210 | /* Loop over all affix entries with this name. */ |
| 3211 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3212 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3213 | /* Check the condition. It's not logical to match case |
| 3214 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3215 | * Myspell. |
| 3216 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 3217 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3218 | regmatch.regprog = ae->ae_prog; |
| 3219 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3220 | if ((xht != NULL || !affile->af_pfxpostpone |
| 3221 | || ae->ae_chop != NULL) |
| 3222 | && (ae->ae_prog == NULL |
| 3223 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3224 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3225 | /* Match. Remove the chop and add the affix. */ |
| 3226 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3227 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3228 | /* prefix: chop/add at the start of the word */ |
| 3229 | if (ae->ae_add == NULL) |
| 3230 | *newword = NUL; |
| 3231 | else |
| 3232 | STRCPY(newword, ae->ae_add); |
| 3233 | p = word; |
| 3234 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3235 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3236 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3237 | #ifdef FEAT_MBYTE |
| 3238 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3239 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3240 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3241 | for ( ; i > 0; --i) |
| 3242 | mb_ptr_adv(p); |
| 3243 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3244 | else |
| 3245 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3246 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3247 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3248 | STRCAT(newword, p); |
| 3249 | } |
| 3250 | else |
| 3251 | { |
| 3252 | /* suffix: chop/add at the end of the word */ |
| 3253 | STRCPY(newword, word); |
| 3254 | if (ae->ae_chop != NULL) |
| 3255 | { |
| 3256 | /* Remove chop string. */ |
| 3257 | p = newword + STRLEN(newword); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3258 | #ifdef FEAT_MBYTE |
| 3259 | if (has_mbyte) |
| 3260 | i = mb_charlen(ae->ae_chop); |
| 3261 | else |
| 3262 | #endif |
| 3263 | i = STRLEN(ae->ae_chop); |
| 3264 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3265 | mb_ptr_back(newword, p); |
| 3266 | *p = NUL; |
| 3267 | } |
| 3268 | if (ae->ae_add != NULL) |
| 3269 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3272 | /* Store the modified word. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3273 | if (store_word(newword, spin, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3274 | flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3275 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3276 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3277 | /* When added a suffix and combining is allowed also |
| 3278 | * try adding prefixes additionally. */ |
| 3279 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3280 | if (store_aff_word(newword, spin, afflist, affile, |
| 3281 | xht, NULL, TRUE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3282 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3283 | } |
| 3284 | } |
| 3285 | } |
| 3286 | } |
| 3287 | } |
| 3288 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3289 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3293 | * Read a file with a list of words. |
| 3294 | */ |
| 3295 | static int |
| 3296 | spell_read_wordfile(fname, spin) |
| 3297 | char_u *fname; |
| 3298 | spellinfo_T *spin; |
| 3299 | { |
| 3300 | FILE *fd; |
| 3301 | long lnum = 0; |
| 3302 | char_u rline[MAXLINELEN]; |
| 3303 | char_u *line; |
| 3304 | char_u *pc = NULL; |
| 3305 | int l; |
| 3306 | int retval = OK; |
| 3307 | int did_word = FALSE; |
| 3308 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3309 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3310 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3311 | |
| 3312 | /* |
| 3313 | * Open the file. |
| 3314 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3315 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3316 | if (fd == NULL) |
| 3317 | { |
| 3318 | EMSG2(_(e_notopen), fname); |
| 3319 | return FAIL; |
| 3320 | } |
| 3321 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3322 | if (spin->si_verbose || p_verbose > 2) |
| 3323 | { |
| 3324 | if (!spin->si_verbose) |
| 3325 | verbose_enter(); |
| 3326 | smsg((char_u *)_("Reading word file %s..."), fname); |
| 3327 | out_flush(); |
| 3328 | if (!spin->si_verbose) |
| 3329 | verbose_leave(); |
| 3330 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3331 | |
| 3332 | /* |
| 3333 | * Read all the lines in the file one by one. |
| 3334 | */ |
| 3335 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 3336 | { |
| 3337 | line_breakcheck(); |
| 3338 | ++lnum; |
| 3339 | |
| 3340 | /* Skip comment lines. */ |
| 3341 | if (*rline == '#') |
| 3342 | continue; |
| 3343 | |
| 3344 | /* Remove CR, LF and white space from the end. */ |
| 3345 | l = STRLEN(rline); |
| 3346 | while (l > 0 && rline[l - 1] <= ' ') |
| 3347 | --l; |
| 3348 | if (l == 0) |
| 3349 | continue; /* empty or blank line */ |
| 3350 | rline[l] = NUL; |
| 3351 | |
| 3352 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 3353 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3354 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3355 | if (spin->si_conv.vc_type != CONV_NONE) |
| 3356 | { |
| 3357 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 3358 | if (pc == NULL) |
| 3359 | { |
| 3360 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3361 | fname, lnum, rline); |
| 3362 | continue; |
| 3363 | } |
| 3364 | line = pc; |
| 3365 | } |
| 3366 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3367 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3368 | { |
| 3369 | pc = NULL; |
| 3370 | line = rline; |
| 3371 | } |
| 3372 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3373 | flags = 0; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3374 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3375 | |
| 3376 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3377 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3378 | ++line; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3379 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3380 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3381 | { |
| 3382 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3383 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 3384 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3385 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3386 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 3387 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3388 | else |
| 3389 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3390 | #ifdef FEAT_MBYTE |
| 3391 | char_u *enc; |
| 3392 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3393 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3394 | line += 10; |
| 3395 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3396 | if (enc != NULL && !spin->si_ascii |
| 3397 | && convert_setup(&spin->si_conv, enc, |
| 3398 | p_enc) == FAIL) |
| 3399 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3400 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3401 | vim_free(enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3402 | #else |
| 3403 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3404 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3405 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3406 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3407 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3408 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3409 | if (STRNCMP(line, "regions=", 8) == 0) |
| 3410 | { |
| 3411 | if (spin->si_region_count > 1) |
| 3412 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 3413 | fname, lnum, line); |
| 3414 | else |
| 3415 | { |
| 3416 | line += 8; |
| 3417 | if (STRLEN(line) > 16) |
| 3418 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 3419 | fname, lnum, line); |
| 3420 | else |
| 3421 | { |
| 3422 | spin->si_region_count = STRLEN(line) / 2; |
| 3423 | STRCPY(spin->si_region_name, line); |
| 3424 | } |
| 3425 | } |
| 3426 | continue; |
| 3427 | } |
| 3428 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3429 | if (*line == '=') |
| 3430 | { |
| 3431 | /* keep-case word */ |
| 3432 | flags |= WF_KEEPCAP; |
| 3433 | ++line; |
| 3434 | } |
| 3435 | |
| 3436 | if (*line == '!') |
| 3437 | { |
| 3438 | /* Bad, bad, wicked word. */ |
| 3439 | flags |= WF_BANNED; |
| 3440 | ++line; |
| 3441 | } |
| 3442 | else if (*line == '?') |
| 3443 | { |
| 3444 | /* Rare word. */ |
| 3445 | flags |= WF_RARE; |
| 3446 | ++line; |
| 3447 | } |
| 3448 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3449 | if (VIM_ISDIGIT(*line)) |
| 3450 | { |
| 3451 | /* region number(s) */ |
| 3452 | regionmask = 0; |
| 3453 | while (VIM_ISDIGIT(*line)) |
| 3454 | { |
| 3455 | l = *line - '0'; |
| 3456 | if (l > spin->si_region_count) |
| 3457 | { |
| 3458 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
| 3459 | fname, lnum, line); |
| 3460 | break; |
| 3461 | } |
| 3462 | regionmask |= 1 << (l - 1); |
| 3463 | ++line; |
| 3464 | } |
| 3465 | flags |= WF_REGION; |
| 3466 | } |
| 3467 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3468 | if (flags == 0) |
| 3469 | { |
| 3470 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3471 | fname, lnum, line); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3472 | continue; |
| 3473 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3477 | if (spin->si_ascii && has_non_ascii(line)) |
| 3478 | { |
| 3479 | ++non_ascii; |
| 3480 | continue; |
| 3481 | } |
| 3482 | |
| 3483 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3484 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3485 | { |
| 3486 | retval = FAIL; |
| 3487 | break; |
| 3488 | } |
| 3489 | did_word = TRUE; |
| 3490 | } |
| 3491 | |
| 3492 | vim_free(pc); |
| 3493 | fclose(fd); |
| 3494 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3495 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 3496 | { |
| 3497 | if (p_verbose > 2) |
| 3498 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3499 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3500 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3501 | if (p_verbose > 2) |
| 3502 | verbose_leave(); |
| 3503 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3504 | return retval; |
| 3505 | } |
| 3506 | |
| 3507 | /* |
| 3508 | * Get part of an sblock_T, "len" bytes long. |
| 3509 | * This avoids calling free() for every little struct we use. |
| 3510 | * The memory is cleared to all zeros. |
| 3511 | * Returns NULL when out of memory. |
| 3512 | */ |
| 3513 | static void * |
| 3514 | getroom(blp, len) |
| 3515 | sblock_T **blp; |
| 3516 | size_t len; /* length needed */ |
| 3517 | { |
| 3518 | char_u *p; |
| 3519 | sblock_T *bl = *blp; |
| 3520 | |
| 3521 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 3522 | { |
| 3523 | /* Allocate a block of memory. This is not freed until much later. */ |
| 3524 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 3525 | if (bl == NULL) |
| 3526 | return NULL; |
| 3527 | bl->sb_next = *blp; |
| 3528 | *blp = bl; |
| 3529 | bl->sb_used = 0; |
| 3530 | } |
| 3531 | |
| 3532 | p = bl->sb_data + bl->sb_used; |
| 3533 | bl->sb_used += len; |
| 3534 | |
| 3535 | return p; |
| 3536 | } |
| 3537 | |
| 3538 | /* |
| 3539 | * Make a copy of a string into memory allocated with getroom(). |
| 3540 | */ |
| 3541 | static char_u * |
| 3542 | getroom_save(blp, s) |
| 3543 | sblock_T **blp; |
| 3544 | char_u *s; |
| 3545 | { |
| 3546 | char_u *sc; |
| 3547 | |
| 3548 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 3549 | if (sc != NULL) |
| 3550 | STRCPY(sc, s); |
| 3551 | return sc; |
| 3552 | } |
| 3553 | |
| 3554 | |
| 3555 | /* |
| 3556 | * Free the list of allocated sblock_T. |
| 3557 | */ |
| 3558 | static void |
| 3559 | free_blocks(bl) |
| 3560 | sblock_T *bl; |
| 3561 | { |
| 3562 | sblock_T *next; |
| 3563 | |
| 3564 | while (bl != NULL) |
| 3565 | { |
| 3566 | next = bl->sb_next; |
| 3567 | vim_free(bl); |
| 3568 | bl = next; |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | /* |
| 3573 | * Allocate the root of a word tree. |
| 3574 | */ |
| 3575 | static wordnode_T * |
| 3576 | wordtree_alloc(blp) |
| 3577 | sblock_T **blp; |
| 3578 | { |
| 3579 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3580 | } |
| 3581 | |
| 3582 | /* |
| 3583 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3584 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 3585 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3586 | * 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] | 3587 | * When "pfxlist" is not NULL store the word for each prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3588 | */ |
| 3589 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3590 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3591 | char_u *word; |
| 3592 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3593 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3594 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3595 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3596 | { |
| 3597 | int len = STRLEN(word); |
| 3598 | int ct = captype(word, word + len); |
| 3599 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3600 | int res = OK; |
| 3601 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3602 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3603 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3604 | for (p = pfxlist; res == OK; ++p) |
| 3605 | { |
| 3606 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 3607 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 3608 | if (p == NULL || *p == NUL) |
| 3609 | break; |
| 3610 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3611 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3612 | |
| 3613 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3614 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3615 | for (p = pfxlist; res == OK; ++p) |
| 3616 | { |
| 3617 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 3618 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 3619 | if (p == NULL || *p == NUL) |
| 3620 | break; |
| 3621 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3622 | ++spin->si_keepwcount; |
| 3623 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3624 | return res; |
| 3625 | } |
| 3626 | |
| 3627 | /* |
| 3628 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3629 | * When "flags" is -1 we are adding to the prefix tree where flags don't |
| 3630 | * matter and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3631 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3632 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3633 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3634 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3635 | char_u *word; |
| 3636 | wordnode_T *root; |
| 3637 | int flags; |
| 3638 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3639 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3640 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3641 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3642 | wordnode_T *node = root; |
| 3643 | wordnode_T *np; |
| 3644 | wordnode_T **prev = NULL; |
| 3645 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3646 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3647 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 3648 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3649 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3650 | /* Look for the sibling that has the same character. They are sorted |
| 3651 | * 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] | 3652 | * higher byte value. For zero bytes (end of word) the sorting is |
| 3653 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3654 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3655 | while (node != NULL |
| 3656 | && (node->wn_byte < word[i] |
| 3657 | || (node->wn_byte == NUL |
| 3658 | && (flags < 0 |
| 3659 | ? node->wn_prefixID < prefixID |
| 3660 | : node->wn_flags < (flags & 0xff) |
| 3661 | || (node->wn_flags == (flags & 0xff) |
| 3662 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3663 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3664 | prev = &node->wn_sibling; |
| 3665 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3666 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3667 | if (node == NULL |
| 3668 | || node->wn_byte != word[i] |
| 3669 | || (word[i] == NUL |
| 3670 | && (flags < 0 |
| 3671 | || node->wn_flags != (flags & 0xff) |
| 3672 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3673 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3674 | /* Allocate a new node. */ |
| 3675 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3676 | if (np == NULL) |
| 3677 | return FAIL; |
| 3678 | np->wn_byte = word[i]; |
| 3679 | *prev = np; |
| 3680 | np->wn_sibling = node; |
| 3681 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3682 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3683 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3684 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3685 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3686 | node->wn_flags = flags; |
| 3687 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3688 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3689 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3690 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3691 | prev = &node->wn_child; |
| 3692 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3699 | * Compress a tree: find tails that are identical and can be shared. |
| 3700 | */ |
| 3701 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3702 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3703 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3704 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3705 | { |
| 3706 | hashtab_T ht; |
| 3707 | int n; |
| 3708 | int tot = 0; |
| 3709 | |
| 3710 | if (root != NULL) |
| 3711 | { |
| 3712 | hash_init(&ht); |
| 3713 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3714 | if (spin->si_verbose || p_verbose > 2) |
| 3715 | { |
| 3716 | if (!spin->si_verbose) |
| 3717 | verbose_enter(); |
| 3718 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3719 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3720 | if (p_verbose > 2) |
| 3721 | verbose_leave(); |
| 3722 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3723 | hash_clear(&ht); |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | /* |
| 3728 | * Compress a node, its siblings and its children, depth first. |
| 3729 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3730 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3731 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3732 | node_compress(node, ht, tot) |
| 3733 | wordnode_T *node; |
| 3734 | hashtab_T *ht; |
| 3735 | int *tot; /* total count of nodes before compressing, |
| 3736 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3737 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3738 | wordnode_T *np; |
| 3739 | wordnode_T *tp; |
| 3740 | wordnode_T *child; |
| 3741 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3742 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3743 | int len = 0; |
| 3744 | unsigned nr, n; |
| 3745 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3746 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3747 | /* |
| 3748 | * Go through the list of siblings. Compress each child and then try |
| 3749 | * finding an identical child to replace it. |
| 3750 | * Note that with "child" we mean not just the node that is pointed to, |
| 3751 | * but the whole list of siblings, of which the node is the first. |
| 3752 | */ |
| 3753 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3754 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3755 | ++len; |
| 3756 | if ((child = np->wn_child) != NULL) |
| 3757 | { |
| 3758 | /* Compress the child. This fills wn_hashkey. */ |
| 3759 | compressed += node_compress(child, ht, tot); |
| 3760 | |
| 3761 | /* Try to find an identical child. */ |
| 3762 | hash = hash_hash(child->wn_hashkey); |
| 3763 | hi = hash_lookup(ht, child->wn_hashkey, hash); |
| 3764 | tp = NULL; |
| 3765 | if (!HASHITEM_EMPTY(hi)) |
| 3766 | { |
| 3767 | /* There are children with an identical hash value. Now check |
| 3768 | * if there is one that is really identical. */ |
| 3769 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next) |
| 3770 | if (node_equal(child, tp)) |
| 3771 | { |
| 3772 | /* Found one! Now use that child in place of the |
| 3773 | * current one. This means the current child is |
| 3774 | * dropped from the tree. */ |
| 3775 | np->wn_child = tp; |
| 3776 | ++compressed; |
| 3777 | break; |
| 3778 | } |
| 3779 | if (tp == NULL) |
| 3780 | { |
| 3781 | /* No other child with this hash value equals the child of |
| 3782 | * the node, add it to the linked list after the first |
| 3783 | * item. */ |
| 3784 | tp = HI2WN(hi); |
| 3785 | child->wn_next = tp->wn_next; |
| 3786 | tp->wn_next = child; |
| 3787 | } |
| 3788 | } |
| 3789 | else |
| 3790 | /* No other child has this hash value, add it to the |
| 3791 | * hashtable. */ |
| 3792 | hash_add_item(ht, hi, child->wn_hashkey, hash); |
| 3793 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3794 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3795 | *tot += len; |
| 3796 | |
| 3797 | /* |
| 3798 | * Make a hash key for the node and its siblings, so that we can quickly |
| 3799 | * find a lookalike node. This must be done after compressing the sibling |
| 3800 | * list, otherwise the hash key would become invalid by the compression. |
| 3801 | */ |
| 3802 | node->wn_hashkey[0] = len; |
| 3803 | nr = 0; |
| 3804 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3805 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3806 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3807 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 3808 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3809 | else |
| 3810 | /* byte node: use the byte value and the child pointer */ |
| 3811 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 3812 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3813 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3814 | |
| 3815 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 3816 | n = nr & 0xff; |
| 3817 | node->wn_hashkey[1] = n == 0 ? 1 : n; |
| 3818 | n = (nr >> 8) & 0xff; |
| 3819 | node->wn_hashkey[2] = n == 0 ? 1 : n; |
| 3820 | n = (nr >> 16) & 0xff; |
| 3821 | node->wn_hashkey[3] = n == 0 ? 1 : n; |
| 3822 | n = (nr >> 24) & 0xff; |
| 3823 | node->wn_hashkey[4] = n == 0 ? 1 : n; |
| 3824 | node->wn_hashkey[5] = NUL; |
| 3825 | |
| 3826 | return compressed; |
| 3827 | } |
| 3828 | |
| 3829 | /* |
| 3830 | * Return TRUE when two nodes have identical siblings and children. |
| 3831 | */ |
| 3832 | static int |
| 3833 | node_equal(n1, n2) |
| 3834 | wordnode_T *n1; |
| 3835 | wordnode_T *n2; |
| 3836 | { |
| 3837 | wordnode_T *p1; |
| 3838 | wordnode_T *p2; |
| 3839 | |
| 3840 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 3841 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 3842 | if (p1->wn_byte != p2->wn_byte |
| 3843 | || (p1->wn_byte == NUL |
| 3844 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3845 | || p1->wn_region != p2->wn_region |
| 3846 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3847 | : (p1->wn_child != p2->wn_child))) |
| 3848 | break; |
| 3849 | |
| 3850 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | /* |
| 3854 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 3855 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3856 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3857 | put_bytes(fd, nr, len) |
| 3858 | FILE *fd; |
| 3859 | long_u nr; |
| 3860 | int len; |
| 3861 | { |
| 3862 | int i; |
| 3863 | |
| 3864 | for (i = len - 1; i >= 0; --i) |
| 3865 | putc((int)(nr >> (i * 8)), fd); |
| 3866 | } |
| 3867 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3868 | static int |
| 3869 | #ifdef __BORLANDC__ |
| 3870 | _RTLENTRYF |
| 3871 | #endif |
| 3872 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 3873 | |
| 3874 | /* |
| 3875 | * Function given to qsort() to sort the REP items on "from" string. |
| 3876 | */ |
| 3877 | static int |
| 3878 | #ifdef __BORLANDC__ |
| 3879 | _RTLENTRYF |
| 3880 | #endif |
| 3881 | rep_compare(s1, s2) |
| 3882 | const void *s1; |
| 3883 | const void *s2; |
| 3884 | { |
| 3885 | fromto_T *p1 = (fromto_T *)s1; |
| 3886 | fromto_T *p2 = (fromto_T *)s2; |
| 3887 | |
| 3888 | return STRCMP(p1->ft_from, p2->ft_from); |
| 3889 | } |
| 3890 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3891 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3892 | * Write the Vim spell file "fname". |
| 3893 | */ |
| 3894 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3895 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3896 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3897 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3898 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3899 | FILE *fd; |
| 3900 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3901 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3902 | wordnode_T *tree; |
| 3903 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3904 | int i; |
| 3905 | int l; |
| 3906 | garray_T *gap; |
| 3907 | fromto_T *ftp; |
| 3908 | char_u *p; |
| 3909 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3910 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3911 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3912 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3913 | { |
| 3914 | EMSG2(_(e_notopen), fname); |
| 3915 | return; |
| 3916 | } |
| 3917 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3918 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3919 | * <charflagslen> <charflags> |
| 3920 | * <fcharslen> <fchars> |
| 3921 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3922 | |
| 3923 | /* <fileID> */ |
| 3924 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 3925 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3926 | |
| 3927 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3928 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3929 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3930 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 3931 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 3932 | (size_t)1, fd); |
| 3933 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3934 | } |
| 3935 | else |
| 3936 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3937 | putc(0, fd); |
| 3938 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3941 | /* |
| 3942 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3943 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 3944 | * 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] | 3945 | * 'encoding'. |
| 3946 | * Also skip this for an .add.spl file, the main spell file must contain |
| 3947 | * the table (avoids that it conflicts). File is shorter too. |
| 3948 | */ |
| 3949 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3950 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3951 | putc(0, fd); |
| 3952 | putc(0, fd); |
| 3953 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3954 | } |
| 3955 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3956 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3957 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3958 | /* Write the prefix conditions. */ |
| 3959 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 3960 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3961 | /* Sort the REP items. */ |
| 3962 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 3963 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3964 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3965 | /* <SUGGEST> : <repcount> <rep> ... |
| 3966 | * <salflags> <salcount> <sal> ... |
| 3967 | * <maplen> <mapstr> */ |
| 3968 | for (round = 1; round <= 2; ++round) |
| 3969 | { |
| 3970 | if (round == 1) |
| 3971 | gap = &spin->si_rep; |
| 3972 | else |
| 3973 | { |
| 3974 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3975 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3976 | i = 0; |
| 3977 | if (spin->si_followup) |
| 3978 | i |= SAL_F0LLOWUP; |
| 3979 | if (spin->si_collapse) |
| 3980 | i |= SAL_COLLAPSE; |
| 3981 | if (spin->si_rem_accents) |
| 3982 | i |= SAL_REM_ACCENTS; |
| 3983 | putc(i, fd); /* <salflags> */ |
| 3984 | } |
| 3985 | |
| 3986 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 3987 | for (i = 0; i < gap->ga_len; ++i) |
| 3988 | { |
| 3989 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 3990 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 3991 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 3992 | for (rr = 1; rr <= 2; ++rr) |
| 3993 | { |
| 3994 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 3995 | l = STRLEN(p); |
| 3996 | putc(l, fd); |
| 3997 | fwrite(p, l, (size_t)1, fd); |
| 3998 | } |
| 3999 | } |
| 4000 | } |
| 4001 | |
| 4002 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 4003 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 4004 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 4005 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4006 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4007 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4008 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4009 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4010 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4011 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4012 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4013 | if (round == 1) |
| 4014 | tree = spin->si_foldroot; |
| 4015 | else if (round == 2) |
| 4016 | tree = spin->si_keeproot; |
| 4017 | else |
| 4018 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4019 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4020 | /* Count the number of nodes. Needed to be able to allocate the |
| 4021 | * memory when reading the nodes. Also fills in the index for shared |
| 4022 | * nodes. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4023 | nodecount = put_tree(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4024 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4025 | /* number of nodes in 4 bytes */ |
| 4026 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4027 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4028 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4029 | /* Write the nodes. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4030 | (void)put_tree(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4033 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4037 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4038 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4039 | * This first writes the list of possible bytes (siblings). Then for each |
| 4040 | * byte recursively write the children. |
| 4041 | * |
| 4042 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 4043 | * are made about the indexes (so that we don't have to write them in the |
| 4044 | * file). |
| 4045 | * |
| 4046 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4047 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4048 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4049 | put_tree(fd, node, index, regionmask, prefixtree) |
| 4050 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4051 | wordnode_T *node; |
| 4052 | int index; |
| 4053 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4054 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4055 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4056 | int newindex = index; |
| 4057 | int siblingcount = 0; |
| 4058 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4059 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4060 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4061 | /* If "node" is zero the tree is empty. */ |
| 4062 | if (node == NULL) |
| 4063 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4064 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4065 | /* Store the index where this node is written. */ |
| 4066 | node->wn_index = index; |
| 4067 | |
| 4068 | /* Count the number of siblings. */ |
| 4069 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4070 | ++siblingcount; |
| 4071 | |
| 4072 | /* Write the sibling count. */ |
| 4073 | if (fd != NULL) |
| 4074 | putc(siblingcount, fd); /* <siblingcount> */ |
| 4075 | |
| 4076 | /* Write each sibling byte and optionally extra info. */ |
| 4077 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4078 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4079 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4080 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4081 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4082 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4083 | /* For a NUL byte (end of word) write the flags etc. */ |
| 4084 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4085 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4086 | /* In PREFIXTREE write the required prefixID and the |
| 4087 | * associated condition nr (stored in wn_region). */ |
| 4088 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4089 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4090 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4091 | } |
| 4092 | else |
| 4093 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4094 | /* For word trees we write the flag/region items. */ |
| 4095 | flags = np->wn_flags; |
| 4096 | if (regionmask != 0 && np->wn_region != regionmask) |
| 4097 | flags |= WF_REGION; |
| 4098 | if (np->wn_prefixID != 0) |
| 4099 | flags |= WF_PFX; |
| 4100 | if (flags == 0) |
| 4101 | { |
| 4102 | /* word without flags or region */ |
| 4103 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 4104 | } |
| 4105 | else |
| 4106 | { |
| 4107 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4108 | putc(flags, fd); /* <flags> */ |
| 4109 | if (flags & WF_REGION) |
| 4110 | putc(np->wn_region, fd); /* <region> */ |
| 4111 | if (flags & WF_PFX) |
| 4112 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4113 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4116 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4117 | else |
| 4118 | { |
| 4119 | if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node) |
| 4120 | { |
| 4121 | /* The child is written elsewhere, write the reference. */ |
| 4122 | if (fd != NULL) |
| 4123 | { |
| 4124 | putc(BY_INDEX, fd); /* <byte> */ |
| 4125 | /* <nodeidx> */ |
| 4126 | put_bytes(fd, (long_u)np->wn_child->wn_index, 3); |
| 4127 | } |
| 4128 | } |
| 4129 | else if (np->wn_child->wn_wnode == NULL) |
| 4130 | /* We will write the child below and give it an index. */ |
| 4131 | np->wn_child->wn_wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4132 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4133 | if (fd != NULL) |
| 4134 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 4135 | { |
| 4136 | EMSG(_(e_write)); |
| 4137 | return 0; |
| 4138 | } |
| 4139 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4140 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4141 | |
| 4142 | /* Space used in the array when reading: one for each sibling and one for |
| 4143 | * the count. */ |
| 4144 | newindex += siblingcount + 1; |
| 4145 | |
| 4146 | /* Recursively dump the children of each sibling. */ |
| 4147 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4148 | if (np->wn_byte != 0 && np->wn_child->wn_wnode == node) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4149 | newindex = put_tree(fd, np->wn_child, newindex, regionmask, |
| 4150 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4151 | |
| 4152 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | |
| 4156 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4157 | * ":mkspell [-ascii] outfile infile ..." |
| 4158 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4159 | */ |
| 4160 | void |
| 4161 | ex_mkspell(eap) |
| 4162 | exarg_T *eap; |
| 4163 | { |
| 4164 | int fcount; |
| 4165 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4166 | char_u *arg = eap->arg; |
| 4167 | int ascii = FALSE; |
| 4168 | |
| 4169 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 4170 | { |
| 4171 | ascii = TRUE; |
| 4172 | arg = skipwhite(arg + 6); |
| 4173 | } |
| 4174 | |
| 4175 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 4176 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 4177 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4178 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4179 | FreeWild(fcount, fnames); |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | /* |
| 4184 | * Create a Vim spell file from one or more word lists. |
| 4185 | * "fnames[0]" is the output file name. |
| 4186 | * "fnames[fcount - 1]" is the last input file name. |
| 4187 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 4188 | * and ".spl" is appended to make the output file name. |
| 4189 | */ |
| 4190 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4191 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4192 | int fcount; |
| 4193 | char_u **fnames; |
| 4194 | int ascii; /* -ascii argument given */ |
| 4195 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4196 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4197 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4198 | char_u fname[MAXPATHL]; |
| 4199 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4200 | char_u **innames; |
| 4201 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4202 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4203 | int i; |
| 4204 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4205 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4206 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4207 | spellinfo_T spin; |
| 4208 | |
| 4209 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4210 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4211 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4212 | spin.si_followup = TRUE; |
| 4213 | spin.si_rem_accents = TRUE; |
| 4214 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 4215 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 4216 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4217 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4218 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4219 | /* default: fnames[0] is output file, following are input files */ |
| 4220 | innames = &fnames[1]; |
| 4221 | incount = fcount - 1; |
| 4222 | |
| 4223 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4224 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4225 | len = STRLEN(fnames[0]); |
| 4226 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 4227 | { |
| 4228 | /* For ":mkspell path/en.latin1.add" output file is |
| 4229 | * "path/en.latin1.add.spl". */ |
| 4230 | innames = &fnames[0]; |
| 4231 | incount = 1; |
| 4232 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 4233 | } |
| 4234 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 4235 | { |
| 4236 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4237 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4238 | } |
| 4239 | else |
| 4240 | /* Name should be language, make the file name from it. */ |
| 4241 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4242 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4243 | |
| 4244 | /* Check for .ascii.spl. */ |
| 4245 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 4246 | spin.si_ascii = TRUE; |
| 4247 | |
| 4248 | /* Check for .add.spl. */ |
| 4249 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 4250 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4253 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4254 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4255 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4256 | EMSG(_("E754: Only up to 8 regions supported")); |
| 4257 | else |
| 4258 | { |
| 4259 | /* Check for overwriting before doing things that may take a lot of |
| 4260 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4261 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4262 | { |
| 4263 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4264 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4265 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4266 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4267 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4268 | EMSG2(_(e_isadir2), wfname); |
| 4269 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | /* |
| 4273 | * Init the aff and dic pointers. |
| 4274 | * Get the region names if there are more than 2 arguments. |
| 4275 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4276 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4277 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4278 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4279 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4280 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4281 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4282 | len = STRLEN(innames[i]); |
| 4283 | if (STRLEN(gettail(innames[i])) < 5 |
| 4284 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4285 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4286 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 4287 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4288 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4289 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 4290 | spin.si_region_name[i * 2 + 1] = |
| 4291 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4292 | } |
| 4293 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4294 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4295 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4296 | if (!spin.si_add) |
| 4297 | /* Clear the char type tables, don't want to use any of the |
| 4298 | * currently used spell properties. */ |
| 4299 | init_spell_chartab(); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4300 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4301 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 4302 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4303 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 4304 | if (spin.si_foldroot == NULL |
| 4305 | || spin.si_keeproot == NULL |
| 4306 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4307 | { |
| 4308 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4309 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4312 | /* |
| 4313 | * Read all the .aff and .dic files. |
| 4314 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4315 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4316 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4317 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4318 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4319 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4320 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4321 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4322 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4323 | if (mch_stat((char *)fname, &st) >= 0) |
| 4324 | { |
| 4325 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 4326 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4327 | afile[i] = spell_read_aff(fname, &spin); |
| 4328 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4329 | error = TRUE; |
| 4330 | else |
| 4331 | { |
| 4332 | /* Read the .dic file and store the words in the trees. */ |
| 4333 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4334 | innames[i]); |
| 4335 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4336 | error = TRUE; |
| 4337 | } |
| 4338 | } |
| 4339 | else |
| 4340 | { |
| 4341 | /* No .aff file, try reading the file as a word list. Store |
| 4342 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4343 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4344 | error = TRUE; |
| 4345 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4346 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4347 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4348 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4349 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4350 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4353 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4354 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4355 | /* |
| 4356 | * Remove the dummy NUL from the start of the tree root. |
| 4357 | */ |
| 4358 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 4359 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4360 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4361 | |
| 4362 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4363 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4364 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4365 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4366 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4367 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4368 | verbose_enter(); |
| 4369 | MSG(_("Compressing word tree...")); |
| 4370 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4371 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4372 | verbose_leave(); |
| 4373 | } |
| 4374 | wordtree_compress(spin.si_foldroot, &spin); |
| 4375 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4376 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4379 | if (!error) |
| 4380 | { |
| 4381 | /* |
| 4382 | * Write the info in the spell file. |
| 4383 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4384 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4385 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4386 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4387 | verbose_enter(); |
| 4388 | smsg((char_u *)_("Writing spell file %s..."), wfname); |
| 4389 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4390 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4391 | verbose_leave(); |
| 4392 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4393 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4394 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4395 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4396 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4397 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4398 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4399 | verbose_enter(); |
| 4400 | MSG(_("Done!")); |
| 4401 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4402 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4403 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4404 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4405 | verbose_leave(); |
| 4406 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4407 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4408 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4409 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4413 | ga_clear(&spin.si_rep); |
| 4414 | ga_clear(&spin.si_sal); |
| 4415 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4416 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4417 | |
| 4418 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4419 | for (i = 0; i < incount; ++i) |
| 4420 | if (afile[i] != NULL) |
| 4421 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4422 | |
| 4423 | /* Free all the bits and pieces at once. */ |
| 4424 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4425 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4428 | |
| 4429 | /* |
| 4430 | * ":spellgood {word}" |
| 4431 | * ":spellwrong {word}" |
| 4432 | */ |
| 4433 | void |
| 4434 | ex_spell(eap) |
| 4435 | exarg_T *eap; |
| 4436 | { |
| 4437 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong); |
| 4438 | } |
| 4439 | |
| 4440 | /* |
| 4441 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 4442 | */ |
| 4443 | void |
| 4444 | spell_add_word(word, len, bad) |
| 4445 | char_u *word; |
| 4446 | int len; |
| 4447 | int bad; |
| 4448 | { |
| 4449 | FILE *fd; |
| 4450 | buf_T *buf; |
| 4451 | |
| 4452 | if (*curbuf->b_p_spf == NUL) |
| 4453 | init_spellfile(); |
| 4454 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4455 | EMSG(_("E764: 'spellfile' is not set")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4456 | else |
| 4457 | { |
| 4458 | /* Check that the user isn't editing the .add file somewhere. */ |
| 4459 | buf = buflist_findname_exp(curbuf->b_p_spf); |
| 4460 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 4461 | buf = NULL; |
| 4462 | if (buf != NULL && bufIsChanged(buf)) |
| 4463 | EMSG(_(e_bufloaded)); |
| 4464 | else |
| 4465 | { |
| 4466 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
| 4467 | if (fd == NULL) |
| 4468 | EMSG2(_(e_notopen), curbuf->b_p_spf); |
| 4469 | else |
| 4470 | { |
| 4471 | if (bad) |
| 4472 | fprintf(fd, "/!%.*s\n", len, word); |
| 4473 | else |
| 4474 | fprintf(fd, "%.*s\n", len, word); |
| 4475 | fclose(fd); |
| 4476 | |
| 4477 | /* Update the .add.spl file. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4478 | mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4479 | |
| 4480 | /* If the .add file is edited somewhere, reload it. */ |
| 4481 | if (buf != NULL) |
| 4482 | buf_reload(buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4483 | |
| 4484 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4485 | } |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | /* |
| 4491 | * Initialize 'spellfile' for the current buffer. |
| 4492 | */ |
| 4493 | static void |
| 4494 | init_spellfile() |
| 4495 | { |
| 4496 | char_u buf[MAXPATHL]; |
| 4497 | int l; |
| 4498 | slang_T *sl; |
| 4499 | char_u *rtp; |
| 4500 | |
| 4501 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 4502 | { |
| 4503 | /* Loop over all entries in 'runtimepath'. */ |
| 4504 | rtp = p_rtp; |
| 4505 | while (*rtp != NUL) |
| 4506 | { |
| 4507 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 4508 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 4509 | if (filewritable(buf) == 2) |
| 4510 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4511 | /* Use the first language name from 'spelllang' and the |
| 4512 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4513 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 4514 | l = STRLEN(buf); |
| 4515 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4516 | "/spell/%.*s.%s.add", |
| 4517 | 2, curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4518 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 4519 | ? (char_u *)"ascii" : spell_enc()); |
| 4520 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 4521 | break; |
| 4522 | } |
| 4523 | } |
| 4524 | } |
| 4525 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4526 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4527 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4528 | /* |
| 4529 | * Init the chartab used for spelling for ASCII. |
| 4530 | * EBCDIC is not supported! |
| 4531 | */ |
| 4532 | static void |
| 4533 | clear_spell_chartab(sp) |
| 4534 | spelltab_T *sp; |
| 4535 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4536 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4537 | |
| 4538 | /* Init everything to FALSE. */ |
| 4539 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 4540 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 4541 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4542 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4543 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4544 | sp->st_upper[i] = i; |
| 4545 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4546 | |
| 4547 | /* We include digits. A word shouldn't start with a digit, but handling |
| 4548 | * that is done separately. */ |
| 4549 | for (i = '0'; i <= '9'; ++i) |
| 4550 | sp->st_isw[i] = TRUE; |
| 4551 | for (i = 'A'; i <= 'Z'; ++i) |
| 4552 | { |
| 4553 | sp->st_isw[i] = TRUE; |
| 4554 | sp->st_isu[i] = TRUE; |
| 4555 | sp->st_fold[i] = i + 0x20; |
| 4556 | } |
| 4557 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4558 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4559 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4560 | sp->st_upper[i] = i - 0x20; |
| 4561 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* |
| 4565 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 4566 | * Called once while starting up and when 'encoding' changes. |
| 4567 | * The default is to use isalpha(), but the spell file should define the word |
| 4568 | * characters to make it possible that 'encoding' differs from the current |
| 4569 | * locale. |
| 4570 | */ |
| 4571 | void |
| 4572 | init_spell_chartab() |
| 4573 | { |
| 4574 | int i; |
| 4575 | |
| 4576 | did_set_spelltab = FALSE; |
| 4577 | clear_spell_chartab(&spelltab); |
| 4578 | |
| 4579 | #ifdef FEAT_MBYTE |
| 4580 | if (enc_dbcs) |
| 4581 | { |
| 4582 | /* DBCS: assume double-wide characters are word characters. */ |
| 4583 | for (i = 128; i <= 255; ++i) |
| 4584 | if (MB_BYTE2LEN(i) == 2) |
| 4585 | spelltab.st_isw[i] = TRUE; |
| 4586 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4587 | else if (enc_utf8) |
| 4588 | { |
| 4589 | for (i = 128; i < 256; ++i) |
| 4590 | { |
| 4591 | spelltab.st_isu[i] = utf_isupper(i); |
| 4592 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 4593 | spelltab.st_fold[i] = utf_fold(i); |
| 4594 | spelltab.st_upper[i] = utf_toupper(i); |
| 4595 | } |
| 4596 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4597 | else |
| 4598 | #endif |
| 4599 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4600 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4601 | for (i = 128; i < 256; ++i) |
| 4602 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4603 | if (MB_ISUPPER(i)) |
| 4604 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4605 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4606 | spelltab.st_isu[i] = TRUE; |
| 4607 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 4608 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4609 | else if (MB_ISLOWER(i)) |
| 4610 | { |
| 4611 | spelltab.st_isw[i] = TRUE; |
| 4612 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 4613 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4618 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 4619 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 4620 | |
| 4621 | /* |
| 4622 | * Set the spell character tables from strings in the affix file. |
| 4623 | */ |
| 4624 | static int |
| 4625 | set_spell_chartab(fol, low, upp) |
| 4626 | char_u *fol; |
| 4627 | char_u *low; |
| 4628 | char_u *upp; |
| 4629 | { |
| 4630 | /* We build the new tables here first, so that we can compare with the |
| 4631 | * previous one. */ |
| 4632 | spelltab_T new_st; |
| 4633 | char_u *pf = fol, *pl = low, *pu = upp; |
| 4634 | int f, l, u; |
| 4635 | |
| 4636 | clear_spell_chartab(&new_st); |
| 4637 | |
| 4638 | while (*pf != NUL) |
| 4639 | { |
| 4640 | if (*pl == NUL || *pu == NUL) |
| 4641 | { |
| 4642 | EMSG(_(e_affform)); |
| 4643 | return FAIL; |
| 4644 | } |
| 4645 | #ifdef FEAT_MBYTE |
| 4646 | f = mb_ptr2char_adv(&pf); |
| 4647 | l = mb_ptr2char_adv(&pl); |
| 4648 | u = mb_ptr2char_adv(&pu); |
| 4649 | #else |
| 4650 | f = *pf++; |
| 4651 | l = *pl++; |
| 4652 | u = *pu++; |
| 4653 | #endif |
| 4654 | /* Every character that appears is a word character. */ |
| 4655 | if (f < 256) |
| 4656 | new_st.st_isw[f] = TRUE; |
| 4657 | if (l < 256) |
| 4658 | new_st.st_isw[l] = TRUE; |
| 4659 | if (u < 256) |
| 4660 | new_st.st_isw[u] = TRUE; |
| 4661 | |
| 4662 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 4663 | * case-folding */ |
| 4664 | if (l < 256 && l != f) |
| 4665 | { |
| 4666 | if (f >= 256) |
| 4667 | { |
| 4668 | EMSG(_(e_affrange)); |
| 4669 | return FAIL; |
| 4670 | } |
| 4671 | new_st.st_fold[l] = f; |
| 4672 | } |
| 4673 | |
| 4674 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4675 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 4676 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4677 | if (u < 256 && u != f) |
| 4678 | { |
| 4679 | if (f >= 256) |
| 4680 | { |
| 4681 | EMSG(_(e_affrange)); |
| 4682 | return FAIL; |
| 4683 | } |
| 4684 | new_st.st_fold[u] = f; |
| 4685 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4686 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | if (*pl != NUL || *pu != NUL) |
| 4691 | { |
| 4692 | EMSG(_(e_affform)); |
| 4693 | return FAIL; |
| 4694 | } |
| 4695 | |
| 4696 | return set_spell_finish(&new_st); |
| 4697 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4698 | |
| 4699 | /* |
| 4700 | * Set the spell character tables from strings in the .spl file. |
| 4701 | */ |
| 4702 | static int |
| 4703 | set_spell_charflags(flags, cnt, upp) |
| 4704 | char_u *flags; |
| 4705 | int cnt; |
| 4706 | char_u *upp; |
| 4707 | { |
| 4708 | /* We build the new tables here first, so that we can compare with the |
| 4709 | * previous one. */ |
| 4710 | spelltab_T new_st; |
| 4711 | int i; |
| 4712 | char_u *p = upp; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4713 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4714 | |
| 4715 | clear_spell_chartab(&new_st); |
| 4716 | |
| 4717 | for (i = 0; i < cnt; ++i) |
| 4718 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4719 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 4720 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4721 | |
| 4722 | if (*p == NUL) |
| 4723 | return FAIL; |
| 4724 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4725 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4726 | #else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4727 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4728 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4729 | new_st.st_fold[i + 128] = c; |
| 4730 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 4731 | new_st.st_upper[c] = i + 128; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | return set_spell_finish(&new_st); |
| 4735 | } |
| 4736 | |
| 4737 | static int |
| 4738 | set_spell_finish(new_st) |
| 4739 | spelltab_T *new_st; |
| 4740 | { |
| 4741 | int i; |
| 4742 | |
| 4743 | if (did_set_spelltab) |
| 4744 | { |
| 4745 | /* check that it's the same table */ |
| 4746 | for (i = 0; i < 256; ++i) |
| 4747 | { |
| 4748 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 4749 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4750 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 4751 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4752 | { |
| 4753 | EMSG(_("E763: Word characters differ between spell files")); |
| 4754 | return FAIL; |
| 4755 | } |
| 4756 | } |
| 4757 | } |
| 4758 | else |
| 4759 | { |
| 4760 | /* copy the new spelltab into the one being used */ |
| 4761 | spelltab = *new_st; |
| 4762 | did_set_spelltab = TRUE; |
| 4763 | } |
| 4764 | |
| 4765 | return OK; |
| 4766 | } |
| 4767 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4768 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4769 | * Write the table with prefix conditions to the .spl file. |
| 4770 | */ |
| 4771 | static void |
| 4772 | write_spell_prefcond(fd, gap) |
| 4773 | FILE *fd; |
| 4774 | garray_T *gap; |
| 4775 | { |
| 4776 | int i; |
| 4777 | char_u *p; |
| 4778 | int len; |
| 4779 | |
| 4780 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 4781 | |
| 4782 | for (i = 0; i < gap->ga_len; ++i) |
| 4783 | { |
| 4784 | /* <prefcond> : <condlen> <condstr> */ |
| 4785 | p = ((char_u **)gap->ga_data)[i]; |
| 4786 | if (p == NULL) |
| 4787 | fputc(0, fd); |
| 4788 | else |
| 4789 | { |
| 4790 | len = STRLEN(p); |
| 4791 | fputc(len, fd); |
| 4792 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 4793 | } |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4798 | * Write the current tables into the .spl file. |
| 4799 | * This makes sure the same characters are recognized as word characters when |
| 4800 | * generating an when using a spell file. |
| 4801 | */ |
| 4802 | static void |
| 4803 | write_spell_chartab(fd) |
| 4804 | FILE *fd; |
| 4805 | { |
| 4806 | char_u charbuf[256 * 4]; |
| 4807 | int len = 0; |
| 4808 | int flags; |
| 4809 | int i; |
| 4810 | |
| 4811 | fputc(128, fd); /* <charflagslen> */ |
| 4812 | for (i = 128; i < 256; ++i) |
| 4813 | { |
| 4814 | flags = 0; |
| 4815 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4816 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4817 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4818 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4819 | fputc(flags, fd); /* <charflags> */ |
| 4820 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4821 | #ifdef FEAT_MBYTE |
| 4822 | if (has_mbyte) |
| 4823 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 4824 | else |
| 4825 | #endif |
| 4826 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 4830 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 4831 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4832 | |
| 4833 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4834 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 4835 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4836 | * When using a multi-byte 'encoding' the length may change! |
| 4837 | * Returns FAIL when something wrong. |
| 4838 | */ |
| 4839 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4840 | spell_casefold(str, len, buf, buflen) |
| 4841 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4842 | int len; |
| 4843 | char_u *buf; |
| 4844 | int buflen; |
| 4845 | { |
| 4846 | int i; |
| 4847 | |
| 4848 | if (len >= buflen) |
| 4849 | { |
| 4850 | buf[0] = NUL; |
| 4851 | return FAIL; /* result will not fit */ |
| 4852 | } |
| 4853 | |
| 4854 | #ifdef FEAT_MBYTE |
| 4855 | if (has_mbyte) |
| 4856 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4857 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4858 | char_u *p; |
| 4859 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4860 | |
| 4861 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4862 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4863 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4864 | if (outi + MB_MAXBYTES > buflen) |
| 4865 | { |
| 4866 | buf[outi] = NUL; |
| 4867 | return FAIL; |
| 4868 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4869 | c = mb_ptr2char_adv(&p); |
| 4870 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4871 | } |
| 4872 | buf[outi] = NUL; |
| 4873 | } |
| 4874 | else |
| 4875 | #endif |
| 4876 | { |
| 4877 | /* Be quick for non-multibyte encodings. */ |
| 4878 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4879 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4880 | buf[i] = NUL; |
| 4881 | } |
| 4882 | |
| 4883 | return OK; |
| 4884 | } |
| 4885 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4886 | /* |
| 4887 | * "z?": Find badly spelled word under or after the cursor. |
| 4888 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4889 | */ |
| 4890 | void |
| 4891 | spell_suggest() |
| 4892 | { |
| 4893 | char_u *line; |
| 4894 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4895 | char_u wcopy[MAXWLEN + 2]; |
| 4896 | char_u *p; |
| 4897 | int i; |
| 4898 | int c; |
| 4899 | suginfo_T sug; |
| 4900 | suggest_T *stp; |
| 4901 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4902 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4903 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL) |
| 4904 | { |
| 4905 | beep_flush(); |
| 4906 | return; |
| 4907 | } |
| 4908 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4909 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4910 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4911 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4912 | /* Get the list of suggestions */ |
| 4913 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4914 | |
| 4915 | if (sug.su_ga.ga_len == 0) |
| 4916 | MSG(_("Sorry, no suggestions")); |
| 4917 | else |
| 4918 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4919 | /* List the suggestions. */ |
| 4920 | msg_start(); |
| 4921 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 4922 | sug.su_badlen, sug.su_badptr); |
| 4923 | msg_puts(IObuff); |
| 4924 | msg_clr_eos(); |
| 4925 | msg_putchar('\n'); |
| 4926 | msg_scroll = TRUE; |
| 4927 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 4928 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4929 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4930 | |
| 4931 | /* The suggested word may replace only part of the bad word, add |
| 4932 | * the not replaced part. */ |
| 4933 | STRCPY(wcopy, stp->st_word); |
| 4934 | if (sug.su_badlen > stp->st_orglen) |
| 4935 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 4936 | sug.su_badptr + stp->st_orglen, |
| 4937 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4938 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4939 | { |
| 4940 | if (sps_flags & SPS_DOUBLE) |
| 4941 | vim_snprintf((char *)IObuff, IOSIZE, |
| 4942 | _("%2d \"%s\" (%s%d - %d)"), |
| 4943 | i + 1, wcopy, |
| 4944 | stp->st_salscore ? "s " : "", |
| 4945 | stp->st_score, stp->st_altscore); |
| 4946 | else |
| 4947 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\" (%d)"), |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4948 | i + 1, wcopy, stp->st_score); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4949 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4950 | else |
| 4951 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), |
| 4952 | i + 1, wcopy); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4953 | msg_puts(IObuff); |
| 4954 | lines_left = 3; /* avoid more prompt */ |
| 4955 | msg_putchar('\n'); |
| 4956 | } |
| 4957 | |
| 4958 | /* Ask for choice. */ |
| 4959 | i = prompt_for_number(); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4960 | 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] | 4961 | { |
| 4962 | /* Replace the word. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4963 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4964 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 4965 | if (p != NULL) |
| 4966 | { |
| 4967 | c = sug.su_badptr - line; |
| 4968 | mch_memmove(p, line, c); |
| 4969 | STRCPY(p + c, stp->st_word); |
| 4970 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 4971 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 4972 | curwin->w_cursor.col = c; |
| 4973 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4974 | |
| 4975 | /* For redo we use a change-word command. */ |
| 4976 | ResetRedobuff(); |
| 4977 | AppendToRedobuff((char_u *)"ciw"); |
| 4978 | AppendToRedobuff(stp->st_word); |
| 4979 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4980 | } |
| 4981 | } |
| 4982 | else |
| 4983 | curwin->w_cursor = prev_cursor; |
| 4984 | } |
| 4985 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4986 | spell_find_cleanup(&sug); |
| 4987 | } |
| 4988 | |
| 4989 | /* |
| 4990 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 4991 | * a list of allocated strings. |
| 4992 | */ |
| 4993 | void |
| 4994 | spell_suggest_list(gap, word, maxcount) |
| 4995 | garray_T *gap; |
| 4996 | char_u *word; |
| 4997 | int maxcount; /* maximum nr of suggestions */ |
| 4998 | { |
| 4999 | suginfo_T sug; |
| 5000 | int i; |
| 5001 | suggest_T *stp; |
| 5002 | char_u *wcopy; |
| 5003 | |
| 5004 | spell_find_suggest(word, &sug, maxcount); |
| 5005 | |
| 5006 | /* Make room in "gap". */ |
| 5007 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 5008 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 5009 | return; |
| 5010 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5011 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5012 | { |
| 5013 | stp = &SUG(sug.su_ga, i); |
| 5014 | |
| 5015 | /* The suggested word may replace only part of "word", add the not |
| 5016 | * replaced part. */ |
| 5017 | wcopy = alloc(STRLEN(stp->st_word) |
| 5018 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 5019 | if (wcopy == NULL) |
| 5020 | break; |
| 5021 | STRCPY(wcopy, stp->st_word); |
| 5022 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 5023 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 5024 | } |
| 5025 | |
| 5026 | spell_find_cleanup(&sug); |
| 5027 | } |
| 5028 | |
| 5029 | /* |
| 5030 | * Find spell suggestions for the word at the start of "badptr". |
| 5031 | * Return the suggestions in "su->su_ga". |
| 5032 | * The maximum number of suggestions is "maxcount". |
| 5033 | * Note: does use info for the current window. |
| 5034 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 5035 | */ |
| 5036 | static void |
| 5037 | spell_find_suggest(badptr, su, maxcount) |
| 5038 | char_u *badptr; |
| 5039 | suginfo_T *su; |
| 5040 | int maxcount; |
| 5041 | { |
| 5042 | int attr; |
| 5043 | |
| 5044 | /* |
| 5045 | * Set the info in "*su". |
| 5046 | */ |
| 5047 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 5048 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 5049 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
| 5050 | hash_init(&su->su_banned); |
| 5051 | |
| 5052 | su->su_badptr = badptr; |
| 5053 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr); |
| 5054 | su->su_maxcount = maxcount; |
| 5055 | |
| 5056 | if (su->su_badlen >= MAXWLEN) |
| 5057 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 5058 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 5059 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 5060 | su->su_fbadword, MAXWLEN); |
| 5061 | |
| 5062 | /* Ban the bad word itself. It may appear in another region. */ |
| 5063 | add_banned(su, su->su_badword); |
| 5064 | |
| 5065 | /* |
| 5066 | * 1. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 5067 | * from the .aff file and inserting a space (split the word). |
| 5068 | * |
| 5069 | * Set a maximum score to limit the combination of operations that is |
| 5070 | * tried. |
| 5071 | */ |
| 5072 | su->su_maxscore = SCORE_MAXINIT; |
| 5073 | spell_try_change(su); |
| 5074 | |
| 5075 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 5076 | if (sps_flags & SPS_DOUBLE) |
| 5077 | score_comp_sal(su); |
| 5078 | |
| 5079 | /* |
| 5080 | * 2. Try finding sound-a-like words. |
| 5081 | * |
| 5082 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 5083 | * very slow and often doesn't find new suggestions. |
| 5084 | */ |
| 5085 | if ((sps_flags & SPS_DOUBLE) |
| 5086 | || (!(sps_flags & SPS_FAST) |
| 5087 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 5088 | { |
| 5089 | /* Allow a higher score now. */ |
| 5090 | su->su_maxscore = SCORE_MAXMAX; |
| 5091 | spell_try_soundalike(su); |
| 5092 | } |
| 5093 | |
| 5094 | /* When CTRL-C was hit while searching do show the results. */ |
| 5095 | ui_breakcheck(); |
| 5096 | if (got_int) |
| 5097 | { |
| 5098 | (void)vgetc(); |
| 5099 | got_int = FALSE; |
| 5100 | } |
| 5101 | |
| 5102 | if (sps_flags & SPS_DOUBLE) |
| 5103 | { |
| 5104 | /* Combine the two list of suggestions. */ |
| 5105 | score_combine(su); |
| 5106 | } |
| 5107 | else if (su->su_ga.ga_len != 0) |
| 5108 | { |
| 5109 | if (sps_flags & SPS_BEST) |
| 5110 | /* Adjust the word score for how it sounds like. */ |
| 5111 | rescore_suggestions(su); |
| 5112 | |
| 5113 | /* Sort the suggestions and truncate at "maxcount". */ |
| 5114 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount); |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | /* |
| 5119 | * Free the info put in "*su" by spell_find_suggest(). |
| 5120 | */ |
| 5121 | static void |
| 5122 | spell_find_cleanup(su) |
| 5123 | suginfo_T *su; |
| 5124 | { |
| 5125 | int i; |
| 5126 | |
| 5127 | /* Free the suggestions. */ |
| 5128 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 5129 | vim_free(SUG(su->su_ga, i).st_word); |
| 5130 | ga_clear(&su->su_ga); |
| 5131 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 5132 | vim_free(SUG(su->su_sga, i).st_word); |
| 5133 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5134 | |
| 5135 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5136 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5140 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 5141 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 5142 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5143 | */ |
| 5144 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5145 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5146 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5147 | char_u *wcopy; |
| 5148 | int upper; /* TRUE: first letter made upper case */ |
| 5149 | { |
| 5150 | char_u *p; |
| 5151 | int c; |
| 5152 | int l; |
| 5153 | |
| 5154 | p = word; |
| 5155 | #ifdef FEAT_MBYTE |
| 5156 | if (has_mbyte) |
| 5157 | c = mb_ptr2char_adv(&p); |
| 5158 | else |
| 5159 | #endif |
| 5160 | c = *p++; |
| 5161 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5162 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5163 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5164 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5165 | #ifdef FEAT_MBYTE |
| 5166 | if (has_mbyte) |
| 5167 | l = mb_char2bytes(c, wcopy); |
| 5168 | else |
| 5169 | #endif |
| 5170 | { |
| 5171 | l = 1; |
| 5172 | wcopy[0] = c; |
| 5173 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5174 | vim_strncpy(wcopy + l, p, MAXWLEN - l); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5178 | * Make a copy of "word" with all the letters upper cased into |
| 5179 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5180 | */ |
| 5181 | static void |
| 5182 | allcap_copy(word, wcopy) |
| 5183 | char_u *word; |
| 5184 | char_u *wcopy; |
| 5185 | { |
| 5186 | char_u *s; |
| 5187 | char_u *d; |
| 5188 | int c; |
| 5189 | |
| 5190 | d = wcopy; |
| 5191 | for (s = word; *s != NUL; ) |
| 5192 | { |
| 5193 | #ifdef FEAT_MBYTE |
| 5194 | if (has_mbyte) |
| 5195 | c = mb_ptr2char_adv(&s); |
| 5196 | else |
| 5197 | #endif |
| 5198 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5199 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5200 | |
| 5201 | #ifdef FEAT_MBYTE |
| 5202 | if (has_mbyte) |
| 5203 | { |
| 5204 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 5205 | break; |
| 5206 | d += mb_char2bytes(c, d); |
| 5207 | } |
| 5208 | else |
| 5209 | #endif |
| 5210 | { |
| 5211 | if (d - wcopy >= MAXWLEN - 1) |
| 5212 | break; |
| 5213 | *d++ = c; |
| 5214 | } |
| 5215 | } |
| 5216 | *d = NUL; |
| 5217 | } |
| 5218 | |
| 5219 | /* |
| 5220 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5221 | * |
| 5222 | * This uses a state machine. At each node in the tree we try various |
| 5223 | * operations. When trying if an operation work "depth" is increased and the |
| 5224 | * stack[] is used to store info. This allows combinations, thus insert one |
| 5225 | * character, replace one and delete another. The number of changes is |
| 5226 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5227 | */ |
| 5228 | static void |
| 5229 | spell_try_change(su) |
| 5230 | suginfo_T *su; |
| 5231 | { |
| 5232 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 5233 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 5234 | trystate_T stack[MAXWLEN]; |
| 5235 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 5236 | * to for word split) */ |
| 5237 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 5238 | int splitoff = 0; /* index in tword after last split */ |
| 5239 | trystate_T *sp; |
| 5240 | int newscore; |
| 5241 | langp_T *lp; |
| 5242 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5243 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5244 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5245 | int c, c2, c3; |
| 5246 | int n = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5247 | int flags; |
| 5248 | int badflags; |
| 5249 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5250 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5251 | int len; |
| 5252 | char_u *p; |
| 5253 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5254 | int fl = 0, tl; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5255 | |
| 5256 | /* get caps flags for bad word */ |
| 5257 | badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen); |
| 5258 | |
| 5259 | /* We make a copy of the case-folded bad word, so that we can modify it |
| 5260 | * to find matches (esp. REP items). */ |
| 5261 | STRCPY(fword, su->su_fbadword); |
| 5262 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5263 | |
| 5264 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 5265 | lp->lp_slang != NULL; ++lp) |
| 5266 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5267 | /* |
| 5268 | * Go through the whole case-fold tree, try changes at each node. |
| 5269 | * "tword[]" contains the word collected from nodes in the tree. |
| 5270 | * "fword[]" the word we are trying to match with (initially the bad |
| 5271 | * word). |
| 5272 | */ |
| 5273 | byts = lp->lp_slang->sl_fbyts; |
| 5274 | idxs = lp->lp_slang->sl_fidxs; |
| 5275 | |
| 5276 | depth = 0; |
| 5277 | stack[0].ts_state = STATE_START; |
| 5278 | stack[0].ts_score = 0; |
| 5279 | stack[0].ts_curi = 1; |
| 5280 | stack[0].ts_fidx = 0; |
| 5281 | stack[0].ts_fidxtry = 0; |
| 5282 | stack[0].ts_twordlen = 0; |
| 5283 | stack[0].ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5284 | #ifdef FEAT_MBYTE |
| 5285 | stack[0].ts_tcharlen = 0; |
| 5286 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5287 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5288 | /* |
| 5289 | * Loop to find all suggestions. At each round we either: |
| 5290 | * - For the current state try one operation, advance "ts_curi", |
| 5291 | * increase "depth". |
| 5292 | * - When a state is done go to the next, set "ts_state". |
| 5293 | * - When all states are tried decrease "depth". |
| 5294 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5295 | while (depth >= 0 && !got_int) |
| 5296 | { |
| 5297 | sp = &stack[depth]; |
| 5298 | switch (sp->ts_state) |
| 5299 | { |
| 5300 | case STATE_START: |
| 5301 | /* |
| 5302 | * Start of node: Deal with NUL bytes, which means |
| 5303 | * tword[] may end here. |
| 5304 | */ |
| 5305 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 5306 | len = byts[arridx]; /* bytes in this node */ |
| 5307 | arridx += sp->ts_curi; /* index of current byte */ |
| 5308 | |
| 5309 | if (sp->ts_curi > len || (c = byts[arridx]) != 0) |
| 5310 | { |
| 5311 | /* Past bytes in node and/or past NUL bytes. */ |
| 5312 | sp->ts_state = STATE_ENDNUL; |
| 5313 | break; |
| 5314 | } |
| 5315 | |
| 5316 | /* |
| 5317 | * End of word in tree. |
| 5318 | */ |
| 5319 | ++sp->ts_curi; /* eat one NUL byte */ |
| 5320 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5321 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5322 | |
| 5323 | /* |
| 5324 | * Form the word with proper case in preword. |
| 5325 | * If there is a word from a previous split, append. |
| 5326 | */ |
| 5327 | tword[sp->ts_twordlen] = NUL; |
| 5328 | if (flags & WF_KEEPCAP) |
| 5329 | /* Must find the word in the keep-case tree. */ |
| 5330 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 5331 | preword + prewordlen); |
| 5332 | else |
| 5333 | /* Include badflags: if the badword is onecap or allcap |
| 5334 | * use that for the goodword too. */ |
| 5335 | make_case_word(tword + splitoff, |
| 5336 | preword + prewordlen, flags | badflags); |
| 5337 | |
| 5338 | /* Don't use a banned word. It may appear again as a good |
| 5339 | * word, thus remember it. */ |
| 5340 | if (flags & WF_BANNED) |
| 5341 | { |
| 5342 | add_banned(su, preword + prewordlen); |
| 5343 | break; |
| 5344 | } |
| 5345 | if (was_banned(su, preword + prewordlen)) |
| 5346 | break; |
| 5347 | |
| 5348 | newscore = 0; |
| 5349 | if ((flags & WF_REGION) |
| 5350 | && (((unsigned)flags >> 8) & lp->lp_region) == 0) |
| 5351 | newscore += SCORE_REGION; |
| 5352 | if (flags & WF_RARE) |
| 5353 | newscore += SCORE_RARE; |
| 5354 | |
| 5355 | if (!spell_valid_case(badflags, |
| 5356 | captype(preword + prewordlen, NULL))) |
| 5357 | newscore += SCORE_ICASE; |
| 5358 | |
| 5359 | if (fword[sp->ts_fidx] == 0) |
| 5360 | { |
| 5361 | /* The badword also ends: add suggestions, */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5362 | add_suggestion(su, &su->su_ga, preword, |
| 5363 | sp->ts_score + newscore, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5364 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5365 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 5366 | #ifdef FEAT_MBYTE |
| 5367 | /* Don't split halfway a character. */ |
| 5368 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 5369 | #endif |
| 5370 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5371 | { |
| 5372 | /* The word in the tree ends but the badword |
| 5373 | * continues: try inserting a space and check that a valid |
| 5374 | * words starts at fword[sp->ts_fidx]. */ |
| 5375 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 5376 | { |
| 5377 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 5378 | sp->ts_save_prewordlen = prewordlen; |
| 5379 | sp->ts_save_badflags = badflags; |
| 5380 | sp->ts_save_splitoff = splitoff; |
| 5381 | |
| 5382 | /* Append a space to preword. */ |
| 5383 | STRCAT(preword, " "); |
| 5384 | prewordlen = STRLEN(preword); |
| 5385 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5386 | #ifdef FEAT_MBYTE |
| 5387 | if (has_mbyte) |
| 5388 | { |
| 5389 | int i = 0; |
| 5390 | |
| 5391 | /* Case-folding may change the number of bytes: |
| 5392 | * Count nr of chars in fword[sp->ts_fidx] and |
| 5393 | * advance that many chars in su->su_badptr. */ |
| 5394 | for (p = fword; p < fword + sp->ts_fidx; |
| 5395 | mb_ptr_adv(p)) |
| 5396 | ++i; |
| 5397 | for (p = su->su_badptr; i > 0; mb_ptr_adv(p)) |
| 5398 | --i; |
| 5399 | } |
| 5400 | else |
| 5401 | #endif |
| 5402 | p = su->su_badptr + sp->ts_fidx; |
| 5403 | badflags = captype(p, su->su_badptr + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5404 | |
| 5405 | sp->ts_state = STATE_SPLITUNDO; |
| 5406 | ++depth; |
| 5407 | /* Restart at top of the tree. */ |
| 5408 | stack[depth].ts_arridx = 0; |
| 5409 | } |
| 5410 | } |
| 5411 | break; |
| 5412 | |
| 5413 | case STATE_SPLITUNDO: |
| 5414 | /* Fixup the changes done for word split. */ |
| 5415 | badflags = sp->ts_save_badflags; |
| 5416 | splitoff = sp->ts_save_splitoff; |
| 5417 | prewordlen = sp->ts_save_prewordlen; |
| 5418 | |
| 5419 | /* Continue looking for NUL bytes. */ |
| 5420 | sp->ts_state = STATE_START; |
| 5421 | break; |
| 5422 | |
| 5423 | case STATE_ENDNUL: |
| 5424 | /* Past the NUL bytes in the node. */ |
| 5425 | if (fword[sp->ts_fidx] == 0) |
| 5426 | { |
| 5427 | /* The badword ends, can't use the bytes in this node. */ |
| 5428 | sp->ts_state = STATE_DEL; |
| 5429 | break; |
| 5430 | } |
| 5431 | sp->ts_state = STATE_PLAIN; |
| 5432 | /*FALLTHROUGH*/ |
| 5433 | |
| 5434 | case STATE_PLAIN: |
| 5435 | /* |
| 5436 | * Go over all possible bytes at this node, add each to |
| 5437 | * tword[] and use child node. "ts_curi" is the index. |
| 5438 | */ |
| 5439 | arridx = sp->ts_arridx; |
| 5440 | if (sp->ts_curi > byts[arridx]) |
| 5441 | { |
| 5442 | /* Done all bytes at this node, do next state. When still |
| 5443 | * at already changed bytes skip the other tricks. */ |
| 5444 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 5445 | sp->ts_state = STATE_DEL; |
| 5446 | else |
| 5447 | sp->ts_state = STATE_FINAL; |
| 5448 | } |
| 5449 | else |
| 5450 | { |
| 5451 | arridx += sp->ts_curi++; |
| 5452 | c = byts[arridx]; |
| 5453 | |
| 5454 | /* Normal byte, go one level deeper. If it's not equal to |
| 5455 | * the byte in the bad word adjust the score. But don't |
| 5456 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5457 | if (c == fword[sp->ts_fidx] |
| 5458 | #ifdef FEAT_MBYTE |
| 5459 | || (sp->ts_tcharlen > 0 |
| 5460 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5461 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5462 | ) |
| 5463 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5464 | else |
| 5465 | newscore = SCORE_SUBST; |
| 5466 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 5467 | && try_deeper(su, stack, depth, newscore)) |
| 5468 | { |
| 5469 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5470 | sp = &stack[depth]; |
| 5471 | ++sp->ts_fidx; |
| 5472 | tword[sp->ts_twordlen++] = c; |
| 5473 | sp->ts_arridx = idxs[arridx]; |
| 5474 | #ifdef FEAT_MBYTE |
| 5475 | if (newscore == SCORE_SUBST) |
| 5476 | sp->ts_isdiff = DIFF_YES; |
| 5477 | if (has_mbyte) |
| 5478 | { |
| 5479 | /* Multi-byte characters are a bit complicated to |
| 5480 | * handle: They differ when any of the bytes |
| 5481 | * differ and then their length may also differ. */ |
| 5482 | if (sp->ts_tcharlen == 0) |
| 5483 | { |
| 5484 | /* First byte. */ |
| 5485 | sp->ts_tcharidx = 0; |
| 5486 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 5487 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 5488 | sp->ts_isdiff = (newscore != 0) |
| 5489 | ? DIFF_YES : DIFF_NONE; |
| 5490 | } |
| 5491 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 5492 | /* When inserting trail bytes don't advance in |
| 5493 | * the bad word. */ |
| 5494 | --sp->ts_fidx; |
| 5495 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 5496 | { |
| 5497 | /* Last byte of character. */ |
| 5498 | if (sp->ts_isdiff == DIFF_YES) |
| 5499 | { |
| 5500 | /* Correct ts_fidx for the byte length of |
| 5501 | * the character (we didn't check that |
| 5502 | * before). */ |
| 5503 | sp->ts_fidx = sp->ts_fcharstart |
| 5504 | + MB_BYTE2LEN( |
| 5505 | fword[sp->ts_fcharstart]); |
| 5506 | |
| 5507 | /* For a similar character adjust score |
| 5508 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 5509 | if (lp->lp_slang->sl_has_map |
| 5510 | && similar_chars(lp->lp_slang, |
| 5511 | mb_ptr2char(tword |
| 5512 | + sp->ts_twordlen |
| 5513 | - sp->ts_tcharlen), |
| 5514 | mb_ptr2char(fword |
| 5515 | + sp->ts_fcharstart))) |
| 5516 | sp->ts_score -= |
| 5517 | SCORE_SUBST - SCORE_SIMILAR; |
| 5518 | } |
| 5519 | |
| 5520 | /* Starting a new char, reset the length. */ |
| 5521 | sp->ts_tcharlen = 0; |
| 5522 | } |
| 5523 | } |
| 5524 | else |
| 5525 | #endif |
| 5526 | { |
| 5527 | /* If we found a similar char adjust the score. |
| 5528 | * We do this after calling try_deeper() because |
| 5529 | * it's slow. */ |
| 5530 | if (newscore != 0 |
| 5531 | && lp->lp_slang->sl_has_map |
| 5532 | && similar_chars(lp->lp_slang, |
| 5533 | c, fword[sp->ts_fidx - 1])) |
| 5534 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 5535 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5536 | } |
| 5537 | } |
| 5538 | break; |
| 5539 | |
| 5540 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5541 | #ifdef FEAT_MBYTE |
| 5542 | /* When past the first byte of a multi-byte char don't try |
| 5543 | * delete/insert/swap a character. */ |
| 5544 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 5545 | { |
| 5546 | sp->ts_state = STATE_FINAL; |
| 5547 | break; |
| 5548 | } |
| 5549 | #endif |
| 5550 | /* |
| 5551 | * Try skipping one character in the bad word (delete it). |
| 5552 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5553 | sp->ts_state = STATE_INS; |
| 5554 | sp->ts_curi = 1; |
| 5555 | if (fword[sp->ts_fidx] != NUL |
| 5556 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 5557 | { |
| 5558 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5559 | #ifdef FEAT_MBYTE |
| 5560 | if (has_mbyte) |
| 5561 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
| 5562 | else |
| 5563 | #endif |
| 5564 | ++stack[depth].ts_fidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5565 | break; |
| 5566 | } |
| 5567 | /*FALLTHROUGH*/ |
| 5568 | |
| 5569 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5570 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5571 | * node. */ |
| 5572 | n = sp->ts_arridx; |
| 5573 | if (sp->ts_curi > byts[n]) |
| 5574 | { |
| 5575 | /* Done all bytes at this node, do next state. */ |
| 5576 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5577 | } |
| 5578 | else |
| 5579 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5580 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5581 | n += sp->ts_curi++; |
| 5582 | c = byts[n]; |
| 5583 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 5584 | { |
| 5585 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5586 | sp = &stack[depth]; |
| 5587 | tword[sp->ts_twordlen++] = c; |
| 5588 | sp->ts_arridx = idxs[n]; |
| 5589 | #ifdef FEAT_MBYTE |
| 5590 | if (has_mbyte) |
| 5591 | { |
| 5592 | fl = MB_BYTE2LEN(c); |
| 5593 | if (fl > 1) |
| 5594 | { |
| 5595 | /* There are following bytes for the same |
| 5596 | * character. We must find all bytes before |
| 5597 | * trying delete/insert/swap/etc. */ |
| 5598 | sp->ts_tcharlen = fl; |
| 5599 | sp->ts_tcharidx = 1; |
| 5600 | sp->ts_isdiff = DIFF_INSERT; |
| 5601 | } |
| 5602 | } |
| 5603 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | break; |
| 5607 | |
| 5608 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5609 | /* |
| 5610 | * Swap two bytes in the bad word: "12" -> "21". |
| 5611 | * We change "fword" here, it's changed back afterwards. |
| 5612 | */ |
| 5613 | p = fword + sp->ts_fidx; |
| 5614 | c = *p; |
| 5615 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5616 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5617 | /* End of word, can't swap or replace. */ |
| 5618 | sp->ts_state = STATE_FINAL; |
| 5619 | break; |
| 5620 | } |
| 5621 | #ifdef FEAT_MBYTE |
| 5622 | if (has_mbyte) |
| 5623 | { |
| 5624 | n = mb_ptr2len_check(p); |
| 5625 | c = mb_ptr2char(p); |
| 5626 | c2 = mb_ptr2char(p + n); |
| 5627 | } |
| 5628 | else |
| 5629 | #endif |
| 5630 | c2 = p[1]; |
| 5631 | if (c == c2) |
| 5632 | { |
| 5633 | /* Characters are identical, swap won't do anything. */ |
| 5634 | sp->ts_state = STATE_SWAP3; |
| 5635 | break; |
| 5636 | } |
| 5637 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 5638 | { |
| 5639 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5640 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5641 | #ifdef FEAT_MBYTE |
| 5642 | if (has_mbyte) |
| 5643 | { |
| 5644 | fl = mb_char2len(c2); |
| 5645 | mch_memmove(p, p + n, fl); |
| 5646 | mb_char2bytes(c, p + fl); |
| 5647 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 5648 | } |
| 5649 | else |
| 5650 | #endif |
| 5651 | { |
| 5652 | p[0] = c2; |
| 5653 | p[1] = c; |
| 5654 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 5655 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5656 | } |
| 5657 | else |
| 5658 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 5659 | sp->ts_state = STATE_REP_INI; |
| 5660 | break; |
| 5661 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5662 | case STATE_UNSWAP: |
| 5663 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 5664 | p = fword + sp->ts_fidx; |
| 5665 | #ifdef FEAT_MBYTE |
| 5666 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5667 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5668 | n = MB_BYTE2LEN(*p); |
| 5669 | c = mb_ptr2char(p + n); |
| 5670 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 5671 | mb_char2bytes(c, p); |
| 5672 | } |
| 5673 | else |
| 5674 | #endif |
| 5675 | { |
| 5676 | c = *p; |
| 5677 | *p = p[1]; |
| 5678 | p[1] = c; |
| 5679 | } |
| 5680 | /*FALLTHROUGH*/ |
| 5681 | |
| 5682 | case STATE_SWAP3: |
| 5683 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 5684 | * "fword" here, it's changed back afterwards. */ |
| 5685 | p = fword + sp->ts_fidx; |
| 5686 | #ifdef FEAT_MBYTE |
| 5687 | if (has_mbyte) |
| 5688 | { |
| 5689 | n = mb_ptr2len_check(p); |
| 5690 | c = mb_ptr2char(p); |
| 5691 | fl = mb_ptr2len_check(p + n); |
| 5692 | c2 = mb_ptr2char(p + n); |
| 5693 | c3 = mb_ptr2char(p + n + fl); |
| 5694 | } |
| 5695 | else |
| 5696 | #endif |
| 5697 | { |
| 5698 | c = *p; |
| 5699 | c2 = p[1]; |
| 5700 | c3 = p[2]; |
| 5701 | } |
| 5702 | |
| 5703 | /* When characters are identical: "121" then SWAP3 result is |
| 5704 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 5705 | * result is same as SWAP on next char: "112". Thus skip all |
| 5706 | * swapping. Also skip when c3 is NUL. */ |
| 5707 | if (c == c3 || c3 == NUL) |
| 5708 | { |
| 5709 | sp->ts_state = STATE_REP_INI; |
| 5710 | break; |
| 5711 | } |
| 5712 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 5713 | { |
| 5714 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5715 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5716 | #ifdef FEAT_MBYTE |
| 5717 | if (has_mbyte) |
| 5718 | { |
| 5719 | tl = mb_char2len(c3); |
| 5720 | mch_memmove(p, p + n + fl, tl); |
| 5721 | mb_char2bytes(c2, p + tl); |
| 5722 | mb_char2bytes(c, p + fl + tl); |
| 5723 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 5724 | } |
| 5725 | else |
| 5726 | #endif |
| 5727 | { |
| 5728 | p[0] = p[2]; |
| 5729 | p[2] = c; |
| 5730 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 5731 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5732 | } |
| 5733 | else |
| 5734 | sp->ts_state = STATE_REP_INI; |
| 5735 | break; |
| 5736 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5737 | case STATE_UNSWAP3: |
| 5738 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 5739 | p = fword + sp->ts_fidx; |
| 5740 | #ifdef FEAT_MBYTE |
| 5741 | if (has_mbyte) |
| 5742 | { |
| 5743 | n = MB_BYTE2LEN(*p); |
| 5744 | c2 = mb_ptr2char(p + n); |
| 5745 | fl = MB_BYTE2LEN(p[n]); |
| 5746 | c = mb_ptr2char(p + n + fl); |
| 5747 | tl = MB_BYTE2LEN(p[n + fl]); |
| 5748 | mch_memmove(p + fl + tl, p, n); |
| 5749 | mb_char2bytes(c, p); |
| 5750 | mb_char2bytes(c2, p + tl); |
| 5751 | } |
| 5752 | else |
| 5753 | #endif |
| 5754 | { |
| 5755 | c = *p; |
| 5756 | *p = p[2]; |
| 5757 | p[2] = c; |
| 5758 | } |
| 5759 | /*FALLTHROUGH*/ |
| 5760 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5761 | case STATE_ROT3L: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5762 | /* Rotate three characters left: "123" -> "231". We change |
| 5763 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5764 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 5765 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5766 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5767 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5768 | p = fword + sp->ts_fidx; |
| 5769 | #ifdef FEAT_MBYTE |
| 5770 | if (has_mbyte) |
| 5771 | { |
| 5772 | n = mb_ptr2len_check(p); |
| 5773 | c = mb_ptr2char(p); |
| 5774 | fl = mb_ptr2len_check(p + n); |
| 5775 | fl += mb_ptr2len_check(p + n + fl); |
| 5776 | mch_memmove(p, p + n, fl); |
| 5777 | mb_char2bytes(c, p + fl); |
| 5778 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 5779 | } |
| 5780 | else |
| 5781 | #endif |
| 5782 | { |
| 5783 | c = *p; |
| 5784 | *p = p[1]; |
| 5785 | p[1] = p[2]; |
| 5786 | p[2] = c; |
| 5787 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 5788 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5789 | } |
| 5790 | else |
| 5791 | sp->ts_state = STATE_REP_INI; |
| 5792 | break; |
| 5793 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5794 | case STATE_UNROT3L: |
| 5795 | /* Undo STATE_ROT3L: "231" -> "123" */ |
| 5796 | p = fword + sp->ts_fidx; |
| 5797 | #ifdef FEAT_MBYTE |
| 5798 | if (has_mbyte) |
| 5799 | { |
| 5800 | n = MB_BYTE2LEN(*p); |
| 5801 | n += MB_BYTE2LEN(p[n]); |
| 5802 | c = mb_ptr2char(p + n); |
| 5803 | tl = MB_BYTE2LEN(p[n]); |
| 5804 | mch_memmove(p + tl, p, n); |
| 5805 | mb_char2bytes(c, p); |
| 5806 | } |
| 5807 | else |
| 5808 | #endif |
| 5809 | { |
| 5810 | c = p[2]; |
| 5811 | p[2] = p[1]; |
| 5812 | p[1] = *p; |
| 5813 | *p = c; |
| 5814 | } |
| 5815 | /*FALLTHROUGH*/ |
| 5816 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5817 | case STATE_ROT3R: |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5818 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5819 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5820 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 5821 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5822 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5823 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5824 | p = fword + sp->ts_fidx; |
| 5825 | #ifdef FEAT_MBYTE |
| 5826 | if (has_mbyte) |
| 5827 | { |
| 5828 | n = mb_ptr2len_check(p); |
| 5829 | n += mb_ptr2len_check(p + n); |
| 5830 | c = mb_ptr2char(p + n); |
| 5831 | tl = mb_ptr2len_check(p + n); |
| 5832 | mch_memmove(p + tl, p, n); |
| 5833 | mb_char2bytes(c, p); |
| 5834 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 5835 | } |
| 5836 | else |
| 5837 | #endif |
| 5838 | { |
| 5839 | c = p[2]; |
| 5840 | p[2] = p[1]; |
| 5841 | p[1] = *p; |
| 5842 | *p = c; |
| 5843 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 5844 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5845 | } |
| 5846 | else |
| 5847 | sp->ts_state = STATE_REP_INI; |
| 5848 | break; |
| 5849 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5850 | case STATE_UNROT3R: |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5851 | /* Undo STATE_ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5852 | p = fword + sp->ts_fidx; |
| 5853 | #ifdef FEAT_MBYTE |
| 5854 | if (has_mbyte) |
| 5855 | { |
| 5856 | c = mb_ptr2char(p); |
| 5857 | tl = MB_BYTE2LEN(*p); |
| 5858 | n = MB_BYTE2LEN(p[tl]); |
| 5859 | n += MB_BYTE2LEN(p[tl + n]); |
| 5860 | mch_memmove(p, p + tl, n); |
| 5861 | mb_char2bytes(c, p + n); |
| 5862 | } |
| 5863 | else |
| 5864 | #endif |
| 5865 | { |
| 5866 | c = *p; |
| 5867 | *p = p[1]; |
| 5868 | p[1] = p[2]; |
| 5869 | p[2] = c; |
| 5870 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5871 | /*FALLTHROUGH*/ |
| 5872 | |
| 5873 | case STATE_REP_INI: |
| 5874 | /* Check if matching with REP items from the .aff file would |
| 5875 | * work. Quickly skip if there are no REP items or the score |
| 5876 | * is going to be too high anyway. */ |
| 5877 | gap = &lp->lp_slang->sl_rep; |
| 5878 | if (gap->ga_len == 0 |
| 5879 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 5880 | { |
| 5881 | sp->ts_state = STATE_FINAL; |
| 5882 | break; |
| 5883 | } |
| 5884 | |
| 5885 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5886 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5887 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 5888 | if (sp->ts_curi < 0) |
| 5889 | { |
| 5890 | sp->ts_state = STATE_FINAL; |
| 5891 | break; |
| 5892 | } |
| 5893 | |
| 5894 | sp->ts_state = STATE_REP; |
| 5895 | /*FALLTHROUGH*/ |
| 5896 | |
| 5897 | case STATE_REP: |
| 5898 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5899 | * match replace the characters and check if the resulting |
| 5900 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5901 | p = fword + sp->ts_fidx; |
| 5902 | |
| 5903 | gap = &lp->lp_slang->sl_rep; |
| 5904 | while (sp->ts_curi < gap->ga_len) |
| 5905 | { |
| 5906 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 5907 | if (*ftp->ft_from != *p) |
| 5908 | { |
| 5909 | /* past possible matching entries */ |
| 5910 | sp->ts_curi = gap->ga_len; |
| 5911 | break; |
| 5912 | } |
| 5913 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 5914 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 5915 | { |
| 5916 | /* Need to undo this afterwards. */ |
| 5917 | sp->ts_state = STATE_REP_UNDO; |
| 5918 | |
| 5919 | /* Change the "from" to the "to" string. */ |
| 5920 | ++depth; |
| 5921 | fl = STRLEN(ftp->ft_from); |
| 5922 | tl = STRLEN(ftp->ft_to); |
| 5923 | if (fl != tl) |
| 5924 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
| 5925 | mch_memmove(p, ftp->ft_to, tl); |
| 5926 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5927 | #ifdef FEAT_MBYTE |
| 5928 | stack[depth].ts_tcharlen = 0; |
| 5929 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5930 | break; |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | if (sp->ts_curi >= gap->ga_len) |
| 5935 | /* No (more) matches. */ |
| 5936 | sp->ts_state = STATE_FINAL; |
| 5937 | |
| 5938 | break; |
| 5939 | |
| 5940 | case STATE_REP_UNDO: |
| 5941 | /* Undo a REP replacement and continue with the next one. */ |
| 5942 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 5943 | + sp->ts_curi - 1; |
| 5944 | fl = STRLEN(ftp->ft_from); |
| 5945 | tl = STRLEN(ftp->ft_to); |
| 5946 | p = fword + sp->ts_fidx; |
| 5947 | if (fl != tl) |
| 5948 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
| 5949 | mch_memmove(p, ftp->ft_from, fl); |
| 5950 | sp->ts_state = STATE_REP; |
| 5951 | break; |
| 5952 | |
| 5953 | default: |
| 5954 | /* Did all possible states at this level, go up one level. */ |
| 5955 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5956 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5957 | /* Don't check for CTRL-C too often, it takes time. */ |
| 5958 | line_breakcheck(); |
| 5959 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5960 | } |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | /* |
| 5965 | * Try going one level deeper in the tree. |
| 5966 | */ |
| 5967 | static int |
| 5968 | try_deeper(su, stack, depth, score_add) |
| 5969 | suginfo_T *su; |
| 5970 | trystate_T *stack; |
| 5971 | int depth; |
| 5972 | int score_add; |
| 5973 | { |
| 5974 | int newscore; |
| 5975 | |
| 5976 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 5977 | newscore = stack[depth].ts_score + score_add; |
| 5978 | if (newscore >= su->su_maxscore) |
| 5979 | return FALSE; |
| 5980 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 5981 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5982 | stack[depth + 1].ts_state = STATE_START; |
| 5983 | stack[depth + 1].ts_score = newscore; |
| 5984 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5985 | return TRUE; |
| 5986 | } |
| 5987 | |
| 5988 | /* |
| 5989 | * "fword" is a good word with case folded. Find the matching keep-case |
| 5990 | * words and put it in "kword". |
| 5991 | * Theoretically there could be several keep-case words that result in the |
| 5992 | * same case-folded word, but we only find one... |
| 5993 | */ |
| 5994 | static void |
| 5995 | find_keepcap_word(slang, fword, kword) |
| 5996 | slang_T *slang; |
| 5997 | char_u *fword; |
| 5998 | char_u *kword; |
| 5999 | { |
| 6000 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 6001 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6002 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6003 | |
| 6004 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6005 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6006 | int round[MAXWLEN]; |
| 6007 | int fwordidx[MAXWLEN]; |
| 6008 | int uwordidx[MAXWLEN]; |
| 6009 | int kwordlen[MAXWLEN]; |
| 6010 | |
| 6011 | int flen, ulen; |
| 6012 | int l; |
| 6013 | int len; |
| 6014 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6015 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6016 | char_u *p; |
| 6017 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6018 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6019 | |
| 6020 | if (byts == NULL) |
| 6021 | { |
| 6022 | /* array is empty: "cannot happen" */ |
| 6023 | *kword = NUL; |
| 6024 | return; |
| 6025 | } |
| 6026 | |
| 6027 | /* Make an all-cap version of "fword". */ |
| 6028 | allcap_copy(fword, uword); |
| 6029 | |
| 6030 | /* |
| 6031 | * Each character needs to be tried both case-folded and upper-case. |
| 6032 | * All this gets very complicated if we keep in mind that changing case |
| 6033 | * may change the byte length of a multi-byte character... |
| 6034 | */ |
| 6035 | depth = 0; |
| 6036 | arridx[0] = 0; |
| 6037 | round[0] = 0; |
| 6038 | fwordidx[0] = 0; |
| 6039 | uwordidx[0] = 0; |
| 6040 | kwordlen[0] = 0; |
| 6041 | while (depth >= 0) |
| 6042 | { |
| 6043 | if (fword[fwordidx[depth]] == NUL) |
| 6044 | { |
| 6045 | /* We are at the end of "fword". If the tree allows a word to end |
| 6046 | * here we have found a match. */ |
| 6047 | if (byts[arridx[depth] + 1] == 0) |
| 6048 | { |
| 6049 | kword[kwordlen[depth]] = NUL; |
| 6050 | return; |
| 6051 | } |
| 6052 | |
| 6053 | /* kword is getting too long, continue one level up */ |
| 6054 | --depth; |
| 6055 | } |
| 6056 | else if (++round[depth] > 2) |
| 6057 | { |
| 6058 | /* tried both fold-case and upper-case character, continue one |
| 6059 | * level up */ |
| 6060 | --depth; |
| 6061 | } |
| 6062 | else |
| 6063 | { |
| 6064 | /* |
| 6065 | * round[depth] == 1: Try using the folded-case character. |
| 6066 | * round[depth] == 2: Try using the upper-case character. |
| 6067 | */ |
| 6068 | #ifdef FEAT_MBYTE |
| 6069 | if (has_mbyte) |
| 6070 | { |
| 6071 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 6072 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 6073 | } |
| 6074 | else |
| 6075 | #endif |
| 6076 | ulen = flen = 1; |
| 6077 | if (round[depth] == 1) |
| 6078 | { |
| 6079 | p = fword + fwordidx[depth]; |
| 6080 | l = flen; |
| 6081 | } |
| 6082 | else |
| 6083 | { |
| 6084 | p = uword + uwordidx[depth]; |
| 6085 | l = ulen; |
| 6086 | } |
| 6087 | |
| 6088 | for (tryidx = arridx[depth]; l > 0; --l) |
| 6089 | { |
| 6090 | /* Perform a binary search in the list of accepted bytes. */ |
| 6091 | len = byts[tryidx++]; |
| 6092 | c = *p++; |
| 6093 | lo = tryidx; |
| 6094 | hi = tryidx + len - 1; |
| 6095 | while (lo < hi) |
| 6096 | { |
| 6097 | m = (lo + hi) / 2; |
| 6098 | if (byts[m] > c) |
| 6099 | hi = m - 1; |
| 6100 | else if (byts[m] < c) |
| 6101 | lo = m + 1; |
| 6102 | else |
| 6103 | { |
| 6104 | lo = hi = m; |
| 6105 | break; |
| 6106 | } |
| 6107 | } |
| 6108 | |
| 6109 | /* Stop if there is no matching byte. */ |
| 6110 | if (hi < lo || byts[lo] != c) |
| 6111 | break; |
| 6112 | |
| 6113 | /* Continue at the child (if there is one). */ |
| 6114 | tryidx = idxs[lo]; |
| 6115 | } |
| 6116 | |
| 6117 | if (l == 0) |
| 6118 | { |
| 6119 | /* |
| 6120 | * Found the matching char. Copy it to "kword" and go a |
| 6121 | * level deeper. |
| 6122 | */ |
| 6123 | if (round[depth] == 1) |
| 6124 | { |
| 6125 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 6126 | flen); |
| 6127 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 6128 | } |
| 6129 | else |
| 6130 | { |
| 6131 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 6132 | ulen); |
| 6133 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 6134 | } |
| 6135 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 6136 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 6137 | |
| 6138 | ++depth; |
| 6139 | arridx[depth] = tryidx; |
| 6140 | round[depth] = 0; |
| 6141 | } |
| 6142 | } |
| 6143 | } |
| 6144 | |
| 6145 | /* Didn't find it: "cannot happen". */ |
| 6146 | *kword = NUL; |
| 6147 | } |
| 6148 | |
| 6149 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6150 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 6151 | * su->su_sga. |
| 6152 | */ |
| 6153 | static void |
| 6154 | score_comp_sal(su) |
| 6155 | suginfo_T *su; |
| 6156 | { |
| 6157 | langp_T *lp; |
| 6158 | char_u badsound[MAXWLEN]; |
| 6159 | int i; |
| 6160 | suggest_T *stp; |
| 6161 | suggest_T *sstp; |
| 6162 | char_u fword[MAXWLEN]; |
| 6163 | char_u goodsound[MAXWLEN]; |
| 6164 | int score; |
| 6165 | |
| 6166 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 6167 | return; |
| 6168 | |
| 6169 | /* Use the sound-folding of the first language that supports it. */ |
| 6170 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6171 | lp->lp_slang != NULL; ++lp) |
| 6172 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6173 | { |
| 6174 | /* soundfold the bad word */ |
| 6175 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 6176 | |
| 6177 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6178 | { |
| 6179 | stp = &SUG(su->su_ga, i); |
| 6180 | |
| 6181 | /* Case-fold the suggested word and sound-fold it. */ |
| 6182 | (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), |
| 6183 | fword, MAXWLEN); |
| 6184 | spell_soundfold(lp->lp_slang, fword, goodsound); |
| 6185 | score = soundalike_score(goodsound, badsound); |
| 6186 | if (score < SCORE_MAXMAX) |
| 6187 | { |
| 6188 | /* Add the suggestion. */ |
| 6189 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 6190 | sstp->st_word = vim_strsave(stp->st_word); |
| 6191 | if (sstp->st_word != NULL) |
| 6192 | { |
| 6193 | sstp->st_score = score; |
| 6194 | sstp->st_altscore = 0; |
| 6195 | sstp->st_orglen = stp->st_orglen; |
| 6196 | ++su->su_sga.ga_len; |
| 6197 | } |
| 6198 | } |
| 6199 | } |
| 6200 | break; |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | /* |
| 6205 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 6206 | * They are intwined. |
| 6207 | */ |
| 6208 | static void |
| 6209 | score_combine(su) |
| 6210 | suginfo_T *su; |
| 6211 | { |
| 6212 | int i; |
| 6213 | int j; |
| 6214 | garray_T ga; |
| 6215 | garray_T *gap; |
| 6216 | langp_T *lp; |
| 6217 | suggest_T *stp; |
| 6218 | char_u *p; |
| 6219 | char_u badsound[MAXWLEN]; |
| 6220 | char_u goodsound[MAXWLEN]; |
| 6221 | char_u fword[MAXWLEN]; |
| 6222 | int round; |
| 6223 | |
| 6224 | /* Add the alternate score to su_ga. */ |
| 6225 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6226 | lp->lp_slang != NULL; ++lp) |
| 6227 | { |
| 6228 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6229 | { |
| 6230 | /* soundfold the bad word */ |
| 6231 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 6232 | |
| 6233 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6234 | { |
| 6235 | stp = &SUG(su->su_ga, i); |
| 6236 | |
| 6237 | /* Case-fold the word, sound-fold the word and compute the |
| 6238 | * score for the difference. */ |
| 6239 | (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), |
| 6240 | fword, MAXWLEN); |
| 6241 | spell_soundfold(lp->lp_slang, fword, goodsound); |
| 6242 | stp->st_altscore = soundalike_score(goodsound, badsound); |
| 6243 | if (stp->st_altscore == SCORE_MAXMAX) |
| 6244 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 6245 | else |
| 6246 | stp->st_score = (stp->st_score * 3 |
| 6247 | + stp->st_altscore) / 4; |
| 6248 | stp->st_salscore = FALSE; |
| 6249 | } |
| 6250 | break; |
| 6251 | } |
| 6252 | } |
| 6253 | |
| 6254 | /* Add the alternate score to su_sga. */ |
| 6255 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6256 | { |
| 6257 | stp = &SUG(su->su_sga, i); |
| 6258 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 6259 | if (stp->st_score == SCORE_MAXMAX) |
| 6260 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 6261 | else |
| 6262 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 6263 | stp->st_salscore = TRUE; |
| 6264 | } |
| 6265 | |
| 6266 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 6267 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6268 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 6269 | |
| 6270 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 6271 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 6272 | return; |
| 6273 | |
| 6274 | stp = &SUG(ga, 0); |
| 6275 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 6276 | { |
| 6277 | /* round 1: get a suggestion from su_ga |
| 6278 | * round 2: get a suggestion from su_sga */ |
| 6279 | for (round = 1; round <= 2; ++round) |
| 6280 | { |
| 6281 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 6282 | if (i < gap->ga_len) |
| 6283 | { |
| 6284 | /* Don't add a word if it's already there. */ |
| 6285 | p = SUG(*gap, i).st_word; |
| 6286 | for (j = 0; j < ga.ga_len; ++j) |
| 6287 | if (STRCMP(stp[j].st_word, p) == 0) |
| 6288 | break; |
| 6289 | if (j == ga.ga_len) |
| 6290 | stp[ga.ga_len++] = SUG(*gap, i); |
| 6291 | else |
| 6292 | vim_free(p); |
| 6293 | } |
| 6294 | } |
| 6295 | } |
| 6296 | |
| 6297 | ga_clear(&su->su_ga); |
| 6298 | ga_clear(&su->su_sga); |
| 6299 | |
| 6300 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 6301 | if (ga.ga_len > su->su_maxcount) |
| 6302 | { |
| 6303 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 6304 | vim_free(stp[i].st_word); |
| 6305 | ga.ga_len = su->su_maxcount; |
| 6306 | } |
| 6307 | |
| 6308 | su->su_ga = ga; |
| 6309 | } |
| 6310 | |
| 6311 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6312 | * Find suggestions by comparing the word in a sound-a-like form. |
| 6313 | */ |
| 6314 | static void |
| 6315 | spell_try_soundalike(su) |
| 6316 | suginfo_T *su; |
| 6317 | { |
| 6318 | char_u salword[MAXWLEN]; |
| 6319 | char_u tword[MAXWLEN]; |
| 6320 | char_u tfword[MAXWLEN]; |
| 6321 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6322 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6323 | int curi[MAXWLEN]; |
| 6324 | langp_T *lp; |
| 6325 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6326 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6327 | int depth; |
| 6328 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6329 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6330 | int round; |
| 6331 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6332 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6333 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6334 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6335 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6336 | lp->lp_slang != NULL; ++lp) |
| 6337 | { |
| 6338 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6339 | { |
| 6340 | /* soundfold the bad word */ |
| 6341 | spell_soundfold(lp->lp_slang, su->su_fbadword, salword); |
| 6342 | |
| 6343 | /* |
| 6344 | * Go through the whole tree, soundfold each word and compare. |
| 6345 | * round 1: use the case-folded tree. |
| 6346 | * round 2: use the keep-case tree. |
| 6347 | */ |
| 6348 | for (round = 1; round <= 2; ++round) |
| 6349 | { |
| 6350 | if (round == 1) |
| 6351 | { |
| 6352 | byts = lp->lp_slang->sl_fbyts; |
| 6353 | idxs = lp->lp_slang->sl_fidxs; |
| 6354 | } |
| 6355 | else |
| 6356 | { |
| 6357 | byts = lp->lp_slang->sl_kbyts; |
| 6358 | idxs = lp->lp_slang->sl_kidxs; |
| 6359 | } |
| 6360 | |
| 6361 | depth = 0; |
| 6362 | arridx[0] = 0; |
| 6363 | curi[0] = 1; |
| 6364 | while (depth >= 0 && !got_int) |
| 6365 | { |
| 6366 | if (curi[depth] > byts[arridx[depth]]) |
| 6367 | /* Done all bytes at this node, go up one level. */ |
| 6368 | --depth; |
| 6369 | else |
| 6370 | { |
| 6371 | /* Do one more byte at this node. */ |
| 6372 | n = arridx[depth] + curi[depth]; |
| 6373 | ++curi[depth]; |
| 6374 | c = byts[n]; |
| 6375 | if (c == 0) |
| 6376 | { |
| 6377 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6378 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6379 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 6380 | { |
| 6381 | tword[depth] = NUL; |
| 6382 | if (round == 1) |
| 6383 | spell_soundfold(lp->lp_slang, |
| 6384 | tword, tsalword); |
| 6385 | else |
| 6386 | { |
| 6387 | /* In keep-case tree need to case-fold the |
| 6388 | * word. */ |
| 6389 | (void)spell_casefold(tword, depth, |
| 6390 | tfword, MAXWLEN); |
| 6391 | spell_soundfold(lp->lp_slang, |
| 6392 | tfword, tsalword); |
| 6393 | } |
| 6394 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6395 | /* Compute the edit distance between the |
| 6396 | * sound-a-like words. */ |
| 6397 | sound_score = soundalike_score(salword, |
| 6398 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6399 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6400 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6401 | char_u cword[MAXWLEN]; |
| 6402 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6403 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6404 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6405 | if (round == 1 && flags != 0) |
| 6406 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6407 | /* Need to fix case according to |
| 6408 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6409 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6410 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6411 | } |
| 6412 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6413 | p = tword; |
| 6414 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6415 | if (sps_flags & SPS_DOUBLE) |
| 6416 | add_suggestion(su, &su->su_sga, p, |
| 6417 | sound_score, FALSE); |
| 6418 | else |
| 6419 | { |
| 6420 | /* Compute the score. */ |
| 6421 | score = spell_edit_score( |
| 6422 | su->su_badword, p); |
| 6423 | if (sps_flags & SPS_BEST) |
| 6424 | /* give a bonus for the good word |
| 6425 | * sounding the same as the bad |
| 6426 | * word */ |
| 6427 | add_suggestion(su, &su->su_ga, p, |
| 6428 | RESCORE(score, sound_score), |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6429 | TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6430 | else |
| 6431 | add_suggestion(su, &su->su_ga, p, |
| 6432 | score + sound_score, FALSE); |
| 6433 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
| 6437 | /* Skip over other NUL bytes. */ |
| 6438 | while (byts[n + 1] == 0) |
| 6439 | { |
| 6440 | ++n; |
| 6441 | ++curi[depth]; |
| 6442 | } |
| 6443 | } |
| 6444 | else |
| 6445 | { |
| 6446 | /* Normal char, go one level deeper. */ |
| 6447 | tword[depth++] = c; |
| 6448 | arridx[depth] = idxs[n]; |
| 6449 | curi[depth] = 1; |
| 6450 | } |
| 6451 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6452 | |
| 6453 | line_breakcheck(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6454 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6461 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6462 | */ |
| 6463 | static void |
| 6464 | make_case_word(fword, cword, flags) |
| 6465 | char_u *fword; |
| 6466 | char_u *cword; |
| 6467 | int flags; |
| 6468 | { |
| 6469 | if (flags & WF_ALLCAP) |
| 6470 | /* Make it all upper-case */ |
| 6471 | allcap_copy(fword, cword); |
| 6472 | else if (flags & WF_ONECAP) |
| 6473 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6474 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6475 | else |
| 6476 | /* Use goodword as-is. */ |
| 6477 | STRCPY(cword, fword); |
| 6478 | } |
| 6479 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6480 | /* |
| 6481 | * Use map string "map" for languages "lp". |
| 6482 | */ |
| 6483 | static void |
| 6484 | set_map_str(lp, map) |
| 6485 | slang_T *lp; |
| 6486 | char_u *map; |
| 6487 | { |
| 6488 | char_u *p; |
| 6489 | int headc = 0; |
| 6490 | int c; |
| 6491 | int i; |
| 6492 | |
| 6493 | if (*map == NUL) |
| 6494 | { |
| 6495 | lp->sl_has_map = FALSE; |
| 6496 | return; |
| 6497 | } |
| 6498 | lp->sl_has_map = TRUE; |
| 6499 | |
| 6500 | /* Init the array and hash table empty. */ |
| 6501 | for (i = 0; i < 256; ++i) |
| 6502 | lp->sl_map_array[i] = 0; |
| 6503 | #ifdef FEAT_MBYTE |
| 6504 | hash_init(&lp->sl_map_hash); |
| 6505 | #endif |
| 6506 | |
| 6507 | /* |
| 6508 | * The similar characters are stored separated with slashes: |
| 6509 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 6510 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 6511 | */ |
| 6512 | for (p = map; *p != NUL; ) |
| 6513 | { |
| 6514 | #ifdef FEAT_MBYTE |
| 6515 | c = mb_ptr2char_adv(&p); |
| 6516 | #else |
| 6517 | c = *p++; |
| 6518 | #endif |
| 6519 | if (c == '/') |
| 6520 | headc = 0; |
| 6521 | else |
| 6522 | { |
| 6523 | if (headc == 0) |
| 6524 | headc = c; |
| 6525 | |
| 6526 | #ifdef FEAT_MBYTE |
| 6527 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 6528 | * the hash table. Each entry is the char, a NUL the headchar and |
| 6529 | * a NUL. */ |
| 6530 | if (c >= 256) |
| 6531 | { |
| 6532 | int cl = mb_char2len(c); |
| 6533 | int headcl = mb_char2len(headc); |
| 6534 | char_u *b; |
| 6535 | hash_T hash; |
| 6536 | hashitem_T *hi; |
| 6537 | |
| 6538 | b = alloc((unsigned)(cl + headcl + 2)); |
| 6539 | if (b == NULL) |
| 6540 | return; |
| 6541 | mb_char2bytes(c, b); |
| 6542 | b[cl] = NUL; |
| 6543 | mb_char2bytes(headc, b + cl + 1); |
| 6544 | b[cl + 1 + headcl] = NUL; |
| 6545 | hash = hash_hash(b); |
| 6546 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 6547 | if (HASHITEM_EMPTY(hi)) |
| 6548 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 6549 | else |
| 6550 | { |
| 6551 | /* This should have been checked when generating the .spl |
| 6552 | * file. */ |
| 6553 | EMSG(_("E999: duplicate char in MAP entry")); |
| 6554 | vim_free(b); |
| 6555 | } |
| 6556 | } |
| 6557 | else |
| 6558 | #endif |
| 6559 | lp->sl_map_array[c] = headc; |
| 6560 | } |
| 6561 | } |
| 6562 | } |
| 6563 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6564 | /* |
| 6565 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 6566 | * lines in the .aff file. |
| 6567 | */ |
| 6568 | static int |
| 6569 | similar_chars(slang, c1, c2) |
| 6570 | slang_T *slang; |
| 6571 | int c1; |
| 6572 | int c2; |
| 6573 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6574 | int m1, m2; |
| 6575 | #ifdef FEAT_MBYTE |
| 6576 | char_u buf[MB_MAXBYTES]; |
| 6577 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6578 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6579 | if (c1 >= 256) |
| 6580 | { |
| 6581 | buf[mb_char2bytes(c1, buf)] = 0; |
| 6582 | hi = hash_find(&slang->sl_map_hash, buf); |
| 6583 | if (HASHITEM_EMPTY(hi)) |
| 6584 | m1 = 0; |
| 6585 | else |
| 6586 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 6587 | } |
| 6588 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6589 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6590 | m1 = slang->sl_map_array[c1]; |
| 6591 | if (m1 == 0) |
| 6592 | return FALSE; |
| 6593 | |
| 6594 | |
| 6595 | #ifdef FEAT_MBYTE |
| 6596 | if (c2 >= 256) |
| 6597 | { |
| 6598 | buf[mb_char2bytes(c2, buf)] = 0; |
| 6599 | hi = hash_find(&slang->sl_map_hash, buf); |
| 6600 | if (HASHITEM_EMPTY(hi)) |
| 6601 | m2 = 0; |
| 6602 | else |
| 6603 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 6604 | } |
| 6605 | else |
| 6606 | #endif |
| 6607 | m2 = slang->sl_map_array[c2]; |
| 6608 | |
| 6609 | return m1 == m2; |
| 6610 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6611 | |
| 6612 | /* |
| 6613 | * Add a suggestion to the list of suggestions. |
| 6614 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 6615 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 6616 | * with spell_edit_score(). |
| 6617 | */ |
| 6618 | static void |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6619 | add_suggestion(su, gap, goodword, score, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6620 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6621 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6622 | char_u *goodword; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6623 | int score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6624 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6625 | { |
| 6626 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6627 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6628 | |
| 6629 | /* Check that the word wasn't banned. */ |
| 6630 | if (was_banned(su, goodword)) |
| 6631 | return; |
| 6632 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6633 | if (score <= su->su_maxscore) |
| 6634 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6635 | /* Check if the word is already there. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6636 | stp = &SUG(*gap, 0); |
| 6637 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6638 | if (STRCMP(stp[i].st_word, goodword) == 0) |
| 6639 | { |
| 6640 | /* Found it. Remember the lowest score. */ |
| 6641 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6642 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6643 | stp[i].st_score = score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6644 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6645 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6646 | break; |
| 6647 | } |
| 6648 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6649 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6650 | { |
| 6651 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6652 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6653 | stp->st_word = vim_strsave(goodword); |
| 6654 | if (stp->st_word != NULL) |
| 6655 | { |
| 6656 | stp->st_score = score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6657 | stp->st_altscore = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6658 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6659 | stp->st_orglen = su->su_badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6660 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6661 | |
| 6662 | /* If we have too many suggestions now, sort the list and keep |
| 6663 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6664 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 6665 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 6666 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6667 | } |
| 6668 | } |
| 6669 | } |
| 6670 | } |
| 6671 | |
| 6672 | /* |
| 6673 | * Add a word to be banned. |
| 6674 | */ |
| 6675 | static void |
| 6676 | add_banned(su, word) |
| 6677 | suginfo_T *su; |
| 6678 | char_u *word; |
| 6679 | { |
| 6680 | char_u *s = vim_strsave(word); |
| 6681 | hash_T hash; |
| 6682 | hashitem_T *hi; |
| 6683 | |
| 6684 | if (s != NULL) |
| 6685 | { |
| 6686 | hash = hash_hash(s); |
| 6687 | hi = hash_lookup(&su->su_banned, s, hash); |
| 6688 | if (HASHITEM_EMPTY(hi)) |
| 6689 | hash_add_item(&su->su_banned, hi, s, hash); |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | /* |
| 6694 | * Return TRUE if a word appears in the list of banned words. |
| 6695 | */ |
| 6696 | static int |
| 6697 | was_banned(su, word) |
| 6698 | suginfo_T *su; |
| 6699 | char_u *word; |
| 6700 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6701 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 6702 | |
| 6703 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | /* |
| 6707 | * Free the banned words in "su". |
| 6708 | */ |
| 6709 | static void |
| 6710 | free_banned(su) |
| 6711 | suginfo_T *su; |
| 6712 | { |
| 6713 | int todo; |
| 6714 | hashitem_T *hi; |
| 6715 | |
| 6716 | todo = su->su_banned.ht_used; |
| 6717 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 6718 | { |
| 6719 | if (!HASHITEM_EMPTY(hi)) |
| 6720 | { |
| 6721 | vim_free(hi->hi_key); |
| 6722 | --todo; |
| 6723 | } |
| 6724 | } |
| 6725 | hash_clear(&su->su_banned); |
| 6726 | } |
| 6727 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6728 | /* |
| 6729 | * Recompute the score if sound-folding is possible. This is slow, |
| 6730 | * thus only done for the final results. |
| 6731 | */ |
| 6732 | static void |
| 6733 | rescore_suggestions(su) |
| 6734 | suginfo_T *su; |
| 6735 | { |
| 6736 | langp_T *lp; |
| 6737 | suggest_T *stp; |
| 6738 | char_u sal_badword[MAXWLEN]; |
| 6739 | int score; |
| 6740 | int i; |
| 6741 | |
| 6742 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6743 | lp->lp_slang != NULL; ++lp) |
| 6744 | { |
| 6745 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 6746 | { |
| 6747 | /* soundfold the bad word */ |
| 6748 | spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword); |
| 6749 | |
| 6750 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6751 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6752 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6753 | if (!stp->st_had_bonus) |
| 6754 | { |
| 6755 | score = spell_sound_score(lp->lp_slang, stp->st_word, |
| 6756 | sal_badword); |
| 6757 | stp->st_score = RESCORE(stp->st_score, score); |
| 6758 | } |
| 6759 | } |
| 6760 | break; |
| 6761 | } |
| 6762 | } |
| 6763 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6764 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6765 | static int |
| 6766 | #ifdef __BORLANDC__ |
| 6767 | _RTLENTRYF |
| 6768 | #endif |
| 6769 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 6770 | |
| 6771 | /* |
| 6772 | * Function given to qsort() to sort the suggestions on st_score. |
| 6773 | */ |
| 6774 | static int |
| 6775 | #ifdef __BORLANDC__ |
| 6776 | _RTLENTRYF |
| 6777 | #endif |
| 6778 | sug_compare(s1, s2) |
| 6779 | const void *s1; |
| 6780 | const void *s2; |
| 6781 | { |
| 6782 | suggest_T *p1 = (suggest_T *)s1; |
| 6783 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6784 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6785 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6786 | if (n == 0) |
| 6787 | return p1->st_altscore - p2->st_altscore; |
| 6788 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | /* |
| 6792 | * Cleanup the suggestions: |
| 6793 | * - Sort on score. |
| 6794 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6795 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6796 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6797 | static int |
| 6798 | cleanup_suggestions(gap, maxscore, keep) |
| 6799 | garray_T *gap; |
| 6800 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6801 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6802 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6803 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6804 | int i; |
| 6805 | |
| 6806 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6807 | 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] | 6808 | |
| 6809 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6810 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6811 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6812 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6813 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6814 | gap->ga_len = keep; |
| 6815 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6816 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6817 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
| 6820 | /* |
| 6821 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 6822 | */ |
| 6823 | static void |
| 6824 | spell_soundfold(slang, inword, res) |
| 6825 | slang_T *slang; |
| 6826 | char_u *inword; |
| 6827 | char_u *res; |
| 6828 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6829 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6830 | char_u word[MAXWLEN]; |
| 6831 | #ifdef FEAT_MBYTE |
| 6832 | int l; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6833 | int found_mbyte = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6834 | #endif |
| 6835 | char_u *s; |
| 6836 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6837 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6838 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6839 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6840 | int n, k = 0; |
| 6841 | int z0; |
| 6842 | int k0; |
| 6843 | int n0; |
| 6844 | int c; |
| 6845 | int pri; |
| 6846 | int p0 = -333; |
| 6847 | int c0; |
| 6848 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6849 | /* Remove accents, if wanted. We actually remove all non-word characters. |
| 6850 | * But keep white space. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6851 | if (slang->sl_rem_accents) |
| 6852 | { |
| 6853 | t = word; |
| 6854 | for (s = inword; *s != NUL; ) |
| 6855 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6856 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6857 | { |
| 6858 | *t++ = ' '; |
| 6859 | s = skipwhite(s); |
| 6860 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6861 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6862 | else if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6863 | { |
| 6864 | l = mb_ptr2len_check(s); |
| 6865 | if (SPELL_ISWORDP(s)) |
| 6866 | { |
| 6867 | mch_memmove(t, s, l); |
| 6868 | t += l; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6869 | if (l > 1) |
| 6870 | found_mbyte = TRUE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6871 | } |
| 6872 | s += l; |
| 6873 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6874 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6875 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6876 | { |
| 6877 | if (SPELL_ISWORDP(s)) |
| 6878 | *t++ = *s; |
| 6879 | ++s; |
| 6880 | } |
| 6881 | } |
| 6882 | *t = NUL; |
| 6883 | } |
| 6884 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6885 | { |
| 6886 | #ifdef FEAT_MBYTE |
| 6887 | if (has_mbyte) |
| 6888 | for (s = inword; *s != NUL; s += l) |
| 6889 | if ((l = mb_ptr2len_check(s)) > 1) |
| 6890 | { |
| 6891 | found_mbyte = TRUE; |
| 6892 | break; |
| 6893 | } |
| 6894 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6895 | STRCPY(word, inword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
| 6898 | #ifdef FEAT_MBYTE |
| 6899 | /* If there are multi-byte characters in the word return it as-is, because |
| 6900 | * the following won't work. */ |
| 6901 | if (found_mbyte) |
| 6902 | { |
| 6903 | STRCPY(res, word); |
| 6904 | return; |
| 6905 | } |
| 6906 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6907 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6908 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6909 | |
| 6910 | /* |
| 6911 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6912 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6913 | * TODO: support for multi-byte chars. |
| 6914 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6915 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6916 | while ((c = word[i]) != NUL) |
| 6917 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6918 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6919 | n = slang->sl_sal_first[c]; |
| 6920 | z0 = 0; |
| 6921 | |
| 6922 | if (n >= 0) |
| 6923 | { |
| 6924 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6925 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6926 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6927 | /* Quickly skip entries that don't match the word. Most |
| 6928 | * entries are less then three chars, optimize for that. */ |
| 6929 | k = smp[n].sm_leadlen; |
| 6930 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6931 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6932 | if (word[i + 1] != s[1]) |
| 6933 | continue; |
| 6934 | if (k > 2) |
| 6935 | { |
| 6936 | for (j = 2; j < k; ++j) |
| 6937 | if (word[i + j] != s[j]) |
| 6938 | break; |
| 6939 | if (j < k) |
| 6940 | continue; |
| 6941 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6944 | if ((pf = smp[n].sm_oneoff) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6945 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6946 | /* Check for match with one of the chars in "sm_oneoff". */ |
| 6947 | while (*pf != NUL && *pf != word[i + k]) |
| 6948 | ++pf; |
| 6949 | if (*pf == NUL) |
| 6950 | continue; |
| 6951 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6952 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6953 | s = smp[n].sm_rules; |
| 6954 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6955 | |
| 6956 | p0 = *s; |
| 6957 | k0 = k; |
| 6958 | while (*s == '-' && k > 1) |
| 6959 | { |
| 6960 | k--; |
| 6961 | s++; |
| 6962 | } |
| 6963 | if (*s == '<') |
| 6964 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6965 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6966 | { |
| 6967 | /* determine priority */ |
| 6968 | pri = *s - '0'; |
| 6969 | s++; |
| 6970 | } |
| 6971 | if (*s == '^' && *(s + 1) == '^') |
| 6972 | s++; |
| 6973 | |
| 6974 | if (*s == NUL |
| 6975 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6976 | && (i == 0 || !(word[i - 1] == ' ' |
| 6977 | || SPELL_ISWORDP(word + i - 1))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6978 | && (*(s + 1) != '$' |
| 6979 | || (!SPELL_ISWORDP(word + i + k0)))) |
| 6980 | || (*s == '$' && i > 0 |
| 6981 | && SPELL_ISWORDP(word + i - 1) |
| 6982 | && (!SPELL_ISWORDP(word + i + k0)))) |
| 6983 | { |
| 6984 | /* search for followup rules, if: */ |
| 6985 | /* followup and k > 1 and NO '-' in searchstring */ |
| 6986 | c0 = word[i + k - 1]; |
| 6987 | n0 = slang->sl_sal_first[c0]; |
| 6988 | |
| 6989 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6990 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6991 | { |
| 6992 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6993 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6994 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6995 | /* Quickly skip entries that don't match the word. |
| 6996 | * */ |
| 6997 | k0 = smp[n0].sm_leadlen; |
| 6998 | if (k0 > 1) |
| 6999 | { |
| 7000 | if (word[i + k] != s[1]) |
| 7001 | continue; |
| 7002 | if (k0 > 2) |
| 7003 | { |
| 7004 | pf = word + i + k + 1; |
| 7005 | for (j = 2; j < k0; ++j) |
| 7006 | if (*pf++ != s[j]) |
| 7007 | break; |
| 7008 | if (j < k0) |
| 7009 | continue; |
| 7010 | } |
| 7011 | } |
| 7012 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7013 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7014 | if ((pf = smp[n0].sm_oneoff) != NULL) |
| 7015 | { |
| 7016 | /* Check for match with one of the chars in |
| 7017 | * "sm_oneoff". */ |
| 7018 | while (*pf != NUL && *pf != word[i + k0]) |
| 7019 | ++pf; |
| 7020 | if (*pf == NUL) |
| 7021 | continue; |
| 7022 | ++k0; |
| 7023 | } |
| 7024 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7025 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7026 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7027 | while (*s == '-') |
| 7028 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7029 | /* "k0" gets NOT reduced because |
| 7030 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7031 | s++; |
| 7032 | } |
| 7033 | if (*s == '<') |
| 7034 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7035 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7036 | { |
| 7037 | p0 = *s - '0'; |
| 7038 | s++; |
| 7039 | } |
| 7040 | |
| 7041 | if (*s == NUL |
| 7042 | /* *s == '^' cuts */ |
| 7043 | || (*s == '$' |
| 7044 | && !SPELL_ISWORDP(word + i + k0))) |
| 7045 | { |
| 7046 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7047 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7048 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7049 | |
| 7050 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7051 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7052 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7053 | /* rule fits; stop search */ |
| 7054 | break; |
| 7055 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7058 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7059 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7063 | s = smp[n].sm_to; |
| 7064 | pf = smp[n].sm_rules; |
| 7065 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7066 | if (p0 == 1 && z == 0) |
| 7067 | { |
| 7068 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7069 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 7070 | || res[reslen - 1] == *s)) |
| 7071 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7072 | z0 = 1; |
| 7073 | z = 1; |
| 7074 | k0 = 0; |
| 7075 | while (*s != NUL && word[i+k0] != NUL) |
| 7076 | { |
| 7077 | word[i + k0] = *s; |
| 7078 | k0++; |
| 7079 | s++; |
| 7080 | } |
| 7081 | if (k > k0) |
| 7082 | mch_memmove(word + i + k0, word + i + k, |
| 7083 | STRLEN(word + i + k) + 1); |
| 7084 | |
| 7085 | /* new "actual letter" */ |
| 7086 | c = word[i]; |
| 7087 | } |
| 7088 | else |
| 7089 | { |
| 7090 | /* no '<' rule used */ |
| 7091 | i += k - 1; |
| 7092 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7093 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7094 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7095 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7096 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7097 | res[reslen] = *s; |
| 7098 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7099 | } |
| 7100 | s++; |
| 7101 | } |
| 7102 | /* new "actual letter" */ |
| 7103 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7104 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7105 | { |
| 7106 | if (c != NUL) |
| 7107 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7108 | res[reslen] = c; |
| 7109 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7110 | } |
| 7111 | mch_memmove(word, word + i + 1, |
| 7112 | STRLEN(word + i + 1) + 1); |
| 7113 | i = 0; |
| 7114 | z0 = 1; |
| 7115 | } |
| 7116 | } |
| 7117 | break; |
| 7118 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7119 | } |
| 7120 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7121 | else if (vim_iswhite(c)) |
| 7122 | { |
| 7123 | c = ' '; |
| 7124 | k = 1; |
| 7125 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7126 | |
| 7127 | if (z0 == 0) |
| 7128 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7129 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 7130 | && (!slang->sl_collapse || reslen == 0 |
| 7131 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7132 | { |
| 7133 | /* condense only double letters */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7134 | res[reslen] = c; |
| 7135 | reslen++; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
| 7138 | i++; |
| 7139 | z = 0; |
| 7140 | k = 0; |
| 7141 | } |
| 7142 | } |
| 7143 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7144 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7147 | /* |
| 7148 | * Return the score for how much words sound different. |
| 7149 | */ |
| 7150 | static int |
| 7151 | spell_sound_score(slang, goodword, badsound) |
| 7152 | slang_T *slang; |
| 7153 | char_u *goodword; /* good word */ |
| 7154 | char_u *badsound; /* sound-folded bad word */ |
| 7155 | { |
| 7156 | char_u fword[MAXWLEN]; |
| 7157 | char_u goodsound[MAXWLEN]; |
| 7158 | int score; |
| 7159 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7160 | /* Case-fold the goodword, needed for sound folding. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7161 | (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN); |
| 7162 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7163 | /* sound-fold the goodword */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7164 | spell_soundfold(slang, fword, goodsound); |
| 7165 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7166 | /* Compute the edit distance-score of the sounds. This is slow but we |
| 7167 | * only do it for a small number of words. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7168 | score = spell_edit_score(badsound, goodsound); |
| 7169 | |
| 7170 | /* Correction: adding/inserting "*" at the start (word starts with vowel) |
| 7171 | * shouldn't be counted so much, vowels halfway the word aren't counted at |
| 7172 | * all. */ |
| 7173 | if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*')) |
| 7174 | score -= SCORE_DEL / 2; |
| 7175 | |
| 7176 | return score; |
| 7177 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7178 | |
| 7179 | /* |
| 7180 | * Compute a score for two sound-a-like words. |
| 7181 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 7182 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 7183 | * avoiding checks that will not be possible. |
| 7184 | */ |
| 7185 | static int |
| 7186 | soundalike_score(goodsound, badsound) |
| 7187 | char_u *goodsound; /* sound-folded good word */ |
| 7188 | char_u *badsound; /* sound-folded bad word */ |
| 7189 | { |
| 7190 | int goodlen = STRLEN(goodsound); |
| 7191 | int badlen = STRLEN(badsound); |
| 7192 | int n; |
| 7193 | char_u *pl, *ps; |
| 7194 | char_u *pl2, *ps2; |
| 7195 | |
| 7196 | /* Return quickly if the lenghts are too different to be fixed by two |
| 7197 | * changes. */ |
| 7198 | n = goodlen - badlen; |
| 7199 | if (n < -2 || n > 2) |
| 7200 | return SCORE_MAXMAX; |
| 7201 | |
| 7202 | if (n > 0) |
| 7203 | { |
| 7204 | pl = goodsound; /* longest */ |
| 7205 | ps = badsound; |
| 7206 | } |
| 7207 | else |
| 7208 | { |
| 7209 | pl = badsound; /* longest */ |
| 7210 | ps = goodsound; |
| 7211 | } |
| 7212 | |
| 7213 | /* Skip over the identical part. */ |
| 7214 | while (*pl == *ps && *pl != NUL) |
| 7215 | { |
| 7216 | ++pl; |
| 7217 | ++ps; |
| 7218 | } |
| 7219 | |
| 7220 | switch (n) |
| 7221 | { |
| 7222 | case -2: |
| 7223 | case 2: |
| 7224 | /* |
| 7225 | * Must delete two characters from "pl". |
| 7226 | */ |
| 7227 | ++pl; /* first delete */ |
| 7228 | while (*pl == *ps) |
| 7229 | { |
| 7230 | ++pl; |
| 7231 | ++ps; |
| 7232 | } |
| 7233 | /* strings must be equal after second delete */ |
| 7234 | if (STRCMP(pl + 1, ps) == 0) |
| 7235 | return SCORE_DEL * 2; |
| 7236 | |
| 7237 | /* Failed to compare. */ |
| 7238 | break; |
| 7239 | |
| 7240 | case -1: |
| 7241 | case 1: |
| 7242 | /* |
| 7243 | * Minimal one delete from "pl" required. |
| 7244 | */ |
| 7245 | |
| 7246 | /* 1: delete */ |
| 7247 | pl2 = pl + 1; |
| 7248 | ps2 = ps; |
| 7249 | while (*pl2 == *ps2) |
| 7250 | { |
| 7251 | if (*pl2 == NUL) /* reached the end */ |
| 7252 | return SCORE_DEL; |
| 7253 | ++pl2; |
| 7254 | ++ps2; |
| 7255 | } |
| 7256 | |
| 7257 | /* 2: delete then swap, then rest must be equal */ |
| 7258 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7259 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
| 7260 | return SCORE_DEL + SCORE_SWAP; |
| 7261 | |
| 7262 | /* 3: delete then substitute, then the rest must be equal */ |
| 7263 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
| 7264 | return SCORE_DEL + SCORE_SUBST; |
| 7265 | |
| 7266 | /* 4: first swap then delete */ |
| 7267 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 7268 | { |
| 7269 | pl2 = pl + 2; /* swap, skip two chars */ |
| 7270 | ps2 = ps + 2; |
| 7271 | while (*pl2 == *ps2) |
| 7272 | { |
| 7273 | ++pl2; |
| 7274 | ++ps2; |
| 7275 | } |
| 7276 | /* delete a char and then strings must be equal */ |
| 7277 | if (STRCMP(pl2 + 1, ps2) == 0) |
| 7278 | return SCORE_SWAP + SCORE_DEL; |
| 7279 | } |
| 7280 | |
| 7281 | /* 5: first substitute then delete */ |
| 7282 | pl2 = pl + 1; /* substitute, skip one char */ |
| 7283 | ps2 = ps + 1; |
| 7284 | while (*pl2 == *ps2) |
| 7285 | { |
| 7286 | ++pl2; |
| 7287 | ++ps2; |
| 7288 | } |
| 7289 | /* delete a char and then strings must be equal */ |
| 7290 | if (STRCMP(pl2 + 1, ps2) == 0) |
| 7291 | return SCORE_SUBST + SCORE_DEL; |
| 7292 | |
| 7293 | /* Failed to compare. */ |
| 7294 | break; |
| 7295 | |
| 7296 | case 0: |
| 7297 | /* |
| 7298 | * Lenghts are equal, thus changes must result in same length: An |
| 7299 | * insert is only possible in combination with a delete. |
| 7300 | * 1: check if for identical strings |
| 7301 | */ |
| 7302 | if (*pl == NUL) |
| 7303 | return 0; |
| 7304 | |
| 7305 | /* 2: swap */ |
| 7306 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 7307 | { |
| 7308 | pl2 = pl + 2; /* swap, skip two chars */ |
| 7309 | ps2 = ps + 2; |
| 7310 | while (*pl2 == *ps2) |
| 7311 | { |
| 7312 | if (*pl2 == NUL) /* reached the end */ |
| 7313 | return SCORE_SWAP; |
| 7314 | ++pl2; |
| 7315 | ++ps2; |
| 7316 | } |
| 7317 | /* 3: swap and swap again */ |
| 7318 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7319 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
| 7320 | return SCORE_SWAP + SCORE_SWAP; |
| 7321 | |
| 7322 | /* 4: swap and substitute */ |
| 7323 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
| 7324 | return SCORE_SWAP + SCORE_SUBST; |
| 7325 | } |
| 7326 | |
| 7327 | /* 5: substitute */ |
| 7328 | pl2 = pl + 1; |
| 7329 | ps2 = ps + 1; |
| 7330 | while (*pl2 == *ps2) |
| 7331 | { |
| 7332 | if (*pl2 == NUL) /* reached the end */ |
| 7333 | return SCORE_SUBST; |
| 7334 | ++pl2; |
| 7335 | ++ps2; |
| 7336 | } |
| 7337 | |
| 7338 | /* 6: substitute and swap */ |
| 7339 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 7340 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
| 7341 | return SCORE_SUBST + SCORE_SWAP; |
| 7342 | |
| 7343 | /* 7: substitute and substitute */ |
| 7344 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
| 7345 | return SCORE_SUBST + SCORE_SUBST; |
| 7346 | |
| 7347 | /* 8: insert then delete */ |
| 7348 | pl2 = pl; |
| 7349 | ps2 = ps + 1; |
| 7350 | while (*pl2 == *ps2) |
| 7351 | { |
| 7352 | ++pl2; |
| 7353 | ++ps2; |
| 7354 | } |
| 7355 | if (STRCMP(pl2 + 1, ps2) == 0) |
| 7356 | return SCORE_INS + SCORE_DEL; |
| 7357 | |
| 7358 | /* 9: delete then insert */ |
| 7359 | pl2 = pl + 1; |
| 7360 | ps2 = ps; |
| 7361 | while (*pl2 == *ps2) |
| 7362 | { |
| 7363 | ++pl2; |
| 7364 | ++ps2; |
| 7365 | } |
| 7366 | if (STRCMP(pl2, ps2 + 1) == 0) |
| 7367 | return SCORE_INS + SCORE_DEL; |
| 7368 | |
| 7369 | /* Failed to compare. */ |
| 7370 | break; |
| 7371 | } |
| 7372 | |
| 7373 | return SCORE_MAXMAX; |
| 7374 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7375 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7376 | /* |
| 7377 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7378 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7379 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7380 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7381 | * It has been converted from C++ to C and modified to support multi-byte |
| 7382 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7383 | */ |
| 7384 | static int |
| 7385 | spell_edit_score(badword, goodword) |
| 7386 | char_u *badword; |
| 7387 | char_u *goodword; |
| 7388 | { |
| 7389 | int *cnt; |
| 7390 | int badlen, goodlen; |
| 7391 | int j, i; |
| 7392 | int t; |
| 7393 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7394 | int pbc, pgc; |
| 7395 | #ifdef FEAT_MBYTE |
| 7396 | char_u *p; |
| 7397 | int wbadword[MAXWLEN]; |
| 7398 | int wgoodword[MAXWLEN]; |
| 7399 | |
| 7400 | if (has_mbyte) |
| 7401 | { |
| 7402 | /* Get the characters from the multi-byte strings and put them in an |
| 7403 | * int array for easy access. */ |
| 7404 | for (p = badword, badlen = 0; *p != NUL; ) |
| 7405 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
| 7406 | ++badlen; |
| 7407 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 7408 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
| 7409 | ++goodlen; |
| 7410 | } |
| 7411 | else |
| 7412 | #endif |
| 7413 | { |
| 7414 | badlen = STRLEN(badword) + 1; |
| 7415 | goodlen = STRLEN(goodword) + 1; |
| 7416 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7417 | |
| 7418 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 7419 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7420 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 7421 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7422 | if (cnt == NULL) |
| 7423 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7424 | |
| 7425 | CNT(0, 0) = 0; |
| 7426 | for (j = 1; j <= goodlen; ++j) |
| 7427 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 7428 | |
| 7429 | for (i = 1; i <= badlen; ++i) |
| 7430 | { |
| 7431 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 7432 | for (j = 1; j <= goodlen; ++j) |
| 7433 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7434 | #ifdef FEAT_MBYTE |
| 7435 | if (has_mbyte) |
| 7436 | { |
| 7437 | bc = wbadword[i - 1]; |
| 7438 | gc = wgoodword[j - 1]; |
| 7439 | } |
| 7440 | else |
| 7441 | #endif |
| 7442 | { |
| 7443 | bc = badword[i - 1]; |
| 7444 | gc = goodword[j - 1]; |
| 7445 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7446 | if (bc == gc) |
| 7447 | CNT(i, j) = CNT(i - 1, j - 1); |
| 7448 | else |
| 7449 | { |
| 7450 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7451 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7452 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 7453 | else |
| 7454 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 7455 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7456 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7457 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7458 | #ifdef FEAT_MBYTE |
| 7459 | if (has_mbyte) |
| 7460 | { |
| 7461 | pbc = wbadword[i - 2]; |
| 7462 | pgc = wgoodword[j - 2]; |
| 7463 | } |
| 7464 | else |
| 7465 | #endif |
| 7466 | { |
| 7467 | pbc = badword[i - 2]; |
| 7468 | pgc = goodword[j - 2]; |
| 7469 | } |
| 7470 | if (bc == pgc && pbc == gc) |
| 7471 | { |
| 7472 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 7473 | if (t < CNT(i, j)) |
| 7474 | CNT(i, j) = t; |
| 7475 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7476 | } |
| 7477 | t = SCORE_DEL + CNT(i - 1, j); |
| 7478 | if (t < CNT(i, j)) |
| 7479 | CNT(i, j) = t; |
| 7480 | t = SCORE_INS + CNT(i, j - 1); |
| 7481 | if (t < CNT(i, j)) |
| 7482 | CNT(i, j) = t; |
| 7483 | } |
| 7484 | } |
| 7485 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7486 | |
| 7487 | i = CNT(badlen - 1, goodlen - 1); |
| 7488 | vim_free(cnt); |
| 7489 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7490 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7491 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7492 | #endif /* FEAT_SYN_HL */ |