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> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 89 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 90 | * <prefcondcnt> <prefcond> ... |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 91 | * |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 92 | * <fileID> 10 bytes "VIMspell08" |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 93 | * <regioncnt> 1 byte number of regions following (8 supported) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 94 | * <regionname> 2 bytes Region name: ca, au, etc. Lower case. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 95 | * First <regionname> is region 1. |
| 96 | * |
| 97 | * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128). |
| 98 | * <charflags> N bytes List of flags (first one is for character 128): |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 99 | * 0x01 word character CF_WORD |
| 100 | * 0x02 upper-case character CF_UPPER |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 101 | * <fcharslen> 2 bytes Number of bytes in <fchars>. |
| 102 | * <fchars> N bytes Folded characters, first one is for character 128. |
| 103 | * |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 104 | * <midwordlen> 2 bytes Number of bytes in <midword>. |
| 105 | * <midword> N bytes Characters that are word characters only when used |
| 106 | * in the middle of a word. |
| 107 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 108 | * <prefcondcnt> 2 bytes Number of <prefcond> items following. |
| 109 | * |
| 110 | * <prefcond> : <condlen> <condstr> |
| 111 | * |
| 112 | * <condlen> 1 byte Length of <condstr>. |
| 113 | * |
| 114 | * <condstr> N bytes Condition for the prefix. |
| 115 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 116 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 117 | * <SUGGEST> : <repcount> <rep> ... |
| 118 | * <salflags> <salcount> <sal> ... |
| 119 | * <maplen> <mapstr> |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 120 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 121 | * <repcount> 2 bytes number of <rep> items, MSB first. |
| 122 | * |
| 123 | * <rep> : <repfromlen> <repfrom> <reptolen> <repto> |
| 124 | * |
| 125 | * <repfromlen> 1 byte length of <repfrom> |
| 126 | * |
| 127 | * <repfrom> N bytes "from" part of replacement |
| 128 | * |
| 129 | * <reptolen> 1 byte length of <repto> |
| 130 | * |
| 131 | * <repto> N bytes "to" part of replacement |
| 132 | * |
| 133 | * <salflags> 1 byte flags for soundsalike conversion: |
| 134 | * SAL_F0LLOWUP |
| 135 | * SAL_COLLAPSE |
| 136 | * SAL_REM_ACCENTS |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 137 | * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL |
| 138 | * |
| 139 | * <salcount> 2 bytes number of <sal> items following |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 140 | * |
| 141 | * <sal> : <salfromlen> <salfrom> <saltolen> <salto> |
| 142 | * |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 143 | * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 144 | * |
| 145 | * <salfrom> N bytes "from" part of soundsalike |
| 146 | * |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 147 | * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 148 | * |
| 149 | * <salto> N bytes "to" part of soundsalike |
| 150 | * |
| 151 | * <maplen> 2 bytes length of <mapstr>, MSB first |
| 152 | * |
| 153 | * <mapstr> N bytes String with sequences of similar characters, |
| 154 | * separated by slashes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 155 | * |
| 156 | * |
| 157 | * <LWORDTREE>: <wordtree> |
| 158 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 159 | * <KWORDTREE>: <wordtree> |
| 160 | * |
| 161 | * <PREFIXTREE>: <wordtree> |
| 162 | * |
| 163 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 164 | * <wordtree>: <nodecount> <nodedata> ... |
| 165 | * |
| 166 | * <nodecount> 4 bytes Number of nodes following. MSB first. |
| 167 | * |
| 168 | * <nodedata>: <siblingcount> <sibling> ... |
| 169 | * |
| 170 | * <siblingcount> 1 byte Number of siblings in this node. The siblings |
| 171 | * follow in sorted order. |
| 172 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 173 | * <sibling>: <byte> [ <nodeidx> <xbyte> |
| 174 | * | <flags> [<region>] [<prefixID>] |
| 175 | * | <prefixID> <prefcondnr> ] |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 176 | * |
| 177 | * <byte> 1 byte Byte value of the sibling. Special cases: |
| 178 | * BY_NOFLAGS: End of word without flags and for all |
| 179 | * regions. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 180 | * For PREFIXTREE <prefixID> and |
| 181 | * <prefcondnr> follow. |
| 182 | * BY_FLAGS: End of word, <flags> follow. |
| 183 | * For PREFIXTREE <prefixID> and |
| 184 | * <prefcondnr> follow for rare prefix. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 185 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
| 186 | * and <xbyte> follow. |
| 187 | * |
| 188 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 189 | * |
| 190 | * <xbyte> 1 byte byte value of the sibling. |
| 191 | * |
| 192 | * <flags> 1 byte bitmask of: |
| 193 | * WF_ALLCAP word must have only capitals |
| 194 | * WF_ONECAP first char of word must be capital |
| 195 | * WF_RARE rare word |
| 196 | * WF_REGION <region> follows |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 197 | * WF_PFX <prefixID> follows |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 198 | * |
| 199 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 200 | * omitted it's valid in all regions. |
| 201 | * Lowest bit is for region 1. |
| 202 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 203 | * <prefixID> 1 byte ID of prefix that can be used with this word. For |
| 204 | * PREFIXTREE used for the required prefix ID. |
| 205 | * |
| 206 | * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list |
| 207 | * from HEADER. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 208 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 209 | * All text characters are in 'encoding', but stored as single bytes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 210 | */ |
| 211 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 212 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 213 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 214 | #endif |
| 215 | |
| 216 | #include "vim.h" |
| 217 | |
| 218 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 219 | |
| 220 | #ifdef HAVE_FCNTL_H |
| 221 | # include <fcntl.h> |
| 222 | #endif |
| 223 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 224 | #define MAXWLEN 250 /* Assume max. word len is this many bytes. |
| 225 | Some places assume a word length fits in a |
| 226 | byte, thus it can't be above 255. */ |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 227 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 228 | /* Type used for indexes in the word tree need to be at least 3 bytes. If int |
| 229 | * is 8 bytes we could use something smaller, but what? */ |
| 230 | #if SIZEOF_INT > 2 |
| 231 | typedef int idx_T; |
| 232 | #else |
| 233 | typedef long idx_T; |
| 234 | #endif |
| 235 | |
| 236 | /* Flags used for a word. Only the lowest byte can be used, the region byte |
| 237 | * comes above it. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 238 | #define WF_REGION 0x01 /* region byte follows */ |
| 239 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 240 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 241 | #define WF_RARE 0x08 /* rare word */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 242 | #define WF_BANNED 0x10 /* bad word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 243 | #define WF_PFX 0x20 /* prefix ID list follows */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 244 | #define WF_KEEPCAP 0x80 /* keep-case word */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 245 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 246 | #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 247 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 248 | #define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed |
| 249 | prefix; must be above prefixID (one byte) |
| 250 | and prefcondnr (two bytes) */ |
| 251 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 252 | #define BY_NOFLAGS 0 /* end of word without flags or region */ |
| 253 | #define BY_FLAGS 1 /* end of word, flag byte follows */ |
| 254 | #define BY_INDEX 2 /* child is shared, index follows */ |
| 255 | #define BY_SPECIAL BY_INDEX /* hightest special byte value */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 256 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 257 | /* 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] | 258 | * and si_sal. Not for sl_sal! |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 259 | * One replacement: from "ft_from" to "ft_to". */ |
| 260 | typedef struct fromto_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 261 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 262 | char_u *ft_from; |
| 263 | char_u *ft_to; |
| 264 | } fromto_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 265 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 266 | /* Info from "SAL" entries in ".aff" file used in sl_sal. |
| 267 | * The info is split for quick processing by spell_soundfold(). |
| 268 | * Note that "sm_oneof" and "sm_rules" point into sm_lead. */ |
| 269 | typedef struct salitem_S |
| 270 | { |
| 271 | char_u *sm_lead; /* leading letters */ |
| 272 | int sm_leadlen; /* length of "sm_lead" */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 273 | char_u *sm_oneof; /* letters from () or NULL */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 274 | char_u *sm_rules; /* rules like ^, $, priority */ |
| 275 | char_u *sm_to; /* replacement. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 276 | #ifdef FEAT_MBYTE |
| 277 | int *sm_lead_w; /* wide character copy of "sm_lead" */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 278 | int *sm_oneof_w; /* wide character copy of "sm_oneof" */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 279 | int *sm_to_w; /* wide character copy of "sm_to" */ |
| 280 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 281 | } salitem_T; |
| 282 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 283 | #ifdef FEAT_MBYTE |
| 284 | typedef int salfirst_T; |
| 285 | #else |
| 286 | typedef short salfirst_T; |
| 287 | #endif |
| 288 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 289 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 290 | * Structure used to store words and other info for one language, loaded from |
| 291 | * a .spl file. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 292 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 293 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 294 | * |
| 295 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 296 | * the number of possible bytes, sorted on byte value: |
| 297 | * <len> <byte1> <byte2> ... |
| 298 | * The "idxs" array stores the index of the child node corresponding to the |
| 299 | * byte in "byts". |
| 300 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
| 301 | * the flags and region for the word. There may be several zeros in sequence |
| 302 | * for alternative flag/region combinations. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 303 | */ |
| 304 | typedef struct slang_S slang_T; |
| 305 | struct slang_S |
| 306 | { |
| 307 | slang_T *sl_next; /* next language */ |
| 308 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 309 | char_u *sl_fname; /* name of .spl file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 310 | int sl_add; /* TRUE if it's a .add file. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 311 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 312 | char_u *sl_fbyts; /* case-folded word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 313 | idx_T *sl_fidxs; /* case-folded word indexes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 314 | char_u *sl_kbyts; /* keep-case word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 315 | idx_T *sl_kidxs; /* keep-case word indexes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 316 | char_u *sl_pbyts; /* prefix tree word bytes */ |
| 317 | idx_T *sl_pidxs; /* prefix tree word indexes */ |
| 318 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 319 | 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] | 320 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 321 | char_u *sl_midword; /* MIDWORD string or NULL */ |
| 322 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 323 | int sl_prefixcnt; /* number of items in "sl_prefprog" */ |
| 324 | regprog_T **sl_prefprog; /* table with regprogs for prefixes */ |
| 325 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 326 | garray_T sl_rep; /* list of fromto_T entries from REP lines */ |
| 327 | short sl_rep_first[256]; /* indexes where byte first appears, -1 if |
| 328 | there is none */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 329 | garray_T sl_sal; /* list of salitem_T entries from SAL lines */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 330 | salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 331 | there is none */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 332 | int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items: |
| 333 | * "sl_sal_first" maps chars, when has_mbyte |
| 334 | * "sl_sal" is a list of wide char lists. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 335 | int sl_followup; /* SAL followup */ |
| 336 | int sl_collapse; /* SAL collapse_result */ |
| 337 | int sl_rem_accents; /* SAL remove_accents */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 338 | int sl_has_map; /* TRUE if there is a MAP line */ |
| 339 | #ifdef FEAT_MBYTE |
| 340 | hashtab_T sl_map_hash; /* MAP for multi-byte chars */ |
| 341 | int sl_map_array[256]; /* MAP for first 256 chars */ |
| 342 | #else |
| 343 | char_u sl_map_array[256]; /* MAP for first 256 chars */ |
| 344 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 347 | /* First language that is loaded, start of the linked list of loaded |
| 348 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 349 | static slang_T *first_lang = NULL; |
| 350 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 351 | /* Flags used in .spl file for soundsalike flags. */ |
| 352 | #define SAL_F0LLOWUP 1 |
| 353 | #define SAL_COLLAPSE 2 |
| 354 | #define SAL_REM_ACCENTS 4 |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 355 | #define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 356 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 357 | /* |
| 358 | * Structure used in "b_langp", filled from 'spelllang'. |
| 359 | */ |
| 360 | typedef struct langp_S |
| 361 | { |
| 362 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 363 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 364 | } langp_T; |
| 365 | |
| 366 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 367 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 368 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 369 | |
| 370 | /* Result values. Lower number is accepted over higher one. */ |
| 371 | #define SP_BANNED -1 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 372 | #define SP_OK 0 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 373 | #define SP_RARE 1 |
| 374 | #define SP_LOCAL 2 |
| 375 | #define SP_BAD 3 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 376 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 377 | #define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 378 | #define VIMSPELLMAGICL 10 |
| 379 | |
| 380 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 381 | * Information used when looking for suggestions. |
| 382 | */ |
| 383 | typedef struct suginfo_S |
| 384 | { |
| 385 | garray_T su_ga; /* suggestions, contains "suggest_T" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 386 | int su_maxcount; /* max. number of suggestions displayed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 387 | int su_maxscore; /* maximum score for adding to su_ga */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 388 | garray_T su_sga; /* like su_ga, sound-folded scoring */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 389 | char_u *su_badptr; /* start of bad word in line */ |
| 390 | int su_badlen; /* length of detected bad word in line */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 391 | int su_badflags; /* caps flags for bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 392 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 393 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| 394 | hashtab_T su_banned; /* table with banned words */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 395 | } suginfo_T; |
| 396 | |
| 397 | /* One word suggestion. Used in "si_ga". */ |
| 398 | typedef struct suggest_S |
| 399 | { |
| 400 | char_u *st_word; /* suggested word, allocated string */ |
| 401 | int st_orglen; /* length of replaced text */ |
| 402 | int st_score; /* lower is better */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 403 | int st_altscore; /* used when st_score compares equal */ |
| 404 | int st_salscore; /* st_score is for soundalike */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 405 | int st_had_bonus; /* bonus already included in score */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 406 | } suggest_T; |
| 407 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 408 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 409 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 410 | /* Number of suggestions kept when cleaning up. When rescore_suggestions() is |
| 411 | * called the score may change, thus we need to keep more than what is |
| 412 | * displayed. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 413 | #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 414 | |
| 415 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 416 | * of suggestions that are not going to be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 417 | #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 418 | |
| 419 | /* score for various changes */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 420 | #define SCORE_SPLIT 149 /* split bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 421 | #define SCORE_ICASE 52 /* slightly different case */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 422 | #define SCORE_REGION 70 /* word is for different region */ |
| 423 | #define SCORE_RARE 180 /* rare word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 424 | #define SCORE_SWAP 90 /* swap two characters */ |
| 425 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| 426 | #define SCORE_REP 87 /* REP replacement */ |
| 427 | #define SCORE_SUBST 93 /* substitute a character */ |
| 428 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 429 | #define SCORE_DEL 94 /* delete a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 430 | #define SCORE_DELDUP 64 /* delete a duplicated character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 431 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 432 | #define SCORE_INSDUP 66 /* insert a duplicate character */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 433 | #define SCORE_NONWORD 103 /* change non-word to word char */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 434 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 435 | #define SCORE_FILE 30 /* suggestion from a file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 436 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 437 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 438 | |
| 439 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 440 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 441 | |
| 442 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 443 | * Structure to store info for word matching. |
| 444 | */ |
| 445 | typedef struct matchinf_S |
| 446 | { |
| 447 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 448 | |
| 449 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 450 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 451 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 452 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 453 | char_u *mi_cend; /* char after what was used for |
| 454 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 455 | |
| 456 | /* case-folded text */ |
| 457 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 458 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 459 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 460 | /* for when checking word after a prefix */ |
| 461 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 462 | prefixID/condition */ |
| 463 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 464 | int mi_prefixlen; /* byte length of prefix */ |
| 465 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 466 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 467 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 468 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 469 | buf_T *mi_buf; /* buffer being checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 470 | } matchinf_T; |
| 471 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 472 | /* |
| 473 | * The tables used for recognizing word characters according to spelling. |
| 474 | * These are only used for the first 256 characters of 'encoding'. |
| 475 | */ |
| 476 | typedef struct spelltab_S |
| 477 | { |
| 478 | char_u st_isw[256]; /* flags: is word char */ |
| 479 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 480 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 481 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 482 | } spelltab_T; |
| 483 | |
| 484 | static spelltab_T spelltab; |
| 485 | static int did_set_spelltab; |
| 486 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 487 | #define CF_WORD 0x01 |
| 488 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 489 | |
| 490 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 491 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 492 | static int spell_iswordp __ARGS((char_u *p, buf_T *buf)); |
| 493 | static int spell_iswordp_nmw __ARGS((char_u *p)); |
| 494 | #ifdef FEAT_MBYTE |
| 495 | static int spell_iswordp_w __ARGS((int *p, buf_T *buf)); |
| 496 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 497 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 498 | |
| 499 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 500 | * Return TRUE if "p" points to a word character. Like spell_iswordp() but |
| 501 | * without the special handling of a single quote. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 502 | * Checking for a word character is done very often, avoid the function call |
| 503 | * overhead. |
| 504 | */ |
| 505 | #ifdef FEAT_MBYTE |
| 506 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 507 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 508 | #else |
| 509 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 510 | #endif |
| 511 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 512 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 513 | * 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] | 514 | */ |
| 515 | typedef enum |
| 516 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 517 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 518 | * ends); if badword ends there is a match, otherwise |
| 519 | * try splitting word. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 520 | STATE_NOPREFIX, /* try without prefix */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 521 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 522 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 523 | STATE_PLAIN, /* Use each byte of the node. */ |
| 524 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 525 | STATE_INS, /* Insert a byte in the bad word. */ |
| 526 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 527 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 528 | STATE_SWAP3, /* Swap two characters over three. */ |
| 529 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 530 | STATE_UNROT3L, /* Undo rotate three characters left */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 531 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 532 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 533 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 534 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 535 | STATE_FINAL /* End of this node. */ |
| 536 | } state_T; |
| 537 | |
| 538 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 539 | * Struct to keep the state at each level in suggest_try_change(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 540 | */ |
| 541 | typedef struct trystate_S |
| 542 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 543 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 544 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 545 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 546 | short ts_curi; /* index in list of child nodes */ |
| 547 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 548 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 549 | char_u ts_twordlen; /* valid length of tword[] */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 550 | char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE |
| 551 | * or NOPREFIX */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 552 | #ifdef FEAT_MBYTE |
| 553 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 554 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 555 | char_u ts_isdiff; /* DIFF_ values */ |
| 556 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 557 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 558 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 559 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 560 | char_u ts_save_badflags; /* su_badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 561 | } trystate_T; |
| 562 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 563 | /* values for ts_isdiff */ |
| 564 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 565 | #define DIFF_YES 1 /* different byte found */ |
| 566 | #define DIFF_INSERT 2 /* inserting character */ |
| 567 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 568 | /* special values ts_prefixdepth */ |
| 569 | #define PREFIXTREE 0xfe /* walking through the prefix tree */ |
| 570 | #define NOPREFIX 0xff /* not using prefixes */ |
| 571 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 572 | /* mode values for find_word */ |
| 573 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 574 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 575 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 576 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 577 | /* values for read_cnt_string() */ |
| 578 | #define ERR_NOMEM -1 |
| 579 | #define ERR_TRUNC -2 |
| 580 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 581 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 582 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 583 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 584 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 585 | static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 586 | static void find_prefix __ARGS((matchinf_T *mip)); |
| 587 | static int fold_more __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 588 | static int spell_valid_case __ARGS((int origflags, int treeflags)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 589 | static int no_spell_checking __ARGS((void)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 590 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 591 | static char_u *spell_enc __ARGS((void)); |
| 592 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 593 | static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent)); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 594 | static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *errp)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 595 | #ifdef FEAT_MBYTE |
| 596 | static int *mb_str2wide __ARGS((char_u *s)); |
| 597 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 598 | static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr)); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 599 | static void clear_midword __ARGS((buf_T *buf)); |
| 600 | static void use_midword __ARGS((slang_T *lp, buf_T *buf)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 601 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 602 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 603 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 604 | 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] | 605 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 606 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 607 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 608 | static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 609 | #ifdef FEAT_EVAL |
| 610 | static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr)); |
| 611 | #endif |
| 612 | static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname)); |
| 613 | static void spell_suggest_intern __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 614 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 615 | 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] | 616 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 617 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 618 | static void suggest_try_change __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 619 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
| 620 | 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] | 621 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 622 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 623 | static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 624 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 625 | 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] | 626 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 627 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 628 | static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 629 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 630 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 631 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 632 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 633 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 634 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res)); |
| 635 | static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| 636 | static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 637 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 638 | static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 639 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 640 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 641 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 642 | static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum)); |
| 643 | static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 644 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 645 | /* |
| 646 | * Use our own character-case definitions, because the current locale may |
| 647 | * differ from what the .spl file uses. |
| 648 | * These must not be called with negative number! |
| 649 | */ |
| 650 | #ifndef FEAT_MBYTE |
| 651 | /* Non-multi-byte implementation. */ |
| 652 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 653 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 654 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 655 | #else |
| 656 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 657 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 658 | * the "w" library function for characters above 255 if available. */ |
| 659 | # ifdef HAVE_TOWLOWER |
| 660 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 661 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 662 | # else |
| 663 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 664 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 665 | # endif |
| 666 | |
| 667 | # ifdef HAVE_TOWUPPER |
| 668 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 669 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 670 | # else |
| 671 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 672 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 673 | # endif |
| 674 | |
| 675 | # ifdef HAVE_ISWUPPER |
| 676 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 677 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 678 | # else |
| 679 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 680 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 681 | # endif |
| 682 | #endif |
| 683 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 684 | |
| 685 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 686 | |
| 687 | /* |
| 688 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 689 | * "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] | 690 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 691 | * or when it's OK it remains unchanged. |
| 692 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 693 | * |
| 694 | * "sug" is normally NULL. When looking for suggestions it points to |
| 695 | * suginfo_T. It's passed as a void pointer to keep the struct local. |
| 696 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 697 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 698 | * caller can skip over the word. |
| 699 | */ |
| 700 | int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 701 | spell_check(wp, ptr, attrp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 702 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 703 | char_u *ptr; |
| 704 | int *attrp; |
| 705 | { |
| 706 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 707 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 708 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 709 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 710 | /* A word never starts at a space or a control character. Return quickly |
| 711 | * then, skipping over the character. */ |
| 712 | if (*ptr <= ' ') |
| 713 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 714 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 715 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 716 | * 0X99FF. But when a word character follows do check spelling to find |
| 717 | * "3GPP". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 718 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 719 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 720 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 721 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 722 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 723 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 724 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 725 | nrlen = mi.mi_end - ptr; |
| 726 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 727 | if (!spell_iswordp(mi.mi_end, wp->w_buffer)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 728 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 729 | |
| 730 | /* Try including the digits in the word. */ |
| 731 | mi.mi_fend = ptr + nrlen; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 732 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 733 | else |
| 734 | mi.mi_fend = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 735 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 736 | /* Find the normal end of the word (until the next non-word character). */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 737 | mi.mi_word = ptr; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 738 | if (spell_iswordp(mi.mi_fend, wp->w_buffer)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 739 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 740 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 741 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 742 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 743 | } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 744 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 745 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 746 | /* We always use the characters up to the next non-word character, |
| 747 | * also for bad words. */ |
| 748 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 749 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 750 | /* Check caps type later. */ |
| 751 | mi.mi_capflags = 0; |
| 752 | mi.mi_cend = NULL; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 753 | mi.mi_buf = wp->w_buffer; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 754 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 755 | /* Include one non-word character so that we can check for the |
| 756 | * word end. */ |
| 757 | if (*mi.mi_fend != NUL) |
| 758 | mb_ptr_adv(mi.mi_fend); |
| 759 | |
| 760 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 761 | MAXWLEN + 1); |
| 762 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 763 | |
| 764 | /* The word is bad unless we recognize it. */ |
| 765 | mi.mi_result = SP_BAD; |
| 766 | |
| 767 | /* |
| 768 | * Loop over the languages specified in 'spelllang'. |
| 769 | * We check them all, because a matching word may be longer than an |
| 770 | * already found matching word. |
| 771 | */ |
| 772 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 773 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 774 | { |
| 775 | /* Check for a matching word in case-folded words. */ |
| 776 | find_word(&mi, FIND_FOLDWORD); |
| 777 | |
| 778 | /* Check for a matching word in keep-case words. */ |
| 779 | find_word(&mi, FIND_KEEPWORD); |
| 780 | |
| 781 | /* Check for matching prefixes. */ |
| 782 | find_prefix(&mi); |
| 783 | } |
| 784 | |
| 785 | if (mi.mi_result != SP_OK) |
| 786 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 787 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 788 | * rare and local words, e.g., "3GPP". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 789 | if (nrlen > 0) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 790 | { |
| 791 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 792 | return nrlen; |
| 793 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 794 | |
| 795 | /* When we are at a non-word character there is no error, just |
| 796 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 797 | else if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 798 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 799 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 800 | if (has_mbyte) |
| 801 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 802 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 803 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 804 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 805 | |
| 806 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 807 | *attrp = highlight_attr[HLF_SPB]; |
| 808 | else if (mi.mi_result == SP_RARE) |
| 809 | *attrp = highlight_attr[HLF_SPR]; |
| 810 | else |
| 811 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 814 | return (int)(mi.mi_end - ptr); |
| 815 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 816 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 817 | /* |
| 818 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 819 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 820 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 821 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 822 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 823 | * |
| 824 | * For a match mip->mi_result is updated. |
| 825 | */ |
| 826 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 827 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 828 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 829 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 830 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 831 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 832 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 833 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 834 | int endidxcnt = 0; |
| 835 | int len; |
| 836 | int wlen = 0; |
| 837 | int flen; |
| 838 | int c; |
| 839 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 840 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 841 | #ifdef FEAT_MBYTE |
| 842 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 843 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 844 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 845 | int res = SP_BAD; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 846 | slang_T *slang = mip->mi_lp->lp_slang; |
| 847 | unsigned flags; |
| 848 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 849 | idx_T *idxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 850 | int prefid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 851 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 852 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 853 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 854 | /* Check for word with matching case in keep-case tree. */ |
| 855 | ptr = mip->mi_word; |
| 856 | flen = 9999; /* no case folding, always enough bytes */ |
| 857 | byts = slang->sl_kbyts; |
| 858 | idxs = slang->sl_kidxs; |
| 859 | } |
| 860 | else |
| 861 | { |
| 862 | /* Check for case-folded in case-folded tree. */ |
| 863 | ptr = mip->mi_fword; |
| 864 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 865 | byts = slang->sl_fbyts; |
| 866 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 867 | |
| 868 | if (mode == FIND_PREFIX) |
| 869 | { |
| 870 | /* Skip over the prefix. */ |
| 871 | wlen = mip->mi_prefixlen; |
| 872 | flen -= mip->mi_prefixlen; |
| 873 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 876 | if (byts == NULL) |
| 877 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 878 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 879 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 880 | * Repeat advancing in the tree until: |
| 881 | * - there is a byte that doesn't match, |
| 882 | * - we reach the end of the tree, |
| 883 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 884 | */ |
| 885 | for (;;) |
| 886 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 887 | if (flen <= 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 888 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 889 | |
| 890 | len = byts[arridx++]; |
| 891 | |
| 892 | /* If the first possible byte is a zero the word could end here. |
| 893 | * Remember this index, we first check for the longest word. */ |
| 894 | if (byts[arridx] == 0) |
| 895 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 896 | if (endidxcnt == MAXWLEN) |
| 897 | { |
| 898 | /* Must be a corrupted spell file. */ |
| 899 | EMSG(_(e_format)); |
| 900 | return; |
| 901 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 902 | endlen[endidxcnt] = wlen; |
| 903 | endidx[endidxcnt++] = arridx++; |
| 904 | --len; |
| 905 | |
| 906 | /* Skip over the zeros, there can be several flag/region |
| 907 | * combinations. */ |
| 908 | while (len > 0 && byts[arridx] == 0) |
| 909 | { |
| 910 | ++arridx; |
| 911 | --len; |
| 912 | } |
| 913 | if (len == 0) |
| 914 | break; /* no children, word must end here */ |
| 915 | } |
| 916 | |
| 917 | /* Stop looking at end of the line. */ |
| 918 | if (ptr[wlen] == NUL) |
| 919 | break; |
| 920 | |
| 921 | /* Perform a binary search in the list of accepted bytes. */ |
| 922 | c = ptr[wlen]; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 923 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 924 | c = ' '; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 925 | lo = arridx; |
| 926 | hi = arridx + len - 1; |
| 927 | while (lo < hi) |
| 928 | { |
| 929 | m = (lo + hi) / 2; |
| 930 | if (byts[m] > c) |
| 931 | hi = m - 1; |
| 932 | else if (byts[m] < c) |
| 933 | lo = m + 1; |
| 934 | else |
| 935 | { |
| 936 | lo = hi = m; |
| 937 | break; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /* Stop if there is no matching byte. */ |
| 942 | if (hi < lo || byts[lo] != c) |
| 943 | break; |
| 944 | |
| 945 | /* Continue at the child (if there is one). */ |
| 946 | arridx = idxs[lo]; |
| 947 | ++wlen; |
| 948 | --flen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 949 | |
| 950 | /* One space in the good word may stand for several spaces in the |
| 951 | * checked word. */ |
| 952 | if (c == ' ') |
| 953 | { |
| 954 | for (;;) |
| 955 | { |
| 956 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 957 | flen = fold_more(mip); |
| 958 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 959 | break; |
| 960 | ++wlen; |
| 961 | --flen; |
| 962 | } |
| 963 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Verify that one of the possible endings is valid. Try the longest |
| 968 | * first. |
| 969 | */ |
| 970 | while (endidxcnt > 0) |
| 971 | { |
| 972 | --endidxcnt; |
| 973 | arridx = endidx[endidxcnt]; |
| 974 | wlen = endlen[endidxcnt]; |
| 975 | |
| 976 | #ifdef FEAT_MBYTE |
| 977 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 978 | continue; /* not at first byte of character */ |
| 979 | #endif |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 980 | if (spell_iswordp(ptr + wlen, mip->mi_buf)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 981 | continue; /* next char is a word character */ |
| 982 | |
| 983 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 984 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 985 | { |
| 986 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 987 | * when folding case. This can be slow, take a shortcut when the |
| 988 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 989 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 990 | if (STRNCMP(ptr, p, wlen) != 0) |
| 991 | { |
| 992 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 993 | mb_ptr_adv(p); |
| 994 | wlen = p - mip->mi_word; |
| 995 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 996 | } |
| 997 | #endif |
| 998 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 999 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 1000 | * prefix ID. |
| 1001 | * Repeat this if there are more flags/region alternatives until there |
| 1002 | * is a match. */ |
| 1003 | res = SP_BAD; |
| 1004 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 1005 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1006 | { |
| 1007 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1008 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1009 | /* For the fold-case tree check that the case of the checked word |
| 1010 | * matches with what the word in the tree requires. |
| 1011 | * For keep-case tree the case is always right. For prefixes we |
| 1012 | * don't bother to check. */ |
| 1013 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1014 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1015 | if (mip->mi_cend != mip->mi_word + wlen) |
| 1016 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1017 | /* mi_capflags was set for a different word length, need |
| 1018 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1019 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1020 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1023 | if (mip->mi_capflags == WF_KEEPCAP |
| 1024 | || !spell_valid_case(mip->mi_capflags, flags)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1025 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1028 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 1029 | * check the prefix ID and the condition. Do that for the list at |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1030 | * mip->mi_prefarridx that find_prefix() filled. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1031 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1032 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1033 | /* The prefix ID is stored two bytes above the flags. */ |
| 1034 | prefid = (unsigned)flags >> 16; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1035 | c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1036 | prefid, mip->mi_fword + mip->mi_prefixlen, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1037 | slang); |
| 1038 | if (c == 0) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1039 | continue; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Use the WF_RARE flag for a rare prefix. */ |
| 1042 | if (c & WF_RAREPFX) |
| 1043 | flags |= WF_RARE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | if (flags & WF_BANNED) |
| 1047 | res = SP_BANNED; |
| 1048 | else if (flags & WF_REGION) |
| 1049 | { |
| 1050 | /* Check region. */ |
| 1051 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 1052 | res = SP_OK; |
| 1053 | else |
| 1054 | res = SP_LOCAL; |
| 1055 | } |
| 1056 | else if (flags & WF_RARE) |
| 1057 | res = SP_RARE; |
| 1058 | else |
| 1059 | res = SP_OK; |
| 1060 | |
| 1061 | /* Always use the longest match and the best result. */ |
| 1062 | if (mip->mi_result > res) |
| 1063 | { |
| 1064 | mip->mi_result = res; |
| 1065 | mip->mi_end = mip->mi_word + wlen; |
| 1066 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1067 | else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1068 | mip->mi_end = mip->mi_word + wlen; |
| 1069 | |
| 1070 | if (res == SP_OK) |
| 1071 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1074 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1075 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1076 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1079 | /* |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1080 | * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches |
| 1081 | * with the prefix ID "prefid" for the word "word". |
| 1082 | * The WF_RAREPFX flag is included in the return value for a rare prefix. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1083 | */ |
| 1084 | static int |
| 1085 | valid_word_prefix(totprefcnt, arridx, prefid, word, slang) |
| 1086 | int totprefcnt; /* nr of prefix IDs */ |
| 1087 | int arridx; /* idx in sl_pidxs[] */ |
| 1088 | int prefid; |
| 1089 | char_u *word; |
| 1090 | slang_T *slang; |
| 1091 | { |
| 1092 | int prefcnt; |
| 1093 | int pidx; |
| 1094 | regprog_T *rp; |
| 1095 | regmatch_T regmatch; |
| 1096 | |
| 1097 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1098 | { |
| 1099 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1100 | |
| 1101 | /* Check the prefix ID. */ |
| 1102 | if (prefid != (pidx & 0xff)) |
| 1103 | continue; |
| 1104 | |
| 1105 | /* Check the condition, if there is one. The condition index is |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1106 | * stored in the two bytes above the prefix ID byte. */ |
| 1107 | rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1108 | if (rp != NULL) |
| 1109 | { |
| 1110 | regmatch.regprog = rp; |
| 1111 | regmatch.rm_ic = FALSE; |
| 1112 | if (!vim_regexec(®match, word, 0)) |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1116 | /* It's a match! Return the WF_RAREPFX flag. */ |
| 1117 | return pidx; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1118 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1119 | return 0; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1123 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1124 | * If it does, then check the following word. |
| 1125 | * |
| 1126 | * For a match mip->mi_result is updated. |
| 1127 | */ |
| 1128 | static void |
| 1129 | find_prefix(mip) |
| 1130 | matchinf_T *mip; |
| 1131 | { |
| 1132 | idx_T arridx = 0; |
| 1133 | int len; |
| 1134 | int wlen = 0; |
| 1135 | int flen; |
| 1136 | int c; |
| 1137 | char_u *ptr; |
| 1138 | idx_T lo, hi, m; |
| 1139 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1140 | char_u *byts; |
| 1141 | idx_T *idxs; |
| 1142 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1143 | byts = slang->sl_pbyts; |
| 1144 | if (byts == NULL) |
| 1145 | return; /* array is empty */ |
| 1146 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1147 | /* We use the case-folded word here, since prefixes are always |
| 1148 | * case-folded. */ |
| 1149 | ptr = mip->mi_fword; |
| 1150 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1151 | idxs = slang->sl_pidxs; |
| 1152 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * Repeat advancing in the tree until: |
| 1155 | * - there is a byte that doesn't match, |
| 1156 | * - we reach the end of the tree, |
| 1157 | * - or we reach the end of the line. |
| 1158 | */ |
| 1159 | for (;;) |
| 1160 | { |
| 1161 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1162 | flen = fold_more(mip); |
| 1163 | |
| 1164 | len = byts[arridx++]; |
| 1165 | |
| 1166 | /* If the first possible byte is a zero the prefix could end here. |
| 1167 | * Check if the following word matches and supports the prefix. */ |
| 1168 | if (byts[arridx] == 0) |
| 1169 | { |
| 1170 | /* There can be several prefixes with different conditions. We |
| 1171 | * try them all, since we don't know which one will give the |
| 1172 | * longest match. The word is the same each time, pass the list |
| 1173 | * of possible prefixes to find_word(). */ |
| 1174 | mip->mi_prefarridx = arridx; |
| 1175 | mip->mi_prefcnt = len; |
| 1176 | while (len > 0 && byts[arridx] == 0) |
| 1177 | { |
| 1178 | ++arridx; |
| 1179 | --len; |
| 1180 | } |
| 1181 | mip->mi_prefcnt -= len; |
| 1182 | |
| 1183 | /* Find the word that comes after the prefix. */ |
| 1184 | mip->mi_prefixlen = wlen; |
| 1185 | find_word(mip, FIND_PREFIX); |
| 1186 | |
| 1187 | |
| 1188 | if (len == 0) |
| 1189 | break; /* no children, word must end here */ |
| 1190 | } |
| 1191 | |
| 1192 | /* Stop looking at end of the line. */ |
| 1193 | if (ptr[wlen] == NUL) |
| 1194 | break; |
| 1195 | |
| 1196 | /* Perform a binary search in the list of accepted bytes. */ |
| 1197 | c = ptr[wlen]; |
| 1198 | lo = arridx; |
| 1199 | hi = arridx + len - 1; |
| 1200 | while (lo < hi) |
| 1201 | { |
| 1202 | m = (lo + hi) / 2; |
| 1203 | if (byts[m] > c) |
| 1204 | hi = m - 1; |
| 1205 | else if (byts[m] < c) |
| 1206 | lo = m + 1; |
| 1207 | else |
| 1208 | { |
| 1209 | lo = hi = m; |
| 1210 | break; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | /* Stop if there is no matching byte. */ |
| 1215 | if (hi < lo || byts[lo] != c) |
| 1216 | break; |
| 1217 | |
| 1218 | /* Continue at the child (if there is one). */ |
| 1219 | arridx = idxs[lo]; |
| 1220 | ++wlen; |
| 1221 | --flen; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * Need to fold at least one more character. Do until next non-word character |
| 1227 | * for efficiency. |
| 1228 | * Return the length of the folded chars in bytes. |
| 1229 | */ |
| 1230 | static int |
| 1231 | fold_more(mip) |
| 1232 | matchinf_T *mip; |
| 1233 | { |
| 1234 | int flen; |
| 1235 | char_u *p; |
| 1236 | |
| 1237 | p = mip->mi_fend; |
| 1238 | do |
| 1239 | { |
| 1240 | mb_ptr_adv(mip->mi_fend); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1241 | } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1242 | |
| 1243 | /* Include the non-word character so that we can check for the |
| 1244 | * word end. */ |
| 1245 | if (*mip->mi_fend != NUL) |
| 1246 | mb_ptr_adv(mip->mi_fend); |
| 1247 | |
| 1248 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1249 | mip->mi_fword + mip->mi_fwordlen, |
| 1250 | MAXWLEN - mip->mi_fwordlen); |
| 1251 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1252 | mip->mi_fwordlen += flen; |
| 1253 | return flen; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1257 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1258 | * case. |
| 1259 | */ |
| 1260 | static int |
| 1261 | spell_valid_case(origflags, treeflags) |
| 1262 | int origflags; /* flags for the checked word. */ |
| 1263 | int treeflags; /* flags for the word in the spell tree */ |
| 1264 | { |
| 1265 | return (origflags == WF_ALLCAP |
| 1266 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
| 1267 | && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP))); |
| 1268 | } |
| 1269 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1270 | /* |
| 1271 | * Return TRUE if spell checking is not enabled. |
| 1272 | */ |
| 1273 | static int |
| 1274 | no_spell_checking() |
| 1275 | { |
| 1276 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 1277 | { |
| 1278 | EMSG(_("E756: Spell checking is not enabled")); |
| 1279 | return TRUE; |
| 1280 | } |
| 1281 | return FALSE; |
| 1282 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1283 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1284 | /* |
| 1285 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1286 | * "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] | 1287 | * Return OK if found, FAIL otherwise. |
| 1288 | */ |
| 1289 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1290 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1291 | int dir; /* FORWARD or BACKWARD */ |
| 1292 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1293 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1294 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1295 | linenr_T lnum; |
| 1296 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1297 | char_u *line; |
| 1298 | char_u *p; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1299 | char_u *endp; |
| 1300 | int attr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1301 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1302 | int has_syntax = syntax_present(curbuf); |
| 1303 | int col; |
| 1304 | int can_spell; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1305 | char_u *buf = NULL; |
| 1306 | int buflen = 0; |
| 1307 | int skip = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1308 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1309 | if (no_spell_checking()) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1310 | return FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1311 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1312 | /* |
| 1313 | * Start looking for bad word at the start of the line, because we can't |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1314 | * start halfway a word, we don't know where the it starts or ends. |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1315 | * |
| 1316 | * When searching backwards, we continue in the line to find the last |
| 1317 | * bad word (in the cursor line: before the cursor). |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1318 | * |
| 1319 | * We concatenate the start of the next line, so that wrapped words work |
| 1320 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 1321 | * though... |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1322 | */ |
| 1323 | lnum = curwin->w_cursor.lnum; |
| 1324 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1325 | |
| 1326 | while (!got_int) |
| 1327 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1328 | line = ml_get(lnum); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1329 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1330 | len = STRLEN(line); |
| 1331 | if (buflen < len + MAXWLEN + 2) |
| 1332 | { |
| 1333 | vim_free(buf); |
| 1334 | buflen = len + MAXWLEN + 2; |
| 1335 | buf = alloc(buflen); |
| 1336 | if (buf == NULL) |
| 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | /* Copy the line into "buf" and append the start of the next line if |
| 1341 | * possible. */ |
| 1342 | STRCPY(buf, line); |
| 1343 | if (lnum < curbuf->b_ml.ml_line_count) |
| 1344 | spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN); |
| 1345 | |
| 1346 | p = buf + skip; |
| 1347 | endp = buf + len; |
| 1348 | while (p < endp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1349 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1350 | /* When searching backward don't search after the cursor. */ |
| 1351 | if (dir == BACKWARD |
| 1352 | && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1353 | && (colnr_T)(p - buf) >= curwin->w_cursor.col) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1354 | break; |
| 1355 | |
| 1356 | /* start of word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1357 | attr = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1358 | len = spell_check(curwin, p, &attr); |
| 1359 | |
| 1360 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1361 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1362 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1363 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1364 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1365 | /* When searching forward only accept a bad word after |
| 1366 | * the cursor. */ |
| 1367 | if (dir == BACKWARD |
| 1368 | || lnum > curwin->w_cursor.lnum |
| 1369 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1370 | && (colnr_T)(curline ? p - buf + len |
| 1371 | : p - buf) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1372 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1373 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1374 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1375 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1376 | col = p - buf; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1377 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1378 | FALSE, &can_spell); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1379 | } |
| 1380 | else |
| 1381 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1382 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1383 | if (can_spell) |
| 1384 | { |
| 1385 | found_pos.lnum = lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1386 | found_pos.col = p - buf; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1387 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1388 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1389 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1390 | if (dir == FORWARD) |
| 1391 | { |
| 1392 | /* No need to search further. */ |
| 1393 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1394 | vim_free(buf); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1395 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1398 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1399 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1402 | /* advance to character after the word */ |
| 1403 | p += len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1406 | if (curline) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1407 | break; /* only check cursor line */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1408 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1409 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1410 | if (dir == BACKWARD) |
| 1411 | { |
| 1412 | if (found_pos.lnum != 0) |
| 1413 | { |
| 1414 | /* Use the last match in the line. */ |
| 1415 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1416 | vim_free(buf); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1417 | return OK; |
| 1418 | } |
| 1419 | if (lnum == 1) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1420 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1421 | --lnum; |
| 1422 | } |
| 1423 | else |
| 1424 | { |
| 1425 | if (lnum == curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1426 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1427 | ++lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1428 | |
| 1429 | /* Skip the characters at the start of the next line that were |
| 1430 | * included in a match crossing line boundaries. */ |
| 1431 | if (attr == 0) |
| 1432 | skip = p - endp; |
| 1433 | else |
| 1434 | skip = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1435 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1436 | |
| 1437 | line_breakcheck(); |
| 1438 | } |
| 1439 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1440 | vim_free(buf); |
| 1441 | return FAIL; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * For spell checking: concatenate the start of the following line "line" into |
| 1446 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| 1447 | */ |
| 1448 | void |
| 1449 | spell_cat_line(buf, line, maxlen) |
| 1450 | char_u *buf; |
| 1451 | char_u *line; |
| 1452 | int maxlen; |
| 1453 | { |
| 1454 | char_u *p; |
| 1455 | int n; |
| 1456 | |
| 1457 | p = skipwhite(line); |
| 1458 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 1459 | p = skipwhite(p + 1); |
| 1460 | |
| 1461 | if (*p != NUL) |
| 1462 | { |
| 1463 | *buf = ' '; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1464 | vim_strncpy(buf + 1, line, maxlen - 2); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1465 | n = p - line; |
| 1466 | if (n >= maxlen) |
| 1467 | n = maxlen - 1; |
| 1468 | vim_memset(buf + 1, ' ', n); |
| 1469 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1473 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1474 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1475 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1476 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1477 | spell_load_lang(lang) |
| 1478 | char_u *lang; |
| 1479 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1480 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1481 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1482 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1483 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1484 | /* 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] | 1485 | * It's truncated when an error is detected. */ |
| 1486 | STRCPY(langcp, lang); |
| 1487 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1488 | /* |
| 1489 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1490 | */ |
| 1491 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1492 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1493 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1494 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1495 | if (r == FAIL && *langcp != NUL) |
| 1496 | { |
| 1497 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1498 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1499 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1500 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1503 | if (r == FAIL) |
| 1504 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1505 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1506 | else if (*langcp != NUL) |
| 1507 | { |
| 1508 | /* Load all the additions. */ |
| 1509 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1510 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1516 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1517 | */ |
| 1518 | static char_u * |
| 1519 | spell_enc() |
| 1520 | { |
| 1521 | |
| 1522 | #ifdef FEAT_MBYTE |
| 1523 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1524 | return p_enc; |
| 1525 | #endif |
| 1526 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * Allocate a new slang_T. |
| 1531 | * Caller must fill "sl_next". |
| 1532 | */ |
| 1533 | static slang_T * |
| 1534 | slang_alloc(lang) |
| 1535 | char_u *lang; |
| 1536 | { |
| 1537 | slang_T *lp; |
| 1538 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1539 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1540 | if (lp != NULL) |
| 1541 | { |
| 1542 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1543 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1544 | ga_init2(&lp->sl_sal, sizeof(salitem_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1545 | } |
| 1546 | return lp; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Free the contents of an slang_T and the structure itself. |
| 1551 | */ |
| 1552 | static void |
| 1553 | slang_free(lp) |
| 1554 | slang_T *lp; |
| 1555 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1556 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1557 | vim_free(lp->sl_fname); |
| 1558 | slang_clear(lp); |
| 1559 | vim_free(lp); |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Clear an slang_T so that the file can be reloaded. |
| 1564 | */ |
| 1565 | static void |
| 1566 | slang_clear(lp) |
| 1567 | slang_T *lp; |
| 1568 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1569 | garray_T *gap; |
| 1570 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1571 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1572 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1573 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1574 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1575 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1576 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1577 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1578 | vim_free(lp->sl_pbyts); |
| 1579 | lp->sl_pbyts = NULL; |
| 1580 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1581 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1582 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1583 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1584 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1585 | vim_free(lp->sl_pidxs); |
| 1586 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1587 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1588 | gap = &lp->sl_rep; |
| 1589 | while (gap->ga_len > 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1590 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1591 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1592 | vim_free(ftp->ft_from); |
| 1593 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1594 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1595 | ga_clear(gap); |
| 1596 | |
| 1597 | gap = &lp->sl_sal; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1598 | if (lp->sl_sofo) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1599 | { |
| 1600 | /* "ga_len" is set to 1 without adding an item for latin1 */ |
| 1601 | if (gap->ga_data != NULL) |
| 1602 | /* SOFOFROM and SOFOTO items: free lists of wide characters. */ |
| 1603 | for (i = 0; i < gap->ga_len; ++i) |
| 1604 | vim_free(((int **)gap->ga_data)[i]); |
| 1605 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1606 | else |
| 1607 | /* SAL items: free salitem_T items */ |
| 1608 | while (gap->ga_len > 0) |
| 1609 | { |
| 1610 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1611 | vim_free(smp->sm_lead); |
| 1612 | /* Don't free sm_oneof and sm_rules, they point into sm_lead. */ |
| 1613 | vim_free(smp->sm_to); |
| 1614 | #ifdef FEAT_MBYTE |
| 1615 | vim_free(smp->sm_lead_w); |
| 1616 | vim_free(smp->sm_oneof_w); |
| 1617 | vim_free(smp->sm_to_w); |
| 1618 | #endif |
| 1619 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1620 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1621 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1622 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1623 | vim_free(lp->sl_prefprog[i]); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1624 | lp->sl_prefixcnt = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1625 | vim_free(lp->sl_prefprog); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1626 | lp->sl_prefprog = NULL; |
| 1627 | |
| 1628 | vim_free(lp->sl_midword); |
| 1629 | lp->sl_midword = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1630 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1631 | #ifdef FEAT_MBYTE |
| 1632 | { |
| 1633 | int todo = lp->sl_map_hash.ht_used; |
| 1634 | hashitem_T *hi; |
| 1635 | |
| 1636 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1637 | if (!HASHITEM_EMPTY(hi)) |
| 1638 | { |
| 1639 | --todo; |
| 1640 | vim_free(hi->hi_key); |
| 1641 | } |
| 1642 | } |
| 1643 | hash_clear(&lp->sl_map_hash); |
| 1644 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1648 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1649 | * Invoked through do_in_runtimepath(). |
| 1650 | */ |
| 1651 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1652 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1653 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1654 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1655 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1656 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | /* |
| 1660 | * Load one spell file and store the info into a slang_T. |
| 1661 | * |
| 1662 | * This is invoked in two ways: |
| 1663 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1664 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1665 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1666 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1667 | * 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] | 1668 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1669 | static slang_T * |
| 1670 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1671 | char_u *fname; |
| 1672 | char_u *lang; |
| 1673 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1674 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1675 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1676 | FILE *fd; |
| 1677 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1678 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1679 | char_u *bp; |
| 1680 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1681 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1682 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1683 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1684 | int round; |
| 1685 | char_u *save_sourcing_name = sourcing_name; |
| 1686 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1687 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1688 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1689 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1690 | garray_T *gap; |
| 1691 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1692 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1693 | int rr; |
| 1694 | short *first; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1695 | salfirst_T *sfirst; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1696 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1697 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1698 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1699 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1700 | if (fd == NULL) |
| 1701 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1702 | if (!silent) |
| 1703 | EMSG2(_(e_notopen), fname); |
| 1704 | else if (p_verbose > 2) |
| 1705 | { |
| 1706 | verbose_enter(); |
| 1707 | smsg((char_u *)e_notopen, fname); |
| 1708 | verbose_leave(); |
| 1709 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1710 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1711 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1712 | if (p_verbose > 2) |
| 1713 | { |
| 1714 | verbose_enter(); |
| 1715 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1716 | verbose_leave(); |
| 1717 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1718 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1719 | if (old_lp == NULL) |
| 1720 | { |
| 1721 | lp = slang_alloc(lang); |
| 1722 | if (lp == NULL) |
| 1723 | goto endFAIL; |
| 1724 | |
| 1725 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1726 | lp->sl_fname = vim_strsave(fname); |
| 1727 | if (lp->sl_fname == NULL) |
| 1728 | goto endFAIL; |
| 1729 | |
| 1730 | /* Check for .add.spl. */ |
| 1731 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1732 | } |
| 1733 | else |
| 1734 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1735 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1736 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1737 | sourcing_name = fname; |
| 1738 | sourcing_lnum = 0; |
| 1739 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1740 | /* <HEADER>: <fileID> |
| 1741 | * <regioncnt> <regionname> ... |
| 1742 | * <charflagslen> <charflags> |
| 1743 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1744 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1745 | * <prefcondcnt> <prefcond> ... |
| 1746 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1747 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1748 | buf[i] = getc(fd); /* <fileID> */ |
| 1749 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1750 | { |
| 1751 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1752 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1756 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1757 | { |
| 1758 | truncerr: |
| 1759 | EMSG(_("E758: Truncated spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1760 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1761 | } |
| 1762 | if (cnt > 8) |
| 1763 | { |
| 1764 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1765 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1766 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1767 | } |
| 1768 | for (i = 0; i < cnt; ++i) |
| 1769 | { |
| 1770 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1771 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1772 | } |
| 1773 | lp->sl_regions[cnt * 2] = NUL; |
| 1774 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1775 | cnt = getc(fd); /* <charflagslen> */ |
| 1776 | if (cnt > 0) |
| 1777 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1778 | p = alloc((unsigned)cnt); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1779 | if (p == NULL) |
| 1780 | goto endFAIL; |
| 1781 | for (i = 0; i < cnt; ++i) |
| 1782 | p[i] = getc(fd); /* <charflags> */ |
| 1783 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1784 | /* <fcharslen> <fchars> */ |
| 1785 | fol = read_cnt_string(fd, 2, &ccnt); |
| 1786 | if (ccnt != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1787 | { |
| 1788 | vim_free(p); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1789 | if (ccnt == ERR_NOMEM) |
| 1790 | goto endFAIL; |
| 1791 | if (ccnt == ERR_TRUNC) |
| 1792 | goto formerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1793 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1794 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1795 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1796 | i = set_spell_charflags(p, cnt, fol); |
| 1797 | vim_free(p); |
| 1798 | vim_free(fol); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1799 | #if 0 /* tolerate the differences */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1800 | if (i == FAIL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1801 | goto formerr; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1802 | #endif |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1803 | } |
| 1804 | else |
| 1805 | { |
| 1806 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1807 | cnt = (getc(fd) << 8) + getc(fd); |
| 1808 | if (cnt != 0) |
| 1809 | goto formerr; |
| 1810 | } |
| 1811 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1812 | /* <midwordlen> <midword> */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1813 | lp->sl_midword = read_cnt_string(fd, 2, &cnt); |
| 1814 | if (cnt == ERR_TRUNC) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1815 | goto truncerr; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1816 | if (cnt == ERR_NOMEM) |
| 1817 | goto endFAIL; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1818 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1819 | /* <prefcondcnt> <prefcond> ... */ |
| 1820 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1821 | if (cnt > 0) |
| 1822 | { |
| 1823 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1824 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1825 | if (lp->sl_prefprog == NULL) |
| 1826 | goto endFAIL; |
| 1827 | lp->sl_prefixcnt = cnt; |
| 1828 | |
| 1829 | for (i = 0; i < cnt; ++i) |
| 1830 | { |
| 1831 | /* <prefcond> : <condlen> <condstr> */ |
| 1832 | n = getc(fd); /* <condlen> */ |
| 1833 | if (n < 0) |
| 1834 | goto formerr; |
| 1835 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1836 | * compile the regexp program used to check for the condition. */ |
| 1837 | if (n > 0) |
| 1838 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1839 | buf[0] = '^'; /* always match at one position only */ |
| 1840 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1841 | while (n-- > 0) |
| 1842 | *p++ = getc(fd); /* <condstr> */ |
| 1843 | *p = NUL; |
| 1844 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1850 | /* <SUGGEST> : <repcount> <rep> ... |
| 1851 | * <salflags> <salcount> <sal> ... |
| 1852 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1853 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1854 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1855 | if (cnt < 0) |
| 1856 | goto formerr; |
| 1857 | |
| 1858 | gap = &lp->sl_rep; |
| 1859 | if (ga_grow(gap, cnt) == FAIL) |
| 1860 | goto endFAIL; |
| 1861 | |
| 1862 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1863 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1864 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1865 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
| 1866 | for (rr = 1; rr <= 2; ++rr) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1867 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1868 | ccnt = getc(fd); |
| 1869 | if (ccnt < 0) |
| 1870 | { |
| 1871 | if (rr == 2) |
| 1872 | vim_free(ftp->ft_from); |
| 1873 | goto formerr; |
| 1874 | } |
| 1875 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1876 | { |
| 1877 | if (rr == 2) |
| 1878 | vim_free(ftp->ft_from); |
| 1879 | goto endFAIL; |
| 1880 | } |
| 1881 | for (i = 0; i < ccnt; ++i) |
| 1882 | p[i] = getc(fd); /* <repfrom> or <repto> */ |
| 1883 | p[i] = NUL; |
| 1884 | if (rr == 1) |
| 1885 | ftp->ft_from = p; |
| 1886 | else |
| 1887 | ftp->ft_to = p; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /* Fill the first-index table. */ |
| 1892 | first = lp->sl_rep_first; |
| 1893 | for (i = 0; i < 256; ++i) |
| 1894 | first[i] = -1; |
| 1895 | for (i = 0; i < gap->ga_len; ++i) |
| 1896 | { |
| 1897 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1898 | if (first[*ftp->ft_from] == -1) |
| 1899 | first[*ftp->ft_from] = i; |
| 1900 | } |
| 1901 | |
| 1902 | i = getc(fd); /* <salflags> */ |
| 1903 | if (i & SAL_F0LLOWUP) |
| 1904 | lp->sl_followup = TRUE; |
| 1905 | if (i & SAL_COLLAPSE) |
| 1906 | lp->sl_collapse = TRUE; |
| 1907 | if (i & SAL_REM_ACCENTS) |
| 1908 | lp->sl_rem_accents = TRUE; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1909 | if (i & SAL_SOFO) |
| 1910 | lp->sl_sofo = TRUE; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1911 | |
| 1912 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1913 | if (cnt < 0) |
| 1914 | goto formerr; |
| 1915 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1916 | if (lp->sl_sofo) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1917 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1918 | /* |
| 1919 | * SOFOFROM and SOFOTO items come in one <salfrom> and <salto> |
| 1920 | */ |
| 1921 | if (cnt != 1) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1922 | goto formerr; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1923 | |
| 1924 | cnt = (getc(fd) << 8) + getc(fd); /* <salfromlen> */ |
| 1925 | if (cnt < 0) |
| 1926 | goto formerr; |
| 1927 | if ((bp = alloc(cnt + 1)) == NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1928 | goto endFAIL; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1929 | for (i = 0; i < cnt; ++i) |
| 1930 | bp[i] = getc(fd); /* <salfrom> */ |
| 1931 | bp[i] = NUL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1932 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1933 | ccnt = (getc(fd) << 8) + getc(fd); /* <saltolen> */ |
| 1934 | if (ccnt < 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1935 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1936 | vim_free(bp); |
| 1937 | goto formerr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1938 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1939 | if ((fol = alloc(ccnt + 1)) == NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1940 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1941 | vim_free(bp); |
| 1942 | goto endFAIL; |
| 1943 | } |
| 1944 | for (i = 0; i < ccnt; ++i) |
| 1945 | fol[i] = getc(fd); /* <salto> */ |
| 1946 | fol[i] = NUL; |
| 1947 | |
| 1948 | #ifdef FEAT_MBYTE |
| 1949 | if (has_mbyte) |
| 1950 | { |
| 1951 | char_u *s; |
| 1952 | |
| 1953 | /* Use "sl_sal" as an array with 256 pointers to a list of wide |
| 1954 | * characters. The index is the low byte of the character. |
| 1955 | * The list contains from-to pairs with a terminating NUL. |
| 1956 | * sl_sal_first[] is used for latin1 "from" characters. */ |
| 1957 | gap = &lp->sl_sal; |
| 1958 | ga_init2(gap, sizeof(int *), 1); |
| 1959 | if (ga_grow(gap, 256) == FAIL) |
| 1960 | { |
| 1961 | sofoFAIL: |
| 1962 | vim_free(bp); |
| 1963 | vim_free(fol); |
| 1964 | goto endFAIL; |
| 1965 | } |
| 1966 | vim_memset(gap->ga_data, 0, sizeof(int *) * 256); |
| 1967 | gap->ga_len = 256; |
| 1968 | |
| 1969 | /* First count the number of items for each list. Temporarily use |
| 1970 | * sl_sal_first[] for this. */ |
| 1971 | for (p = bp, s = fol; *p != NUL && *s != NUL; ) |
| 1972 | { |
| 1973 | c = mb_ptr2char_adv(&p); |
| 1974 | mb_ptr_adv(s); |
| 1975 | if (c >= 256) |
| 1976 | ++lp->sl_sal_first[c & 0xff]; |
| 1977 | } |
| 1978 | if (*p != NUL || *s != NUL) /* lengths differ */ |
| 1979 | goto sofoerr; |
| 1980 | |
| 1981 | /* Allocate the lists. */ |
| 1982 | for (i = 0; i < 256; ++i) |
| 1983 | if (lp->sl_sal_first[i] > 0) |
| 1984 | { |
| 1985 | p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1)); |
| 1986 | if (p == NULL) |
| 1987 | goto sofoFAIL; |
| 1988 | ((int **)gap->ga_data)[i] = (int *)p; |
| 1989 | *(int *)p = 0; |
| 1990 | } |
| 1991 | |
| 1992 | /* Put the characters in sl_sal_first[] or a sl_sal list. */ |
| 1993 | vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256); |
| 1994 | for (p = bp, s = fol; *p != NUL && *s != NUL; ) |
| 1995 | { |
| 1996 | c = mb_ptr2char_adv(&p); |
| 1997 | i = mb_ptr2char_adv(&s); |
| 1998 | if (c >= 256) |
| 1999 | { |
| 2000 | int *inp; |
| 2001 | |
| 2002 | /* Append the from-to chars at the end of the list with |
| 2003 | * the low byte. */ |
| 2004 | inp = ((int **)gap->ga_data)[c & 0xff]; |
| 2005 | while (*inp != 0) |
| 2006 | ++inp; |
| 2007 | *inp++ = c; /* from char */ |
| 2008 | *inp++ = i; /* to char */ |
| 2009 | *inp++ = NUL; /* NUL at the end */ |
| 2010 | } |
| 2011 | else |
| 2012 | /* mapping byte to char is done in sl_sal_first[] */ |
| 2013 | lp->sl_sal_first[c] = i; |
| 2014 | } |
| 2015 | } |
| 2016 | else |
| 2017 | #endif |
| 2018 | { |
| 2019 | /* mapping bytes to bytes is done in sl_sal_first[] */ |
| 2020 | if (cnt != ccnt) |
| 2021 | { |
| 2022 | #ifdef FEAT_MBYTE |
| 2023 | sofoerr: |
| 2024 | #endif |
| 2025 | vim_free(bp); |
| 2026 | vim_free(fol); |
| 2027 | goto formerr; |
| 2028 | } |
| 2029 | for (i = 0; i < cnt; ++i) |
| 2030 | lp->sl_sal_first[bp[i]] = fol[i]; |
| 2031 | lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */ |
| 2032 | } |
| 2033 | vim_free(bp); |
| 2034 | vim_free(fol); |
| 2035 | } |
| 2036 | else |
| 2037 | { |
| 2038 | /* |
| 2039 | * SAL items |
| 2040 | */ |
| 2041 | gap = &lp->sl_sal; |
| 2042 | if (ga_grow(gap, cnt) == FAIL) |
| 2043 | goto endFAIL; |
| 2044 | |
| 2045 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 2046 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 2047 | { |
| 2048 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 2049 | ccnt = getc(fd); /* <salfromlen> */ |
| 2050 | if (ccnt < 0) |
| 2051 | goto formerr; |
| 2052 | if ((p = alloc(ccnt + 2)) == NULL) |
| 2053 | goto endFAIL; |
| 2054 | smp->sm_lead = p; |
| 2055 | |
| 2056 | /* Read up to the first special char into sm_lead. */ |
| 2057 | for (i = 0; i < ccnt; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2058 | { |
| 2059 | c = getc(fd); /* <salfrom> */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2060 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2061 | break; |
| 2062 | *p++ = c; |
| 2063 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2064 | smp->sm_leadlen = p - smp->sm_lead; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2065 | *p++ = NUL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2066 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2067 | /* Put (abc) chars in sm_oneof, if any. */ |
| 2068 | if (c == '(') |
| 2069 | { |
| 2070 | smp->sm_oneof = p; |
| 2071 | for (++i; i < ccnt; ++i) |
| 2072 | { |
| 2073 | c = getc(fd); /* <salfrom> */ |
| 2074 | if (c == ')') |
| 2075 | break; |
| 2076 | *p++ = c; |
| 2077 | } |
| 2078 | *p++ = NUL; |
| 2079 | if (++i < ccnt) |
| 2080 | c = getc(fd); |
| 2081 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2082 | else |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2083 | smp->sm_oneof = NULL; |
| 2084 | |
| 2085 | /* Any following chars go in sm_rules. */ |
| 2086 | smp->sm_rules = p; |
| 2087 | if (i < ccnt) |
| 2088 | /* store the char we got while checking for end of sm_lead */ |
| 2089 | *p++ = c; |
| 2090 | for (++i; i < ccnt; ++i) |
| 2091 | *p++ = getc(fd); /* <salfrom> */ |
| 2092 | *p++ = NUL; |
| 2093 | |
| 2094 | ccnt = getc(fd); /* <saltolen> */ |
| 2095 | if (ccnt < 0) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2096 | { |
| 2097 | vim_free(smp->sm_lead); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2098 | goto formerr; |
| 2099 | } |
| 2100 | if ((p = alloc(ccnt + 1)) == NULL) |
| 2101 | { |
| 2102 | vim_free(smp->sm_lead); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2103 | goto endFAIL; |
| 2104 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2105 | smp->sm_to = p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2106 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2107 | for (i = 0; i < ccnt; ++i) |
| 2108 | *p++ = getc(fd); /* <salto> */ |
| 2109 | *p++ = NUL; |
| 2110 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2111 | #ifdef FEAT_MBYTE |
| 2112 | if (has_mbyte) |
| 2113 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2114 | /* convert the multi-byte strings to wide char strings */ |
| 2115 | smp->sm_lead_w = mb_str2wide(smp->sm_lead); |
| 2116 | smp->sm_leadlen = mb_charlen(smp->sm_lead); |
| 2117 | if (smp->sm_oneof == NULL) |
| 2118 | smp->sm_oneof_w = NULL; |
| 2119 | else |
| 2120 | smp->sm_oneof_w = mb_str2wide(smp->sm_oneof); |
| 2121 | smp->sm_to_w = mb_str2wide(smp->sm_to); |
| 2122 | if (smp->sm_lead_w == NULL |
| 2123 | || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL) |
| 2124 | || smp->sm_to_w == NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2125 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2126 | vim_free(smp->sm_lead); |
| 2127 | vim_free(smp->sm_to); |
| 2128 | vim_free(smp->sm_lead_w); |
| 2129 | vim_free(smp->sm_oneof_w); |
| 2130 | vim_free(smp->sm_to_w); |
| 2131 | goto endFAIL; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2132 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2133 | } |
| 2134 | #endif |
| 2135 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2136 | |
| 2137 | /* Fill the first-index table. */ |
| 2138 | sfirst = lp->sl_sal_first; |
| 2139 | for (i = 0; i < 256; ++i) |
| 2140 | sfirst[i] = -1; |
| 2141 | smp = (salitem_T *)gap->ga_data; |
| 2142 | for (i = 0; i < gap->ga_len; ++i) |
| 2143 | { |
| 2144 | #ifdef FEAT_MBYTE |
| 2145 | if (has_mbyte) |
| 2146 | /* Use the lowest byte of the first character. For latin1 it's |
| 2147 | * the character, for other encodings it should differ for most |
| 2148 | * characters. */ |
| 2149 | c = *smp[i].sm_lead_w & 0xff; |
| 2150 | else |
| 2151 | #endif |
| 2152 | c = *smp[i].sm_lead; |
| 2153 | if (sfirst[c] == -1) |
| 2154 | { |
| 2155 | sfirst[c] = i; |
| 2156 | #ifdef FEAT_MBYTE |
| 2157 | if (has_mbyte) |
| 2158 | { |
| 2159 | /* Make sure all entries with this byte are following each |
| 2160 | * other. Move the ones that are in the wrong position. Do |
| 2161 | * keep the same ordering! */ |
| 2162 | while (i + 1 < gap->ga_len |
| 2163 | && (*smp[i + 1].sm_lead_w & 0xff) == c) |
| 2164 | /* Skip over entry with same index byte. */ |
| 2165 | ++i; |
| 2166 | |
| 2167 | for (n = 1; i + n < gap->ga_len; ++n) |
| 2168 | if ((*smp[i + n].sm_lead_w & 0xff) == c) |
| 2169 | { |
| 2170 | salitem_T tsal; |
| 2171 | |
| 2172 | /* Move entry with same index byte after the entries |
| 2173 | * we already found. */ |
| 2174 | ++i; |
| 2175 | --n; |
| 2176 | tsal = smp[i + n]; |
| 2177 | mch_memmove(smp + i + 1, smp + i, |
| 2178 | sizeof(salitem_T) * n); |
| 2179 | smp[i] = tsal; |
| 2180 | } |
| 2181 | } |
| 2182 | #endif |
| 2183 | } |
| 2184 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */ |
| 2188 | if (cnt < 0) |
| 2189 | goto formerr; |
| 2190 | p = alloc(cnt + 1); |
| 2191 | if (p == NULL) |
| 2192 | goto endFAIL; |
| 2193 | for (i = 0; i < cnt; ++i) |
| 2194 | p[i] = getc(fd); /* <mapstr> */ |
| 2195 | p[i] = NUL; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 2196 | set_map_str(lp, p); |
| 2197 | vim_free(p); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2198 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2199 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2200 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2201 | * round 2: <KWORDTREE> |
| 2202 | * round 3: <PREFIXTREE> */ |
| 2203 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2204 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2205 | /* The tree size was computed when writing the file, so that we can |
| 2206 | * allocate it as one long block. <nodecount> */ |
| 2207 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2208 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2209 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2210 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2211 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2212 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2213 | bp = lalloc((long_u)len, TRUE); |
| 2214 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2215 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2216 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2217 | lp->sl_fbyts = bp; |
| 2218 | else if (round == 2) |
| 2219 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2220 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2221 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2222 | |
| 2223 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2224 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 2225 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2226 | goto endFAIL; |
| 2227 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2228 | lp->sl_fidxs = ip; |
| 2229 | else if (round == 2) |
| 2230 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2231 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2232 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2233 | |
| 2234 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2235 | 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] | 2236 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2237 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2238 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2239 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2240 | } |
| 2241 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2242 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2243 | /* For a new file link it in the list of spell files. */ |
| 2244 | if (old_lp == NULL) |
| 2245 | { |
| 2246 | lp->sl_next = first_lang; |
| 2247 | first_lang = lp; |
| 2248 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2249 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2250 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2251 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2252 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2253 | if (lang != NULL) |
| 2254 | /* truncating the name signals the error to spell_load_lang() */ |
| 2255 | *lang = NUL; |
| 2256 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2257 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2258 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2259 | lp = NULL; |
| 2260 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2261 | |
| 2262 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2263 | if (fd != NULL) |
| 2264 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2265 | sourcing_name = save_sourcing_name; |
| 2266 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2267 | |
| 2268 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2271 | /* |
| 2272 | * Read a length field from "fd" in "cnt_bytes" bytes. |
| 2273 | * Allocate memory and read the string into it. |
| 2274 | * Returns NULL when the count is zero. |
| 2275 | * Sets "errp" to ERR_TRUNC when reading failed, ERR_NOMEM when out of |
| 2276 | * memory, zero when OK. |
| 2277 | */ |
| 2278 | static char_u * |
| 2279 | read_cnt_string(fd, cnt_bytes, errp) |
| 2280 | FILE *fd; |
| 2281 | int cnt_bytes; |
| 2282 | int *errp; |
| 2283 | { |
| 2284 | int cnt = 0; |
| 2285 | int i; |
| 2286 | char_u *str; |
| 2287 | |
| 2288 | /* read the length bytes, MSB first */ |
| 2289 | for (i = 0; i < cnt_bytes; ++i) |
| 2290 | cnt = (cnt << 8) + getc(fd); |
| 2291 | if (cnt < 0) |
| 2292 | { |
| 2293 | *errp = ERR_TRUNC; |
| 2294 | return NULL; |
| 2295 | } |
| 2296 | |
| 2297 | /* allocate memory */ |
| 2298 | str = alloc((unsigned)cnt + 1); |
| 2299 | if (str == NULL) |
| 2300 | { |
| 2301 | *errp = ERR_NOMEM; |
| 2302 | return NULL; |
| 2303 | } |
| 2304 | *errp = 0; |
| 2305 | |
| 2306 | /* Read the string. Doesn't check for truncated file. */ |
| 2307 | for (i = 0; i < cnt; ++i) |
| 2308 | str[i] = getc(fd); |
| 2309 | str[i] = NUL; |
| 2310 | |
| 2311 | return str; |
| 2312 | } |
| 2313 | |
| 2314 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2315 | #ifdef FEAT_MBYTE |
| 2316 | /* |
| 2317 | * Turn a multi-byte string into a wide character string. |
| 2318 | * Return it in allocated memory (NULL for out-of-memory) |
| 2319 | */ |
| 2320 | static int * |
| 2321 | mb_str2wide(s) |
| 2322 | char_u *s; |
| 2323 | { |
| 2324 | int *res; |
| 2325 | char_u *p; |
| 2326 | int i = 0; |
| 2327 | |
| 2328 | res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1)); |
| 2329 | if (res != NULL) |
| 2330 | { |
| 2331 | for (p = s; *p != NUL; ) |
| 2332 | res[i++] = mb_ptr2char_adv(&p); |
| 2333 | res[i] = NUL; |
| 2334 | } |
| 2335 | return res; |
| 2336 | } |
| 2337 | #endif |
| 2338 | |
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 | * Read one row of siblings from the spell file and store it in the byte array |
| 2341 | * "byts" and index array "idxs". Recursively read the children. |
| 2342 | * |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2343 | * NOTE: The code here must match put_node(). |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2344 | * |
| 2345 | * Returns the index follosing the siblings. |
| 2346 | * Returns -1 if the file is shorter than expected. |
| 2347 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2348 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2349 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2350 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2351 | FILE *fd; |
| 2352 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2353 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2354 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2355 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2356 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 2357 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2358 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2359 | int len; |
| 2360 | int i; |
| 2361 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2362 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2363 | int c; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2364 | int c2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2365 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2366 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2367 | len = getc(fd); /* <siblingcount> */ |
| 2368 | if (len <= 0) |
| 2369 | return -1; |
| 2370 | |
| 2371 | if (startidx + len >= maxidx) |
| 2372 | return -2; |
| 2373 | byts[idx++] = len; |
| 2374 | |
| 2375 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 2376 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2377 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2378 | c = getc(fd); /* <byte> */ |
| 2379 | if (c < 0) |
| 2380 | return -1; |
| 2381 | if (c <= BY_SPECIAL) |
| 2382 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2383 | if (c == BY_NOFLAGS && !prefixtree) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2384 | { |
| 2385 | /* No flags, all regions. */ |
| 2386 | idxs[idx] = 0; |
| 2387 | c = 0; |
| 2388 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2389 | else if (c == BY_FLAGS || c == BY_NOFLAGS) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2390 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2391 | if (prefixtree) |
| 2392 | { |
| 2393 | /* Read the prefix ID and the condition nr. In idxs[] |
| 2394 | * store the prefix ID in the low byte, the condition |
| 2395 | * index shifted up 8 bits. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2396 | c2 = getc(fd); /* <prefixID> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2397 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 2398 | if (n >= maxprefcondnr) |
| 2399 | return -2; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2400 | c2 += (n << 8); |
| 2401 | if (c == BY_NOFLAGS) |
| 2402 | c = c2; |
| 2403 | else |
| 2404 | c = c2 | WF_RAREPFX; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2405 | } |
| 2406 | else |
| 2407 | { |
| 2408 | /* Read flags and optional region and prefix ID. In |
| 2409 | * idxs[] the flags go in the low byte, region above that |
| 2410 | * and prefix ID above the region. */ |
| 2411 | c = getc(fd); /* <flags> */ |
| 2412 | if (c & WF_REGION) |
| 2413 | c = (getc(fd) << 8) + c; /* <region> */ |
| 2414 | if (c & WF_PFX) |
| 2415 | c = (getc(fd) << 16) + c; /* <prefixID> */ |
| 2416 | } |
| 2417 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2418 | idxs[idx] = c; |
| 2419 | c = 0; |
| 2420 | } |
| 2421 | else /* c == BY_INDEX */ |
| 2422 | { |
| 2423 | /* <nodeidx> */ |
| 2424 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2425 | if (n < 0 || n >= maxidx) |
| 2426 | return -2; |
| 2427 | idxs[idx] = n + SHARED_MASK; |
| 2428 | c = getc(fd); /* <xbyte> */ |
| 2429 | } |
| 2430 | } |
| 2431 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2434 | /* Recursively read the children for non-shared siblings. |
| 2435 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 2436 | * remove SHARED_MASK) */ |
| 2437 | for (i = 1; i <= len; ++i) |
| 2438 | if (byts[startidx + i] != 0) |
| 2439 | { |
| 2440 | if (idxs[startidx + i] & SHARED_MASK) |
| 2441 | idxs[startidx + i] &= ~SHARED_MASK; |
| 2442 | else |
| 2443 | { |
| 2444 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2445 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 2446 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2447 | if (idx < 0) |
| 2448 | break; |
| 2449 | } |
| 2450 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2451 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2452 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * Parse 'spelllang' and set buf->b_langp accordingly. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2457 | * Returns NULL if it's OK, an error message otherwise. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2458 | */ |
| 2459 | char_u * |
| 2460 | did_set_spelllang(buf) |
| 2461 | buf_T *buf; |
| 2462 | { |
| 2463 | garray_T ga; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2464 | char_u *splp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2465 | char_u *region; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2466 | int filename; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2467 | int region_mask; |
| 2468 | slang_T *lp; |
| 2469 | int c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2470 | char_u lang[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2471 | char_u spf_name[MAXPATHL]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2472 | int load_spf; |
| 2473 | int len; |
| 2474 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2475 | |
| 2476 | ga_init2(&ga, sizeof(langp_T), 2); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2477 | clear_midword(buf); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2478 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2479 | /* Make the name of the .spl file associated with 'spellfile'. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2480 | if (*buf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2481 | load_spf = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2482 | else |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2483 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2484 | vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl", |
| 2485 | buf->b_p_spf); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2486 | load_spf = TRUE; |
| 2487 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2488 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2489 | /* loop over comma separated language names. */ |
| 2490 | for (splp = buf->b_p_spl; *splp != NUL; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2491 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2492 | /* Get one language name. */ |
| 2493 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 2494 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2495 | region = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2496 | len = STRLEN(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2497 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2498 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 2499 | * If there is a region name let "region" point to it and remove it |
| 2500 | * from the name. */ |
| 2501 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 2502 | { |
| 2503 | filename = TRUE; |
| 2504 | |
| 2505 | /* Check if we loaded this language before. */ |
| 2506 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2507 | if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME) |
| 2508 | break; |
| 2509 | } |
| 2510 | else |
| 2511 | { |
| 2512 | filename = FALSE; |
| 2513 | if (len > 3 && lang[len - 3] == '_') |
| 2514 | { |
| 2515 | region = lang + len - 2; |
| 2516 | len -= 3; |
| 2517 | lang[len] = NUL; |
| 2518 | } |
| 2519 | |
| 2520 | /* Check if we loaded this language before. */ |
| 2521 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2522 | if (STRICMP(lang, lp->sl_name) == 0) |
| 2523 | break; |
| 2524 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2525 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2526 | /* If not found try loading the language now. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2527 | if (lp == NULL) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2528 | { |
| 2529 | if (filename) |
| 2530 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 2531 | else |
| 2532 | spell_load_lang(lang); |
| 2533 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2534 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2535 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2536 | * Loop over the languages, there can be several files for "lang". |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2537 | */ |
| 2538 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2539 | if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME |
| 2540 | : STRICMP(lang, lp->sl_name) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2541 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2542 | region_mask = REGION_ALL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2543 | if (!filename && region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2544 | { |
| 2545 | /* find region in sl_regions */ |
| 2546 | c = find_region(lp->sl_regions, region); |
| 2547 | if (c == REGION_ALL) |
| 2548 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2549 | if (!lp->sl_add) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2550 | smsg((char_u *) |
| 2551 | _("Warning: region %s not supported"), |
| 2552 | region); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2553 | } |
| 2554 | else |
| 2555 | region_mask = 1 << c; |
| 2556 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2557 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2558 | if (ga_grow(&ga, 1) == FAIL) |
| 2559 | { |
| 2560 | ga_clear(&ga); |
| 2561 | return e_outofmem; |
| 2562 | } |
| 2563 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2564 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2565 | ++ga.ga_len; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2566 | use_midword(lp, buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2567 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2568 | /* Check if this is the spell file related to 'spellfile'. */ |
| 2569 | if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE) |
| 2570 | == FPC_SAME) |
| 2571 | load_spf = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2572 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2575 | /* |
| 2576 | * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath', |
| 2577 | * then it's probably loaded above already. Otherwise load it here. |
| 2578 | */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2579 | if (load_spf) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2580 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2581 | /* Check if it was loaded already. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2582 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2583 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2584 | break; |
| 2585 | if (lp == NULL) |
| 2586 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2587 | /* Not loaded, try loading it now. The language name includes the |
| 2588 | * region name, the region is ignored otherwise. */ |
| 2589 | vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN); |
| 2590 | p = vim_strchr(lang, '.'); |
| 2591 | if (p != NULL) |
| 2592 | *p = NUL; /* truncate at ".encoding.add" */ |
| 2593 | lp = spell_load_file(spf_name, lang, NULL, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2594 | } |
| 2595 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2596 | { |
| 2597 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2598 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL; |
| 2599 | ++ga.ga_len; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2600 | use_midword(lp, buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2604 | /* Add a NULL entry to mark the end of the list. */ |
| 2605 | if (ga_grow(&ga, 1) == FAIL) |
| 2606 | { |
| 2607 | ga_clear(&ga); |
| 2608 | return e_outofmem; |
| 2609 | } |
| 2610 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2611 | ++ga.ga_len; |
| 2612 | |
| 2613 | /* Everything is fine, store the new b_langp value. */ |
| 2614 | ga_clear(&buf->b_langp); |
| 2615 | buf->b_langp = ga; |
| 2616 | |
| 2617 | return NULL; |
| 2618 | } |
| 2619 | |
| 2620 | /* |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2621 | * Clear the midword characters for buffer "buf". |
| 2622 | */ |
| 2623 | static void |
| 2624 | clear_midword(buf) |
| 2625 | buf_T *buf; |
| 2626 | { |
| 2627 | vim_memset(buf->b_spell_ismw, 0, 256); |
| 2628 | #ifdef FEAT_MBYTE |
| 2629 | vim_free(buf->b_spell_ismw_mb); |
| 2630 | buf->b_spell_ismw_mb = NULL; |
| 2631 | #endif |
| 2632 | } |
| 2633 | |
| 2634 | /* |
| 2635 | * Use the "sl_midword" field of language "lp" for buffer "buf". |
| 2636 | * They add up to any currently used midword characters. |
| 2637 | */ |
| 2638 | static void |
| 2639 | use_midword(lp, buf) |
| 2640 | slang_T *lp; |
| 2641 | buf_T *buf; |
| 2642 | { |
| 2643 | char_u *p; |
| 2644 | |
| 2645 | for (p = lp->sl_midword; *p != NUL; ) |
| 2646 | #ifdef FEAT_MBYTE |
| 2647 | if (has_mbyte) |
| 2648 | { |
| 2649 | int c, l, n; |
| 2650 | char_u *bp; |
| 2651 | |
| 2652 | c = mb_ptr2char(p); |
| 2653 | l = mb_ptr2len_check(p); |
| 2654 | if (c < 256) |
| 2655 | buf->b_spell_ismw[c] = TRUE; |
| 2656 | else if (buf->b_spell_ismw_mb == NULL) |
| 2657 | /* First multi-byte char in "b_spell_ismw_mb". */ |
| 2658 | buf->b_spell_ismw_mb = vim_strnsave(p, l); |
| 2659 | else |
| 2660 | { |
| 2661 | /* Append multi-byte chars to "b_spell_ismw_mb". */ |
| 2662 | n = STRLEN(buf->b_spell_ismw_mb); |
| 2663 | bp = vim_strnsave(buf->b_spell_ismw_mb, n + l); |
| 2664 | if (bp != NULL) |
| 2665 | { |
| 2666 | vim_free(buf->b_spell_ismw_mb); |
| 2667 | buf->b_spell_ismw_mb = bp; |
| 2668 | vim_strncpy(bp + n, p, l); |
| 2669 | } |
| 2670 | } |
| 2671 | p += l; |
| 2672 | } |
| 2673 | else |
| 2674 | #endif |
| 2675 | buf->b_spell_ismw[*p++] = TRUE; |
| 2676 | } |
| 2677 | |
| 2678 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2679 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2680 | * Each region is simply stored as the two characters of it's name. |
| 2681 | * Returns the index if found, REGION_ALL if not found. |
| 2682 | */ |
| 2683 | static int |
| 2684 | find_region(rp, region) |
| 2685 | char_u *rp; |
| 2686 | char_u *region; |
| 2687 | { |
| 2688 | int i; |
| 2689 | |
| 2690 | for (i = 0; ; i += 2) |
| 2691 | { |
| 2692 | if (rp[i] == NUL) |
| 2693 | return REGION_ALL; |
| 2694 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2695 | break; |
| 2696 | } |
| 2697 | return i / 2; |
| 2698 | } |
| 2699 | |
| 2700 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2701 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2702 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2703 | * Word WF_ONECAP |
| 2704 | * W WORD WF_ALLCAP |
| 2705 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2706 | */ |
| 2707 | static int |
| 2708 | captype(word, end) |
| 2709 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2710 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2711 | { |
| 2712 | char_u *p; |
| 2713 | int c; |
| 2714 | int firstcap; |
| 2715 | int allcap; |
| 2716 | int past_second = FALSE; /* past second word char */ |
| 2717 | |
| 2718 | /* find first letter */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2719 | for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2720 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2721 | return 0; /* only non-word characters, illegal word */ |
| 2722 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2723 | if (has_mbyte) |
| 2724 | c = mb_ptr2char_adv(&p); |
| 2725 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2726 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2727 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2728 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2729 | |
| 2730 | /* |
| 2731 | * Need to check all letters to find a word with mixed upper/lower. |
| 2732 | * But a word with an upper char only at start is a ONECAP. |
| 2733 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2734 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2735 | if (spell_iswordp_nmw(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2736 | { |
| 2737 | #ifdef FEAT_MBYTE |
| 2738 | c = mb_ptr2char(p); |
| 2739 | #else |
| 2740 | c = *p; |
| 2741 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2742 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2743 | { |
| 2744 | /* UUl -> KEEPCAP */ |
| 2745 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2746 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2747 | allcap = FALSE; |
| 2748 | } |
| 2749 | else if (!allcap) |
| 2750 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2751 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2752 | past_second = TRUE; |
| 2753 | } |
| 2754 | |
| 2755 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2756 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2757 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2758 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2759 | return 0; |
| 2760 | } |
| 2761 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2762 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 2763 | /* |
| 2764 | * Free all languages. |
| 2765 | */ |
| 2766 | void |
| 2767 | spell_free_all() |
| 2768 | { |
| 2769 | slang_T *lp; |
| 2770 | buf_T *buf; |
| 2771 | |
| 2772 | /* Go through all buffers and handle 'spelllang'. */ |
| 2773 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2774 | ga_clear(&buf->b_langp); |
| 2775 | |
| 2776 | while (first_lang != NULL) |
| 2777 | { |
| 2778 | lp = first_lang; |
| 2779 | first_lang = lp->sl_next; |
| 2780 | slang_free(lp); |
| 2781 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2782 | |
| 2783 | init_spell_chartab(); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2784 | } |
| 2785 | # endif |
| 2786 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2787 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2788 | /* |
| 2789 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2790 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2791 | */ |
| 2792 | void |
| 2793 | spell_reload() |
| 2794 | { |
| 2795 | buf_T *buf; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2796 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2797 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2798 | /* Initialize the table for spell_iswordp(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2799 | init_spell_chartab(); |
| 2800 | |
| 2801 | /* Unload all allocated memory. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2802 | spell_free_all(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2803 | |
| 2804 | /* Go through all buffers and handle 'spelllang'. */ |
| 2805 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2806 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2807 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2808 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2809 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2810 | { |
| 2811 | FOR_ALL_WINDOWS(wp) |
| 2812 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2813 | { |
| 2814 | (void)did_set_spelllang(buf); |
| 2815 | # ifdef FEAT_WINDOWS |
| 2816 | break; |
| 2817 | # endif |
| 2818 | } |
| 2819 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2820 | } |
| 2821 | } |
| 2822 | # endif |
| 2823 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2824 | /* |
| 2825 | * Reload the spell file "fname" if it's loaded. |
| 2826 | */ |
| 2827 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2828 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2829 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2830 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2831 | { |
| 2832 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2833 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2834 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2835 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2836 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 2837 | { |
| 2838 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2839 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2840 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2841 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2842 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2843 | |
| 2844 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 2845 | * 'spelllang' to get it loaded. */ |
| 2846 | if (added_word && !didit) |
| 2847 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2851 | /* |
| 2852 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2853 | */ |
| 2854 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2855 | #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] | 2856 | and .dic file. */ |
| 2857 | /* |
| 2858 | * Main structure to store the contents of a ".aff" file. |
| 2859 | */ |
| 2860 | typedef struct afffile_S |
| 2861 | { |
| 2862 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2863 | int af_rar; /* RAR ID for rare word */ |
| 2864 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2865 | int af_bad; /* BAD ID for banned word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2866 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2867 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2868 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2869 | } afffile_T; |
| 2870 | |
| 2871 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2872 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2873 | struct affentry_S |
| 2874 | { |
| 2875 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2876 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 2877 | 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] | 2878 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 2879 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2880 | int ae_rare; /* rare affix */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2881 | }; |
| 2882 | |
| 2883 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 2884 | typedef struct affheader_S |
| 2885 | { |
| 2886 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2887 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2888 | int ah_combine; /* suffix may combine with prefix */ |
| 2889 | affentry_T *ah_first; /* first affix entry */ |
| 2890 | } affheader_T; |
| 2891 | |
| 2892 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 2893 | |
| 2894 | /* |
| 2895 | * Structure that is used to store the items in the word tree. This avoids |
| 2896 | * the need to keep track of each allocated thing, it's freed all at once |
| 2897 | * after ":mkspell" is done. |
| 2898 | */ |
| 2899 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 2900 | typedef struct sblock_S sblock_T; |
| 2901 | struct sblock_S |
| 2902 | { |
| 2903 | sblock_T *sb_next; /* next block in list */ |
| 2904 | int sb_used; /* nr of bytes already in use */ |
| 2905 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2906 | }; |
| 2907 | |
| 2908 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2909 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2910 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2911 | typedef struct wordnode_S wordnode_T; |
| 2912 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2913 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2914 | union /* shared to save space */ |
| 2915 | { |
| 2916 | char_u hashkey[6]; /* room for the hash key */ |
| 2917 | int index; /* index in written nodes (valid after first |
| 2918 | round) */ |
| 2919 | } wn_u1; |
| 2920 | union /* shared to save space */ |
| 2921 | { |
| 2922 | wordnode_T *next; /* next node with same hash key */ |
| 2923 | wordnode_T *wnode; /* parent node that will write this node */ |
| 2924 | } wn_u2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2925 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 2926 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 2927 | always sorted) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2928 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 2929 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2930 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 2931 | PREFIXTREE it's the prefcondnr */ |
| 2932 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2933 | }; |
| 2934 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2935 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2936 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2937 | /* |
| 2938 | * Info used while reading the spell files. |
| 2939 | */ |
| 2940 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2941 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2942 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2943 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2944 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2945 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2946 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2947 | sblock_T *si_blocks; /* memory blocks used */ |
| 2948 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2949 | int si_add; /* addition file */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2950 | int si_clear_chartab; /* when TRUE clear char tables */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2951 | int si_region; /* region mask */ |
| 2952 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2953 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2954 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2955 | int si_region_count; /* number of regions supported (1 when there |
| 2956 | are no regions) */ |
| 2957 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2958 | |
| 2959 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 2960 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2961 | char_u *si_sofofr; /* SOFOFROM text */ |
| 2962 | char_u *si_sofoto; /* SOFOTO text */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2963 | int si_followup; /* soundsalike: ? */ |
| 2964 | int si_collapse; /* soundsalike: ? */ |
| 2965 | int si_rem_accents; /* soundsalike: remove accents */ |
| 2966 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2967 | char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2968 | garray_T si_prefcond; /* table with conditions for postponed |
| 2969 | * prefixes, each stored as a string */ |
| 2970 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2971 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2972 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2973 | 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] | 2974 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2975 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 2976 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2977 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2978 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 2979 | 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] | 2980 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 2981 | 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] | 2982 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 2983 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 2984 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 2985 | static void free_blocks __ARGS((sblock_T *bl)); |
| 2986 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2987 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 2988 | 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] | 2989 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2990 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 2991 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2992 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2993 | static void clear_node __ARGS((wordnode_T *node)); |
| 2994 | static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2995 | 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] | 2996 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2997 | |
| 2998 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2999 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3000 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3001 | */ |
| 3002 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3003 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3004 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3005 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3006 | { |
| 3007 | FILE *fd; |
| 3008 | afffile_T *aff; |
| 3009 | char_u rline[MAXLINELEN]; |
| 3010 | char_u *line; |
| 3011 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3012 | #define MAXITEMCNT 7 |
| 3013 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3014 | int itemcnt; |
| 3015 | char_u *p; |
| 3016 | int lnum = 0; |
| 3017 | affheader_T *cur_aff = NULL; |
| 3018 | int aff_todo = 0; |
| 3019 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3020 | char_u *low = NULL; |
| 3021 | char_u *fol = NULL; |
| 3022 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3023 | 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] | 3024 | int do_rep; |
| 3025 | int do_sal; |
| 3026 | int do_map; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3027 | int do_midword; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3028 | int do_sofo; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3029 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3030 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3031 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3032 | /* |
| 3033 | * Open the file. |
| 3034 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3035 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3036 | if (fd == NULL) |
| 3037 | { |
| 3038 | EMSG2(_(e_notopen), fname); |
| 3039 | return NULL; |
| 3040 | } |
| 3041 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3042 | if (spin->si_verbose || p_verbose > 2) |
| 3043 | { |
| 3044 | if (!spin->si_verbose) |
| 3045 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3046 | smsg((char_u *)_("Reading affix file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3047 | out_flush(); |
| 3048 | if (!spin->si_verbose) |
| 3049 | verbose_leave(); |
| 3050 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3051 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3052 | /* Only do REP lines when not done in another .aff file already. */ |
| 3053 | do_rep = spin->si_rep.ga_len == 0; |
| 3054 | |
| 3055 | /* Only do SAL lines when not done in another .aff file already. */ |
| 3056 | do_sal = spin->si_sal.ga_len == 0; |
| 3057 | |
| 3058 | /* Only do MAP lines when not done in another .aff file already. */ |
| 3059 | do_map = spin->si_map.ga_len == 0; |
| 3060 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3061 | /* Only do MIDWORD line when not done in another .aff file already */ |
| 3062 | do_midword = spin->si_midword == NULL; |
| 3063 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3064 | /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */ |
| 3065 | do_sofo = spin->si_sofofr == NULL; |
| 3066 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3067 | /* |
| 3068 | * Allocate and init the afffile_T structure. |
| 3069 | */ |
| 3070 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3071 | if (aff == NULL) |
| 3072 | return NULL; |
| 3073 | hash_init(&aff->af_pref); |
| 3074 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3075 | |
| 3076 | /* |
| 3077 | * Read all the lines in the file one by one. |
| 3078 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3079 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3080 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3081 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3082 | ++lnum; |
| 3083 | |
| 3084 | /* Skip comment lines. */ |
| 3085 | if (*rline == '#') |
| 3086 | continue; |
| 3087 | |
| 3088 | /* Convert from "SET" to 'encoding' when needed. */ |
| 3089 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3090 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3091 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3092 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3093 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3094 | if (pc == NULL) |
| 3095 | { |
| 3096 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3097 | fname, lnum, rline); |
| 3098 | continue; |
| 3099 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3100 | line = pc; |
| 3101 | } |
| 3102 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3103 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3104 | { |
| 3105 | pc = NULL; |
| 3106 | line = rline; |
| 3107 | } |
| 3108 | |
| 3109 | /* Split the line up in white separated items. Put a NUL after each |
| 3110 | * item. */ |
| 3111 | itemcnt = 0; |
| 3112 | for (p = line; ; ) |
| 3113 | { |
| 3114 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 3115 | ++p; |
| 3116 | if (*p == NUL) |
| 3117 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3118 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3119 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3120 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3121 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3122 | ++p; |
| 3123 | if (*p == NUL) |
| 3124 | break; |
| 3125 | *p++ = NUL; |
| 3126 | } |
| 3127 | |
| 3128 | /* Handle non-empty lines. */ |
| 3129 | if (itemcnt > 0) |
| 3130 | { |
| 3131 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 3132 | && aff->af_enc == NULL) |
| 3133 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3134 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3135 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 3136 | aff->af_enc = enc_canonize(items[1]); |
| 3137 | if (aff->af_enc != NULL && !spin->si_ascii |
| 3138 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 3139 | p_enc) == FAIL) |
| 3140 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 3141 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3142 | spin->si_conv.vc_fail = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3143 | #else |
| 3144 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3145 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3146 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3147 | else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2) |
| 3148 | { |
| 3149 | if (do_midword) |
| 3150 | spin->si_midword = vim_strsave(items[1]); |
| 3151 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3152 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 3153 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3154 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3155 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3156 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3157 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3158 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3159 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3160 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 3161 | && aff->af_rar == 0) |
| 3162 | { |
| 3163 | aff->af_rar = items[1][0]; |
| 3164 | if (items[1][1] != NUL) |
| 3165 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3166 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3167 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 3168 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3169 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3170 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3171 | if (items[1][1] != NUL) |
| 3172 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3173 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3174 | else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2 |
| 3175 | && aff->af_bad == 0) |
| 3176 | { |
| 3177 | aff->af_bad = items[1][0]; |
| 3178 | if (items[1][1] != NUL) |
| 3179 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3180 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3181 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 3182 | { |
| 3183 | aff->af_pfxpostpone = TRUE; |
| 3184 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3185 | else if ((STRCMP(items[0], "PFX") == 0 |
| 3186 | || STRCMP(items[0], "SFX") == 0) |
| 3187 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3188 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3189 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3190 | /* Myspell allows extra text after the item, but that might |
| 3191 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 3192 | if (itemcnt > 4 && *items[4] != '#') |
| 3193 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 3194 | fname, lnum, items[4]); |
| 3195 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3196 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3197 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 3198 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3199 | if (cur_aff == NULL) |
| 3200 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3201 | cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3202 | cur_aff->ah_key[1] = NUL; |
| 3203 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3204 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3205 | if (*items[2] == 'Y') |
| 3206 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3207 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3208 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 3209 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3210 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3211 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3212 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3213 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3214 | /* Use a new number in the .spl file later, to be able to |
| 3215 | * handle multiple .aff files. */ |
| 3216 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3217 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3218 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3219 | else |
| 3220 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3221 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3222 | hi = hash_find(tp, cur_aff->ah_key); |
| 3223 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3224 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3225 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 3226 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3227 | aff_todo = 0; |
| 3228 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3229 | else |
| 3230 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3231 | } |
| 3232 | else if ((STRCMP(items[0], "PFX") == 0 |
| 3233 | || STRCMP(items[0], "SFX") == 0) |
| 3234 | && aff_todo > 0 |
| 3235 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3236 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3237 | { |
| 3238 | affentry_T *aff_entry; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3239 | int rare = FALSE; |
| 3240 | int lasti = 5; |
| 3241 | |
| 3242 | /* Check for "rare" after the other info. */ |
| 3243 | if (itemcnt > 5 && STRICMP(items[5], "rare") == 0) |
| 3244 | { |
| 3245 | rare = TRUE; |
| 3246 | lasti = 6; |
| 3247 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3248 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3249 | /* Myspell allows extra text after the item, but that might |
| 3250 | * mean mistakes go unnoticed. Require a comment-starter. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3251 | if (itemcnt > lasti && *items[lasti] != '#') |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3252 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3253 | fname, lnum, items[lasti]); |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3254 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3255 | /* New item for an affix letter. */ |
| 3256 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3257 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 3258 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3259 | if (aff_entry == NULL) |
| 3260 | break; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3261 | aff_entry->ae_rare = rare; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3262 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3263 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3264 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 3265 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3266 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3267 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 3268 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3269 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3270 | /* Don't use an affix entry with non-ASCII characters when |
| 3271 | * "spin->si_ascii" is TRUE. */ |
| 3272 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3273 | || has_non_ascii(aff_entry->ae_add))) |
| 3274 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3275 | aff_entry->ae_next = cur_aff->ah_first; |
| 3276 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3277 | |
| 3278 | if (STRCMP(items[4], ".") != 0) |
| 3279 | { |
| 3280 | char_u buf[MAXLINELEN]; |
| 3281 | |
| 3282 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 3283 | items[4]); |
| 3284 | if (*items[0] == 'P') |
| 3285 | sprintf((char *)buf, "^%s", items[4]); |
| 3286 | else |
| 3287 | sprintf((char *)buf, "%s$", items[4]); |
| 3288 | aff_entry->ae_prog = vim_regcomp(buf, |
| 3289 | RE_MAGIC + RE_STRING); |
| 3290 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3291 | |
| 3292 | /* For postponed prefixes we need an entry in si_prefcond |
| 3293 | * for the condition. Use an existing one if possible. */ |
| 3294 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 3295 | && aff_entry->ae_chop == NULL) |
| 3296 | { |
| 3297 | int idx; |
| 3298 | char_u **pp; |
| 3299 | |
| 3300 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 3301 | --idx) |
| 3302 | { |
| 3303 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 3304 | if (str_equal(p, aff_entry->ae_cond)) |
| 3305 | break; |
| 3306 | } |
| 3307 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 3308 | { |
| 3309 | /* Not found, add a new condition. */ |
| 3310 | idx = spin->si_prefcond.ga_len++; |
| 3311 | pp = ((char_u **)spin->si_prefcond.ga_data) + idx; |
| 3312 | if (aff_entry->ae_cond == NULL) |
| 3313 | *pp = NULL; |
| 3314 | else |
| 3315 | *pp = getroom_save(&spin->si_blocks, |
| 3316 | aff_entry->ae_cond); |
| 3317 | } |
| 3318 | |
| 3319 | /* Add the prefix to the prefix tree. */ |
| 3320 | if (aff_entry->ae_add == NULL) |
| 3321 | p = (char_u *)""; |
| 3322 | else |
| 3323 | p = aff_entry->ae_add; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3324 | tree_add_word(p, spin->si_prefroot, rare ? -2 : -1, |
| 3325 | idx, cur_aff->ah_newID, &spin->si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3326 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3327 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3328 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3329 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 3330 | { |
| 3331 | if (fol != NULL) |
| 3332 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 3333 | fname, lnum); |
| 3334 | else |
| 3335 | fol = vim_strsave(items[1]); |
| 3336 | } |
| 3337 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 3338 | { |
| 3339 | if (low != NULL) |
| 3340 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 3341 | fname, lnum); |
| 3342 | else |
| 3343 | low = vim_strsave(items[1]); |
| 3344 | } |
| 3345 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 3346 | { |
| 3347 | if (upp != NULL) |
| 3348 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 3349 | fname, lnum); |
| 3350 | else |
| 3351 | upp = vim_strsave(items[1]); |
| 3352 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3353 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3354 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3355 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3356 | if (!isdigit(*items[1])) |
| 3357 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 3358 | fname, lnum); |
| 3359 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3360 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 3361 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3362 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3363 | if (do_rep) |
| 3364 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 3365 | } |
| 3366 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 3367 | { |
| 3368 | /* MAP item or count */ |
| 3369 | if (!found_map) |
| 3370 | { |
| 3371 | /* First line contains the count. */ |
| 3372 | found_map = TRUE; |
| 3373 | if (!isdigit(*items[1])) |
| 3374 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 3375 | fname, lnum); |
| 3376 | } |
| 3377 | else if (do_map) |
| 3378 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3379 | int c; |
| 3380 | |
| 3381 | /* Check that every character appears only once. */ |
| 3382 | for (p = items[1]; *p != NUL; ) |
| 3383 | { |
| 3384 | #ifdef FEAT_MBYTE |
| 3385 | c = mb_ptr2char_adv(&p); |
| 3386 | #else |
| 3387 | c = *p++; |
| 3388 | #endif |
| 3389 | if ((spin->si_map.ga_len > 0 |
| 3390 | && vim_strchr(spin->si_map.ga_data, c) |
| 3391 | != NULL) |
| 3392 | || vim_strchr(p, c) != NULL) |
| 3393 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 3394 | fname, lnum); |
| 3395 | } |
| 3396 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3397 | /* We simply concatenate all the MAP strings, separated by |
| 3398 | * slashes. */ |
| 3399 | ga_concat(&spin->si_map, items[1]); |
| 3400 | ga_append(&spin->si_map, '/'); |
| 3401 | } |
| 3402 | } |
| 3403 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 3404 | { |
| 3405 | if (do_sal) |
| 3406 | { |
| 3407 | /* SAL item (sounds-a-like) |
| 3408 | * Either one of the known keys or a from-to pair. */ |
| 3409 | if (STRCMP(items[1], "followup") == 0) |
| 3410 | spin->si_followup = sal_to_bool(items[2]); |
| 3411 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 3412 | spin->si_collapse = sal_to_bool(items[2]); |
| 3413 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 3414 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 3415 | else |
| 3416 | /* when "to" is "_" it means empty */ |
| 3417 | add_fromto(spin, &spin->si_sal, items[1], |
| 3418 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 3419 | : items[2]); |
| 3420 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3421 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3422 | else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2 |
| 3423 | && (!do_sofo || spin->si_sofofr == NULL)) |
| 3424 | { |
| 3425 | if (do_sofo) |
| 3426 | spin->si_sofofr = vim_strsave(items[1]); |
| 3427 | } |
| 3428 | else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2 |
| 3429 | && (!do_sofo || spin->si_sofoto == NULL)) |
| 3430 | { |
| 3431 | if (do_sofo) |
| 3432 | spin->si_sofoto = vim_strsave(items[1]); |
| 3433 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3434 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3435 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 3436 | fname, lnum, items[0]); |
| 3437 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3440 | if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL)) |
| 3441 | smsg((char_u *)_("Missing SOFO%s line in %s"), |
| 3442 | spin->si_sofofr == NULL ? "FROM" : "TO", fname); |
| 3443 | if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0) |
| 3444 | smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname); |
| 3445 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3446 | if (fol != NULL || low != NULL || upp != NULL) |
| 3447 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3448 | if (spin->si_clear_chartab) |
| 3449 | { |
| 3450 | /* Clear the char type tables, don't want to use any of the |
| 3451 | * currently used spell properties. */ |
| 3452 | init_spell_chartab(); |
| 3453 | spin->si_clear_chartab = FALSE; |
| 3454 | } |
| 3455 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3456 | /* |
| 3457 | * Don't write a word table for an ASCII file, so that we don't check |
| 3458 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3459 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3460 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 3461 | */ |
| 3462 | if (!spin->si_ascii |
| 3463 | #ifdef FEAT_MBYTE |
| 3464 | && !enc_utf8 |
| 3465 | #endif |
| 3466 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3467 | { |
| 3468 | if (fol == NULL || low == NULL || upp == NULL) |
| 3469 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 3470 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3471 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3472 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3473 | |
| 3474 | vim_free(fol); |
| 3475 | vim_free(low); |
| 3476 | vim_free(upp); |
| 3477 | } |
| 3478 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3479 | vim_free(pc); |
| 3480 | fclose(fd); |
| 3481 | return aff; |
| 3482 | } |
| 3483 | |
| 3484 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3485 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 3486 | * NULL as equal. |
| 3487 | */ |
| 3488 | static int |
| 3489 | str_equal(s1, s2) |
| 3490 | char_u *s1; |
| 3491 | char_u *s2; |
| 3492 | { |
| 3493 | if (s1 == NULL || s2 == NULL) |
| 3494 | return s1 == s2; |
| 3495 | return STRCMP(s1, s2) == 0; |
| 3496 | } |
| 3497 | |
| 3498 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3499 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 3500 | * They are stored case-folded. |
| 3501 | */ |
| 3502 | static void |
| 3503 | add_fromto(spin, gap, from, to) |
| 3504 | spellinfo_T *spin; |
| 3505 | garray_T *gap; |
| 3506 | char_u *from; |
| 3507 | char_u *to; |
| 3508 | { |
| 3509 | fromto_T *ftp; |
| 3510 | char_u word[MAXWLEN]; |
| 3511 | |
| 3512 | if (ga_grow(gap, 1) == OK) |
| 3513 | { |
| 3514 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 3515 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 3516 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 3517 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 3518 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 3519 | ++gap->ga_len; |
| 3520 | } |
| 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 3525 | */ |
| 3526 | static int |
| 3527 | sal_to_bool(s) |
| 3528 | char_u *s; |
| 3529 | { |
| 3530 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 3531 | } |
| 3532 | |
| 3533 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3534 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 3535 | * When "s" is NULL FALSE is returned. |
| 3536 | */ |
| 3537 | static int |
| 3538 | has_non_ascii(s) |
| 3539 | char_u *s; |
| 3540 | { |
| 3541 | char_u *p; |
| 3542 | |
| 3543 | if (s != NULL) |
| 3544 | for (p = s; *p != NUL; ++p) |
| 3545 | if (*p >= 128) |
| 3546 | return TRUE; |
| 3547 | return FALSE; |
| 3548 | } |
| 3549 | |
| 3550 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3551 | * Free the structure filled by spell_read_aff(). |
| 3552 | */ |
| 3553 | static void |
| 3554 | spell_free_aff(aff) |
| 3555 | afffile_T *aff; |
| 3556 | { |
| 3557 | hashtab_T *ht; |
| 3558 | hashitem_T *hi; |
| 3559 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3560 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3561 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3562 | |
| 3563 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3564 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3565 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3566 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 3567 | { |
| 3568 | todo = ht->ht_used; |
| 3569 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3570 | { |
| 3571 | if (!HASHITEM_EMPTY(hi)) |
| 3572 | { |
| 3573 | --todo; |
| 3574 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3575 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3576 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | if (ht == &aff->af_suff) |
| 3580 | break; |
| 3581 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3582 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3583 | hash_clear(&aff->af_pref); |
| 3584 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3588 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3589 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3590 | */ |
| 3591 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3592 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3593 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3594 | spellinfo_T *spin; |
| 3595 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3596 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3597 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3598 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3599 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3600 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3601 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3602 | char_u *pc; |
| 3603 | char_u *w; |
| 3604 | int l; |
| 3605 | hash_T hash; |
| 3606 | hashitem_T *hi; |
| 3607 | FILE *fd; |
| 3608 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3609 | int non_ascii = 0; |
| 3610 | int retval = OK; |
| 3611 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3612 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3613 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3614 | /* |
| 3615 | * Open the file. |
| 3616 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3617 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3618 | if (fd == NULL) |
| 3619 | { |
| 3620 | EMSG2(_(e_notopen), fname); |
| 3621 | return FAIL; |
| 3622 | } |
| 3623 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3624 | /* The hashtable is only used to detect duplicated words. */ |
| 3625 | hash_init(&ht); |
| 3626 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3627 | spin->si_foldwcount = 0; |
| 3628 | spin->si_keepwcount = 0; |
| 3629 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3630 | if (spin->si_verbose || p_verbose > 2) |
| 3631 | { |
| 3632 | if (!spin->si_verbose) |
| 3633 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3634 | smsg((char_u *)_("Reading dictionary file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3635 | out_flush(); |
| 3636 | if (!spin->si_verbose) |
| 3637 | verbose_leave(); |
| 3638 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3639 | |
| 3640 | /* Read and ignore the first line: word count. */ |
| 3641 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3642 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3643 | EMSG2(_("E760: No word count in %s"), fname); |
| 3644 | |
| 3645 | /* |
| 3646 | * Read all the lines in the file one by one. |
| 3647 | * The words are converted to 'encoding' here, before being added to |
| 3648 | * the hashtable. |
| 3649 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3650 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3651 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3652 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3653 | ++lnum; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3654 | if (line[0] == '#') |
| 3655 | continue; /* comment line */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3656 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3657 | /* Remove CR, LF and white space from the end. White space halfway |
| 3658 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3659 | l = STRLEN(line); |
| 3660 | while (l > 0 && line[l - 1] <= ' ') |
| 3661 | --l; |
| 3662 | if (l == 0) |
| 3663 | continue; /* empty line */ |
| 3664 | line[l] = NUL; |
| 3665 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3666 | /* Find the optional affix names. */ |
| 3667 | afflist = vim_strchr(line, '/'); |
| 3668 | if (afflist != NULL) |
| 3669 | *afflist++ = NUL; |
| 3670 | |
| 3671 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3672 | if (spin->si_ascii && has_non_ascii(line)) |
| 3673 | { |
| 3674 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3675 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3676 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3677 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3678 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3679 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3680 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3681 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3682 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3683 | if (pc == NULL) |
| 3684 | { |
| 3685 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3686 | fname, lnum, line); |
| 3687 | continue; |
| 3688 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3689 | w = pc; |
| 3690 | } |
| 3691 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3692 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3693 | { |
| 3694 | pc = NULL; |
| 3695 | w = line; |
| 3696 | } |
| 3697 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3698 | /* This takes time, print a message now and then. */ |
| 3699 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3700 | { |
| 3701 | vim_snprintf((char *)message, sizeof(message), |
| 3702 | _("line %6d, word %6d - %s"), |
| 3703 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3704 | msg_start(); |
| 3705 | msg_puts_long_attr(message, 0); |
| 3706 | msg_clr_eos(); |
| 3707 | msg_didout = FALSE; |
| 3708 | msg_col = 0; |
| 3709 | out_flush(); |
| 3710 | } |
| 3711 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3712 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 3713 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3714 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3715 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3716 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3717 | if (retval == FAIL) |
| 3718 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3719 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3720 | hash = hash_hash(dw); |
| 3721 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3722 | if (!HASHITEM_EMPTY(hi)) |
| 3723 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3724 | fname, lnum, dw); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3725 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3726 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3727 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3728 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3729 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3730 | if (afflist != NULL) |
| 3731 | { |
| 3732 | /* Check for affix name that stands for keep-case word and stands |
| 3733 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3734 | if (affile->af_kep != NUL |
| 3735 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3736 | flags |= WF_KEEPCAP; |
| 3737 | if (affile->af_rar != NUL |
| 3738 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 3739 | flags |= WF_RARE; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3740 | if (affile->af_bad != NUL |
| 3741 | && vim_strchr(afflist, affile->af_bad) != NULL) |
| 3742 | flags |= WF_BANNED; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3743 | |
| 3744 | if (affile->af_pfxpostpone) |
| 3745 | /* Need to store the list of prefix IDs with the word. */ |
| 3746 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3749 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3750 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3751 | retval = FAIL; |
| 3752 | |
| 3753 | if (afflist != NULL) |
| 3754 | { |
| 3755 | /* Find all matching suffixes and add the resulting words. |
| 3756 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3757 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3758 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3759 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3760 | retval = FAIL; |
| 3761 | |
| 3762 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3763 | if (store_aff_word(dw, spin, afflist, affile, |
| 3764 | &affile->af_pref, NULL, |
| 3765 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3766 | retval = FAIL; |
| 3767 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3770 | if (spin->si_ascii && non_ascii > 0) |
| 3771 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3772 | non_ascii); |
| 3773 | hash_clear(&ht); |
| 3774 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3775 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3776 | return retval; |
| 3777 | } |
| 3778 | |
| 3779 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3780 | * Get the list of prefix IDs from the affix list "afflist". |
| 3781 | * Used for PFXPOSTPONE. |
| 3782 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 3783 | * or when out of memory. |
| 3784 | */ |
| 3785 | static char_u * |
| 3786 | get_pfxlist(affile, afflist, blp) |
| 3787 | afffile_T *affile; |
| 3788 | char_u *afflist; |
| 3789 | sblock_T **blp; |
| 3790 | { |
| 3791 | char_u *p; |
| 3792 | int cnt; |
| 3793 | int round; |
| 3794 | char_u *res = NULL; |
| 3795 | char_u key[2]; |
| 3796 | hashitem_T *hi; |
| 3797 | |
| 3798 | key[1] = NUL; |
| 3799 | |
| 3800 | /* round 1: count the number of prefix IDs. |
| 3801 | * round 2: move prefix IDs to "res" */ |
| 3802 | for (round = 1; round <= 2; ++round) |
| 3803 | { |
| 3804 | cnt = 0; |
| 3805 | for (p = afflist; *p != NUL; ++p) |
| 3806 | { |
| 3807 | key[0] = *p; |
| 3808 | hi = hash_find(&affile->af_pref, key); |
| 3809 | if (!HASHITEM_EMPTY(hi)) |
| 3810 | { |
| 3811 | /* This is a prefix ID, use the new number. */ |
| 3812 | if (round == 2) |
| 3813 | res[cnt] = HI2AH(hi)->ah_newID; |
| 3814 | ++cnt; |
| 3815 | } |
| 3816 | } |
| 3817 | if (round == 1 && cnt > 0) |
| 3818 | res = getroom(blp, cnt + 1); |
| 3819 | if (res == NULL) |
| 3820 | break; |
| 3821 | } |
| 3822 | |
| 3823 | if (res != NULL) |
| 3824 | res[cnt] = NUL; |
| 3825 | return res; |
| 3826 | } |
| 3827 | |
| 3828 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3829 | * Apply affixes to a word and store the resulting words. |
| 3830 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 3831 | * prefixes or suffixes. |
| 3832 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 3833 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3834 | * |
| 3835 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3836 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3837 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3838 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3839 | char_u *word; /* basic word start */ |
| 3840 | spellinfo_T *spin; /* spell info */ |
| 3841 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3842 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3843 | hashtab_T *ht; |
| 3844 | hashtab_T *xht; |
| 3845 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3846 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3847 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3848 | { |
| 3849 | int todo; |
| 3850 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3851 | affheader_T *ah; |
| 3852 | affentry_T *ae; |
| 3853 | regmatch_T regmatch; |
| 3854 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3855 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3856 | int i; |
| 3857 | char_u *p; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3858 | int use_flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3859 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3860 | todo = ht->ht_used; |
| 3861 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3862 | { |
| 3863 | if (!HASHITEM_EMPTY(hi)) |
| 3864 | { |
| 3865 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3866 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3867 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3868 | /* Check that the affix combines, if required, and that the word |
| 3869 | * supports this affix. */ |
| 3870 | if ((!comb || ah->ah_combine) |
| 3871 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3872 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3873 | /* Loop over all affix entries with this name. */ |
| 3874 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3875 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3876 | /* Check the condition. It's not logical to match case |
| 3877 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3878 | * Myspell. |
| 3879 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 3880 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3881 | regmatch.regprog = ae->ae_prog; |
| 3882 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3883 | if ((xht != NULL || !affile->af_pfxpostpone |
| 3884 | || ae->ae_chop != NULL) |
| 3885 | && (ae->ae_prog == NULL |
| 3886 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3887 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3888 | /* Match. Remove the chop and add the affix. */ |
| 3889 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3890 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3891 | /* prefix: chop/add at the start of the word */ |
| 3892 | if (ae->ae_add == NULL) |
| 3893 | *newword = NUL; |
| 3894 | else |
| 3895 | STRCPY(newword, ae->ae_add); |
| 3896 | p = word; |
| 3897 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3898 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3899 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3900 | #ifdef FEAT_MBYTE |
| 3901 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3902 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3903 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3904 | for ( ; i > 0; --i) |
| 3905 | mb_ptr_adv(p); |
| 3906 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3907 | else |
| 3908 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3909 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3910 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3911 | STRCAT(newword, p); |
| 3912 | } |
| 3913 | else |
| 3914 | { |
| 3915 | /* suffix: chop/add at the end of the word */ |
| 3916 | STRCPY(newword, word); |
| 3917 | if (ae->ae_chop != NULL) |
| 3918 | { |
| 3919 | /* Remove chop string. */ |
| 3920 | p = newword + STRLEN(newword); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3921 | i = MB_CHARLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3922 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3923 | mb_ptr_back(newword, p); |
| 3924 | *p = NUL; |
| 3925 | } |
| 3926 | if (ae->ae_add != NULL) |
| 3927 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3930 | /* Obey the "rare" flag of the affix. */ |
| 3931 | if (ae->ae_rare) |
| 3932 | use_flags = flags | WF_RARE; |
| 3933 | else |
| 3934 | use_flags = flags; |
| 3935 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3936 | /* Store the modified word. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3937 | if (store_word(newword, spin, use_flags, |
| 3938 | spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3939 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3940 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3941 | /* When added a suffix and combining is allowed also |
| 3942 | * try adding prefixes additionally. */ |
| 3943 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3944 | if (store_aff_word(newword, spin, afflist, affile, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3945 | xht, NULL, TRUE, use_flags, pfxlist) |
| 3946 | == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3947 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | } |
| 3952 | } |
| 3953 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3954 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3958 | * Read a file with a list of words. |
| 3959 | */ |
| 3960 | static int |
| 3961 | spell_read_wordfile(fname, spin) |
| 3962 | char_u *fname; |
| 3963 | spellinfo_T *spin; |
| 3964 | { |
| 3965 | FILE *fd; |
| 3966 | long lnum = 0; |
| 3967 | char_u rline[MAXLINELEN]; |
| 3968 | char_u *line; |
| 3969 | char_u *pc = NULL; |
| 3970 | int l; |
| 3971 | int retval = OK; |
| 3972 | int did_word = FALSE; |
| 3973 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3974 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3975 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3976 | |
| 3977 | /* |
| 3978 | * Open the file. |
| 3979 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3980 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3981 | if (fd == NULL) |
| 3982 | { |
| 3983 | EMSG2(_(e_notopen), fname); |
| 3984 | return FAIL; |
| 3985 | } |
| 3986 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3987 | if (spin->si_verbose || p_verbose > 2) |
| 3988 | { |
| 3989 | if (!spin->si_verbose) |
| 3990 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3991 | smsg((char_u *)_("Reading word file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3992 | out_flush(); |
| 3993 | if (!spin->si_verbose) |
| 3994 | verbose_leave(); |
| 3995 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3996 | |
| 3997 | /* |
| 3998 | * Read all the lines in the file one by one. |
| 3999 | */ |
| 4000 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 4001 | { |
| 4002 | line_breakcheck(); |
| 4003 | ++lnum; |
| 4004 | |
| 4005 | /* Skip comment lines. */ |
| 4006 | if (*rline == '#') |
| 4007 | continue; |
| 4008 | |
| 4009 | /* Remove CR, LF and white space from the end. */ |
| 4010 | l = STRLEN(rline); |
| 4011 | while (l > 0 && rline[l - 1] <= ' ') |
| 4012 | --l; |
| 4013 | if (l == 0) |
| 4014 | continue; /* empty or blank line */ |
| 4015 | rline[l] = NUL; |
| 4016 | |
| 4017 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 4018 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4019 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4020 | if (spin->si_conv.vc_type != CONV_NONE) |
| 4021 | { |
| 4022 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 4023 | if (pc == NULL) |
| 4024 | { |
| 4025 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 4026 | fname, lnum, rline); |
| 4027 | continue; |
| 4028 | } |
| 4029 | line = pc; |
| 4030 | } |
| 4031 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4032 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4033 | { |
| 4034 | pc = NULL; |
| 4035 | line = rline; |
| 4036 | } |
| 4037 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4038 | flags = 0; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4039 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4040 | |
| 4041 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4042 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4043 | ++line; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4044 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4045 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4046 | { |
| 4047 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4048 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 4049 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4050 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4051 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 4052 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4053 | else |
| 4054 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4055 | #ifdef FEAT_MBYTE |
| 4056 | char_u *enc; |
| 4057 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4058 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4059 | line += 10; |
| 4060 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4061 | if (enc != NULL && !spin->si_ascii |
| 4062 | && convert_setup(&spin->si_conv, enc, |
| 4063 | p_enc) == FAIL) |
| 4064 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4065 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4066 | vim_free(enc); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4067 | spin->si_conv.vc_fail = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4068 | #else |
| 4069 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 4070 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4071 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4072 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4073 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4074 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4075 | if (STRNCMP(line, "regions=", 8) == 0) |
| 4076 | { |
| 4077 | if (spin->si_region_count > 1) |
| 4078 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 4079 | fname, lnum, line); |
| 4080 | else |
| 4081 | { |
| 4082 | line += 8; |
| 4083 | if (STRLEN(line) > 16) |
| 4084 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 4085 | fname, lnum, line); |
| 4086 | else |
| 4087 | { |
| 4088 | spin->si_region_count = STRLEN(line) / 2; |
| 4089 | STRCPY(spin->si_region_name, line); |
| 4090 | } |
| 4091 | } |
| 4092 | continue; |
| 4093 | } |
| 4094 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4095 | if (*line == '=') |
| 4096 | { |
| 4097 | /* keep-case word */ |
| 4098 | flags |= WF_KEEPCAP; |
| 4099 | ++line; |
| 4100 | } |
| 4101 | |
| 4102 | if (*line == '!') |
| 4103 | { |
| 4104 | /* Bad, bad, wicked word. */ |
| 4105 | flags |= WF_BANNED; |
| 4106 | ++line; |
| 4107 | } |
| 4108 | else if (*line == '?') |
| 4109 | { |
| 4110 | /* Rare word. */ |
| 4111 | flags |= WF_RARE; |
| 4112 | ++line; |
| 4113 | } |
| 4114 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4115 | if (VIM_ISDIGIT(*line)) |
| 4116 | { |
| 4117 | /* region number(s) */ |
| 4118 | regionmask = 0; |
| 4119 | while (VIM_ISDIGIT(*line)) |
| 4120 | { |
| 4121 | l = *line - '0'; |
| 4122 | if (l > spin->si_region_count) |
| 4123 | { |
| 4124 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
| 4125 | fname, lnum, line); |
| 4126 | break; |
| 4127 | } |
| 4128 | regionmask |= 1 << (l - 1); |
| 4129 | ++line; |
| 4130 | } |
| 4131 | flags |= WF_REGION; |
| 4132 | } |
| 4133 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4134 | if (flags == 0) |
| 4135 | { |
| 4136 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4137 | fname, lnum, line); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4138 | continue; |
| 4139 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 4143 | if (spin->si_ascii && has_non_ascii(line)) |
| 4144 | { |
| 4145 | ++non_ascii; |
| 4146 | continue; |
| 4147 | } |
| 4148 | |
| 4149 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4150 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4151 | { |
| 4152 | retval = FAIL; |
| 4153 | break; |
| 4154 | } |
| 4155 | did_word = TRUE; |
| 4156 | } |
| 4157 | |
| 4158 | vim_free(pc); |
| 4159 | fclose(fd); |
| 4160 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4161 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 4162 | { |
| 4163 | if (p_verbose > 2) |
| 4164 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4165 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 4166 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4167 | if (p_verbose > 2) |
| 4168 | verbose_leave(); |
| 4169 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4170 | return retval; |
| 4171 | } |
| 4172 | |
| 4173 | /* |
| 4174 | * Get part of an sblock_T, "len" bytes long. |
| 4175 | * This avoids calling free() for every little struct we use. |
| 4176 | * The memory is cleared to all zeros. |
| 4177 | * Returns NULL when out of memory. |
| 4178 | */ |
| 4179 | static void * |
| 4180 | getroom(blp, len) |
| 4181 | sblock_T **blp; |
| 4182 | size_t len; /* length needed */ |
| 4183 | { |
| 4184 | char_u *p; |
| 4185 | sblock_T *bl = *blp; |
| 4186 | |
| 4187 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 4188 | { |
| 4189 | /* Allocate a block of memory. This is not freed until much later. */ |
| 4190 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 4191 | if (bl == NULL) |
| 4192 | return NULL; |
| 4193 | bl->sb_next = *blp; |
| 4194 | *blp = bl; |
| 4195 | bl->sb_used = 0; |
| 4196 | } |
| 4197 | |
| 4198 | p = bl->sb_data + bl->sb_used; |
| 4199 | bl->sb_used += len; |
| 4200 | |
| 4201 | return p; |
| 4202 | } |
| 4203 | |
| 4204 | /* |
| 4205 | * Make a copy of a string into memory allocated with getroom(). |
| 4206 | */ |
| 4207 | static char_u * |
| 4208 | getroom_save(blp, s) |
| 4209 | sblock_T **blp; |
| 4210 | char_u *s; |
| 4211 | { |
| 4212 | char_u *sc; |
| 4213 | |
| 4214 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 4215 | if (sc != NULL) |
| 4216 | STRCPY(sc, s); |
| 4217 | return sc; |
| 4218 | } |
| 4219 | |
| 4220 | |
| 4221 | /* |
| 4222 | * Free the list of allocated sblock_T. |
| 4223 | */ |
| 4224 | static void |
| 4225 | free_blocks(bl) |
| 4226 | sblock_T *bl; |
| 4227 | { |
| 4228 | sblock_T *next; |
| 4229 | |
| 4230 | while (bl != NULL) |
| 4231 | { |
| 4232 | next = bl->sb_next; |
| 4233 | vim_free(bl); |
| 4234 | bl = next; |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | /* |
| 4239 | * Allocate the root of a word tree. |
| 4240 | */ |
| 4241 | static wordnode_T * |
| 4242 | wordtree_alloc(blp) |
| 4243 | sblock_T **blp; |
| 4244 | { |
| 4245 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 4246 | } |
| 4247 | |
| 4248 | /* |
| 4249 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4250 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 4251 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4252 | * 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] | 4253 | * When "pfxlist" is not NULL store the word for each prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4254 | */ |
| 4255 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4256 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4257 | char_u *word; |
| 4258 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4259 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4260 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4261 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4262 | { |
| 4263 | int len = STRLEN(word); |
| 4264 | int ct = captype(word, word + len); |
| 4265 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4266 | int res = OK; |
| 4267 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4268 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4269 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4270 | for (p = pfxlist; res == OK; ++p) |
| 4271 | { |
| 4272 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 4273 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4274 | if (p == NULL || *p == NUL) |
| 4275 | break; |
| 4276 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4277 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4278 | |
| 4279 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4280 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4281 | for (p = pfxlist; res == OK; ++p) |
| 4282 | { |
| 4283 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 4284 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4285 | if (p == NULL || *p == NUL) |
| 4286 | break; |
| 4287 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4288 | ++spin->si_keepwcount; |
| 4289 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4290 | return res; |
| 4291 | } |
| 4292 | |
| 4293 | /* |
| 4294 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4295 | * When "flags" < 0 we are adding to the prefix tree where flags is used for |
| 4296 | * "rare" and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4297 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4298 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4299 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4300 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4301 | char_u *word; |
| 4302 | wordnode_T *root; |
| 4303 | int flags; |
| 4304 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4305 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4306 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4307 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4308 | wordnode_T *node = root; |
| 4309 | wordnode_T *np; |
| 4310 | wordnode_T **prev = NULL; |
| 4311 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4312 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4313 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 4314 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4315 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4316 | /* Look for the sibling that has the same character. They are sorted |
| 4317 | * 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] | 4318 | * higher byte value. For zero bytes (end of word) the sorting is |
| 4319 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4320 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4321 | while (node != NULL |
| 4322 | && (node->wn_byte < word[i] |
| 4323 | || (node->wn_byte == NUL |
| 4324 | && (flags < 0 |
| 4325 | ? node->wn_prefixID < prefixID |
| 4326 | : node->wn_flags < (flags & 0xff) |
| 4327 | || (node->wn_flags == (flags & 0xff) |
| 4328 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4329 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4330 | prev = &node->wn_sibling; |
| 4331 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4332 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4333 | if (node == NULL |
| 4334 | || node->wn_byte != word[i] |
| 4335 | || (word[i] == NUL |
| 4336 | && (flags < 0 |
| 4337 | || node->wn_flags != (flags & 0xff) |
| 4338 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4339 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4340 | /* Allocate a new node. */ |
| 4341 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 4342 | if (np == NULL) |
| 4343 | return FAIL; |
| 4344 | np->wn_byte = word[i]; |
| 4345 | *prev = np; |
| 4346 | np->wn_sibling = node; |
| 4347 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4348 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4349 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4350 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4351 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4352 | node->wn_flags = flags; |
| 4353 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4354 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4355 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4356 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4357 | prev = &node->wn_child; |
| 4358 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4365 | * Compress a tree: find tails that are identical and can be shared. |
| 4366 | */ |
| 4367 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4368 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4369 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4370 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4371 | { |
| 4372 | hashtab_T ht; |
| 4373 | int n; |
| 4374 | int tot = 0; |
| 4375 | |
| 4376 | if (root != NULL) |
| 4377 | { |
| 4378 | hash_init(&ht); |
| 4379 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4380 | if (spin->si_verbose || p_verbose > 2) |
| 4381 | { |
| 4382 | if (!spin->si_verbose) |
| 4383 | verbose_enter(); |
| 4384 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4385 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4386 | if (p_verbose > 2) |
| 4387 | verbose_leave(); |
| 4388 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4389 | hash_clear(&ht); |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | /* |
| 4394 | * Compress a node, its siblings and its children, depth first. |
| 4395 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4396 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4397 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4398 | node_compress(node, ht, tot) |
| 4399 | wordnode_T *node; |
| 4400 | hashtab_T *ht; |
| 4401 | int *tot; /* total count of nodes before compressing, |
| 4402 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4403 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4404 | wordnode_T *np; |
| 4405 | wordnode_T *tp; |
| 4406 | wordnode_T *child; |
| 4407 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4408 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4409 | int len = 0; |
| 4410 | unsigned nr, n; |
| 4411 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4412 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4413 | /* |
| 4414 | * Go through the list of siblings. Compress each child and then try |
| 4415 | * finding an identical child to replace it. |
| 4416 | * Note that with "child" we mean not just the node that is pointed to, |
| 4417 | * but the whole list of siblings, of which the node is the first. |
| 4418 | */ |
| 4419 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4420 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4421 | ++len; |
| 4422 | if ((child = np->wn_child) != NULL) |
| 4423 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4424 | /* Compress the child. This fills hashkey. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4425 | compressed += node_compress(child, ht, tot); |
| 4426 | |
| 4427 | /* Try to find an identical child. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4428 | hash = hash_hash(child->wn_u1.hashkey); |
| 4429 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4430 | tp = NULL; |
| 4431 | if (!HASHITEM_EMPTY(hi)) |
| 4432 | { |
| 4433 | /* There are children with an identical hash value. Now check |
| 4434 | * if there is one that is really identical. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4435 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4436 | if (node_equal(child, tp)) |
| 4437 | { |
| 4438 | /* Found one! Now use that child in place of the |
| 4439 | * current one. This means the current child is |
| 4440 | * dropped from the tree. */ |
| 4441 | np->wn_child = tp; |
| 4442 | ++compressed; |
| 4443 | break; |
| 4444 | } |
| 4445 | if (tp == NULL) |
| 4446 | { |
| 4447 | /* No other child with this hash value equals the child of |
| 4448 | * the node, add it to the linked list after the first |
| 4449 | * item. */ |
| 4450 | tp = HI2WN(hi); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4451 | child->wn_u2.next = tp->wn_u2.next; |
| 4452 | tp->wn_u2.next = child; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | else |
| 4456 | /* No other child has this hash value, add it to the |
| 4457 | * hashtable. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4458 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4459 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4460 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4461 | *tot += len; |
| 4462 | |
| 4463 | /* |
| 4464 | * Make a hash key for the node and its siblings, so that we can quickly |
| 4465 | * find a lookalike node. This must be done after compressing the sibling |
| 4466 | * list, otherwise the hash key would become invalid by the compression. |
| 4467 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4468 | node->wn_u1.hashkey[0] = len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4469 | nr = 0; |
| 4470 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4471 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4472 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4473 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 4474 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4475 | else |
| 4476 | /* byte node: use the byte value and the child pointer */ |
| 4477 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 4478 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4479 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4480 | |
| 4481 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 4482 | n = nr & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4483 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4484 | n = (nr >> 8) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4485 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4486 | n = (nr >> 16) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4487 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4488 | n = (nr >> 24) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4489 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 4490 | node->wn_u1.hashkey[5] = NUL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4491 | |
| 4492 | return compressed; |
| 4493 | } |
| 4494 | |
| 4495 | /* |
| 4496 | * Return TRUE when two nodes have identical siblings and children. |
| 4497 | */ |
| 4498 | static int |
| 4499 | node_equal(n1, n2) |
| 4500 | wordnode_T *n1; |
| 4501 | wordnode_T *n2; |
| 4502 | { |
| 4503 | wordnode_T *p1; |
| 4504 | wordnode_T *p2; |
| 4505 | |
| 4506 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 4507 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 4508 | if (p1->wn_byte != p2->wn_byte |
| 4509 | || (p1->wn_byte == NUL |
| 4510 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4511 | || p1->wn_region != p2->wn_region |
| 4512 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4513 | : (p1->wn_child != p2->wn_child))) |
| 4514 | break; |
| 4515 | |
| 4516 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | /* |
| 4520 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 4521 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4522 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4523 | put_bytes(fd, nr, len) |
| 4524 | FILE *fd; |
| 4525 | long_u nr; |
| 4526 | int len; |
| 4527 | { |
| 4528 | int i; |
| 4529 | |
| 4530 | for (i = len - 1; i >= 0; --i) |
| 4531 | putc((int)(nr >> (i * 8)), fd); |
| 4532 | } |
| 4533 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4534 | static int |
| 4535 | #ifdef __BORLANDC__ |
| 4536 | _RTLENTRYF |
| 4537 | #endif |
| 4538 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 4539 | |
| 4540 | /* |
| 4541 | * Function given to qsort() to sort the REP items on "from" string. |
| 4542 | */ |
| 4543 | static int |
| 4544 | #ifdef __BORLANDC__ |
| 4545 | _RTLENTRYF |
| 4546 | #endif |
| 4547 | rep_compare(s1, s2) |
| 4548 | const void *s1; |
| 4549 | const void *s2; |
| 4550 | { |
| 4551 | fromto_T *p1 = (fromto_T *)s1; |
| 4552 | fromto_T *p2 = (fromto_T *)s2; |
| 4553 | |
| 4554 | return STRCMP(p1->ft_from, p2->ft_from); |
| 4555 | } |
| 4556 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4557 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4558 | * Write the Vim spell file "fname". |
| 4559 | */ |
| 4560 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4561 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4562 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4563 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4564 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4565 | FILE *fd; |
| 4566 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4567 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4568 | wordnode_T *tree; |
| 4569 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4570 | int i; |
| 4571 | int l; |
| 4572 | garray_T *gap; |
| 4573 | fromto_T *ftp; |
| 4574 | char_u *p; |
| 4575 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4576 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4577 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4578 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4579 | { |
| 4580 | EMSG2(_(e_notopen), fname); |
| 4581 | return; |
| 4582 | } |
| 4583 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4584 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4585 | * <charflagslen> <charflags> |
| 4586 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4587 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4588 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4589 | |
| 4590 | /* <fileID> */ |
| 4591 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 4592 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4593 | |
| 4594 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4595 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4596 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4597 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 4598 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 4599 | (size_t)1, fd); |
| 4600 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4601 | } |
| 4602 | else |
| 4603 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4604 | putc(0, fd); |
| 4605 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4608 | /* |
| 4609 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4610 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 4611 | * 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] | 4612 | * 'encoding'. |
| 4613 | * Also skip this for an .add.spl file, the main spell file must contain |
| 4614 | * the table (avoids that it conflicts). File is shorter too. |
| 4615 | */ |
| 4616 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4617 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4618 | putc(0, fd); |
| 4619 | putc(0, fd); |
| 4620 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4621 | } |
| 4622 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4623 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4624 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4625 | |
| 4626 | if (spin->si_midword == NULL) |
| 4627 | put_bytes(fd, 0L, 2); /* <midwordlen> */ |
| 4628 | else |
| 4629 | { |
| 4630 | i = STRLEN(spin->si_midword); |
| 4631 | put_bytes(fd, (long_u)i, 2); /* <midwordlen> */ |
| 4632 | fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */ |
| 4633 | } |
| 4634 | |
| 4635 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4636 | /* Write the prefix conditions. */ |
| 4637 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 4638 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4639 | /* <SUGGEST> : <repcount> <rep> ... |
| 4640 | * <salflags> <salcount> <sal> ... |
| 4641 | * <maplen> <mapstr> */ |
| 4642 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4643 | /* Sort the REP items. */ |
| 4644 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 4645 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4646 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4647 | /* round 1: REP items |
| 4648 | * round 2: SAL items (unless SOFO is used) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4649 | for (round = 1; round <= 2; ++round) |
| 4650 | { |
| 4651 | if (round == 1) |
| 4652 | gap = &spin->si_rep; |
| 4653 | else |
| 4654 | { |
| 4655 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4656 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4657 | i = 0; |
| 4658 | if (spin->si_followup) |
| 4659 | i |= SAL_F0LLOWUP; |
| 4660 | if (spin->si_collapse) |
| 4661 | i |= SAL_COLLAPSE; |
| 4662 | if (spin->si_rem_accents) |
| 4663 | i |= SAL_REM_ACCENTS; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4664 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 4665 | i |= SAL_SOFO; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4666 | putc(i, fd); /* <salflags> */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4667 | if (i & SAL_SOFO) |
| 4668 | break; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 4672 | for (i = 0; i < gap->ga_len; ++i) |
| 4673 | { |
| 4674 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 4675 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 4676 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 4677 | for (rr = 1; rr <= 2; ++rr) |
| 4678 | { |
| 4679 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 4680 | l = STRLEN(p); |
| 4681 | putc(l, fd); |
| 4682 | fwrite(p, l, (size_t)1, fd); |
| 4683 | } |
| 4684 | } |
| 4685 | } |
| 4686 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4687 | /* SOFOFROM and SOFOTO */ |
| 4688 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 4689 | { |
| 4690 | put_bytes(fd, 1L, 2); /* <salcount> */ |
| 4691 | |
| 4692 | l = STRLEN(spin->si_sofofr); |
| 4693 | put_bytes(fd, (long_u)l, 2); /* <salfromlen> */ |
| 4694 | fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */ |
| 4695 | |
| 4696 | l = STRLEN(spin->si_sofoto); |
| 4697 | put_bytes(fd, (long_u)l, 2); /* <saltolen> */ |
| 4698 | fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */ |
| 4699 | } |
| 4700 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4701 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 4702 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 4703 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 4704 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4705 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4706 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4707 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4708 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4709 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4710 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4711 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4712 | if (round == 1) |
| 4713 | tree = spin->si_foldroot; |
| 4714 | else if (round == 2) |
| 4715 | tree = spin->si_keeproot; |
| 4716 | else |
| 4717 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4718 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4719 | /* Clear the index and wnode fields in the tree. */ |
| 4720 | clear_node(tree); |
| 4721 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4722 | /* Count the number of nodes. Needed to be able to allocate the |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4723 | * memory when reading the nodes. Also fills in index for shared |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4724 | * nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4725 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4726 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4727 | /* number of nodes in 4 bytes */ |
| 4728 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4729 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4730 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4731 | /* Write the nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4732 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4735 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4739 | * Clear the index and wnode fields of "node", it siblings and its |
| 4740 | * children. This is needed because they are a union with other items to save |
| 4741 | * space. |
| 4742 | */ |
| 4743 | static void |
| 4744 | clear_node(node) |
| 4745 | wordnode_T *node; |
| 4746 | { |
| 4747 | wordnode_T *np; |
| 4748 | |
| 4749 | if (node != NULL) |
| 4750 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4751 | { |
| 4752 | np->wn_u1.index = 0; |
| 4753 | np->wn_u2.wnode = NULL; |
| 4754 | |
| 4755 | if (np->wn_byte != NUL) |
| 4756 | clear_node(np->wn_child); |
| 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | |
| 4761 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4762 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4763 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4764 | * This first writes the list of possible bytes (siblings). Then for each |
| 4765 | * byte recursively write the children. |
| 4766 | * |
| 4767 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 4768 | * are made about the indexes (so that we don't have to write them in the |
| 4769 | * file). |
| 4770 | * |
| 4771 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4772 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4773 | static int |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4774 | put_node(fd, node, index, regionmask, prefixtree) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4775 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4776 | wordnode_T *node; |
| 4777 | int index; |
| 4778 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4779 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4780 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4781 | int newindex = index; |
| 4782 | int siblingcount = 0; |
| 4783 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4784 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4785 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4786 | /* If "node" is zero the tree is empty. */ |
| 4787 | if (node == NULL) |
| 4788 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4789 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4790 | /* Store the index where this node is written. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4791 | node->wn_u1.index = index; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4792 | |
| 4793 | /* Count the number of siblings. */ |
| 4794 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4795 | ++siblingcount; |
| 4796 | |
| 4797 | /* Write the sibling count. */ |
| 4798 | if (fd != NULL) |
| 4799 | putc(siblingcount, fd); /* <siblingcount> */ |
| 4800 | |
| 4801 | /* Write each sibling byte and optionally extra info. */ |
| 4802 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4803 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4804 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4805 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4806 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4807 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4808 | /* For a NUL byte (end of word) write the flags etc. */ |
| 4809 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4810 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4811 | /* In PREFIXTREE write the required prefixID and the |
| 4812 | * associated condition nr (stored in wn_region). */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4813 | if (np->wn_flags == (char_u)-2) |
| 4814 | putc(BY_FLAGS, fd); /* <byte> rare */ |
| 4815 | else |
| 4816 | putc(BY_NOFLAGS, fd); /* <byte> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4817 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4818 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4819 | } |
| 4820 | else |
| 4821 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4822 | /* For word trees we write the flag/region items. */ |
| 4823 | flags = np->wn_flags; |
| 4824 | if (regionmask != 0 && np->wn_region != regionmask) |
| 4825 | flags |= WF_REGION; |
| 4826 | if (np->wn_prefixID != 0) |
| 4827 | flags |= WF_PFX; |
| 4828 | if (flags == 0) |
| 4829 | { |
| 4830 | /* word without flags or region */ |
| 4831 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 4832 | } |
| 4833 | else |
| 4834 | { |
| 4835 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4836 | putc(flags, fd); /* <flags> */ |
| 4837 | if (flags & WF_REGION) |
| 4838 | putc(np->wn_region, fd); /* <region> */ |
| 4839 | if (flags & WF_PFX) |
| 4840 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4841 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4842 | } |
| 4843 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4844 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4845 | else |
| 4846 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4847 | if (np->wn_child->wn_u1.index != 0 |
| 4848 | && np->wn_child->wn_u2.wnode != node) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4849 | { |
| 4850 | /* The child is written elsewhere, write the reference. */ |
| 4851 | if (fd != NULL) |
| 4852 | { |
| 4853 | putc(BY_INDEX, fd); /* <byte> */ |
| 4854 | /* <nodeidx> */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4855 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4856 | } |
| 4857 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4858 | else if (np->wn_child->wn_u2.wnode == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4859 | /* We will write the child below and give it an index. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4860 | np->wn_child->wn_u2.wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4861 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4862 | if (fd != NULL) |
| 4863 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 4864 | { |
| 4865 | EMSG(_(e_write)); |
| 4866 | return 0; |
| 4867 | } |
| 4868 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4869 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4870 | |
| 4871 | /* Space used in the array when reading: one for each sibling and one for |
| 4872 | * the count. */ |
| 4873 | newindex += siblingcount + 1; |
| 4874 | |
| 4875 | /* Recursively dump the children of each sibling. */ |
| 4876 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4877 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 4878 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4879 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4880 | |
| 4881 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | |
| 4885 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4886 | * ":mkspell [-ascii] outfile infile ..." |
| 4887 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4888 | */ |
| 4889 | void |
| 4890 | ex_mkspell(eap) |
| 4891 | exarg_T *eap; |
| 4892 | { |
| 4893 | int fcount; |
| 4894 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4895 | char_u *arg = eap->arg; |
| 4896 | int ascii = FALSE; |
| 4897 | |
| 4898 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 4899 | { |
| 4900 | ascii = TRUE; |
| 4901 | arg = skipwhite(arg + 6); |
| 4902 | } |
| 4903 | |
| 4904 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 4905 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 4906 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4907 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4908 | FreeWild(fcount, fnames); |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | /* |
| 4913 | * Create a Vim spell file from one or more word lists. |
| 4914 | * "fnames[0]" is the output file name. |
| 4915 | * "fnames[fcount - 1]" is the last input file name. |
| 4916 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 4917 | * and ".spl" is appended to make the output file name. |
| 4918 | */ |
| 4919 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4920 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4921 | int fcount; |
| 4922 | char_u **fnames; |
| 4923 | int ascii; /* -ascii argument given */ |
| 4924 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4925 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4926 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4927 | char_u fname[MAXPATHL]; |
| 4928 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4929 | char_u **innames; |
| 4930 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4931 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4932 | int i; |
| 4933 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4934 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4935 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4936 | spellinfo_T spin; |
| 4937 | |
| 4938 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4939 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4940 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4941 | spin.si_followup = TRUE; |
| 4942 | spin.si_rem_accents = TRUE; |
| 4943 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 4944 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 4945 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4946 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4947 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4948 | /* default: fnames[0] is output file, following are input files */ |
| 4949 | innames = &fnames[1]; |
| 4950 | incount = fcount - 1; |
| 4951 | |
| 4952 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4953 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4954 | len = STRLEN(fnames[0]); |
| 4955 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 4956 | { |
| 4957 | /* For ":mkspell path/en.latin1.add" output file is |
| 4958 | * "path/en.latin1.add.spl". */ |
| 4959 | innames = &fnames[0]; |
| 4960 | incount = 1; |
| 4961 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 4962 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4963 | else if (fcount == 1) |
| 4964 | { |
| 4965 | /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */ |
| 4966 | innames = &fnames[0]; |
| 4967 | incount = 1; |
| 4968 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4969 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4970 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4971 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 4972 | { |
| 4973 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4974 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4975 | } |
| 4976 | else |
| 4977 | /* Name should be language, make the file name from it. */ |
| 4978 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4979 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4980 | |
| 4981 | /* Check for .ascii.spl. */ |
| 4982 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 4983 | spin.si_ascii = TRUE; |
| 4984 | |
| 4985 | /* Check for .add.spl. */ |
| 4986 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 4987 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4990 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4991 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4992 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 4993 | EMSG(_("E751: Output file name must not have region name")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4994 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4995 | EMSG(_("E754: Only up to 8 regions supported")); |
| 4996 | else |
| 4997 | { |
| 4998 | /* Check for overwriting before doing things that may take a lot of |
| 4999 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5000 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5001 | { |
| 5002 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5003 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5004 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5005 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5006 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5007 | EMSG2(_(e_isadir2), wfname); |
| 5008 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | /* |
| 5012 | * Init the aff and dic pointers. |
| 5013 | * Get the region names if there are more than 2 arguments. |
| 5014 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5015 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5016 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5017 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5018 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5019 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5020 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5021 | len = STRLEN(innames[i]); |
| 5022 | if (STRLEN(gettail(innames[i])) < 5 |
| 5023 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5024 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5025 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 5026 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5027 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5028 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 5029 | spin.si_region_name[i * 2 + 1] = |
| 5030 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5031 | } |
| 5032 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5033 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5034 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5035 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 5036 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5037 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 5038 | if (spin.si_foldroot == NULL |
| 5039 | || spin.si_keeproot == NULL |
| 5040 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5041 | { |
| 5042 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5043 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5046 | /* When not producing a .add.spl file clear the character table when |
| 5047 | * we encounter one in the .aff file. This means we dump the current |
| 5048 | * one in the .spl file if the .aff file doesn't define one. That's |
| 5049 | * better than guessing the contents, the table will match a |
| 5050 | * previously loaded spell file. */ |
| 5051 | if (!spin.si_add) |
| 5052 | spin.si_clear_chartab = TRUE; |
| 5053 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5054 | /* |
| 5055 | * Read all the .aff and .dic files. |
| 5056 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5057 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5058 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5059 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5060 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5061 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5062 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5063 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5064 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5065 | if (mch_stat((char *)fname, &st) >= 0) |
| 5066 | { |
| 5067 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 5068 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5069 | afile[i] = spell_read_aff(fname, &spin); |
| 5070 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5071 | error = TRUE; |
| 5072 | else |
| 5073 | { |
| 5074 | /* Read the .dic file and store the words in the trees. */ |
| 5075 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5076 | innames[i]); |
| 5077 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5078 | error = TRUE; |
| 5079 | } |
| 5080 | } |
| 5081 | else |
| 5082 | { |
| 5083 | /* No .aff file, try reading the file as a word list. Store |
| 5084 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5085 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5086 | error = TRUE; |
| 5087 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5088 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5089 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5090 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5091 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5092 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5095 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5096 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5097 | /* |
| 5098 | * Remove the dummy NUL from the start of the tree root. |
| 5099 | */ |
| 5100 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 5101 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5102 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5103 | |
| 5104 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5105 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5106 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5107 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5108 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5109 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5110 | verbose_enter(); |
| 5111 | MSG(_("Compressing word tree...")); |
| 5112 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5113 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5114 | verbose_leave(); |
| 5115 | } |
| 5116 | wordtree_compress(spin.si_foldroot, &spin); |
| 5117 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5118 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5121 | if (!error) |
| 5122 | { |
| 5123 | /* |
| 5124 | * Write the info in the spell file. |
| 5125 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5126 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5127 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5128 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5129 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5130 | smsg((char_u *)_("Writing spell file %s ..."), wfname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5131 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5132 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5133 | verbose_leave(); |
| 5134 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5135 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5136 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5137 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5138 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5139 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5140 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5141 | verbose_enter(); |
| 5142 | MSG(_("Done!")); |
| 5143 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5144 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5145 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5146 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5147 | verbose_leave(); |
| 5148 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5149 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5150 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5151 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5155 | ga_clear(&spin.si_rep); |
| 5156 | ga_clear(&spin.si_sal); |
| 5157 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5158 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5159 | vim_free(spin.si_midword); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5160 | vim_free(spin.si_sofofr); |
| 5161 | vim_free(spin.si_sofoto); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5162 | |
| 5163 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5164 | for (i = 0; i < incount; ++i) |
| 5165 | if (afile[i] != NULL) |
| 5166 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5167 | |
| 5168 | /* Free all the bits and pieces at once. */ |
| 5169 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5170 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5173 | |
| 5174 | /* |
| 5175 | * ":spellgood {word}" |
| 5176 | * ":spellwrong {word}" |
| 5177 | */ |
| 5178 | void |
| 5179 | ex_spell(eap) |
| 5180 | exarg_T *eap; |
| 5181 | { |
| 5182 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong); |
| 5183 | } |
| 5184 | |
| 5185 | /* |
| 5186 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 5187 | */ |
| 5188 | void |
| 5189 | spell_add_word(word, len, bad) |
| 5190 | char_u *word; |
| 5191 | int len; |
| 5192 | int bad; |
| 5193 | { |
| 5194 | FILE *fd; |
| 5195 | buf_T *buf; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5196 | int new_spf = FALSE; |
| 5197 | struct stat st; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5198 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5199 | /* If 'spellfile' isn't set figure out a good default value. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5200 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5201 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5202 | init_spellfile(); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5203 | new_spf = TRUE; |
| 5204 | } |
| 5205 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5206 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5207 | EMSG(_("E764: 'spellfile' is not set")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5208 | else |
| 5209 | { |
| 5210 | /* Check that the user isn't editing the .add file somewhere. */ |
| 5211 | buf = buflist_findname_exp(curbuf->b_p_spf); |
| 5212 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 5213 | buf = NULL; |
| 5214 | if (buf != NULL && bufIsChanged(buf)) |
| 5215 | EMSG(_(e_bufloaded)); |
| 5216 | else |
| 5217 | { |
| 5218 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5219 | if (fd == NULL && new_spf) |
| 5220 | { |
| 5221 | /* We just initialized the 'spellfile' option and can't open |
| 5222 | * the file. We may need to create the "spell" directory |
| 5223 | * first. We already checked the runtime directory is |
| 5224 | * writable in init_spellfile(). */ |
| 5225 | STRCPY(NameBuff, curbuf->b_p_spf); |
| 5226 | *gettail_sep(NameBuff) = NUL; |
| 5227 | if (mch_stat((char *)NameBuff, &st) < 0) |
| 5228 | { |
| 5229 | /* The directory doesn't exist. Try creating it and |
| 5230 | * opening the file again. */ |
| 5231 | vim_mkdir(NameBuff, 0755); |
| 5232 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
| 5233 | } |
| 5234 | } |
| 5235 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5236 | if (fd == NULL) |
| 5237 | EMSG2(_(e_notopen), curbuf->b_p_spf); |
| 5238 | else |
| 5239 | { |
| 5240 | if (bad) |
| 5241 | fprintf(fd, "/!%.*s\n", len, word); |
| 5242 | else |
| 5243 | fprintf(fd, "%.*s\n", len, word); |
| 5244 | fclose(fd); |
| 5245 | |
| 5246 | /* Update the .add.spl file. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5247 | mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5248 | |
| 5249 | /* If the .add file is edited somewhere, reload it. */ |
| 5250 | if (buf != NULL) |
| 5251 | buf_reload(buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5252 | |
| 5253 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5254 | } |
| 5255 | } |
| 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | /* |
| 5260 | * Initialize 'spellfile' for the current buffer. |
| 5261 | */ |
| 5262 | static void |
| 5263 | init_spellfile() |
| 5264 | { |
| 5265 | char_u buf[MAXPATHL]; |
| 5266 | int l; |
| 5267 | slang_T *sl; |
| 5268 | char_u *rtp; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5269 | char_u *lend; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5270 | |
| 5271 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 5272 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5273 | /* Find the end of the language name. Exclude the region. */ |
| 5274 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 5275 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| 5276 | ; |
| 5277 | |
| 5278 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 5279 | * are allowed to write. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5280 | rtp = p_rtp; |
| 5281 | while (*rtp != NUL) |
| 5282 | { |
| 5283 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 5284 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 5285 | if (filewritable(buf) == 2) |
| 5286 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5287 | /* Use the first language name from 'spelllang' and the |
| 5288 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5289 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 5290 | l = STRLEN(buf); |
| 5291 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5292 | "/spell/%.*s.%s.add", |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5293 | (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5294 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 5295 | ? (char_u *)"ascii" : spell_enc()); |
| 5296 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 5297 | break; |
| 5298 | } |
| 5299 | } |
| 5300 | } |
| 5301 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5302 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5303 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5304 | /* |
| 5305 | * Init the chartab used for spelling for ASCII. |
| 5306 | * EBCDIC is not supported! |
| 5307 | */ |
| 5308 | static void |
| 5309 | clear_spell_chartab(sp) |
| 5310 | spelltab_T *sp; |
| 5311 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5312 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5313 | |
| 5314 | /* Init everything to FALSE. */ |
| 5315 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 5316 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 5317 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5318 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5319 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5320 | sp->st_upper[i] = i; |
| 5321 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5322 | |
| 5323 | /* We include digits. A word shouldn't start with a digit, but handling |
| 5324 | * that is done separately. */ |
| 5325 | for (i = '0'; i <= '9'; ++i) |
| 5326 | sp->st_isw[i] = TRUE; |
| 5327 | for (i = 'A'; i <= 'Z'; ++i) |
| 5328 | { |
| 5329 | sp->st_isw[i] = TRUE; |
| 5330 | sp->st_isu[i] = TRUE; |
| 5331 | sp->st_fold[i] = i + 0x20; |
| 5332 | } |
| 5333 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5334 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5335 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5336 | sp->st_upper[i] = i - 0x20; |
| 5337 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
| 5340 | /* |
| 5341 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 5342 | * Called once while starting up and when 'encoding' changes. |
| 5343 | * The default is to use isalpha(), but the spell file should define the word |
| 5344 | * characters to make it possible that 'encoding' differs from the current |
| 5345 | * locale. |
| 5346 | */ |
| 5347 | void |
| 5348 | init_spell_chartab() |
| 5349 | { |
| 5350 | int i; |
| 5351 | |
| 5352 | did_set_spelltab = FALSE; |
| 5353 | clear_spell_chartab(&spelltab); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5354 | #ifdef FEAT_MBYTE |
| 5355 | if (enc_dbcs) |
| 5356 | { |
| 5357 | /* DBCS: assume double-wide characters are word characters. */ |
| 5358 | for (i = 128; i <= 255; ++i) |
| 5359 | if (MB_BYTE2LEN(i) == 2) |
| 5360 | spelltab.st_isw[i] = TRUE; |
| 5361 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5362 | else if (enc_utf8) |
| 5363 | { |
| 5364 | for (i = 128; i < 256; ++i) |
| 5365 | { |
| 5366 | spelltab.st_isu[i] = utf_isupper(i); |
| 5367 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 5368 | spelltab.st_fold[i] = utf_fold(i); |
| 5369 | spelltab.st_upper[i] = utf_toupper(i); |
| 5370 | } |
| 5371 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5372 | else |
| 5373 | #endif |
| 5374 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5375 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5376 | for (i = 128; i < 256; ++i) |
| 5377 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5378 | if (MB_ISUPPER(i)) |
| 5379 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5380 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5381 | spelltab.st_isu[i] = TRUE; |
| 5382 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 5383 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5384 | else if (MB_ISLOWER(i)) |
| 5385 | { |
| 5386 | spelltab.st_isw[i] = TRUE; |
| 5387 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 5388 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
| 5391 | } |
| 5392 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5393 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 5394 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 5395 | |
| 5396 | /* |
| 5397 | * Set the spell character tables from strings in the affix file. |
| 5398 | */ |
| 5399 | static int |
| 5400 | set_spell_chartab(fol, low, upp) |
| 5401 | char_u *fol; |
| 5402 | char_u *low; |
| 5403 | char_u *upp; |
| 5404 | { |
| 5405 | /* We build the new tables here first, so that we can compare with the |
| 5406 | * previous one. */ |
| 5407 | spelltab_T new_st; |
| 5408 | char_u *pf = fol, *pl = low, *pu = upp; |
| 5409 | int f, l, u; |
| 5410 | |
| 5411 | clear_spell_chartab(&new_st); |
| 5412 | |
| 5413 | while (*pf != NUL) |
| 5414 | { |
| 5415 | if (*pl == NUL || *pu == NUL) |
| 5416 | { |
| 5417 | EMSG(_(e_affform)); |
| 5418 | return FAIL; |
| 5419 | } |
| 5420 | #ifdef FEAT_MBYTE |
| 5421 | f = mb_ptr2char_adv(&pf); |
| 5422 | l = mb_ptr2char_adv(&pl); |
| 5423 | u = mb_ptr2char_adv(&pu); |
| 5424 | #else |
| 5425 | f = *pf++; |
| 5426 | l = *pl++; |
| 5427 | u = *pu++; |
| 5428 | #endif |
| 5429 | /* Every character that appears is a word character. */ |
| 5430 | if (f < 256) |
| 5431 | new_st.st_isw[f] = TRUE; |
| 5432 | if (l < 256) |
| 5433 | new_st.st_isw[l] = TRUE; |
| 5434 | if (u < 256) |
| 5435 | new_st.st_isw[u] = TRUE; |
| 5436 | |
| 5437 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 5438 | * case-folding */ |
| 5439 | if (l < 256 && l != f) |
| 5440 | { |
| 5441 | if (f >= 256) |
| 5442 | { |
| 5443 | EMSG(_(e_affrange)); |
| 5444 | return FAIL; |
| 5445 | } |
| 5446 | new_st.st_fold[l] = f; |
| 5447 | } |
| 5448 | |
| 5449 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5450 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 5451 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5452 | if (u < 256 && u != f) |
| 5453 | { |
| 5454 | if (f >= 256) |
| 5455 | { |
| 5456 | EMSG(_(e_affrange)); |
| 5457 | return FAIL; |
| 5458 | } |
| 5459 | new_st.st_fold[u] = f; |
| 5460 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5461 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | if (*pl != NUL || *pu != NUL) |
| 5466 | { |
| 5467 | EMSG(_(e_affform)); |
| 5468 | return FAIL; |
| 5469 | } |
| 5470 | |
| 5471 | return set_spell_finish(&new_st); |
| 5472 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5473 | |
| 5474 | /* |
| 5475 | * Set the spell character tables from strings in the .spl file. |
| 5476 | */ |
| 5477 | static int |
| 5478 | set_spell_charflags(flags, cnt, upp) |
| 5479 | char_u *flags; |
| 5480 | int cnt; |
| 5481 | char_u *upp; |
| 5482 | { |
| 5483 | /* We build the new tables here first, so that we can compare with the |
| 5484 | * previous one. */ |
| 5485 | spelltab_T new_st; |
| 5486 | int i; |
| 5487 | char_u *p = upp; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5488 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5489 | |
| 5490 | clear_spell_chartab(&new_st); |
| 5491 | |
| 5492 | for (i = 0; i < cnt; ++i) |
| 5493 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5494 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 5495 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5496 | |
| 5497 | if (*p == NUL) |
| 5498 | return FAIL; |
| 5499 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5500 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5501 | #else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5502 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5503 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5504 | new_st.st_fold[i + 128] = c; |
| 5505 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 5506 | new_st.st_upper[c] = i + 128; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | return set_spell_finish(&new_st); |
| 5510 | } |
| 5511 | |
| 5512 | static int |
| 5513 | set_spell_finish(new_st) |
| 5514 | spelltab_T *new_st; |
| 5515 | { |
| 5516 | int i; |
| 5517 | |
| 5518 | if (did_set_spelltab) |
| 5519 | { |
| 5520 | /* check that it's the same table */ |
| 5521 | for (i = 0; i < 256; ++i) |
| 5522 | { |
| 5523 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 5524 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5525 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 5526 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5527 | { |
| 5528 | EMSG(_("E763: Word characters differ between spell files")); |
| 5529 | return FAIL; |
| 5530 | } |
| 5531 | } |
| 5532 | } |
| 5533 | else |
| 5534 | { |
| 5535 | /* copy the new spelltab into the one being used */ |
| 5536 | spelltab = *new_st; |
| 5537 | did_set_spelltab = TRUE; |
| 5538 | } |
| 5539 | |
| 5540 | return OK; |
| 5541 | } |
| 5542 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5543 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5544 | * Return TRUE if "p" points to a word character. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5545 | * As a special case we see "midword" characters as word character when it is |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5546 | * followed by a word character. This finds they'there but not 'they there'. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5547 | * Thus this only works properly when past the first character of the word. |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5548 | */ |
| 5549 | static int |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5550 | spell_iswordp(p, buf) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5551 | char_u *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5552 | buf_T *buf; /* buffer used */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5553 | { |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5554 | #ifdef FEAT_MBYTE |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5555 | char_u *s; |
| 5556 | int l; |
| 5557 | int c; |
| 5558 | |
| 5559 | if (has_mbyte) |
| 5560 | { |
| 5561 | l = MB_BYTE2LEN(*p); |
| 5562 | s = p; |
| 5563 | if (l == 1) |
| 5564 | { |
| 5565 | /* be quick for ASCII */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5566 | if (buf->b_spell_ismw[*p]) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5567 | { |
| 5568 | s = p + 1; /* skip a mid-word character */ |
| 5569 | l = MB_BYTE2LEN(*s); |
| 5570 | } |
| 5571 | } |
| 5572 | else |
| 5573 | { |
| 5574 | c = mb_ptr2char(p); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5575 | if (c < 256 ? buf->b_spell_ismw[c] |
| 5576 | : (buf->b_spell_ismw_mb != NULL |
| 5577 | && vim_strchr(buf->b_spell_ismw_mb, c) != NULL)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5578 | { |
| 5579 | s = p + l; |
| 5580 | l = MB_BYTE2LEN(*s); |
| 5581 | } |
| 5582 | } |
| 5583 | |
| 5584 | if (l > 1) |
| 5585 | return mb_get_class(s) >= 2; |
| 5586 | return spelltab.st_isw[*s]; |
| 5587 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5588 | #endif |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5589 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5590 | return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]]; |
| 5591 | } |
| 5592 | |
| 5593 | /* |
| 5594 | * Return TRUE if "p" points to a word character. |
| 5595 | * Unlike spell_iswordp() this doesn't check for "midword" characters. |
| 5596 | */ |
| 5597 | static int |
| 5598 | spell_iswordp_nmw(p) |
| 5599 | char_u *p; |
| 5600 | { |
| 5601 | #ifdef FEAT_MBYTE |
| 5602 | if (has_mbyte && MB_BYTE2LEN(*p) > 1) |
| 5603 | return mb_get_class(p) >= 2; |
| 5604 | #endif |
| 5605 | |
| 5606 | return spelltab.st_isw[*p]; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5609 | #ifdef FEAT_MBYTE |
| 5610 | /* |
| 5611 | * Return TRUE if "p" points to a word character. |
| 5612 | * Wide version of spell_iswordp(). |
| 5613 | */ |
| 5614 | static int |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5615 | spell_iswordp_w(p, buf) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5616 | int *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5617 | buf_T *buf; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5618 | { |
| 5619 | int *s; |
| 5620 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5621 | if (*p < 256 ? buf->b_spell_ismw[*p] |
| 5622 | : (buf->b_spell_ismw_mb != NULL |
| 5623 | && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5624 | s = p + 1; |
| 5625 | else |
| 5626 | s = p; |
| 5627 | |
| 5628 | if (mb_char2len(*s) > 1) |
| 5629 | { |
| 5630 | if (enc_utf8) |
| 5631 | return utf_class(*s) >= 2; |
| 5632 | if (enc_dbcs) |
| 5633 | return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2; |
| 5634 | return 0; |
| 5635 | } |
| 5636 | return spelltab.st_isw[*s]; |
| 5637 | } |
| 5638 | #endif |
| 5639 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5640 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5641 | * Write the table with prefix conditions to the .spl file. |
| 5642 | */ |
| 5643 | static void |
| 5644 | write_spell_prefcond(fd, gap) |
| 5645 | FILE *fd; |
| 5646 | garray_T *gap; |
| 5647 | { |
| 5648 | int i; |
| 5649 | char_u *p; |
| 5650 | int len; |
| 5651 | |
| 5652 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 5653 | |
| 5654 | for (i = 0; i < gap->ga_len; ++i) |
| 5655 | { |
| 5656 | /* <prefcond> : <condlen> <condstr> */ |
| 5657 | p = ((char_u **)gap->ga_data)[i]; |
| 5658 | if (p == NULL) |
| 5659 | fputc(0, fd); |
| 5660 | else |
| 5661 | { |
| 5662 | len = STRLEN(p); |
| 5663 | fputc(len, fd); |
| 5664 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 5665 | } |
| 5666 | } |
| 5667 | } |
| 5668 | |
| 5669 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5670 | * Write the current tables into the .spl file. |
| 5671 | * This makes sure the same characters are recognized as word characters when |
| 5672 | * generating an when using a spell file. |
| 5673 | */ |
| 5674 | static void |
| 5675 | write_spell_chartab(fd) |
| 5676 | FILE *fd; |
| 5677 | { |
| 5678 | char_u charbuf[256 * 4]; |
| 5679 | int len = 0; |
| 5680 | int flags; |
| 5681 | int i; |
| 5682 | |
| 5683 | fputc(128, fd); /* <charflagslen> */ |
| 5684 | for (i = 128; i < 256; ++i) |
| 5685 | { |
| 5686 | flags = 0; |
| 5687 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5688 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5689 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5690 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5691 | fputc(flags, fd); /* <charflags> */ |
| 5692 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5693 | #ifdef FEAT_MBYTE |
| 5694 | if (has_mbyte) |
| 5695 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 5696 | else |
| 5697 | #endif |
| 5698 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 5702 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 5703 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5704 | |
| 5705 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5706 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 5707 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5708 | * When using a multi-byte 'encoding' the length may change! |
| 5709 | * Returns FAIL when something wrong. |
| 5710 | */ |
| 5711 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5712 | spell_casefold(str, len, buf, buflen) |
| 5713 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5714 | int len; |
| 5715 | char_u *buf; |
| 5716 | int buflen; |
| 5717 | { |
| 5718 | int i; |
| 5719 | |
| 5720 | if (len >= buflen) |
| 5721 | { |
| 5722 | buf[0] = NUL; |
| 5723 | return FAIL; /* result will not fit */ |
| 5724 | } |
| 5725 | |
| 5726 | #ifdef FEAT_MBYTE |
| 5727 | if (has_mbyte) |
| 5728 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5729 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5730 | char_u *p; |
| 5731 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5732 | |
| 5733 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5734 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5735 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5736 | if (outi + MB_MAXBYTES > buflen) |
| 5737 | { |
| 5738 | buf[outi] = NUL; |
| 5739 | return FAIL; |
| 5740 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5741 | c = mb_ptr2char_adv(&p); |
| 5742 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5743 | } |
| 5744 | buf[outi] = NUL; |
| 5745 | } |
| 5746 | else |
| 5747 | #endif |
| 5748 | { |
| 5749 | /* Be quick for non-multibyte encodings. */ |
| 5750 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5751 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5752 | buf[i] = NUL; |
| 5753 | } |
| 5754 | |
| 5755 | return OK; |
| 5756 | } |
| 5757 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5758 | #define SPS_BEST 1 |
| 5759 | #define SPS_FAST 2 |
| 5760 | #define SPS_DOUBLE 4 |
| 5761 | |
| 5762 | static int sps_flags = SPS_BEST; |
| 5763 | |
| 5764 | /* |
| 5765 | * Check the 'spellsuggest' option. Return FAIL if it's wrong. |
| 5766 | * Sets "sps_flags". |
| 5767 | */ |
| 5768 | int |
| 5769 | spell_check_sps() |
| 5770 | { |
| 5771 | char_u *p; |
| 5772 | char_u buf[MAXPATHL]; |
| 5773 | int f; |
| 5774 | |
| 5775 | sps_flags = 0; |
| 5776 | |
| 5777 | for (p = p_sps; *p != NUL; ) |
| 5778 | { |
| 5779 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 5780 | |
| 5781 | f = 0; |
| 5782 | if (STRCMP(buf, "best") == 0) |
| 5783 | f = SPS_BEST; |
| 5784 | else if (STRCMP(buf, "fast") == 0) |
| 5785 | f = SPS_FAST; |
| 5786 | else if (STRCMP(buf, "double") == 0) |
| 5787 | f = SPS_DOUBLE; |
| 5788 | else if (STRNCMP(buf, "expr:", 5) != 0 |
| 5789 | && STRNCMP(buf, "file:", 5) != 0) |
| 5790 | f = -1; |
| 5791 | |
| 5792 | if (f == -1 || (sps_flags != 0 && f != 0)) |
| 5793 | { |
| 5794 | sps_flags = SPS_BEST; |
| 5795 | return FAIL; |
| 5796 | } |
| 5797 | if (f != 0) |
| 5798 | sps_flags = f; |
| 5799 | } |
| 5800 | |
| 5801 | if (sps_flags == 0) |
| 5802 | sps_flags = SPS_BEST; |
| 5803 | |
| 5804 | return OK; |
| 5805 | } |
| 5806 | |
| 5807 | /* Remember what "z?" replaced. */ |
| 5808 | static char_u *repl_from = NULL; |
| 5809 | static char_u *repl_to = NULL; |
| 5810 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5811 | /* |
| 5812 | * "z?": Find badly spelled word under or after the cursor. |
| 5813 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5814 | */ |
| 5815 | void |
| 5816 | spell_suggest() |
| 5817 | { |
| 5818 | char_u *line; |
| 5819 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5820 | char_u wcopy[MAXWLEN + 2]; |
| 5821 | char_u *p; |
| 5822 | int i; |
| 5823 | int c; |
| 5824 | suginfo_T sug; |
| 5825 | suggest_T *stp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5826 | int mouse_used; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5827 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5828 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5829 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL |
| 5830 | || curwin->w_cursor.col > prev_cursor.col) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5831 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5832 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 5833 | return; |
| 5834 | |
| 5835 | /* No bad word or it starts after the cursor: use the word under the |
| 5836 | * cursor. */ |
| 5837 | curwin->w_cursor = prev_cursor; |
| 5838 | line = ml_get_curline(); |
| 5839 | p = line + curwin->w_cursor.col; |
| 5840 | /* Backup to before start of word. */ |
| 5841 | while (p > line && SPELL_ISWORDP(p)) |
| 5842 | mb_ptr_back(line, p); |
| 5843 | /* Forward to start of word. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5844 | while (*p != NUL && !SPELL_ISWORDP(p)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5845 | mb_ptr_adv(p); |
| 5846 | |
| 5847 | if (!SPELL_ISWORDP(p)) /* No word found. */ |
| 5848 | { |
| 5849 | beep_flush(); |
| 5850 | return; |
| 5851 | } |
| 5852 | curwin->w_cursor.col = p - line; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5853 | } |
| 5854 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5855 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5856 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5857 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5858 | /* Get the list of suggestions */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5859 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5860 | |
| 5861 | if (sug.su_ga.ga_len == 0) |
| 5862 | MSG(_("Sorry, no suggestions")); |
| 5863 | else |
| 5864 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5865 | vim_free(repl_from); |
| 5866 | repl_from = NULL; |
| 5867 | vim_free(repl_to); |
| 5868 | repl_to = NULL; |
| 5869 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5870 | /* List the suggestions. */ |
| 5871 | msg_start(); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5872 | lines_left = Rows; /* avoid more prompt */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5873 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 5874 | sug.su_badlen, sug.su_badptr); |
| 5875 | msg_puts(IObuff); |
| 5876 | msg_clr_eos(); |
| 5877 | msg_putchar('\n'); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5878 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5879 | msg_scroll = TRUE; |
| 5880 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 5881 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5882 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5883 | |
| 5884 | /* The suggested word may replace only part of the bad word, add |
| 5885 | * the not replaced part. */ |
| 5886 | STRCPY(wcopy, stp->st_word); |
| 5887 | if (sug.su_badlen > stp->st_orglen) |
| 5888 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 5889 | sug.su_badptr + stp->st_orglen, |
| 5890 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5891 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy); |
| 5892 | msg_puts(IObuff); |
| 5893 | |
| 5894 | /* The word may replace more than "su_badlen". */ |
| 5895 | if (sug.su_badlen < stp->st_orglen) |
| 5896 | { |
| 5897 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 5898 | stp->st_orglen, sug.su_badptr); |
| 5899 | msg_puts(IObuff); |
| 5900 | } |
| 5901 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5902 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5903 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5904 | /* Add the score. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5905 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5906 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"), |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5907 | stp->st_salscore ? "s " : "", |
| 5908 | stp->st_score, stp->st_altscore); |
| 5909 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5910 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"), |
| 5911 | stp->st_score); |
| 5912 | msg_advance(30); |
| 5913 | msg_puts(IObuff); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5914 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5915 | msg_putchar('\n'); |
| 5916 | } |
| 5917 | |
| 5918 | /* Ask for choice. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5919 | i = prompt_for_number(&mouse_used); |
| 5920 | if (mouse_used) |
| 5921 | i -= lines_left; |
| 5922 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5923 | 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] | 5924 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5925 | /* Save the from and to text for :spellrepall. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5926 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5927 | repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); |
| 5928 | repl_to = vim_strsave(stp->st_word); |
| 5929 | |
| 5930 | /* Replace the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5931 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 5932 | if (p != NULL) |
| 5933 | { |
| 5934 | c = sug.su_badptr - line; |
| 5935 | mch_memmove(p, line, c); |
| 5936 | STRCPY(p + c, stp->st_word); |
| 5937 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 5938 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 5939 | curwin->w_cursor.col = c; |
| 5940 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5941 | |
| 5942 | /* For redo we use a change-word command. */ |
| 5943 | ResetRedobuff(); |
| 5944 | AppendToRedobuff((char_u *)"ciw"); |
| 5945 | AppendToRedobuff(stp->st_word); |
| 5946 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5947 | } |
| 5948 | } |
| 5949 | else |
| 5950 | curwin->w_cursor = prev_cursor; |
| 5951 | } |
| 5952 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5953 | spell_find_cleanup(&sug); |
| 5954 | } |
| 5955 | |
| 5956 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5957 | * ":spellrepall" |
| 5958 | */ |
| 5959 | /*ARGSUSED*/ |
| 5960 | void |
| 5961 | ex_spellrepall(eap) |
| 5962 | exarg_T *eap; |
| 5963 | { |
| 5964 | pos_T pos = curwin->w_cursor; |
| 5965 | char_u *frompat; |
| 5966 | int addlen; |
| 5967 | char_u *line; |
| 5968 | char_u *p; |
| 5969 | int didone = FALSE; |
| 5970 | int save_ws = p_ws; |
| 5971 | |
| 5972 | if (repl_from == NULL || repl_to == NULL) |
| 5973 | { |
| 5974 | EMSG(_("E752: No previous spell replacement")); |
| 5975 | return; |
| 5976 | } |
| 5977 | addlen = STRLEN(repl_to) - STRLEN(repl_from); |
| 5978 | |
| 5979 | frompat = alloc(STRLEN(repl_from) + 7); |
| 5980 | if (frompat == NULL) |
| 5981 | return; |
| 5982 | sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); |
| 5983 | p_ws = FALSE; |
| 5984 | |
| 5985 | curwin->w_cursor.lnum = 0; |
| 5986 | while (!got_int) |
| 5987 | { |
| 5988 | if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0 |
| 5989 | || u_save_cursor() == FAIL) |
| 5990 | break; |
| 5991 | |
| 5992 | /* Only replace when the right word isn't there yet. This happens |
| 5993 | * when changing "etc" to "etc.". */ |
| 5994 | line = ml_get_curline(); |
| 5995 | if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, |
| 5996 | repl_to, STRLEN(repl_to)) != 0) |
| 5997 | { |
| 5998 | p = alloc(STRLEN(line) + addlen + 1); |
| 5999 | if (p == NULL) |
| 6000 | break; |
| 6001 | mch_memmove(p, line, curwin->w_cursor.col); |
| 6002 | STRCPY(p + curwin->w_cursor.col, repl_to); |
| 6003 | STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); |
| 6004 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 6005 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 6006 | didone = TRUE; |
| 6007 | } |
| 6008 | curwin->w_cursor.col += STRLEN(repl_to); |
| 6009 | } |
| 6010 | |
| 6011 | p_ws = save_ws; |
| 6012 | curwin->w_cursor = pos; |
| 6013 | vim_free(frompat); |
| 6014 | |
| 6015 | if (!didone) |
| 6016 | EMSG2(_("E753: Not found: %s"), repl_from); |
| 6017 | } |
| 6018 | |
| 6019 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6020 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 6021 | * a list of allocated strings. |
| 6022 | */ |
| 6023 | void |
| 6024 | spell_suggest_list(gap, word, maxcount) |
| 6025 | garray_T *gap; |
| 6026 | char_u *word; |
| 6027 | int maxcount; /* maximum nr of suggestions */ |
| 6028 | { |
| 6029 | suginfo_T sug; |
| 6030 | int i; |
| 6031 | suggest_T *stp; |
| 6032 | char_u *wcopy; |
| 6033 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6034 | spell_find_suggest(word, &sug, maxcount, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6035 | |
| 6036 | /* Make room in "gap". */ |
| 6037 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 6038 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 6039 | return; |
| 6040 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6041 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6042 | { |
| 6043 | stp = &SUG(sug.su_ga, i); |
| 6044 | |
| 6045 | /* The suggested word may replace only part of "word", add the not |
| 6046 | * replaced part. */ |
| 6047 | wcopy = alloc(STRLEN(stp->st_word) |
| 6048 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 6049 | if (wcopy == NULL) |
| 6050 | break; |
| 6051 | STRCPY(wcopy, stp->st_word); |
| 6052 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 6053 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 6054 | } |
| 6055 | |
| 6056 | spell_find_cleanup(&sug); |
| 6057 | } |
| 6058 | |
| 6059 | /* |
| 6060 | * Find spell suggestions for the word at the start of "badptr". |
| 6061 | * Return the suggestions in "su->su_ga". |
| 6062 | * The maximum number of suggestions is "maxcount". |
| 6063 | * Note: does use info for the current window. |
| 6064 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 6065 | */ |
| 6066 | static void |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6067 | spell_find_suggest(badptr, su, maxcount, banbadword) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6068 | char_u *badptr; |
| 6069 | suginfo_T *su; |
| 6070 | int maxcount; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6071 | int banbadword; /* don't include badword in suggestions */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6072 | { |
| 6073 | int attr; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6074 | char_u buf[MAXPATHL]; |
| 6075 | char_u *p; |
| 6076 | int do_combine = FALSE; |
| 6077 | char_u *sps_copy; |
| 6078 | #ifdef FEAT_EVAL |
| 6079 | static int expr_busy = FALSE; |
| 6080 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6081 | |
| 6082 | /* |
| 6083 | * Set the info in "*su". |
| 6084 | */ |
| 6085 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 6086 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 6087 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 6088 | if (*badptr == NUL) |
| 6089 | return; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6090 | hash_init(&su->su_banned); |
| 6091 | |
| 6092 | su->su_badptr = badptr; |
| 6093 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr); |
| 6094 | su->su_maxcount = maxcount; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6095 | su->su_maxscore = SCORE_MAXINIT; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6096 | |
| 6097 | if (su->su_badlen >= MAXWLEN) |
| 6098 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 6099 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 6100 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 6101 | su->su_fbadword, MAXWLEN); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6102 | /* get caps flags for bad word */ |
| 6103 | su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6104 | |
| 6105 | /* Ban the bad word itself. It may appear in another region. */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6106 | if (banbadword) |
| 6107 | add_banned(su, su->su_badword); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6108 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6109 | /* Make a copy of 'spellsuggest', because the expression may change it. */ |
| 6110 | sps_copy = vim_strsave(p_sps); |
| 6111 | if (sps_copy == NULL) |
| 6112 | return; |
| 6113 | |
| 6114 | /* Loop over the items in 'spellsuggest'. */ |
| 6115 | for (p = sps_copy; *p != NUL; ) |
| 6116 | { |
| 6117 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 6118 | |
| 6119 | if (STRNCMP(buf, "expr:", 5) == 0) |
| 6120 | { |
| 6121 | #ifdef FEAT_EVAL |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6122 | /* Evaluate an expression. Skip this when called recursively, |
| 6123 | * when using spellsuggest() in the expression. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6124 | if (!expr_busy) |
| 6125 | { |
| 6126 | expr_busy = TRUE; |
| 6127 | spell_suggest_expr(su, buf + 5); |
| 6128 | expr_busy = FALSE; |
| 6129 | } |
| 6130 | #endif |
| 6131 | } |
| 6132 | else if (STRNCMP(buf, "file:", 5) == 0) |
| 6133 | /* Use list of suggestions in a file. */ |
| 6134 | spell_suggest_file(su, buf + 5); |
| 6135 | else |
| 6136 | { |
| 6137 | /* Use internal method. */ |
| 6138 | spell_suggest_intern(su); |
| 6139 | if (sps_flags & SPS_DOUBLE) |
| 6140 | do_combine = TRUE; |
| 6141 | } |
| 6142 | } |
| 6143 | |
| 6144 | vim_free(sps_copy); |
| 6145 | |
| 6146 | if (do_combine) |
| 6147 | /* Combine the two list of suggestions. This must be done last, |
| 6148 | * because sorting changes the order again. */ |
| 6149 | score_combine(su); |
| 6150 | } |
| 6151 | |
| 6152 | #ifdef FEAT_EVAL |
| 6153 | /* |
| 6154 | * Find suggestions by evaluating expression "expr". |
| 6155 | */ |
| 6156 | static void |
| 6157 | spell_suggest_expr(su, expr) |
| 6158 | suginfo_T *su; |
| 6159 | char_u *expr; |
| 6160 | { |
| 6161 | list_T *list; |
| 6162 | listitem_T *li; |
| 6163 | int score; |
| 6164 | char_u *p; |
| 6165 | |
| 6166 | /* The work is split up in a few parts to avoid having to export |
| 6167 | * suginfo_T. |
| 6168 | * First evaluate the expression and get the resulting list. */ |
| 6169 | list = eval_spell_expr(su->su_badword, expr); |
| 6170 | if (list != NULL) |
| 6171 | { |
| 6172 | /* Loop over the items in the list. */ |
| 6173 | for (li = list->lv_first; li != NULL; li = li->li_next) |
| 6174 | if (li->li_tv.v_type == VAR_LIST) |
| 6175 | { |
| 6176 | /* Get the word and the score from the items. */ |
| 6177 | score = get_spellword(li->li_tv.vval.v_list, &p); |
| 6178 | if (score >= 0) |
| 6179 | add_suggestion(su, &su->su_ga, p, |
| 6180 | su->su_badlen, score, 0, TRUE); |
| 6181 | } |
| 6182 | list_unref(list); |
| 6183 | } |
| 6184 | |
| 6185 | /* Sort the suggestions and truncate at "maxcount". */ |
| 6186 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6187 | } |
| 6188 | #endif |
| 6189 | |
| 6190 | /* |
| 6191 | * Find suggestions a file "fname". |
| 6192 | */ |
| 6193 | static void |
| 6194 | spell_suggest_file(su, fname) |
| 6195 | suginfo_T *su; |
| 6196 | char_u *fname; |
| 6197 | { |
| 6198 | FILE *fd; |
| 6199 | char_u line[MAXWLEN * 2]; |
| 6200 | char_u *p; |
| 6201 | int len; |
| 6202 | char_u cword[MAXWLEN]; |
| 6203 | |
| 6204 | /* Open the file. */ |
| 6205 | fd = mch_fopen((char *)fname, "r"); |
| 6206 | if (fd == NULL) |
| 6207 | { |
| 6208 | EMSG2(_(e_notopen), fname); |
| 6209 | return; |
| 6210 | } |
| 6211 | |
| 6212 | /* Read it line by line. */ |
| 6213 | while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) |
| 6214 | { |
| 6215 | line_breakcheck(); |
| 6216 | |
| 6217 | p = vim_strchr(line, '/'); |
| 6218 | if (p == NULL) |
| 6219 | continue; /* No Tab found, just skip the line. */ |
| 6220 | *p++ = NUL; |
| 6221 | if (STRICMP(su->su_badword, line) == 0) |
| 6222 | { |
| 6223 | /* Match! Isolate the good word, until CR or NL. */ |
| 6224 | for (len = 0; p[len] >= ' '; ++len) |
| 6225 | ; |
| 6226 | p[len] = NUL; |
| 6227 | |
| 6228 | /* If the suggestion doesn't have specific case duplicate the case |
| 6229 | * of the bad word. */ |
| 6230 | if (captype(p, NULL) == 0) |
| 6231 | { |
| 6232 | make_case_word(p, cword, su->su_badflags); |
| 6233 | p = cword; |
| 6234 | } |
| 6235 | |
| 6236 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| 6237 | SCORE_FILE, 0, TRUE); |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | fclose(fd); |
| 6242 | |
| 6243 | /* Sort the suggestions and truncate at "maxcount". */ |
| 6244 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6245 | } |
| 6246 | |
| 6247 | /* |
| 6248 | * Find suggestions for the internal method indicated by "sps_flags". |
| 6249 | */ |
| 6250 | static void |
| 6251 | spell_suggest_intern(su) |
| 6252 | suginfo_T *su; |
| 6253 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6254 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6255 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6256 | * |
| 6257 | * Set a maximum score to limit the combination of operations that is |
| 6258 | * tried. |
| 6259 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6260 | suggest_try_special(su); |
| 6261 | |
| 6262 | /* |
| 6263 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 6264 | * from the .aff file and inserting a space (split the word). |
| 6265 | */ |
| 6266 | suggest_try_change(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6267 | |
| 6268 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 6269 | if (sps_flags & SPS_DOUBLE) |
| 6270 | score_comp_sal(su); |
| 6271 | |
| 6272 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6273 | * 3. Try finding sound-a-like words. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6274 | * |
| 6275 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 6276 | * very slow and often doesn't find new suggestions. |
| 6277 | */ |
| 6278 | if ((sps_flags & SPS_DOUBLE) |
| 6279 | || (!(sps_flags & SPS_FAST) |
| 6280 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 6281 | { |
| 6282 | /* Allow a higher score now. */ |
| 6283 | su->su_maxscore = SCORE_MAXMAX; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6284 | suggest_try_soundalike(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | /* When CTRL-C was hit while searching do show the results. */ |
| 6288 | ui_breakcheck(); |
| 6289 | if (got_int) |
| 6290 | { |
| 6291 | (void)vgetc(); |
| 6292 | got_int = FALSE; |
| 6293 | } |
| 6294 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6295 | if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6296 | { |
| 6297 | if (sps_flags & SPS_BEST) |
| 6298 | /* Adjust the word score for how it sounds like. */ |
| 6299 | rescore_suggestions(su); |
| 6300 | |
| 6301 | /* Sort the suggestions and truncate at "maxcount". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6302 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6303 | } |
| 6304 | } |
| 6305 | |
| 6306 | /* |
| 6307 | * Free the info put in "*su" by spell_find_suggest(). |
| 6308 | */ |
| 6309 | static void |
| 6310 | spell_find_cleanup(su) |
| 6311 | suginfo_T *su; |
| 6312 | { |
| 6313 | int i; |
| 6314 | |
| 6315 | /* Free the suggestions. */ |
| 6316 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6317 | vim_free(SUG(su->su_ga, i).st_word); |
| 6318 | ga_clear(&su->su_ga); |
| 6319 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6320 | vim_free(SUG(su->su_sga, i).st_word); |
| 6321 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6322 | |
| 6323 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6324 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6328 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 6329 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 6330 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6331 | */ |
| 6332 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6333 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6334 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6335 | char_u *wcopy; |
| 6336 | int upper; /* TRUE: first letter made upper case */ |
| 6337 | { |
| 6338 | char_u *p; |
| 6339 | int c; |
| 6340 | int l; |
| 6341 | |
| 6342 | p = word; |
| 6343 | #ifdef FEAT_MBYTE |
| 6344 | if (has_mbyte) |
| 6345 | c = mb_ptr2char_adv(&p); |
| 6346 | else |
| 6347 | #endif |
| 6348 | c = *p++; |
| 6349 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6350 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6351 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6352 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6353 | #ifdef FEAT_MBYTE |
| 6354 | if (has_mbyte) |
| 6355 | l = mb_char2bytes(c, wcopy); |
| 6356 | else |
| 6357 | #endif |
| 6358 | { |
| 6359 | l = 1; |
| 6360 | wcopy[0] = c; |
| 6361 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6362 | vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6366 | * Make a copy of "word" with all the letters upper cased into |
| 6367 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6368 | */ |
| 6369 | static void |
| 6370 | allcap_copy(word, wcopy) |
| 6371 | char_u *word; |
| 6372 | char_u *wcopy; |
| 6373 | { |
| 6374 | char_u *s; |
| 6375 | char_u *d; |
| 6376 | int c; |
| 6377 | |
| 6378 | d = wcopy; |
| 6379 | for (s = word; *s != NUL; ) |
| 6380 | { |
| 6381 | #ifdef FEAT_MBYTE |
| 6382 | if (has_mbyte) |
| 6383 | c = mb_ptr2char_adv(&s); |
| 6384 | else |
| 6385 | #endif |
| 6386 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6387 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6388 | |
| 6389 | #ifdef FEAT_MBYTE |
| 6390 | if (has_mbyte) |
| 6391 | { |
| 6392 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 6393 | break; |
| 6394 | d += mb_char2bytes(c, d); |
| 6395 | } |
| 6396 | else |
| 6397 | #endif |
| 6398 | { |
| 6399 | if (d - wcopy >= MAXWLEN - 1) |
| 6400 | break; |
| 6401 | *d++ = c; |
| 6402 | } |
| 6403 | } |
| 6404 | *d = NUL; |
| 6405 | } |
| 6406 | |
| 6407 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6408 | * Try finding suggestions by recognizing specific situations. |
| 6409 | */ |
| 6410 | static void |
| 6411 | suggest_try_special(su) |
| 6412 | suginfo_T *su; |
| 6413 | { |
| 6414 | char_u *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6415 | size_t len; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6416 | int c; |
| 6417 | char_u word[MAXWLEN]; |
| 6418 | |
| 6419 | /* |
| 6420 | * Recognize a word that is repeated: "the the". |
| 6421 | */ |
| 6422 | p = skiptowhite(su->su_fbadword); |
| 6423 | len = p - su->su_fbadword; |
| 6424 | p = skipwhite(p); |
| 6425 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 6426 | { |
| 6427 | /* Include badflags: if the badword is onecap or allcap |
| 6428 | * use that for the goodword too: "The the" -> "The". */ |
| 6429 | c = su->su_fbadword[len]; |
| 6430 | su->su_fbadword[len] = NUL; |
| 6431 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 6432 | su->su_fbadword[len] = c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6433 | add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
| 6437 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6438 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6439 | * |
| 6440 | * This uses a state machine. At each node in the tree we try various |
| 6441 | * operations. When trying if an operation work "depth" is increased and the |
| 6442 | * stack[] is used to store info. This allows combinations, thus insert one |
| 6443 | * character, replace one and delete another. The number of changes is |
| 6444 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6445 | */ |
| 6446 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6447 | suggest_try_change(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6448 | suginfo_T *su; |
| 6449 | { |
| 6450 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 6451 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 6452 | trystate_T stack[MAXWLEN]; |
| 6453 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 6454 | * to for word split) */ |
| 6455 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 6456 | int splitoff = 0; /* index in tword after last split */ |
| 6457 | trystate_T *sp; |
| 6458 | int newscore; |
| 6459 | langp_T *lp; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6460 | char_u *byts, *fbyts, *pbyts; |
| 6461 | idx_T *idxs, *fidxs, *pidxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6462 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6463 | int c, c2, c3; |
| 6464 | int n = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6465 | int flags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6466 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6467 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6468 | int len; |
| 6469 | char_u *p; |
| 6470 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6471 | int fl = 0, tl; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6472 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6473 | |
| 6474 | /* We make a copy of the case-folded bad word, so that we can modify it |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6475 | * to find matches (esp. REP items). Append some more text, changing |
| 6476 | * chars after the bad word may help. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6477 | STRCPY(fword, su->su_fbadword); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6478 | n = STRLEN(fword); |
| 6479 | p = su->su_badptr + su->su_badlen; |
| 6480 | (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6481 | |
| 6482 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6483 | lp->lp_slang != NULL; ++lp) |
| 6484 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6485 | /* |
| 6486 | * Go through the whole case-fold tree, try changes at each node. |
| 6487 | * "tword[]" contains the word collected from nodes in the tree. |
| 6488 | * "fword[]" the word we are trying to match with (initially the bad |
| 6489 | * word). |
| 6490 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6491 | depth = 0; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6492 | sp = &stack[0]; |
| 6493 | sp->ts_state = STATE_START; |
| 6494 | sp->ts_score = 0; |
| 6495 | sp->ts_curi = 1; |
| 6496 | sp->ts_fidx = 0; |
| 6497 | sp->ts_fidxtry = 0; |
| 6498 | sp->ts_twordlen = 0; |
| 6499 | sp->ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6500 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6501 | sp->ts_tcharlen = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6502 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6503 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6504 | /* |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6505 | * When there are postponed prefixes we need to use these first. At |
| 6506 | * the end of the prefix we continue in the case-fold tree. |
| 6507 | */ |
| 6508 | fbyts = lp->lp_slang->sl_fbyts; |
| 6509 | fidxs = lp->lp_slang->sl_fidxs; |
| 6510 | pbyts = lp->lp_slang->sl_pbyts; |
| 6511 | pidxs = lp->lp_slang->sl_pidxs; |
| 6512 | if (pbyts != NULL) |
| 6513 | { |
| 6514 | byts = pbyts; |
| 6515 | idxs = pidxs; |
| 6516 | sp->ts_prefixdepth = PREFIXTREE; |
| 6517 | sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
| 6518 | } |
| 6519 | else |
| 6520 | { |
| 6521 | byts = fbyts; |
| 6522 | idxs = fidxs; |
| 6523 | sp->ts_prefixdepth = NOPREFIX; |
| 6524 | } |
| 6525 | |
| 6526 | /* |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6527 | * Loop to find all suggestions. At each round we either: |
| 6528 | * - For the current state try one operation, advance "ts_curi", |
| 6529 | * increase "depth". |
| 6530 | * - When a state is done go to the next, set "ts_state". |
| 6531 | * - When all states are tried decrease "depth". |
| 6532 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6533 | while (depth >= 0 && !got_int) |
| 6534 | { |
| 6535 | sp = &stack[depth]; |
| 6536 | switch (sp->ts_state) |
| 6537 | { |
| 6538 | case STATE_START: |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6539 | case STATE_NOPREFIX: |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6540 | /* |
| 6541 | * Start of node: Deal with NUL bytes, which means |
| 6542 | * tword[] may end here. |
| 6543 | */ |
| 6544 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 6545 | len = byts[arridx]; /* bytes in this node */ |
| 6546 | arridx += sp->ts_curi; /* index of current byte */ |
| 6547 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6548 | if (sp->ts_prefixdepth == PREFIXTREE) |
| 6549 | { |
| 6550 | /* Skip over the NUL bytes, we use them later. */ |
| 6551 | for (n = 0; n < len && byts[arridx + n] == 0; ++n) |
| 6552 | ; |
| 6553 | sp->ts_curi += n; |
| 6554 | |
| 6555 | /* At end of a prefix or at start of prefixtree: check for |
| 6556 | * following word. */ |
| 6557 | if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX) |
| 6558 | { |
| 6559 | sp->ts_state = STATE_START; |
| 6560 | ++depth; |
| 6561 | stack[depth] = stack[depth - 1]; |
| 6562 | sp = &stack[depth]; |
| 6563 | sp->ts_prefixdepth = depth - 1; |
| 6564 | byts = fbyts; |
| 6565 | idxs = fidxs; |
| 6566 | sp->ts_state = STATE_START; |
| 6567 | sp->ts_curi = 1; /* start just after length byte */ |
| 6568 | sp->ts_arridx = 0; |
| 6569 | |
| 6570 | /* Move the prefix to preword[] so that |
| 6571 | * find_keepcap_word() works. */ |
| 6572 | prewordlen = splitoff = sp->ts_twordlen; |
| 6573 | mch_memmove(preword, tword, splitoff); |
| 6574 | break; |
| 6575 | } |
| 6576 | |
| 6577 | /* Always past NUL bytes now. */ |
| 6578 | sp->ts_state = STATE_ENDNUL; |
| 6579 | break; |
| 6580 | } |
| 6581 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6582 | if (sp->ts_curi > len || byts[arridx] != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6583 | { |
| 6584 | /* Past bytes in node and/or past NUL bytes. */ |
| 6585 | sp->ts_state = STATE_ENDNUL; |
| 6586 | break; |
| 6587 | } |
| 6588 | |
| 6589 | /* |
| 6590 | * End of word in tree. |
| 6591 | */ |
| 6592 | ++sp->ts_curi; /* eat one NUL byte */ |
| 6593 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6594 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6595 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6596 | if (sp->ts_prefixdepth < MAXWLEN) |
| 6597 | { |
| 6598 | /* There was a prefix before the word. Check that the |
| 6599 | * prefix can be used with this word. */ |
| 6600 | /* Count the length of the NULs in the prefix. If there |
| 6601 | * are none this must be the first try without a prefix. |
| 6602 | */ |
| 6603 | n = stack[sp->ts_prefixdepth].ts_arridx; |
| 6604 | len = pbyts[n++]; |
| 6605 | for (c = 0; c < len && pbyts[n + c] == 0; ++c) |
| 6606 | ; |
| 6607 | if (c > 0) |
| 6608 | { |
| 6609 | /* The prefix ID is stored two bytes above the flags. */ |
| 6610 | c = valid_word_prefix(c, n, (unsigned)flags >> 16, |
| 6611 | tword + splitoff, lp->lp_slang); |
| 6612 | if (c == 0) |
| 6613 | break; |
| 6614 | |
| 6615 | /* Use the WF_RARE flag for a rare prefix. */ |
| 6616 | if (c & WF_RAREPFX) |
| 6617 | flags |= WF_RARE; |
| 6618 | } |
| 6619 | } |
| 6620 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6621 | /* |
| 6622 | * Form the word with proper case in preword. |
| 6623 | * If there is a word from a previous split, append. |
| 6624 | */ |
| 6625 | tword[sp->ts_twordlen] = NUL; |
| 6626 | if (flags & WF_KEEPCAP) |
| 6627 | /* Must find the word in the keep-case tree. */ |
| 6628 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 6629 | preword + prewordlen); |
| 6630 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6631 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6632 | /* Include badflags: if the badword is onecap or allcap |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6633 | * use that for the goodword too. But if the badword is |
| 6634 | * allcap and it's only one char long use onecap. */ |
| 6635 | c = su->su_badflags; |
| 6636 | if ((c & WF_ALLCAP) |
| 6637 | #ifdef FEAT_MBYTE |
| 6638 | && su->su_badlen == mb_ptr2len_check(su->su_badptr) |
| 6639 | #else |
| 6640 | && su->su_badlen == 1 |
| 6641 | #endif |
| 6642 | ) |
| 6643 | c = WF_ONECAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6644 | make_case_word(tword + splitoff, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6645 | preword + prewordlen, flags | c); |
| 6646 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6647 | |
| 6648 | /* Don't use a banned word. It may appear again as a good |
| 6649 | * word, thus remember it. */ |
| 6650 | if (flags & WF_BANNED) |
| 6651 | { |
| 6652 | add_banned(su, preword + prewordlen); |
| 6653 | break; |
| 6654 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6655 | if (was_banned(su, preword + prewordlen) |
| 6656 | || was_banned(su, preword)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6657 | break; |
| 6658 | |
| 6659 | newscore = 0; |
| 6660 | if ((flags & WF_REGION) |
| 6661 | && (((unsigned)flags >> 8) & lp->lp_region) == 0) |
| 6662 | newscore += SCORE_REGION; |
| 6663 | if (flags & WF_RARE) |
| 6664 | newscore += SCORE_RARE; |
| 6665 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6666 | if (!spell_valid_case(su->su_badflags, |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6667 | captype(preword + prewordlen, NULL))) |
| 6668 | newscore += SCORE_ICASE; |
| 6669 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6670 | if ((fword[sp->ts_fidx] == NUL |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6671 | || !spell_iswordp(fword + sp->ts_fidx, curbuf)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6672 | && sp->ts_fidx >= sp->ts_fidxtry) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6673 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6674 | /* The badword also ends: add suggestions. Give a penalty |
| 6675 | * when changing non-word char to word char, e.g., "thes," |
| 6676 | * -> "these". */ |
| 6677 | p = fword + sp->ts_fidx; |
| 6678 | #ifdef FEAT_MBYTE |
| 6679 | if (has_mbyte) |
| 6680 | mb_ptr_back(fword, p); |
| 6681 | else |
| 6682 | #endif |
| 6683 | --p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6684 | if (!spell_iswordp(p, curbuf)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6685 | { |
| 6686 | p = preword + STRLEN(preword); |
| 6687 | #ifdef FEAT_MBYTE |
| 6688 | if (has_mbyte) |
| 6689 | mb_ptr_back(preword, p); |
| 6690 | else |
| 6691 | #endif |
| 6692 | --p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6693 | if (spell_iswordp(p, curbuf)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6694 | newscore += SCORE_NONWORD; |
| 6695 | } |
| 6696 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6697 | add_suggestion(su, &su->su_ga, preword, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6698 | sp->ts_fidx - repextra, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6699 | sp->ts_score + newscore, 0, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6700 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6701 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 6702 | #ifdef FEAT_MBYTE |
| 6703 | /* Don't split halfway a character. */ |
| 6704 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 6705 | #endif |
| 6706 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6707 | { |
| 6708 | /* The word in the tree ends but the badword |
| 6709 | * continues: try inserting a space and check that a valid |
| 6710 | * words starts at fword[sp->ts_fidx]. */ |
| 6711 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 6712 | { |
| 6713 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 6714 | sp->ts_save_prewordlen = prewordlen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6715 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6716 | sp->ts_save_splitoff = splitoff; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6717 | sp->ts_state = STATE_SPLITUNDO; |
| 6718 | |
| 6719 | ++depth; |
| 6720 | sp = &stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6721 | |
| 6722 | /* Append a space to preword. */ |
| 6723 | STRCAT(preword, " "); |
| 6724 | prewordlen = STRLEN(preword); |
| 6725 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6726 | |
| 6727 | /* If the badword has a non-word character at this |
| 6728 | * position skip it. That means replacing the |
| 6729 | * non-word character with a space. */ |
| 6730 | if (!spell_iswordp_nmw(fword + sp->ts_fidx)) |
| 6731 | { |
| 6732 | sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; |
| 6733 | #ifdef FEAT_MBYTE |
| 6734 | if (has_mbyte) |
| 6735 | sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
| 6736 | else |
| 6737 | #endif |
| 6738 | ++sp->ts_fidx; |
| 6739 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6740 | #ifdef FEAT_MBYTE |
| 6741 | if (has_mbyte) |
| 6742 | { |
| 6743 | int i = 0; |
| 6744 | |
| 6745 | /* Case-folding may change the number of bytes: |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6746 | * Count nr of chars in fword[ts_fidx] and |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6747 | * advance that many chars in su->su_badptr. */ |
| 6748 | for (p = fword; p < fword + sp->ts_fidx; |
| 6749 | mb_ptr_adv(p)) |
| 6750 | ++i; |
| 6751 | for (p = su->su_badptr; i > 0; mb_ptr_adv(p)) |
| 6752 | --i; |
| 6753 | } |
| 6754 | else |
| 6755 | #endif |
| 6756 | p = su->su_badptr + sp->ts_fidx; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6757 | su->su_badflags = captype(p, su->su_badptr |
| 6758 | + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6759 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6760 | /* Restart at top of the tree. */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6761 | sp->ts_arridx = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6762 | } |
| 6763 | } |
| 6764 | break; |
| 6765 | |
| 6766 | case STATE_SPLITUNDO: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6767 | /* Undo the changes done for word split. */ |
| 6768 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6769 | splitoff = sp->ts_save_splitoff; |
| 6770 | prewordlen = sp->ts_save_prewordlen; |
| 6771 | |
| 6772 | /* Continue looking for NUL bytes. */ |
| 6773 | sp->ts_state = STATE_START; |
| 6774 | break; |
| 6775 | |
| 6776 | case STATE_ENDNUL: |
| 6777 | /* Past the NUL bytes in the node. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6778 | if (fword[sp->ts_fidx] == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6779 | { |
| 6780 | /* The badword ends, can't use the bytes in this node. */ |
| 6781 | sp->ts_state = STATE_DEL; |
| 6782 | break; |
| 6783 | } |
| 6784 | sp->ts_state = STATE_PLAIN; |
| 6785 | /*FALLTHROUGH*/ |
| 6786 | |
| 6787 | case STATE_PLAIN: |
| 6788 | /* |
| 6789 | * Go over all possible bytes at this node, add each to |
| 6790 | * tword[] and use child node. "ts_curi" is the index. |
| 6791 | */ |
| 6792 | arridx = sp->ts_arridx; |
| 6793 | if (sp->ts_curi > byts[arridx]) |
| 6794 | { |
| 6795 | /* Done all bytes at this node, do next state. When still |
| 6796 | * at already changed bytes skip the other tricks. */ |
| 6797 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 6798 | sp->ts_state = STATE_DEL; |
| 6799 | else |
| 6800 | sp->ts_state = STATE_FINAL; |
| 6801 | } |
| 6802 | else |
| 6803 | { |
| 6804 | arridx += sp->ts_curi++; |
| 6805 | c = byts[arridx]; |
| 6806 | |
| 6807 | /* Normal byte, go one level deeper. If it's not equal to |
| 6808 | * the byte in the bad word adjust the score. But don't |
| 6809 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6810 | if (c == fword[sp->ts_fidx] |
| 6811 | #ifdef FEAT_MBYTE |
| 6812 | || (sp->ts_tcharlen > 0 |
| 6813 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6814 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6815 | ) |
| 6816 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6817 | else |
| 6818 | newscore = SCORE_SUBST; |
| 6819 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 6820 | && try_deeper(su, stack, depth, newscore)) |
| 6821 | { |
| 6822 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6823 | sp = &stack[depth]; |
| 6824 | ++sp->ts_fidx; |
| 6825 | tword[sp->ts_twordlen++] = c; |
| 6826 | sp->ts_arridx = idxs[arridx]; |
| 6827 | #ifdef FEAT_MBYTE |
| 6828 | if (newscore == SCORE_SUBST) |
| 6829 | sp->ts_isdiff = DIFF_YES; |
| 6830 | if (has_mbyte) |
| 6831 | { |
| 6832 | /* Multi-byte characters are a bit complicated to |
| 6833 | * handle: They differ when any of the bytes |
| 6834 | * differ and then their length may also differ. */ |
| 6835 | if (sp->ts_tcharlen == 0) |
| 6836 | { |
| 6837 | /* First byte. */ |
| 6838 | sp->ts_tcharidx = 0; |
| 6839 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 6840 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 6841 | sp->ts_isdiff = (newscore != 0) |
| 6842 | ? DIFF_YES : DIFF_NONE; |
| 6843 | } |
| 6844 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 6845 | /* When inserting trail bytes don't advance in |
| 6846 | * the bad word. */ |
| 6847 | --sp->ts_fidx; |
| 6848 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 6849 | { |
| 6850 | /* Last byte of character. */ |
| 6851 | if (sp->ts_isdiff == DIFF_YES) |
| 6852 | { |
| 6853 | /* Correct ts_fidx for the byte length of |
| 6854 | * the character (we didn't check that |
| 6855 | * before). */ |
| 6856 | sp->ts_fidx = sp->ts_fcharstart |
| 6857 | + MB_BYTE2LEN( |
| 6858 | fword[sp->ts_fcharstart]); |
| 6859 | |
| 6860 | /* For a similar character adjust score |
| 6861 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 6862 | if (lp->lp_slang->sl_has_map |
| 6863 | && similar_chars(lp->lp_slang, |
| 6864 | mb_ptr2char(tword |
| 6865 | + sp->ts_twordlen |
| 6866 | - sp->ts_tcharlen), |
| 6867 | mb_ptr2char(fword |
| 6868 | + sp->ts_fcharstart))) |
| 6869 | sp->ts_score -= |
| 6870 | SCORE_SUBST - SCORE_SIMILAR; |
| 6871 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6872 | else if (sp->ts_isdiff == DIFF_INSERT |
| 6873 | && sp->ts_twordlen > sp->ts_tcharlen) |
| 6874 | { |
| 6875 | /* If the previous character was the same, |
| 6876 | * thus doubling a character, give a bonus |
| 6877 | * to the score. */ |
| 6878 | p = tword + sp->ts_twordlen |
| 6879 | - sp->ts_tcharlen; |
| 6880 | c = mb_ptr2char(p); |
| 6881 | mb_ptr_back(tword, p); |
| 6882 | if (c == mb_ptr2char(p)) |
| 6883 | sp->ts_score -= SCORE_INS |
| 6884 | - SCORE_INSDUP; |
| 6885 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6886 | |
| 6887 | /* Starting a new char, reset the length. */ |
| 6888 | sp->ts_tcharlen = 0; |
| 6889 | } |
| 6890 | } |
| 6891 | else |
| 6892 | #endif |
| 6893 | { |
| 6894 | /* If we found a similar char adjust the score. |
| 6895 | * We do this after calling try_deeper() because |
| 6896 | * it's slow. */ |
| 6897 | if (newscore != 0 |
| 6898 | && lp->lp_slang->sl_has_map |
| 6899 | && similar_chars(lp->lp_slang, |
| 6900 | c, fword[sp->ts_fidx - 1])) |
| 6901 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 6902 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6903 | } |
| 6904 | } |
| 6905 | break; |
| 6906 | |
| 6907 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6908 | #ifdef FEAT_MBYTE |
| 6909 | /* When past the first byte of a multi-byte char don't try |
| 6910 | * delete/insert/swap a character. */ |
| 6911 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 6912 | { |
| 6913 | sp->ts_state = STATE_FINAL; |
| 6914 | break; |
| 6915 | } |
| 6916 | #endif |
| 6917 | /* |
| 6918 | * Try skipping one character in the bad word (delete it). |
| 6919 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6920 | sp->ts_state = STATE_INS; |
| 6921 | sp->ts_curi = 1; |
| 6922 | if (fword[sp->ts_fidx] != NUL |
| 6923 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 6924 | { |
| 6925 | ++depth; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6926 | |
| 6927 | /* Advance over the character in fword[]. Give a bonus to |
| 6928 | * the score if the same character is following "nn" -> |
| 6929 | * "n". */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6930 | #ifdef FEAT_MBYTE |
| 6931 | if (has_mbyte) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6932 | { |
| 6933 | c = mb_ptr2char(fword + sp->ts_fidx); |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6934 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6935 | if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) |
| 6936 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 6937 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6938 | else |
| 6939 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6940 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6941 | ++stack[depth].ts_fidx; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6942 | if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) |
| 6943 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 6944 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6945 | break; |
| 6946 | } |
| 6947 | /*FALLTHROUGH*/ |
| 6948 | |
| 6949 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6950 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6951 | * node. */ |
| 6952 | n = sp->ts_arridx; |
| 6953 | if (sp->ts_curi > byts[n]) |
| 6954 | { |
| 6955 | /* Done all bytes at this node, do next state. */ |
| 6956 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6957 | } |
| 6958 | else |
| 6959 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6960 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6961 | n += sp->ts_curi++; |
| 6962 | c = byts[n]; |
| 6963 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 6964 | { |
| 6965 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6966 | sp = &stack[depth]; |
| 6967 | tword[sp->ts_twordlen++] = c; |
| 6968 | sp->ts_arridx = idxs[n]; |
| 6969 | #ifdef FEAT_MBYTE |
| 6970 | if (has_mbyte) |
| 6971 | { |
| 6972 | fl = MB_BYTE2LEN(c); |
| 6973 | if (fl > 1) |
| 6974 | { |
| 6975 | /* There are following bytes for the same |
| 6976 | * character. We must find all bytes before |
| 6977 | * trying delete/insert/swap/etc. */ |
| 6978 | sp->ts_tcharlen = fl; |
| 6979 | sp->ts_tcharidx = 1; |
| 6980 | sp->ts_isdiff = DIFF_INSERT; |
| 6981 | } |
| 6982 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6983 | else |
| 6984 | fl = 1; |
| 6985 | if (fl == 1) |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6986 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6987 | { |
| 6988 | /* If the previous character was the same, thus |
| 6989 | * doubling a character, give a bonus to the |
| 6990 | * score. */ |
| 6991 | if (sp->ts_twordlen >= 2 |
| 6992 | && tword[sp->ts_twordlen - 2] == c) |
| 6993 | sp->ts_score -= SCORE_INS - SCORE_INSDUP; |
| 6994 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6995 | } |
| 6996 | } |
| 6997 | break; |
| 6998 | |
| 6999 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7000 | /* |
| 7001 | * Swap two bytes in the bad word: "12" -> "21". |
| 7002 | * We change "fword" here, it's changed back afterwards. |
| 7003 | */ |
| 7004 | p = fword + sp->ts_fidx; |
| 7005 | c = *p; |
| 7006 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7007 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7008 | /* End of word, can't swap or replace. */ |
| 7009 | sp->ts_state = STATE_FINAL; |
| 7010 | break; |
| 7011 | } |
| 7012 | #ifdef FEAT_MBYTE |
| 7013 | if (has_mbyte) |
| 7014 | { |
| 7015 | n = mb_ptr2len_check(p); |
| 7016 | c = mb_ptr2char(p); |
| 7017 | c2 = mb_ptr2char(p + n); |
| 7018 | } |
| 7019 | else |
| 7020 | #endif |
| 7021 | c2 = p[1]; |
| 7022 | if (c == c2) |
| 7023 | { |
| 7024 | /* Characters are identical, swap won't do anything. */ |
| 7025 | sp->ts_state = STATE_SWAP3; |
| 7026 | break; |
| 7027 | } |
| 7028 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 7029 | { |
| 7030 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7031 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7032 | #ifdef FEAT_MBYTE |
| 7033 | if (has_mbyte) |
| 7034 | { |
| 7035 | fl = mb_char2len(c2); |
| 7036 | mch_memmove(p, p + n, fl); |
| 7037 | mb_char2bytes(c, p + fl); |
| 7038 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 7039 | } |
| 7040 | else |
| 7041 | #endif |
| 7042 | { |
| 7043 | p[0] = c2; |
| 7044 | p[1] = c; |
| 7045 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 7046 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7047 | } |
| 7048 | else |
| 7049 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 7050 | sp->ts_state = STATE_REP_INI; |
| 7051 | break; |
| 7052 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7053 | case STATE_UNSWAP: |
| 7054 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 7055 | p = fword + sp->ts_fidx; |
| 7056 | #ifdef FEAT_MBYTE |
| 7057 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7058 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7059 | n = MB_BYTE2LEN(*p); |
| 7060 | c = mb_ptr2char(p + n); |
| 7061 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 7062 | mb_char2bytes(c, p); |
| 7063 | } |
| 7064 | else |
| 7065 | #endif |
| 7066 | { |
| 7067 | c = *p; |
| 7068 | *p = p[1]; |
| 7069 | p[1] = c; |
| 7070 | } |
| 7071 | /*FALLTHROUGH*/ |
| 7072 | |
| 7073 | case STATE_SWAP3: |
| 7074 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 7075 | * "fword" here, it's changed back afterwards. */ |
| 7076 | p = fword + sp->ts_fidx; |
| 7077 | #ifdef FEAT_MBYTE |
| 7078 | if (has_mbyte) |
| 7079 | { |
| 7080 | n = mb_ptr2len_check(p); |
| 7081 | c = mb_ptr2char(p); |
| 7082 | fl = mb_ptr2len_check(p + n); |
| 7083 | c2 = mb_ptr2char(p + n); |
| 7084 | c3 = mb_ptr2char(p + n + fl); |
| 7085 | } |
| 7086 | else |
| 7087 | #endif |
| 7088 | { |
| 7089 | c = *p; |
| 7090 | c2 = p[1]; |
| 7091 | c3 = p[2]; |
| 7092 | } |
| 7093 | |
| 7094 | /* When characters are identical: "121" then SWAP3 result is |
| 7095 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 7096 | * result is same as SWAP on next char: "112". Thus skip all |
| 7097 | * swapping. Also skip when c3 is NUL. */ |
| 7098 | if (c == c3 || c3 == NUL) |
| 7099 | { |
| 7100 | sp->ts_state = STATE_REP_INI; |
| 7101 | break; |
| 7102 | } |
| 7103 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7104 | { |
| 7105 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7106 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7107 | #ifdef FEAT_MBYTE |
| 7108 | if (has_mbyte) |
| 7109 | { |
| 7110 | tl = mb_char2len(c3); |
| 7111 | mch_memmove(p, p + n + fl, tl); |
| 7112 | mb_char2bytes(c2, p + tl); |
| 7113 | mb_char2bytes(c, p + fl + tl); |
| 7114 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 7115 | } |
| 7116 | else |
| 7117 | #endif |
| 7118 | { |
| 7119 | p[0] = p[2]; |
| 7120 | p[2] = c; |
| 7121 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7122 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7123 | } |
| 7124 | else |
| 7125 | sp->ts_state = STATE_REP_INI; |
| 7126 | break; |
| 7127 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7128 | case STATE_UNSWAP3: |
| 7129 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 7130 | p = fword + sp->ts_fidx; |
| 7131 | #ifdef FEAT_MBYTE |
| 7132 | if (has_mbyte) |
| 7133 | { |
| 7134 | n = MB_BYTE2LEN(*p); |
| 7135 | c2 = mb_ptr2char(p + n); |
| 7136 | fl = MB_BYTE2LEN(p[n]); |
| 7137 | c = mb_ptr2char(p + n + fl); |
| 7138 | tl = MB_BYTE2LEN(p[n + fl]); |
| 7139 | mch_memmove(p + fl + tl, p, n); |
| 7140 | mb_char2bytes(c, p); |
| 7141 | mb_char2bytes(c2, p + tl); |
| 7142 | } |
| 7143 | else |
| 7144 | #endif |
| 7145 | { |
| 7146 | c = *p; |
| 7147 | *p = p[2]; |
| 7148 | p[2] = c; |
| 7149 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7150 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7151 | /* Rotate three characters left: "123" -> "231". We change |
| 7152 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7153 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7154 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7155 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7156 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7157 | p = fword + sp->ts_fidx; |
| 7158 | #ifdef FEAT_MBYTE |
| 7159 | if (has_mbyte) |
| 7160 | { |
| 7161 | n = mb_ptr2len_check(p); |
| 7162 | c = mb_ptr2char(p); |
| 7163 | fl = mb_ptr2len_check(p + n); |
| 7164 | fl += mb_ptr2len_check(p + n + fl); |
| 7165 | mch_memmove(p, p + n, fl); |
| 7166 | mb_char2bytes(c, p + fl); |
| 7167 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 7168 | } |
| 7169 | else |
| 7170 | #endif |
| 7171 | { |
| 7172 | c = *p; |
| 7173 | *p = p[1]; |
| 7174 | p[1] = p[2]; |
| 7175 | p[2] = c; |
| 7176 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7177 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7178 | } |
| 7179 | else |
| 7180 | sp->ts_state = STATE_REP_INI; |
| 7181 | break; |
| 7182 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7183 | case STATE_UNROT3L: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7184 | /* Undo ROT3L: "231" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7185 | p = fword + sp->ts_fidx; |
| 7186 | #ifdef FEAT_MBYTE |
| 7187 | if (has_mbyte) |
| 7188 | { |
| 7189 | n = MB_BYTE2LEN(*p); |
| 7190 | n += MB_BYTE2LEN(p[n]); |
| 7191 | c = mb_ptr2char(p + n); |
| 7192 | tl = MB_BYTE2LEN(p[n]); |
| 7193 | mch_memmove(p + tl, p, n); |
| 7194 | mb_char2bytes(c, p); |
| 7195 | } |
| 7196 | else |
| 7197 | #endif |
| 7198 | { |
| 7199 | c = p[2]; |
| 7200 | p[2] = p[1]; |
| 7201 | p[1] = *p; |
| 7202 | *p = c; |
| 7203 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7204 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7205 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7206 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7207 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7208 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7209 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7210 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7211 | p = fword + sp->ts_fidx; |
| 7212 | #ifdef FEAT_MBYTE |
| 7213 | if (has_mbyte) |
| 7214 | { |
| 7215 | n = mb_ptr2len_check(p); |
| 7216 | n += mb_ptr2len_check(p + n); |
| 7217 | c = mb_ptr2char(p + n); |
| 7218 | tl = mb_ptr2len_check(p + n); |
| 7219 | mch_memmove(p + tl, p, n); |
| 7220 | mb_char2bytes(c, p); |
| 7221 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 7222 | } |
| 7223 | else |
| 7224 | #endif |
| 7225 | { |
| 7226 | c = p[2]; |
| 7227 | p[2] = p[1]; |
| 7228 | p[1] = *p; |
| 7229 | *p = c; |
| 7230 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7231 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7232 | } |
| 7233 | else |
| 7234 | sp->ts_state = STATE_REP_INI; |
| 7235 | break; |
| 7236 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7237 | case STATE_UNROT3R: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7238 | /* Undo ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7239 | p = fword + sp->ts_fidx; |
| 7240 | #ifdef FEAT_MBYTE |
| 7241 | if (has_mbyte) |
| 7242 | { |
| 7243 | c = mb_ptr2char(p); |
| 7244 | tl = MB_BYTE2LEN(*p); |
| 7245 | n = MB_BYTE2LEN(p[tl]); |
| 7246 | n += MB_BYTE2LEN(p[tl + n]); |
| 7247 | mch_memmove(p, p + tl, n); |
| 7248 | mb_char2bytes(c, p + n); |
| 7249 | } |
| 7250 | else |
| 7251 | #endif |
| 7252 | { |
| 7253 | c = *p; |
| 7254 | *p = p[1]; |
| 7255 | p[1] = p[2]; |
| 7256 | p[2] = c; |
| 7257 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7258 | /*FALLTHROUGH*/ |
| 7259 | |
| 7260 | case STATE_REP_INI: |
| 7261 | /* Check if matching with REP items from the .aff file would |
| 7262 | * work. Quickly skip if there are no REP items or the score |
| 7263 | * is going to be too high anyway. */ |
| 7264 | gap = &lp->lp_slang->sl_rep; |
| 7265 | if (gap->ga_len == 0 |
| 7266 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 7267 | { |
| 7268 | sp->ts_state = STATE_FINAL; |
| 7269 | break; |
| 7270 | } |
| 7271 | |
| 7272 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7273 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7274 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 7275 | if (sp->ts_curi < 0) |
| 7276 | { |
| 7277 | sp->ts_state = STATE_FINAL; |
| 7278 | break; |
| 7279 | } |
| 7280 | |
| 7281 | sp->ts_state = STATE_REP; |
| 7282 | /*FALLTHROUGH*/ |
| 7283 | |
| 7284 | case STATE_REP: |
| 7285 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7286 | * match replace the characters and check if the resulting |
| 7287 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7288 | p = fword + sp->ts_fidx; |
| 7289 | |
| 7290 | gap = &lp->lp_slang->sl_rep; |
| 7291 | while (sp->ts_curi < gap->ga_len) |
| 7292 | { |
| 7293 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 7294 | if (*ftp->ft_from != *p) |
| 7295 | { |
| 7296 | /* past possible matching entries */ |
| 7297 | sp->ts_curi = gap->ga_len; |
| 7298 | break; |
| 7299 | } |
| 7300 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 7301 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 7302 | { |
| 7303 | /* Need to undo this afterwards. */ |
| 7304 | sp->ts_state = STATE_REP_UNDO; |
| 7305 | |
| 7306 | /* Change the "from" to the "to" string. */ |
| 7307 | ++depth; |
| 7308 | fl = STRLEN(ftp->ft_from); |
| 7309 | tl = STRLEN(ftp->ft_to); |
| 7310 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7311 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7312 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7313 | repextra += tl - fl; |
| 7314 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7315 | mch_memmove(p, ftp->ft_to, tl); |
| 7316 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7317 | #ifdef FEAT_MBYTE |
| 7318 | stack[depth].ts_tcharlen = 0; |
| 7319 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7320 | break; |
| 7321 | } |
| 7322 | } |
| 7323 | |
| 7324 | if (sp->ts_curi >= gap->ga_len) |
| 7325 | /* No (more) matches. */ |
| 7326 | sp->ts_state = STATE_FINAL; |
| 7327 | |
| 7328 | break; |
| 7329 | |
| 7330 | case STATE_REP_UNDO: |
| 7331 | /* Undo a REP replacement and continue with the next one. */ |
| 7332 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 7333 | + sp->ts_curi - 1; |
| 7334 | fl = STRLEN(ftp->ft_from); |
| 7335 | tl = STRLEN(ftp->ft_to); |
| 7336 | p = fword + sp->ts_fidx; |
| 7337 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7338 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7339 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7340 | repextra -= tl - fl; |
| 7341 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7342 | mch_memmove(p, ftp->ft_from, fl); |
| 7343 | sp->ts_state = STATE_REP; |
| 7344 | break; |
| 7345 | |
| 7346 | default: |
| 7347 | /* Did all possible states at this level, go up one level. */ |
| 7348 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7349 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7350 | if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE) |
| 7351 | { |
| 7352 | /* Continue in or go back to the prefix tree. */ |
| 7353 | byts = pbyts; |
| 7354 | idxs = pidxs; |
| 7355 | splitoff = 0; |
| 7356 | } |
| 7357 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7358 | /* Don't check for CTRL-C too often, it takes time. */ |
| 7359 | line_breakcheck(); |
| 7360 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7361 | } |
| 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | /* |
| 7366 | * Try going one level deeper in the tree. |
| 7367 | */ |
| 7368 | static int |
| 7369 | try_deeper(su, stack, depth, score_add) |
| 7370 | suginfo_T *su; |
| 7371 | trystate_T *stack; |
| 7372 | int depth; |
| 7373 | int score_add; |
| 7374 | { |
| 7375 | int newscore; |
| 7376 | |
| 7377 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 7378 | newscore = stack[depth].ts_score + score_add; |
| 7379 | if (newscore >= su->su_maxscore) |
| 7380 | return FALSE; |
| 7381 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7382 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7383 | stack[depth + 1].ts_state = STATE_START; |
| 7384 | stack[depth + 1].ts_score = newscore; |
| 7385 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7386 | return TRUE; |
| 7387 | } |
| 7388 | |
| 7389 | /* |
| 7390 | * "fword" is a good word with case folded. Find the matching keep-case |
| 7391 | * words and put it in "kword". |
| 7392 | * Theoretically there could be several keep-case words that result in the |
| 7393 | * same case-folded word, but we only find one... |
| 7394 | */ |
| 7395 | static void |
| 7396 | find_keepcap_word(slang, fword, kword) |
| 7397 | slang_T *slang; |
| 7398 | char_u *fword; |
| 7399 | char_u *kword; |
| 7400 | { |
| 7401 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 7402 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7403 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7404 | |
| 7405 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7406 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7407 | int round[MAXWLEN]; |
| 7408 | int fwordidx[MAXWLEN]; |
| 7409 | int uwordidx[MAXWLEN]; |
| 7410 | int kwordlen[MAXWLEN]; |
| 7411 | |
| 7412 | int flen, ulen; |
| 7413 | int l; |
| 7414 | int len; |
| 7415 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7416 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7417 | char_u *p; |
| 7418 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7419 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7420 | |
| 7421 | if (byts == NULL) |
| 7422 | { |
| 7423 | /* array is empty: "cannot happen" */ |
| 7424 | *kword = NUL; |
| 7425 | return; |
| 7426 | } |
| 7427 | |
| 7428 | /* Make an all-cap version of "fword". */ |
| 7429 | allcap_copy(fword, uword); |
| 7430 | |
| 7431 | /* |
| 7432 | * Each character needs to be tried both case-folded and upper-case. |
| 7433 | * All this gets very complicated if we keep in mind that changing case |
| 7434 | * may change the byte length of a multi-byte character... |
| 7435 | */ |
| 7436 | depth = 0; |
| 7437 | arridx[0] = 0; |
| 7438 | round[0] = 0; |
| 7439 | fwordidx[0] = 0; |
| 7440 | uwordidx[0] = 0; |
| 7441 | kwordlen[0] = 0; |
| 7442 | while (depth >= 0) |
| 7443 | { |
| 7444 | if (fword[fwordidx[depth]] == NUL) |
| 7445 | { |
| 7446 | /* We are at the end of "fword". If the tree allows a word to end |
| 7447 | * here we have found a match. */ |
| 7448 | if (byts[arridx[depth] + 1] == 0) |
| 7449 | { |
| 7450 | kword[kwordlen[depth]] = NUL; |
| 7451 | return; |
| 7452 | } |
| 7453 | |
| 7454 | /* kword is getting too long, continue one level up */ |
| 7455 | --depth; |
| 7456 | } |
| 7457 | else if (++round[depth] > 2) |
| 7458 | { |
| 7459 | /* tried both fold-case and upper-case character, continue one |
| 7460 | * level up */ |
| 7461 | --depth; |
| 7462 | } |
| 7463 | else |
| 7464 | { |
| 7465 | /* |
| 7466 | * round[depth] == 1: Try using the folded-case character. |
| 7467 | * round[depth] == 2: Try using the upper-case character. |
| 7468 | */ |
| 7469 | #ifdef FEAT_MBYTE |
| 7470 | if (has_mbyte) |
| 7471 | { |
| 7472 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 7473 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 7474 | } |
| 7475 | else |
| 7476 | #endif |
| 7477 | ulen = flen = 1; |
| 7478 | if (round[depth] == 1) |
| 7479 | { |
| 7480 | p = fword + fwordidx[depth]; |
| 7481 | l = flen; |
| 7482 | } |
| 7483 | else |
| 7484 | { |
| 7485 | p = uword + uwordidx[depth]; |
| 7486 | l = ulen; |
| 7487 | } |
| 7488 | |
| 7489 | for (tryidx = arridx[depth]; l > 0; --l) |
| 7490 | { |
| 7491 | /* Perform a binary search in the list of accepted bytes. */ |
| 7492 | len = byts[tryidx++]; |
| 7493 | c = *p++; |
| 7494 | lo = tryidx; |
| 7495 | hi = tryidx + len - 1; |
| 7496 | while (lo < hi) |
| 7497 | { |
| 7498 | m = (lo + hi) / 2; |
| 7499 | if (byts[m] > c) |
| 7500 | hi = m - 1; |
| 7501 | else if (byts[m] < c) |
| 7502 | lo = m + 1; |
| 7503 | else |
| 7504 | { |
| 7505 | lo = hi = m; |
| 7506 | break; |
| 7507 | } |
| 7508 | } |
| 7509 | |
| 7510 | /* Stop if there is no matching byte. */ |
| 7511 | if (hi < lo || byts[lo] != c) |
| 7512 | break; |
| 7513 | |
| 7514 | /* Continue at the child (if there is one). */ |
| 7515 | tryidx = idxs[lo]; |
| 7516 | } |
| 7517 | |
| 7518 | if (l == 0) |
| 7519 | { |
| 7520 | /* |
| 7521 | * Found the matching char. Copy it to "kword" and go a |
| 7522 | * level deeper. |
| 7523 | */ |
| 7524 | if (round[depth] == 1) |
| 7525 | { |
| 7526 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 7527 | flen); |
| 7528 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 7529 | } |
| 7530 | else |
| 7531 | { |
| 7532 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 7533 | ulen); |
| 7534 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 7535 | } |
| 7536 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 7537 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 7538 | |
| 7539 | ++depth; |
| 7540 | arridx[depth] = tryidx; |
| 7541 | round[depth] = 0; |
| 7542 | } |
| 7543 | } |
| 7544 | } |
| 7545 | |
| 7546 | /* Didn't find it: "cannot happen". */ |
| 7547 | *kword = NUL; |
| 7548 | } |
| 7549 | |
| 7550 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7551 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 7552 | * su->su_sga. |
| 7553 | */ |
| 7554 | static void |
| 7555 | score_comp_sal(su) |
| 7556 | suginfo_T *su; |
| 7557 | { |
| 7558 | langp_T *lp; |
| 7559 | char_u badsound[MAXWLEN]; |
| 7560 | int i; |
| 7561 | suggest_T *stp; |
| 7562 | suggest_T *sstp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7563 | int score; |
| 7564 | |
| 7565 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 7566 | return; |
| 7567 | |
| 7568 | /* Use the sound-folding of the first language that supports it. */ |
| 7569 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7570 | lp->lp_slang != NULL; ++lp) |
| 7571 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7572 | { |
| 7573 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7574 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7575 | |
| 7576 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7577 | { |
| 7578 | stp = &SUG(su->su_ga, i); |
| 7579 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7580 | /* Case-fold the suggested word, sound-fold it and compute the |
| 7581 | * sound-a-like score. */ |
| 7582 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7583 | if (score < SCORE_MAXMAX) |
| 7584 | { |
| 7585 | /* Add the suggestion. */ |
| 7586 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 7587 | sstp->st_word = vim_strsave(stp->st_word); |
| 7588 | if (sstp->st_word != NULL) |
| 7589 | { |
| 7590 | sstp->st_score = score; |
| 7591 | sstp->st_altscore = 0; |
| 7592 | sstp->st_orglen = stp->st_orglen; |
| 7593 | ++su->su_sga.ga_len; |
| 7594 | } |
| 7595 | } |
| 7596 | } |
| 7597 | break; |
| 7598 | } |
| 7599 | } |
| 7600 | |
| 7601 | /* |
| 7602 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 7603 | * They are intwined. |
| 7604 | */ |
| 7605 | static void |
| 7606 | score_combine(su) |
| 7607 | suginfo_T *su; |
| 7608 | { |
| 7609 | int i; |
| 7610 | int j; |
| 7611 | garray_T ga; |
| 7612 | garray_T *gap; |
| 7613 | langp_T *lp; |
| 7614 | suggest_T *stp; |
| 7615 | char_u *p; |
| 7616 | char_u badsound[MAXWLEN]; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7617 | int round; |
| 7618 | |
| 7619 | /* Add the alternate score to su_ga. */ |
| 7620 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7621 | lp->lp_slang != NULL; ++lp) |
| 7622 | { |
| 7623 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7624 | { |
| 7625 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7626 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7627 | |
| 7628 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7629 | { |
| 7630 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7631 | stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang, |
| 7632 | badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7633 | if (stp->st_altscore == SCORE_MAXMAX) |
| 7634 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 7635 | else |
| 7636 | stp->st_score = (stp->st_score * 3 |
| 7637 | + stp->st_altscore) / 4; |
| 7638 | stp->st_salscore = FALSE; |
| 7639 | } |
| 7640 | break; |
| 7641 | } |
| 7642 | } |
| 7643 | |
| 7644 | /* Add the alternate score to su_sga. */ |
| 7645 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 7646 | { |
| 7647 | stp = &SUG(su->su_sga, i); |
| 7648 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 7649 | if (stp->st_score == SCORE_MAXMAX) |
| 7650 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 7651 | else |
| 7652 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 7653 | stp->st_salscore = TRUE; |
| 7654 | } |
| 7655 | |
| 7656 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 7657 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 7658 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 7659 | |
| 7660 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 7661 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 7662 | return; |
| 7663 | |
| 7664 | stp = &SUG(ga, 0); |
| 7665 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 7666 | { |
| 7667 | /* round 1: get a suggestion from su_ga |
| 7668 | * round 2: get a suggestion from su_sga */ |
| 7669 | for (round = 1; round <= 2; ++round) |
| 7670 | { |
| 7671 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 7672 | if (i < gap->ga_len) |
| 7673 | { |
| 7674 | /* Don't add a word if it's already there. */ |
| 7675 | p = SUG(*gap, i).st_word; |
| 7676 | for (j = 0; j < ga.ga_len; ++j) |
| 7677 | if (STRCMP(stp[j].st_word, p) == 0) |
| 7678 | break; |
| 7679 | if (j == ga.ga_len) |
| 7680 | stp[ga.ga_len++] = SUG(*gap, i); |
| 7681 | else |
| 7682 | vim_free(p); |
| 7683 | } |
| 7684 | } |
| 7685 | } |
| 7686 | |
| 7687 | ga_clear(&su->su_ga); |
| 7688 | ga_clear(&su->su_sga); |
| 7689 | |
| 7690 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 7691 | if (ga.ga_len > su->su_maxcount) |
| 7692 | { |
| 7693 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 7694 | vim_free(stp[i].st_word); |
| 7695 | ga.ga_len = su->su_maxcount; |
| 7696 | } |
| 7697 | |
| 7698 | su->su_ga = ga; |
| 7699 | } |
| 7700 | |
| 7701 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7702 | * For the goodword in "stp" compute the soundalike score compared to the |
| 7703 | * badword. |
| 7704 | */ |
| 7705 | static int |
| 7706 | stp_sal_score(stp, su, slang, badsound) |
| 7707 | suggest_T *stp; |
| 7708 | suginfo_T *su; |
| 7709 | slang_T *slang; |
| 7710 | char_u *badsound; /* sound-folded badword */ |
| 7711 | { |
| 7712 | char_u *p; |
| 7713 | char_u badsound2[MAXWLEN]; |
| 7714 | char_u fword[MAXWLEN]; |
| 7715 | char_u goodsound[MAXWLEN]; |
| 7716 | |
| 7717 | if (stp->st_orglen <= su->su_badlen) |
| 7718 | p = badsound; |
| 7719 | else |
| 7720 | { |
| 7721 | /* soundfold the bad word with more characters following */ |
| 7722 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 7723 | |
| 7724 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 7725 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 7726 | * removing the space. Don't do it when the good word also contains a |
| 7727 | * space. */ |
| 7728 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 7729 | && *skiptowhite(stp->st_word) == NUL) |
| 7730 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| 7731 | mch_memmove(p, p + 1, STRLEN(p)); |
| 7732 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7733 | spell_soundfold(slang, fword, TRUE, badsound2); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7734 | p = badsound2; |
| 7735 | } |
| 7736 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7737 | /* Sound-fold the word and compute the score for the difference. */ |
| 7738 | spell_soundfold(slang, stp->st_word, FALSE, goodsound); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7739 | |
| 7740 | return soundalike_score(goodsound, p); |
| 7741 | } |
| 7742 | |
| 7743 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7744 | * Find suggestions by comparing the word in a sound-a-like form. |
| 7745 | */ |
| 7746 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7747 | suggest_try_soundalike(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7748 | suginfo_T *su; |
| 7749 | { |
| 7750 | char_u salword[MAXWLEN]; |
| 7751 | char_u tword[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7752 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7753 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7754 | int curi[MAXWLEN]; |
| 7755 | langp_T *lp; |
| 7756 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7757 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7758 | int depth; |
| 7759 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7760 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7761 | int round; |
| 7762 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7763 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7764 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7765 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7766 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7767 | lp->lp_slang != NULL; ++lp) |
| 7768 | { |
| 7769 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7770 | { |
| 7771 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7772 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7773 | |
| 7774 | /* |
| 7775 | * Go through the whole tree, soundfold each word and compare. |
| 7776 | * round 1: use the case-folded tree. |
| 7777 | * round 2: use the keep-case tree. |
| 7778 | */ |
| 7779 | for (round = 1; round <= 2; ++round) |
| 7780 | { |
| 7781 | if (round == 1) |
| 7782 | { |
| 7783 | byts = lp->lp_slang->sl_fbyts; |
| 7784 | idxs = lp->lp_slang->sl_fidxs; |
| 7785 | } |
| 7786 | else |
| 7787 | { |
| 7788 | byts = lp->lp_slang->sl_kbyts; |
| 7789 | idxs = lp->lp_slang->sl_kidxs; |
| 7790 | } |
| 7791 | |
| 7792 | depth = 0; |
| 7793 | arridx[0] = 0; |
| 7794 | curi[0] = 1; |
| 7795 | while (depth >= 0 && !got_int) |
| 7796 | { |
| 7797 | if (curi[depth] > byts[arridx[depth]]) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7798 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7799 | /* Done all bytes at this node, go up one level. */ |
| 7800 | --depth; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7801 | line_breakcheck(); |
| 7802 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7803 | else |
| 7804 | { |
| 7805 | /* Do one more byte at this node. */ |
| 7806 | n = arridx[depth] + curi[depth]; |
| 7807 | ++curi[depth]; |
| 7808 | c = byts[n]; |
| 7809 | if (c == 0) |
| 7810 | { |
| 7811 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7812 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7813 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 7814 | { |
| 7815 | tword[depth] = NUL; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7816 | /* Sound-fold. Only in keep-case tree need to |
| 7817 | * case-fold the word. */ |
| 7818 | spell_soundfold(lp->lp_slang, tword, |
| 7819 | round == 1, tsalword); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7820 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7821 | /* Compute the edit distance between the |
| 7822 | * sound-a-like words. */ |
| 7823 | sound_score = soundalike_score(salword, |
| 7824 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7825 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7826 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7827 | char_u cword[MAXWLEN]; |
| 7828 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7829 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7830 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7831 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7832 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7833 | /* Need to fix case according to |
| 7834 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7835 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7836 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7837 | } |
| 7838 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7839 | p = tword; |
| 7840 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7841 | if (sps_flags & SPS_DOUBLE) |
| 7842 | add_suggestion(su, &su->su_sga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7843 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7844 | sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7845 | else |
| 7846 | { |
| 7847 | /* Compute the score. */ |
| 7848 | score = spell_edit_score( |
| 7849 | su->su_badword, p); |
| 7850 | if (sps_flags & SPS_BEST) |
| 7851 | /* give a bonus for the good word |
| 7852 | * sounding the same as the bad |
| 7853 | * word */ |
| 7854 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7855 | su->su_badlen, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7856 | RESCORE(score, sound_score), |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7857 | sound_score, TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7858 | else |
| 7859 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7860 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7861 | score + sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7862 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7863 | } |
| 7864 | } |
| 7865 | |
| 7866 | /* Skip over other NUL bytes. */ |
| 7867 | while (byts[n + 1] == 0) |
| 7868 | { |
| 7869 | ++n; |
| 7870 | ++curi[depth]; |
| 7871 | } |
| 7872 | } |
| 7873 | else |
| 7874 | { |
| 7875 | /* Normal char, go one level deeper. */ |
| 7876 | tword[depth++] = c; |
| 7877 | arridx[depth] = idxs[n]; |
| 7878 | curi[depth] = 1; |
| 7879 | } |
| 7880 | } |
| 7881 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7882 | } |
| 7883 | } |
| 7884 | } |
| 7885 | } |
| 7886 | |
| 7887 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7888 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7889 | */ |
| 7890 | static void |
| 7891 | make_case_word(fword, cword, flags) |
| 7892 | char_u *fword; |
| 7893 | char_u *cword; |
| 7894 | int flags; |
| 7895 | { |
| 7896 | if (flags & WF_ALLCAP) |
| 7897 | /* Make it all upper-case */ |
| 7898 | allcap_copy(fword, cword); |
| 7899 | else if (flags & WF_ONECAP) |
| 7900 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7901 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7902 | else |
| 7903 | /* Use goodword as-is. */ |
| 7904 | STRCPY(cword, fword); |
| 7905 | } |
| 7906 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7907 | /* |
| 7908 | * Use map string "map" for languages "lp". |
| 7909 | */ |
| 7910 | static void |
| 7911 | set_map_str(lp, map) |
| 7912 | slang_T *lp; |
| 7913 | char_u *map; |
| 7914 | { |
| 7915 | char_u *p; |
| 7916 | int headc = 0; |
| 7917 | int c; |
| 7918 | int i; |
| 7919 | |
| 7920 | if (*map == NUL) |
| 7921 | { |
| 7922 | lp->sl_has_map = FALSE; |
| 7923 | return; |
| 7924 | } |
| 7925 | lp->sl_has_map = TRUE; |
| 7926 | |
| 7927 | /* Init the array and hash table empty. */ |
| 7928 | for (i = 0; i < 256; ++i) |
| 7929 | lp->sl_map_array[i] = 0; |
| 7930 | #ifdef FEAT_MBYTE |
| 7931 | hash_init(&lp->sl_map_hash); |
| 7932 | #endif |
| 7933 | |
| 7934 | /* |
| 7935 | * The similar characters are stored separated with slashes: |
| 7936 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 7937 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 7938 | */ |
| 7939 | for (p = map; *p != NUL; ) |
| 7940 | { |
| 7941 | #ifdef FEAT_MBYTE |
| 7942 | c = mb_ptr2char_adv(&p); |
| 7943 | #else |
| 7944 | c = *p++; |
| 7945 | #endif |
| 7946 | if (c == '/') |
| 7947 | headc = 0; |
| 7948 | else |
| 7949 | { |
| 7950 | if (headc == 0) |
| 7951 | headc = c; |
| 7952 | |
| 7953 | #ifdef FEAT_MBYTE |
| 7954 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 7955 | * the hash table. Each entry is the char, a NUL the headchar and |
| 7956 | * a NUL. */ |
| 7957 | if (c >= 256) |
| 7958 | { |
| 7959 | int cl = mb_char2len(c); |
| 7960 | int headcl = mb_char2len(headc); |
| 7961 | char_u *b; |
| 7962 | hash_T hash; |
| 7963 | hashitem_T *hi; |
| 7964 | |
| 7965 | b = alloc((unsigned)(cl + headcl + 2)); |
| 7966 | if (b == NULL) |
| 7967 | return; |
| 7968 | mb_char2bytes(c, b); |
| 7969 | b[cl] = NUL; |
| 7970 | mb_char2bytes(headc, b + cl + 1); |
| 7971 | b[cl + 1 + headcl] = NUL; |
| 7972 | hash = hash_hash(b); |
| 7973 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 7974 | if (HASHITEM_EMPTY(hi)) |
| 7975 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 7976 | else |
| 7977 | { |
| 7978 | /* This should have been checked when generating the .spl |
| 7979 | * file. */ |
| 7980 | EMSG(_("E999: duplicate char in MAP entry")); |
| 7981 | vim_free(b); |
| 7982 | } |
| 7983 | } |
| 7984 | else |
| 7985 | #endif |
| 7986 | lp->sl_map_array[c] = headc; |
| 7987 | } |
| 7988 | } |
| 7989 | } |
| 7990 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7991 | /* |
| 7992 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 7993 | * lines in the .aff file. |
| 7994 | */ |
| 7995 | static int |
| 7996 | similar_chars(slang, c1, c2) |
| 7997 | slang_T *slang; |
| 7998 | int c1; |
| 7999 | int c2; |
| 8000 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8001 | int m1, m2; |
| 8002 | #ifdef FEAT_MBYTE |
| 8003 | char_u buf[MB_MAXBYTES]; |
| 8004 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8005 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8006 | if (c1 >= 256) |
| 8007 | { |
| 8008 | buf[mb_char2bytes(c1, buf)] = 0; |
| 8009 | hi = hash_find(&slang->sl_map_hash, buf); |
| 8010 | if (HASHITEM_EMPTY(hi)) |
| 8011 | m1 = 0; |
| 8012 | else |
| 8013 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 8014 | } |
| 8015 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8016 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8017 | m1 = slang->sl_map_array[c1]; |
| 8018 | if (m1 == 0) |
| 8019 | return FALSE; |
| 8020 | |
| 8021 | |
| 8022 | #ifdef FEAT_MBYTE |
| 8023 | if (c2 >= 256) |
| 8024 | { |
| 8025 | buf[mb_char2bytes(c2, buf)] = 0; |
| 8026 | hi = hash_find(&slang->sl_map_hash, buf); |
| 8027 | if (HASHITEM_EMPTY(hi)) |
| 8028 | m2 = 0; |
| 8029 | else |
| 8030 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 8031 | } |
| 8032 | else |
| 8033 | #endif |
| 8034 | m2 = slang->sl_map_array[c2]; |
| 8035 | |
| 8036 | return m1 == m2; |
| 8037 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8038 | |
| 8039 | /* |
| 8040 | * Add a suggestion to the list of suggestions. |
| 8041 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 8042 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 8043 | * with spell_edit_score(). |
| 8044 | */ |
| 8045 | static void |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8046 | add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8047 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8048 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8049 | char_u *goodword; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8050 | int badlen; /* length of bad word used */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8051 | int score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8052 | int altscore; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8053 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8054 | { |
| 8055 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8056 | int i; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8057 | char_u *p = NULL; |
| 8058 | int c = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8059 | |
| 8060 | /* Check that the word wasn't banned. */ |
| 8061 | if (was_banned(su, goodword)) |
| 8062 | return; |
| 8063 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8064 | /* If past "su_badlen" and the rest is identical stop at "su_badlen". |
| 8065 | * Remove the common part from "goodword". */ |
| 8066 | i = badlen - su->su_badlen; |
| 8067 | if (i > 0) |
| 8068 | { |
| 8069 | /* This assumes there was no case folding or it didn't change the |
| 8070 | * length... */ |
| 8071 | p = goodword + STRLEN(goodword) - i; |
| 8072 | if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0) |
| 8073 | { |
| 8074 | badlen = su->su_badlen; |
| 8075 | c = *p; |
| 8076 | *p = NUL; |
| 8077 | } |
| 8078 | else |
| 8079 | p = NULL; |
| 8080 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8081 | else if (i < 0) |
| 8082 | { |
| 8083 | /* When replacing part of the word check that we actually change |
| 8084 | * something. For "the the" a suggestion can be replacing the first |
| 8085 | * "the" with itself, since "the" wasn't banned. */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8086 | if (badlen == (int)STRLEN(goodword) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8087 | && STRNCMP(su->su_badword, goodword, badlen) == 0) |
| 8088 | return; |
| 8089 | } |
| 8090 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8091 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8092 | if (score <= su->su_maxscore) |
| 8093 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8094 | /* Check if the word is already there. Also check the length that is |
| 8095 | * being replaced "thes," -> "these" is a different suggestion from |
| 8096 | * "thes" -> "these". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8097 | stp = &SUG(*gap, 0); |
| 8098 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8099 | if (STRCMP(stp[i].st_word, goodword) == 0 |
| 8100 | && stp[i].st_orglen == badlen) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8101 | { |
| 8102 | /* Found it. Remember the lowest score. */ |
| 8103 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8104 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8105 | stp[i].st_score = score; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8106 | stp[i].st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8107 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8108 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8109 | break; |
| 8110 | } |
| 8111 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8112 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8113 | { |
| 8114 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8115 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8116 | stp->st_word = vim_strsave(goodword); |
| 8117 | if (stp->st_word != NULL) |
| 8118 | { |
| 8119 | stp->st_score = score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8120 | stp->st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8121 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8122 | stp->st_orglen = badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8123 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8124 | |
| 8125 | /* If we have too many suggestions now, sort the list and keep |
| 8126 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8127 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 8128 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 8129 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8130 | } |
| 8131 | } |
| 8132 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8133 | |
| 8134 | if (p != NULL) |
| 8135 | *p = c; /* restore "goodword" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | /* |
| 8139 | * Add a word to be banned. |
| 8140 | */ |
| 8141 | static void |
| 8142 | add_banned(su, word) |
| 8143 | suginfo_T *su; |
| 8144 | char_u *word; |
| 8145 | { |
| 8146 | char_u *s = vim_strsave(word); |
| 8147 | hash_T hash; |
| 8148 | hashitem_T *hi; |
| 8149 | |
| 8150 | if (s != NULL) |
| 8151 | { |
| 8152 | hash = hash_hash(s); |
| 8153 | hi = hash_lookup(&su->su_banned, s, hash); |
| 8154 | if (HASHITEM_EMPTY(hi)) |
| 8155 | hash_add_item(&su->su_banned, hi, s, hash); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8156 | else |
| 8157 | vim_free(s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8158 | } |
| 8159 | } |
| 8160 | |
| 8161 | /* |
| 8162 | * Return TRUE if a word appears in the list of banned words. |
| 8163 | */ |
| 8164 | static int |
| 8165 | was_banned(su, word) |
| 8166 | suginfo_T *su; |
| 8167 | char_u *word; |
| 8168 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8169 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 8170 | |
| 8171 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | /* |
| 8175 | * Free the banned words in "su". |
| 8176 | */ |
| 8177 | static void |
| 8178 | free_banned(su) |
| 8179 | suginfo_T *su; |
| 8180 | { |
| 8181 | int todo; |
| 8182 | hashitem_T *hi; |
| 8183 | |
| 8184 | todo = su->su_banned.ht_used; |
| 8185 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 8186 | { |
| 8187 | if (!HASHITEM_EMPTY(hi)) |
| 8188 | { |
| 8189 | vim_free(hi->hi_key); |
| 8190 | --todo; |
| 8191 | } |
| 8192 | } |
| 8193 | hash_clear(&su->su_banned); |
| 8194 | } |
| 8195 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8196 | /* |
| 8197 | * Recompute the score if sound-folding is possible. This is slow, |
| 8198 | * thus only done for the final results. |
| 8199 | */ |
| 8200 | static void |
| 8201 | rescore_suggestions(su) |
| 8202 | suginfo_T *su; |
| 8203 | { |
| 8204 | langp_T *lp; |
| 8205 | suggest_T *stp; |
| 8206 | char_u sal_badword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8207 | int i; |
| 8208 | |
| 8209 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8210 | lp->lp_slang != NULL; ++lp) |
| 8211 | { |
| 8212 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8213 | { |
| 8214 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8215 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8216 | |
| 8217 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 8218 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8219 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8220 | if (!stp->st_had_bonus) |
| 8221 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8222 | stp->st_altscore = stp_sal_score(stp, su, |
| 8223 | lp->lp_slang, sal_badword); |
| 8224 | if (stp->st_altscore == SCORE_MAXMAX) |
| 8225 | stp->st_altscore = SCORE_BIG; |
| 8226 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8227 | } |
| 8228 | } |
| 8229 | break; |
| 8230 | } |
| 8231 | } |
| 8232 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8233 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8234 | static int |
| 8235 | #ifdef __BORLANDC__ |
| 8236 | _RTLENTRYF |
| 8237 | #endif |
| 8238 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 8239 | |
| 8240 | /* |
| 8241 | * Function given to qsort() to sort the suggestions on st_score. |
| 8242 | */ |
| 8243 | static int |
| 8244 | #ifdef __BORLANDC__ |
| 8245 | _RTLENTRYF |
| 8246 | #endif |
| 8247 | sug_compare(s1, s2) |
| 8248 | const void *s1; |
| 8249 | const void *s2; |
| 8250 | { |
| 8251 | suggest_T *p1 = (suggest_T *)s1; |
| 8252 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8253 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8254 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8255 | if (n == 0) |
| 8256 | return p1->st_altscore - p2->st_altscore; |
| 8257 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8258 | } |
| 8259 | |
| 8260 | /* |
| 8261 | * Cleanup the suggestions: |
| 8262 | * - Sort on score. |
| 8263 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8264 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8265 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8266 | static int |
| 8267 | cleanup_suggestions(gap, maxscore, keep) |
| 8268 | garray_T *gap; |
| 8269 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8270 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8271 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8272 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8273 | int i; |
| 8274 | |
| 8275 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8276 | 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] | 8277 | |
| 8278 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8279 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8280 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8281 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8282 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8283 | gap->ga_len = keep; |
| 8284 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8285 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8286 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8287 | } |
| 8288 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8289 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 8290 | /* |
| 8291 | * Soundfold a string, for soundfold(). |
| 8292 | * Result is in allocated memory, NULL for an error. |
| 8293 | */ |
| 8294 | char_u * |
| 8295 | eval_soundfold(word) |
| 8296 | char_u *word; |
| 8297 | { |
| 8298 | langp_T *lp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8299 | char_u sound[MAXWLEN]; |
| 8300 | |
| 8301 | if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) |
| 8302 | /* Use the sound-folding of the first language that supports it. */ |
| 8303 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8304 | lp->lp_slang != NULL; ++lp) |
| 8305 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8306 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8307 | /* soundfold the word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8308 | spell_soundfold(lp->lp_slang, word, FALSE, sound); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8309 | return vim_strsave(sound); |
| 8310 | } |
| 8311 | |
| 8312 | /* No language with sound folding, return word as-is. */ |
| 8313 | return vim_strsave(word); |
| 8314 | } |
| 8315 | #endif |
| 8316 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8317 | /* |
| 8318 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 8319 | */ |
| 8320 | static void |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8321 | spell_soundfold(slang, inword, folded, res) |
| 8322 | slang_T *slang; |
| 8323 | char_u *inword; |
| 8324 | int folded; /* "inword" is already case-folded */ |
| 8325 | char_u *res; |
| 8326 | { |
| 8327 | char_u fword[MAXWLEN]; |
| 8328 | char_u *word; |
| 8329 | |
| 8330 | if (slang->sl_sofo) |
| 8331 | /* SOFOFROM and SOFOTO used */ |
| 8332 | spell_soundfold_sofo(slang, inword, res); |
| 8333 | else |
| 8334 | { |
| 8335 | /* SAL items used. Requires the word to be case-folded. */ |
| 8336 | if (folded) |
| 8337 | word = inword; |
| 8338 | else |
| 8339 | { |
| 8340 | (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN); |
| 8341 | word = fword; |
| 8342 | } |
| 8343 | |
| 8344 | #ifdef FEAT_MBYTE |
| 8345 | if (has_mbyte) |
| 8346 | spell_soundfold_wsal(slang, word, res); |
| 8347 | else |
| 8348 | #endif |
| 8349 | spell_soundfold_sal(slang, word, res); |
| 8350 | } |
| 8351 | } |
| 8352 | |
| 8353 | /* |
| 8354 | * Perform sound folding of "inword" into "res" according to SOFOFROM and |
| 8355 | * SOFOTO lines. |
| 8356 | */ |
| 8357 | static void |
| 8358 | spell_soundfold_sofo(slang, inword, res) |
| 8359 | slang_T *slang; |
| 8360 | char_u *inword; |
| 8361 | char_u *res; |
| 8362 | { |
| 8363 | char_u *s; |
| 8364 | int ri = 0; |
| 8365 | int c; |
| 8366 | |
| 8367 | #ifdef FEAT_MBYTE |
| 8368 | if (has_mbyte) |
| 8369 | { |
| 8370 | int prevc = 0; |
| 8371 | int *ip; |
| 8372 | |
| 8373 | /* The sl_sal_first[] table contains the translation for chars up to |
| 8374 | * 255, sl_sal the rest. */ |
| 8375 | for (s = inword; *s != NUL; ) |
| 8376 | { |
| 8377 | c = mb_ptr2char_adv(&s); |
| 8378 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 8379 | c = ' '; |
| 8380 | else if (c < 256) |
| 8381 | c = slang->sl_sal_first[c]; |
| 8382 | else |
| 8383 | { |
| 8384 | ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; |
| 8385 | if (ip == NULL) /* empty list, can't match */ |
| 8386 | c = NUL; |
| 8387 | else |
| 8388 | for (;;) /* find "c" in the list */ |
| 8389 | { |
| 8390 | if (*ip == 0) /* not found */ |
| 8391 | { |
| 8392 | c = NUL; |
| 8393 | break; |
| 8394 | } |
| 8395 | if (*ip == c) /* match! */ |
| 8396 | { |
| 8397 | c = ip[1]; |
| 8398 | break; |
| 8399 | } |
| 8400 | ip += 2; |
| 8401 | } |
| 8402 | } |
| 8403 | |
| 8404 | if (c != NUL && c != prevc) |
| 8405 | { |
| 8406 | ri += mb_char2bytes(c, res + ri); |
| 8407 | if (ri + MB_MAXBYTES > MAXWLEN) |
| 8408 | break; |
| 8409 | prevc = c; |
| 8410 | } |
| 8411 | } |
| 8412 | } |
| 8413 | else |
| 8414 | #endif |
| 8415 | { |
| 8416 | /* The sl_sal_first[] table contains the translation. */ |
| 8417 | for (s = inword; (c = *s) != NUL; ++s) |
| 8418 | { |
| 8419 | if (vim_iswhite(c)) |
| 8420 | c = ' '; |
| 8421 | else |
| 8422 | c = slang->sl_sal_first[c]; |
| 8423 | if (c != NUL && (ri == 0 || res[ri - 1] != c)) |
| 8424 | res[ri++] = c; |
| 8425 | } |
| 8426 | } |
| 8427 | |
| 8428 | res[ri] = NUL; |
| 8429 | } |
| 8430 | |
| 8431 | static void |
| 8432 | spell_soundfold_sal(slang, inword, res) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8433 | slang_T *slang; |
| 8434 | char_u *inword; |
| 8435 | char_u *res; |
| 8436 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8437 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8438 | char_u word[MAXWLEN]; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8439 | char_u *s = inword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8440 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8441 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8442 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8443 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8444 | int n, k = 0; |
| 8445 | int z0; |
| 8446 | int k0; |
| 8447 | int n0; |
| 8448 | int c; |
| 8449 | int pri; |
| 8450 | int p0 = -333; |
| 8451 | int c0; |
| 8452 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8453 | /* Remove accents, if wanted. We actually remove all non-word characters. |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8454 | * But keep white space. We need a copy, the word may be changed here. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8455 | if (slang->sl_rem_accents) |
| 8456 | { |
| 8457 | t = word; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8458 | while (*s != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8459 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8460 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8461 | { |
| 8462 | *t++ = ' '; |
| 8463 | s = skipwhite(s); |
| 8464 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8465 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8466 | { |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8467 | if (spell_iswordp_nmw(s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8468 | *t++ = *s; |
| 8469 | ++s; |
| 8470 | } |
| 8471 | } |
| 8472 | *t = NUL; |
| 8473 | } |
| 8474 | else |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8475 | STRCPY(word, s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8476 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8477 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8478 | |
| 8479 | /* |
| 8480 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8481 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8482 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8483 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8484 | while ((c = word[i]) != NUL) |
| 8485 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8486 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8487 | n = slang->sl_sal_first[c]; |
| 8488 | z0 = 0; |
| 8489 | |
| 8490 | if (n >= 0) |
| 8491 | { |
| 8492 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8493 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8494 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8495 | /* Quickly skip entries that don't match the word. Most |
| 8496 | * entries are less then three chars, optimize for that. */ |
| 8497 | k = smp[n].sm_leadlen; |
| 8498 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8499 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8500 | if (word[i + 1] != s[1]) |
| 8501 | continue; |
| 8502 | if (k > 2) |
| 8503 | { |
| 8504 | for (j = 2; j < k; ++j) |
| 8505 | if (word[i + j] != s[j]) |
| 8506 | break; |
| 8507 | if (j < k) |
| 8508 | continue; |
| 8509 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8510 | } |
| 8511 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8512 | if ((pf = smp[n].sm_oneof) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8513 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8514 | /* Check for match with one of the chars in "sm_oneof". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8515 | while (*pf != NUL && *pf != word[i + k]) |
| 8516 | ++pf; |
| 8517 | if (*pf == NUL) |
| 8518 | continue; |
| 8519 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8520 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8521 | s = smp[n].sm_rules; |
| 8522 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8523 | |
| 8524 | p0 = *s; |
| 8525 | k0 = k; |
| 8526 | while (*s == '-' && k > 1) |
| 8527 | { |
| 8528 | k--; |
| 8529 | s++; |
| 8530 | } |
| 8531 | if (*s == '<') |
| 8532 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8533 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8534 | { |
| 8535 | /* determine priority */ |
| 8536 | pri = *s - '0'; |
| 8537 | s++; |
| 8538 | } |
| 8539 | if (*s == '^' && *(s + 1) == '^') |
| 8540 | s++; |
| 8541 | |
| 8542 | if (*s == NUL |
| 8543 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8544 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8545 | || spell_iswordp(word + i - 1, curbuf))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8546 | && (*(s + 1) != '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8547 | || (!spell_iswordp(word + i + k0, curbuf)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8548 | || (*s == '$' && i > 0 |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8549 | && spell_iswordp(word + i - 1, curbuf) |
| 8550 | && (!spell_iswordp(word + i + k0, curbuf)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8551 | { |
| 8552 | /* search for followup rules, if: */ |
| 8553 | /* followup and k > 1 and NO '-' in searchstring */ |
| 8554 | c0 = word[i + k - 1]; |
| 8555 | n0 = slang->sl_sal_first[c0]; |
| 8556 | |
| 8557 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8558 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8559 | { |
| 8560 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8561 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8562 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8563 | /* Quickly skip entries that don't match the word. |
| 8564 | * */ |
| 8565 | k0 = smp[n0].sm_leadlen; |
| 8566 | if (k0 > 1) |
| 8567 | { |
| 8568 | if (word[i + k] != s[1]) |
| 8569 | continue; |
| 8570 | if (k0 > 2) |
| 8571 | { |
| 8572 | pf = word + i + k + 1; |
| 8573 | for (j = 2; j < k0; ++j) |
| 8574 | if (*pf++ != s[j]) |
| 8575 | break; |
| 8576 | if (j < k0) |
| 8577 | continue; |
| 8578 | } |
| 8579 | } |
| 8580 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8581 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8582 | if ((pf = smp[n0].sm_oneof) != NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8583 | { |
| 8584 | /* Check for match with one of the chars in |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8585 | * "sm_oneof". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8586 | while (*pf != NUL && *pf != word[i + k0]) |
| 8587 | ++pf; |
| 8588 | if (*pf == NUL) |
| 8589 | continue; |
| 8590 | ++k0; |
| 8591 | } |
| 8592 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8593 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8594 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8595 | while (*s == '-') |
| 8596 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8597 | /* "k0" gets NOT reduced because |
| 8598 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8599 | s++; |
| 8600 | } |
| 8601 | if (*s == '<') |
| 8602 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8603 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8604 | { |
| 8605 | p0 = *s - '0'; |
| 8606 | s++; |
| 8607 | } |
| 8608 | |
| 8609 | if (*s == NUL |
| 8610 | /* *s == '^' cuts */ |
| 8611 | || (*s == '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8612 | && !spell_iswordp(word + i + k0, |
| 8613 | curbuf))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8614 | { |
| 8615 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8616 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8617 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8618 | |
| 8619 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8620 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8621 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8622 | /* rule fits; stop search */ |
| 8623 | break; |
| 8624 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8625 | } |
| 8626 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8627 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8628 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8629 | } |
| 8630 | |
| 8631 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8632 | s = smp[n].sm_to; |
| 8633 | pf = smp[n].sm_rules; |
| 8634 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8635 | if (p0 == 1 && z == 0) |
| 8636 | { |
| 8637 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8638 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 8639 | || res[reslen - 1] == *s)) |
| 8640 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8641 | z0 = 1; |
| 8642 | z = 1; |
| 8643 | k0 = 0; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8644 | while (*s != NUL && word[i + k0] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8645 | { |
| 8646 | word[i + k0] = *s; |
| 8647 | k0++; |
| 8648 | s++; |
| 8649 | } |
| 8650 | if (k > k0) |
| 8651 | mch_memmove(word + i + k0, word + i + k, |
| 8652 | STRLEN(word + i + k) + 1); |
| 8653 | |
| 8654 | /* new "actual letter" */ |
| 8655 | c = word[i]; |
| 8656 | } |
| 8657 | else |
| 8658 | { |
| 8659 | /* no '<' rule used */ |
| 8660 | i += k - 1; |
| 8661 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8662 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8663 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8664 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8665 | res[reslen++] = *s; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8666 | s++; |
| 8667 | } |
| 8668 | /* new "actual letter" */ |
| 8669 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8670 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8671 | { |
| 8672 | if (c != NUL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8673 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8674 | mch_memmove(word, word + i + 1, |
| 8675 | STRLEN(word + i + 1) + 1); |
| 8676 | i = 0; |
| 8677 | z0 = 1; |
| 8678 | } |
| 8679 | } |
| 8680 | break; |
| 8681 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8682 | } |
| 8683 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8684 | else if (vim_iswhite(c)) |
| 8685 | { |
| 8686 | c = ' '; |
| 8687 | k = 1; |
| 8688 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8689 | |
| 8690 | if (z0 == 0) |
| 8691 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8692 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 8693 | && (!slang->sl_collapse || reslen == 0 |
| 8694 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8695 | /* condense only double letters */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8696 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8697 | |
| 8698 | i++; |
| 8699 | z = 0; |
| 8700 | k = 0; |
| 8701 | } |
| 8702 | } |
| 8703 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8704 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8705 | } |
| 8706 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8707 | #ifdef FEAT_MBYTE |
| 8708 | /* |
| 8709 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 8710 | * Multi-byte version of spell_soundfold(). |
| 8711 | */ |
| 8712 | static void |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8713 | spell_soundfold_wsal(slang, inword, res) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8714 | slang_T *slang; |
| 8715 | char_u *inword; |
| 8716 | char_u *res; |
| 8717 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8718 | salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8719 | int word[MAXWLEN]; |
| 8720 | int wres[MAXWLEN]; |
| 8721 | int l; |
| 8722 | char_u *s; |
| 8723 | int *ws; |
| 8724 | char_u *t; |
| 8725 | int *pf; |
| 8726 | int i, j, z; |
| 8727 | int reslen; |
| 8728 | int n, k = 0; |
| 8729 | int z0; |
| 8730 | int k0; |
| 8731 | int n0; |
| 8732 | int c; |
| 8733 | int pri; |
| 8734 | int p0 = -333; |
| 8735 | int c0; |
| 8736 | int did_white = FALSE; |
| 8737 | |
| 8738 | /* |
| 8739 | * Convert the multi-byte string to a wide-character string. |
| 8740 | * Remove accents, if wanted. We actually remove all non-word characters. |
| 8741 | * But keep white space. |
| 8742 | */ |
| 8743 | n = 0; |
| 8744 | for (s = inword; *s != NUL; ) |
| 8745 | { |
| 8746 | t = s; |
| 8747 | c = mb_ptr2char_adv(&s); |
| 8748 | if (slang->sl_rem_accents) |
| 8749 | { |
| 8750 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 8751 | { |
| 8752 | if (did_white) |
| 8753 | continue; |
| 8754 | c = ' '; |
| 8755 | did_white = TRUE; |
| 8756 | } |
| 8757 | else |
| 8758 | { |
| 8759 | did_white = FALSE; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8760 | if (!spell_iswordp_nmw(t)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8761 | continue; |
| 8762 | } |
| 8763 | } |
| 8764 | word[n++] = c; |
| 8765 | } |
| 8766 | word[n] = NUL; |
| 8767 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8768 | /* |
| 8769 | * This comes from Aspell phonet.cpp. |
| 8770 | * Converted from C++ to C. Added support for multi-byte chars. |
| 8771 | * Changed to keep spaces. |
| 8772 | */ |
| 8773 | i = reslen = z = 0; |
| 8774 | while ((c = word[i]) != NUL) |
| 8775 | { |
| 8776 | /* Start with the first rule that has the character in the word. */ |
| 8777 | n = slang->sl_sal_first[c & 0xff]; |
| 8778 | z0 = 0; |
| 8779 | |
| 8780 | if (n >= 0) |
| 8781 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8782 | /* check all rules for the same index byte */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8783 | for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n) |
| 8784 | { |
| 8785 | /* Quickly skip entries that don't match the word. Most |
| 8786 | * entries are less then three chars, optimize for that. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8787 | if (c != ws[0]) |
| 8788 | continue; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8789 | k = smp[n].sm_leadlen; |
| 8790 | if (k > 1) |
| 8791 | { |
| 8792 | if (word[i + 1] != ws[1]) |
| 8793 | continue; |
| 8794 | if (k > 2) |
| 8795 | { |
| 8796 | for (j = 2; j < k; ++j) |
| 8797 | if (word[i + j] != ws[j]) |
| 8798 | break; |
| 8799 | if (j < k) |
| 8800 | continue; |
| 8801 | } |
| 8802 | } |
| 8803 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8804 | if ((pf = smp[n].sm_oneof_w) != NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8805 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8806 | /* Check for match with one of the chars in "sm_oneof". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8807 | while (*pf != NUL && *pf != word[i + k]) |
| 8808 | ++pf; |
| 8809 | if (*pf == NUL) |
| 8810 | continue; |
| 8811 | ++k; |
| 8812 | } |
| 8813 | s = smp[n].sm_rules; |
| 8814 | pri = 5; /* default priority */ |
| 8815 | |
| 8816 | p0 = *s; |
| 8817 | k0 = k; |
| 8818 | while (*s == '-' && k > 1) |
| 8819 | { |
| 8820 | k--; |
| 8821 | s++; |
| 8822 | } |
| 8823 | if (*s == '<') |
| 8824 | s++; |
| 8825 | if (VIM_ISDIGIT(*s)) |
| 8826 | { |
| 8827 | /* determine priority */ |
| 8828 | pri = *s - '0'; |
| 8829 | s++; |
| 8830 | } |
| 8831 | if (*s == '^' && *(s + 1) == '^') |
| 8832 | s++; |
| 8833 | |
| 8834 | if (*s == NUL |
| 8835 | || (*s == '^' |
| 8836 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8837 | || spell_iswordp_w(word + i - 1, curbuf))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8838 | && (*(s + 1) != '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8839 | || (!spell_iswordp_w(word + i + k0, curbuf)))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8840 | || (*s == '$' && i > 0 |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8841 | && spell_iswordp_w(word + i - 1, curbuf) |
| 8842 | && (!spell_iswordp_w(word + i + k0, curbuf)))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8843 | { |
| 8844 | /* search for followup rules, if: */ |
| 8845 | /* followup and k > 1 and NO '-' in searchstring */ |
| 8846 | c0 = word[i + k - 1]; |
| 8847 | n0 = slang->sl_sal_first[c0 & 0xff]; |
| 8848 | |
| 8849 | if (slang->sl_followup && k > 1 && n0 >= 0 |
| 8850 | && p0 != '-' && word[i + k] != NUL) |
| 8851 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8852 | /* Test follow-up rule for "word[i + k]"; loop over |
| 8853 | * all entries with the same index byte. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8854 | for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
| 8855 | == (c0 & 0xff); ++n0) |
| 8856 | { |
| 8857 | /* Quickly skip entries that don't match the word. |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8858 | */ |
| 8859 | if (c0 != ws[0]) |
| 8860 | continue; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8861 | k0 = smp[n0].sm_leadlen; |
| 8862 | if (k0 > 1) |
| 8863 | { |
| 8864 | if (word[i + k] != ws[1]) |
| 8865 | continue; |
| 8866 | if (k0 > 2) |
| 8867 | { |
| 8868 | pf = word + i + k + 1; |
| 8869 | for (j = 2; j < k0; ++j) |
| 8870 | if (*pf++ != ws[j]) |
| 8871 | break; |
| 8872 | if (j < k0) |
| 8873 | continue; |
| 8874 | } |
| 8875 | } |
| 8876 | k0 += k - 1; |
| 8877 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8878 | if ((pf = smp[n0].sm_oneof_w) != NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8879 | { |
| 8880 | /* Check for match with one of the chars in |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8881 | * "sm_oneof". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8882 | while (*pf != NUL && *pf != word[i + k0]) |
| 8883 | ++pf; |
| 8884 | if (*pf == NUL) |
| 8885 | continue; |
| 8886 | ++k0; |
| 8887 | } |
| 8888 | |
| 8889 | p0 = 5; |
| 8890 | s = smp[n0].sm_rules; |
| 8891 | while (*s == '-') |
| 8892 | { |
| 8893 | /* "k0" gets NOT reduced because |
| 8894 | * "if (k0 == k)" */ |
| 8895 | s++; |
| 8896 | } |
| 8897 | if (*s == '<') |
| 8898 | s++; |
| 8899 | if (VIM_ISDIGIT(*s)) |
| 8900 | { |
| 8901 | p0 = *s - '0'; |
| 8902 | s++; |
| 8903 | } |
| 8904 | |
| 8905 | if (*s == NUL |
| 8906 | /* *s == '^' cuts */ |
| 8907 | || (*s == '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8908 | && !spell_iswordp_w(word + i + k0, |
| 8909 | curbuf))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8910 | { |
| 8911 | if (k0 == k) |
| 8912 | /* this is just a piece of the string */ |
| 8913 | continue; |
| 8914 | |
| 8915 | if (p0 < pri) |
| 8916 | /* priority too low */ |
| 8917 | continue; |
| 8918 | /* rule fits; stop search */ |
| 8919 | break; |
| 8920 | } |
| 8921 | } |
| 8922 | |
| 8923 | if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) |
| 8924 | == (c0 & 0xff)) |
| 8925 | continue; |
| 8926 | } |
| 8927 | |
| 8928 | /* replace string */ |
| 8929 | ws = smp[n].sm_to_w; |
| 8930 | s = smp[n].sm_rules; |
| 8931 | p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; |
| 8932 | if (p0 == 1 && z == 0) |
| 8933 | { |
| 8934 | /* rule with '<' is used */ |
| 8935 | if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c |
| 8936 | || wres[reslen - 1] == *ws)) |
| 8937 | reslen--; |
| 8938 | z0 = 1; |
| 8939 | z = 1; |
| 8940 | k0 = 0; |
| 8941 | while (*ws != NUL && word[i + k0] != NUL) |
| 8942 | { |
| 8943 | word[i + k0] = *ws; |
| 8944 | k0++; |
| 8945 | ws++; |
| 8946 | } |
| 8947 | if (k > k0) |
| 8948 | mch_memmove(word + i + k0, word + i + k, |
| 8949 | sizeof(int) * (STRLEN(word + i + k) + 1)); |
| 8950 | |
| 8951 | /* new "actual letter" */ |
| 8952 | c = word[i]; |
| 8953 | } |
| 8954 | else |
| 8955 | { |
| 8956 | /* no '<' rule used */ |
| 8957 | i += k - 1; |
| 8958 | z = 0; |
| 8959 | while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN) |
| 8960 | { |
| 8961 | if (reslen == 0 || wres[reslen - 1] != *ws) |
| 8962 | wres[reslen++] = *ws; |
| 8963 | ws++; |
| 8964 | } |
| 8965 | /* new "actual letter" */ |
| 8966 | c = *ws; |
| 8967 | if (strstr((char *)s, "^^") != NULL) |
| 8968 | { |
| 8969 | if (c != NUL) |
| 8970 | wres[reslen++] = c; |
| 8971 | mch_memmove(word, word + i + 1, |
| 8972 | sizeof(int) * (STRLEN(word + i + 1) + 1)); |
| 8973 | i = 0; |
| 8974 | z0 = 1; |
| 8975 | } |
| 8976 | } |
| 8977 | break; |
| 8978 | } |
| 8979 | } |
| 8980 | } |
| 8981 | else if (vim_iswhite(c)) |
| 8982 | { |
| 8983 | c = ' '; |
| 8984 | k = 1; |
| 8985 | } |
| 8986 | |
| 8987 | if (z0 == 0) |
| 8988 | { |
| 8989 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 8990 | && (!slang->sl_collapse || reslen == 0 |
| 8991 | || wres[reslen - 1] != c)) |
| 8992 | /* condense only double letters */ |
| 8993 | wres[reslen++] = c; |
| 8994 | |
| 8995 | i++; |
| 8996 | z = 0; |
| 8997 | k = 0; |
| 8998 | } |
| 8999 | } |
| 9000 | |
| 9001 | /* Convert wide characters in "wres" to a multi-byte string in "res". */ |
| 9002 | l = 0; |
| 9003 | for (n = 0; n < reslen; ++n) |
| 9004 | { |
| 9005 | l += mb_char2bytes(wres[n], res + l); |
| 9006 | if (l + MB_MAXBYTES > MAXWLEN) |
| 9007 | break; |
| 9008 | } |
| 9009 | res[l] = NUL; |
| 9010 | } |
| 9011 | #endif |
| 9012 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9013 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9014 | * Compute a score for two sound-a-like words. |
| 9015 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 9016 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 9017 | * avoiding checks that will not be possible. |
| 9018 | */ |
| 9019 | static int |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9020 | soundalike_score(goodstart, badstart) |
| 9021 | char_u *goodstart; /* sound-folded good word */ |
| 9022 | char_u *badstart; /* sound-folded bad word */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9023 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9024 | char_u *goodsound = goodstart; |
| 9025 | char_u *badsound = badstart; |
| 9026 | int goodlen; |
| 9027 | int badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9028 | int n; |
| 9029 | char_u *pl, *ps; |
| 9030 | char_u *pl2, *ps2; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9031 | int score = 0; |
| 9032 | |
| 9033 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 9034 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 9035 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 9036 | { |
| 9037 | score = SCORE_DEL / 2; |
| 9038 | if (*badsound == '*') |
| 9039 | ++badsound; |
| 9040 | else |
| 9041 | ++goodsound; |
| 9042 | } |
| 9043 | |
| 9044 | goodlen = STRLEN(goodsound); |
| 9045 | badlen = STRLEN(badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9046 | |
| 9047 | /* Return quickly if the lenghts are too different to be fixed by two |
| 9048 | * changes. */ |
| 9049 | n = goodlen - badlen; |
| 9050 | if (n < -2 || n > 2) |
| 9051 | return SCORE_MAXMAX; |
| 9052 | |
| 9053 | if (n > 0) |
| 9054 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9055 | pl = goodsound; /* goodsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9056 | ps = badsound; |
| 9057 | } |
| 9058 | else |
| 9059 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9060 | pl = badsound; /* badsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9061 | ps = goodsound; |
| 9062 | } |
| 9063 | |
| 9064 | /* Skip over the identical part. */ |
| 9065 | while (*pl == *ps && *pl != NUL) |
| 9066 | { |
| 9067 | ++pl; |
| 9068 | ++ps; |
| 9069 | } |
| 9070 | |
| 9071 | switch (n) |
| 9072 | { |
| 9073 | case -2: |
| 9074 | case 2: |
| 9075 | /* |
| 9076 | * Must delete two characters from "pl". |
| 9077 | */ |
| 9078 | ++pl; /* first delete */ |
| 9079 | while (*pl == *ps) |
| 9080 | { |
| 9081 | ++pl; |
| 9082 | ++ps; |
| 9083 | } |
| 9084 | /* strings must be equal after second delete */ |
| 9085 | if (STRCMP(pl + 1, ps) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9086 | return score + SCORE_DEL * 2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9087 | |
| 9088 | /* Failed to compare. */ |
| 9089 | break; |
| 9090 | |
| 9091 | case -1: |
| 9092 | case 1: |
| 9093 | /* |
| 9094 | * Minimal one delete from "pl" required. |
| 9095 | */ |
| 9096 | |
| 9097 | /* 1: delete */ |
| 9098 | pl2 = pl + 1; |
| 9099 | ps2 = ps; |
| 9100 | while (*pl2 == *ps2) |
| 9101 | { |
| 9102 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9103 | return score + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9104 | ++pl2; |
| 9105 | ++ps2; |
| 9106 | } |
| 9107 | |
| 9108 | /* 2: delete then swap, then rest must be equal */ |
| 9109 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9110 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9111 | return score + SCORE_DEL + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9112 | |
| 9113 | /* 3: delete then substitute, then the rest must be equal */ |
| 9114 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9115 | return score + SCORE_DEL + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9116 | |
| 9117 | /* 4: first swap then delete */ |
| 9118 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 9119 | { |
| 9120 | pl2 = pl + 2; /* swap, skip two chars */ |
| 9121 | ps2 = ps + 2; |
| 9122 | while (*pl2 == *ps2) |
| 9123 | { |
| 9124 | ++pl2; |
| 9125 | ++ps2; |
| 9126 | } |
| 9127 | /* delete a char and then strings must be equal */ |
| 9128 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9129 | return score + SCORE_SWAP + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9130 | } |
| 9131 | |
| 9132 | /* 5: first substitute then delete */ |
| 9133 | pl2 = pl + 1; /* substitute, skip one char */ |
| 9134 | ps2 = ps + 1; |
| 9135 | while (*pl2 == *ps2) |
| 9136 | { |
| 9137 | ++pl2; |
| 9138 | ++ps2; |
| 9139 | } |
| 9140 | /* delete a char and then strings must be equal */ |
| 9141 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9142 | return score + SCORE_SUBST + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9143 | |
| 9144 | /* Failed to compare. */ |
| 9145 | break; |
| 9146 | |
| 9147 | case 0: |
| 9148 | /* |
| 9149 | * Lenghts are equal, thus changes must result in same length: An |
| 9150 | * insert is only possible in combination with a delete. |
| 9151 | * 1: check if for identical strings |
| 9152 | */ |
| 9153 | if (*pl == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9154 | return score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9155 | |
| 9156 | /* 2: swap */ |
| 9157 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 9158 | { |
| 9159 | pl2 = pl + 2; /* swap, skip two chars */ |
| 9160 | ps2 = ps + 2; |
| 9161 | while (*pl2 == *ps2) |
| 9162 | { |
| 9163 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9164 | return score + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9165 | ++pl2; |
| 9166 | ++ps2; |
| 9167 | } |
| 9168 | /* 3: swap and swap again */ |
| 9169 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9170 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9171 | return score + SCORE_SWAP + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9172 | |
| 9173 | /* 4: swap and substitute */ |
| 9174 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9175 | return score + SCORE_SWAP + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9176 | } |
| 9177 | |
| 9178 | /* 5: substitute */ |
| 9179 | pl2 = pl + 1; |
| 9180 | ps2 = ps + 1; |
| 9181 | while (*pl2 == *ps2) |
| 9182 | { |
| 9183 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9184 | return score + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9185 | ++pl2; |
| 9186 | ++ps2; |
| 9187 | } |
| 9188 | |
| 9189 | /* 6: substitute and swap */ |
| 9190 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9191 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9192 | return score + SCORE_SUBST + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9193 | |
| 9194 | /* 7: substitute and substitute */ |
| 9195 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9196 | return score + SCORE_SUBST + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9197 | |
| 9198 | /* 8: insert then delete */ |
| 9199 | pl2 = pl; |
| 9200 | ps2 = ps + 1; |
| 9201 | while (*pl2 == *ps2) |
| 9202 | { |
| 9203 | ++pl2; |
| 9204 | ++ps2; |
| 9205 | } |
| 9206 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9207 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9208 | |
| 9209 | /* 9: delete then insert */ |
| 9210 | pl2 = pl + 1; |
| 9211 | ps2 = ps; |
| 9212 | while (*pl2 == *ps2) |
| 9213 | { |
| 9214 | ++pl2; |
| 9215 | ++ps2; |
| 9216 | } |
| 9217 | if (STRCMP(pl2, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9218 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9219 | |
| 9220 | /* Failed to compare. */ |
| 9221 | break; |
| 9222 | } |
| 9223 | |
| 9224 | return SCORE_MAXMAX; |
| 9225 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9226 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9227 | /* |
| 9228 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9229 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9230 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9231 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9232 | * It has been converted from C++ to C and modified to support multi-byte |
| 9233 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9234 | */ |
| 9235 | static int |
| 9236 | spell_edit_score(badword, goodword) |
| 9237 | char_u *badword; |
| 9238 | char_u *goodword; |
| 9239 | { |
| 9240 | int *cnt; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9241 | int badlen, goodlen; /* lenghts including NUL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9242 | int j, i; |
| 9243 | int t; |
| 9244 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9245 | int pbc, pgc; |
| 9246 | #ifdef FEAT_MBYTE |
| 9247 | char_u *p; |
| 9248 | int wbadword[MAXWLEN]; |
| 9249 | int wgoodword[MAXWLEN]; |
| 9250 | |
| 9251 | if (has_mbyte) |
| 9252 | { |
| 9253 | /* Get the characters from the multi-byte strings and put them in an |
| 9254 | * int array for easy access. */ |
| 9255 | for (p = badword, badlen = 0; *p != NUL; ) |
| 9256 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
| 9257 | ++badlen; |
| 9258 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 9259 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
| 9260 | ++goodlen; |
| 9261 | } |
| 9262 | else |
| 9263 | #endif |
| 9264 | { |
| 9265 | badlen = STRLEN(badword) + 1; |
| 9266 | goodlen = STRLEN(goodword) + 1; |
| 9267 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9268 | |
| 9269 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 9270 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9271 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 9272 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9273 | if (cnt == NULL) |
| 9274 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9275 | |
| 9276 | CNT(0, 0) = 0; |
| 9277 | for (j = 1; j <= goodlen; ++j) |
| 9278 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 9279 | |
| 9280 | for (i = 1; i <= badlen; ++i) |
| 9281 | { |
| 9282 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 9283 | for (j = 1; j <= goodlen; ++j) |
| 9284 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9285 | #ifdef FEAT_MBYTE |
| 9286 | if (has_mbyte) |
| 9287 | { |
| 9288 | bc = wbadword[i - 1]; |
| 9289 | gc = wgoodword[j - 1]; |
| 9290 | } |
| 9291 | else |
| 9292 | #endif |
| 9293 | { |
| 9294 | bc = badword[i - 1]; |
| 9295 | gc = goodword[j - 1]; |
| 9296 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9297 | if (bc == gc) |
| 9298 | CNT(i, j) = CNT(i - 1, j - 1); |
| 9299 | else |
| 9300 | { |
| 9301 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9302 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9303 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 9304 | else |
| 9305 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 9306 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9307 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9308 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9309 | #ifdef FEAT_MBYTE |
| 9310 | if (has_mbyte) |
| 9311 | { |
| 9312 | pbc = wbadword[i - 2]; |
| 9313 | pgc = wgoodword[j - 2]; |
| 9314 | } |
| 9315 | else |
| 9316 | #endif |
| 9317 | { |
| 9318 | pbc = badword[i - 2]; |
| 9319 | pgc = goodword[j - 2]; |
| 9320 | } |
| 9321 | if (bc == pgc && pbc == gc) |
| 9322 | { |
| 9323 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 9324 | if (t < CNT(i, j)) |
| 9325 | CNT(i, j) = t; |
| 9326 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9327 | } |
| 9328 | t = SCORE_DEL + CNT(i - 1, j); |
| 9329 | if (t < CNT(i, j)) |
| 9330 | CNT(i, j) = t; |
| 9331 | t = SCORE_INS + CNT(i, j - 1); |
| 9332 | if (t < CNT(i, j)) |
| 9333 | CNT(i, j) = t; |
| 9334 | } |
| 9335 | } |
| 9336 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9337 | |
| 9338 | i = CNT(badlen - 1, goodlen - 1); |
| 9339 | vim_free(cnt); |
| 9340 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9341 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9342 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9343 | /* |
| 9344 | * ":spelldump" |
| 9345 | */ |
| 9346 | /*ARGSUSED*/ |
| 9347 | void |
| 9348 | ex_spelldump(eap) |
| 9349 | exarg_T *eap; |
| 9350 | { |
| 9351 | buf_T *buf = curbuf; |
| 9352 | langp_T *lp; |
| 9353 | slang_T *slang; |
| 9354 | idx_T arridx[MAXWLEN]; |
| 9355 | int curi[MAXWLEN]; |
| 9356 | char_u word[MAXWLEN]; |
| 9357 | int c; |
| 9358 | char_u *byts; |
| 9359 | idx_T *idxs; |
| 9360 | linenr_T lnum = 0; |
| 9361 | int round; |
| 9362 | int depth; |
| 9363 | int n; |
| 9364 | int flags; |
| 9365 | |
| 9366 | if (no_spell_checking()) |
| 9367 | return; |
| 9368 | |
| 9369 | /* Create a new empty buffer by splitting the window. */ |
| 9370 | do_cmdline_cmd((char_u *)"new"); |
| 9371 | if (!bufempty() || !buf_valid(buf)) |
| 9372 | return; |
| 9373 | |
| 9374 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 9375 | { |
| 9376 | slang = lp->lp_slang; |
| 9377 | |
| 9378 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 9379 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 9380 | |
| 9381 | /* round 1: case-folded tree |
| 9382 | * round 2: keep-case tree */ |
| 9383 | for (round = 1; round <= 2; ++round) |
| 9384 | { |
| 9385 | if (round == 1) |
| 9386 | { |
| 9387 | byts = slang->sl_fbyts; |
| 9388 | idxs = slang->sl_fidxs; |
| 9389 | } |
| 9390 | else |
| 9391 | { |
| 9392 | byts = slang->sl_kbyts; |
| 9393 | idxs = slang->sl_kidxs; |
| 9394 | } |
| 9395 | if (byts == NULL) |
| 9396 | continue; /* array is empty */ |
| 9397 | |
| 9398 | depth = 0; |
| 9399 | arridx[0] = 0; |
| 9400 | curi[0] = 1; |
| 9401 | while (depth >= 0 && !got_int) |
| 9402 | { |
| 9403 | if (curi[depth] > byts[arridx[depth]]) |
| 9404 | { |
| 9405 | /* Done all bytes at this node, go up one level. */ |
| 9406 | --depth; |
| 9407 | line_breakcheck(); |
| 9408 | } |
| 9409 | else |
| 9410 | { |
| 9411 | /* Do one more byte at this node. */ |
| 9412 | n = arridx[depth] + curi[depth]; |
| 9413 | ++curi[depth]; |
| 9414 | c = byts[n]; |
| 9415 | if (c == 0) |
| 9416 | { |
| 9417 | /* End of word, deal with the word. |
| 9418 | * Don't use keep-case words in the fold-case tree, |
| 9419 | * they will appear in the keep-case tree. |
| 9420 | * Only use the word when the region matches. */ |
| 9421 | flags = (int)idxs[n]; |
| 9422 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
| 9423 | && ((flags & WF_REGION) == 0 |
| 9424 | || (((unsigned)flags >> 8) |
| 9425 | & lp->lp_region) != 0)) |
| 9426 | { |
| 9427 | word[depth] = NUL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9428 | |
| 9429 | /* Dump the basic word if there is no prefix or |
| 9430 | * when it's the first one. */ |
| 9431 | c = (unsigned)flags >> 16; |
| 9432 | if (c == 0 || curi[depth] == 2) |
| 9433 | dump_word(word, round, flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9434 | |
| 9435 | /* Apply the prefix, if there is one. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9436 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9437 | lnum = apply_prefixes(slang, word, round, |
| 9438 | flags, lnum); |
| 9439 | } |
| 9440 | } |
| 9441 | else |
| 9442 | { |
| 9443 | /* Normal char, go one level deeper. */ |
| 9444 | word[depth++] = c; |
| 9445 | arridx[depth] = idxs[n]; |
| 9446 | curi[depth] = 1; |
| 9447 | } |
| 9448 | } |
| 9449 | } |
| 9450 | } |
| 9451 | } |
| 9452 | |
| 9453 | /* Delete the empty line that we started with. */ |
| 9454 | if (curbuf->b_ml.ml_line_count > 1) |
| 9455 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 9456 | |
| 9457 | redraw_later(NOT_VALID); |
| 9458 | } |
| 9459 | |
| 9460 | /* |
| 9461 | * Dump one word: apply case modifications and append a line to the buffer. |
| 9462 | */ |
| 9463 | static void |
| 9464 | dump_word(word, round, flags, lnum) |
| 9465 | char_u *word; |
| 9466 | int round; |
| 9467 | int flags; |
| 9468 | linenr_T lnum; |
| 9469 | { |
| 9470 | int keepcap = FALSE; |
| 9471 | char_u *p; |
| 9472 | char_u cword[MAXWLEN]; |
| 9473 | char_u badword[MAXWLEN + 3]; |
| 9474 | |
| 9475 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
| 9476 | { |
| 9477 | /* Need to fix case according to "flags". */ |
| 9478 | make_case_word(word, cword, flags); |
| 9479 | p = cword; |
| 9480 | } |
| 9481 | else |
| 9482 | { |
| 9483 | p = word; |
| 9484 | if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0) |
| 9485 | keepcap = TRUE; |
| 9486 | } |
| 9487 | |
| 9488 | /* Bad word is preceded by "/!" and some other |
| 9489 | * flags. */ |
| 9490 | if ((flags & (WF_BANNED | WF_RARE)) || keepcap) |
| 9491 | { |
| 9492 | STRCPY(badword, "/"); |
| 9493 | if (keepcap) |
| 9494 | STRCAT(badword, "="); |
| 9495 | if (flags & WF_BANNED) |
| 9496 | STRCAT(badword, "!"); |
| 9497 | else if (flags & WF_RARE) |
| 9498 | STRCAT(badword, "?"); |
| 9499 | STRCAT(badword, p); |
| 9500 | p = badword; |
| 9501 | } |
| 9502 | |
| 9503 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 9504 | } |
| 9505 | |
| 9506 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9507 | * For ":spelldump": Find matching prefixes for "word". Prepend each to |
| 9508 | * "word" and append a line to the buffer. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9509 | * Return the updated line number. |
| 9510 | */ |
| 9511 | static linenr_T |
| 9512 | apply_prefixes(slang, word, round, flags, startlnum) |
| 9513 | slang_T *slang; |
| 9514 | char_u *word; /* case-folded word */ |
| 9515 | int round; |
| 9516 | int flags; /* flags with prefix ID */ |
| 9517 | linenr_T startlnum; |
| 9518 | { |
| 9519 | idx_T arridx[MAXWLEN]; |
| 9520 | int curi[MAXWLEN]; |
| 9521 | char_u prefix[MAXWLEN]; |
| 9522 | int c; |
| 9523 | char_u *byts; |
| 9524 | idx_T *idxs; |
| 9525 | linenr_T lnum = startlnum; |
| 9526 | int depth; |
| 9527 | int n; |
| 9528 | int len; |
| 9529 | int prefid = (unsigned)flags >> 16; |
| 9530 | int i; |
| 9531 | |
| 9532 | byts = slang->sl_pbyts; |
| 9533 | idxs = slang->sl_pidxs; |
| 9534 | if (byts != NULL) /* array not is empty */ |
| 9535 | { |
| 9536 | /* |
| 9537 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
| 9538 | * When at the end of a prefix check that it supports "prefid". |
| 9539 | */ |
| 9540 | depth = 0; |
| 9541 | arridx[0] = 0; |
| 9542 | curi[0] = 1; |
| 9543 | while (depth >= 0 && !got_int) |
| 9544 | { |
| 9545 | len = arridx[depth]; |
| 9546 | if (curi[depth] > byts[len]) |
| 9547 | { |
| 9548 | /* Done all bytes at this node, go up one level. */ |
| 9549 | --depth; |
| 9550 | line_breakcheck(); |
| 9551 | } |
| 9552 | else |
| 9553 | { |
| 9554 | /* Do one more byte at this node. */ |
| 9555 | n = len + curi[depth]; |
| 9556 | ++curi[depth]; |
| 9557 | c = byts[n]; |
| 9558 | if (c == 0) |
| 9559 | { |
| 9560 | /* End of prefix, find out how many IDs there are. */ |
| 9561 | for (i = 1; i < len; ++i) |
| 9562 | if (byts[n + i] != 0) |
| 9563 | break; |
| 9564 | curi[depth] += i - 1; |
| 9565 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9566 | i = valid_word_prefix(i, n, prefid, word, slang); |
| 9567 | if (i != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9568 | { |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9569 | vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9570 | dump_word(prefix, round, |
| 9571 | (i & WF_RAREPFX) ? (flags | WF_RARE) |
| 9572 | : flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9573 | } |
| 9574 | } |
| 9575 | else |
| 9576 | { |
| 9577 | /* Normal char, go one level deeper. */ |
| 9578 | prefix[depth++] = c; |
| 9579 | arridx[depth] = idxs[n]; |
| 9580 | curi[depth] = 1; |
| 9581 | } |
| 9582 | } |
| 9583 | } |
| 9584 | } |
| 9585 | |
| 9586 | return lnum; |
| 9587 | } |
| 9588 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9589 | #endif /* FEAT_SYN_HL */ |