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 | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 92 | * <fileID> 10 bytes "VIMspell09" |
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> |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 174 | * | <flags> [<flags2>] [<region>] [<prefixID>] |
| 175 | * | [<pflags>] <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. |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 182 | * BY_FLAGS: End of word, <flags> follow. |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 183 | * For PREFIXTREE <pflags>, <prefixID> |
| 184 | * and <prefcondnr> follow. |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 185 | * BY_FLAGS2: End of word, <flags> and <flags2> |
| 186 | * follow. Not used in PREFIXTREE. |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 187 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 188 | * and <xbyte> follow. |
| 189 | * |
| 190 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 191 | * |
| 192 | * <xbyte> 1 byte byte value of the sibling. |
| 193 | * |
| 194 | * <flags> 1 byte bitmask of: |
| 195 | * WF_ALLCAP word must have only capitals |
| 196 | * WF_ONECAP first char of word must be capital |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 197 | * WF_KEEPCAP keep-case word |
| 198 | * WF_FIXCAP keep-case word, all caps not allowed |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 199 | * WF_RARE rare word |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 200 | * WF_BANNED bad word |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 201 | * WF_REGION <region> follows |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 202 | * WF_PFX <prefixID> follows |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 203 | * |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 204 | * <flags2> 1 byte Only used when there are postponed prefixes. |
| 205 | * Bitmask of: |
| 206 | * WF_HAS_AFF >> 8 word includes affix |
| 207 | * |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 208 | * <pflags> 1 byte bitmask of: |
| 209 | * WFP_RARE rare prefix |
| 210 | * WFP_NC non-combining prefix |
| 211 | * WFP_UP letter after prefix made upper case |
| 212 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 213 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 214 | * omitted it's valid in all regions. |
| 215 | * Lowest bit is for region 1. |
| 216 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 217 | * <prefixID> 1 byte ID of prefix that can be used with this word. For |
| 218 | * PREFIXTREE used for the required prefix ID. |
| 219 | * |
| 220 | * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list |
| 221 | * from HEADER. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 222 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 223 | * All text characters are in 'encoding', but stored as single bytes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 224 | */ |
| 225 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 226 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 227 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 228 | #endif |
| 229 | |
| 230 | #include "vim.h" |
| 231 | |
| 232 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 233 | |
| 234 | #ifdef HAVE_FCNTL_H |
| 235 | # include <fcntl.h> |
| 236 | #endif |
| 237 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 238 | #define MAXWLEN 250 /* Assume max. word len is this many bytes. |
| 239 | Some places assume a word length fits in a |
| 240 | byte, thus it can't be above 255. */ |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 241 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 242 | /* Type used for indexes in the word tree need to be at least 3 bytes. If int |
| 243 | * is 8 bytes we could use something smaller, but what? */ |
| 244 | #if SIZEOF_INT > 2 |
| 245 | typedef int idx_T; |
| 246 | #else |
| 247 | typedef long idx_T; |
| 248 | #endif |
| 249 | |
| 250 | /* Flags used for a word. Only the lowest byte can be used, the region byte |
| 251 | * comes above it. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 252 | #define WF_REGION 0x01 /* region byte follows */ |
| 253 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 254 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 255 | #define WF_RARE 0x08 /* rare word */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 256 | #define WF_BANNED 0x10 /* bad word */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 257 | #define WF_PFX 0x20 /* prefix ID follows */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 258 | #define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 259 | #define WF_KEEPCAP 0x80 /* keep-case word */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 260 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 261 | /* for <flags2>, shifted up one byte to be used in wn_flags */ |
| 262 | #define WF_HAS_AFF 0x0100 /* word includes affix */ |
| 263 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 264 | #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 265 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 266 | /* flags for <pflags> */ |
| 267 | #define WFP_RARE 0x01 /* rare prefix */ |
| 268 | #define WFP_NC 0x02 /* prefix is not combining */ |
| 269 | #define WFP_UP 0x04 /* to-upper prefix */ |
| 270 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 271 | /* flags for postponed prefixes. Must be above prefixID (one byte) |
| 272 | * and prefcondnr (two bytes). */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 273 | #define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare |
| 274 | * postponed prefix */ |
| 275 | #define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining |
| 276 | * postponed prefix */ |
| 277 | #define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper |
| 278 | * postponed prefix */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 279 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 280 | /* Special byte values for <byte>. Some are only used in the tree for |
| 281 | * postponed prefixes, some only in the other trees. This is a bit messy... */ |
| 282 | #define BY_NOFLAGS 0 /* end of word without flags or region; for |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 283 | * postponed prefix: no <pflags> */ |
| 284 | #define BY_INDEX 1 /* child is shared, index follows */ |
| 285 | #define BY_FLAGS 2 /* end of word, <flags> byte follows; for |
| 286 | * postponed prefix: <pflags> follows */ |
| 287 | #define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes |
| 288 | * follow; never used in prefix tree */ |
| 289 | #define BY_SPECIAL BY_FLAGS2 /* highest special byte value */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 290 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 291 | /* 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] | 292 | * and si_sal. Not for sl_sal! |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 293 | * One replacement: from "ft_from" to "ft_to". */ |
| 294 | typedef struct fromto_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 295 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 296 | char_u *ft_from; |
| 297 | char_u *ft_to; |
| 298 | } fromto_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 299 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 300 | /* Info from "SAL" entries in ".aff" file used in sl_sal. |
| 301 | * The info is split for quick processing by spell_soundfold(). |
| 302 | * Note that "sm_oneof" and "sm_rules" point into sm_lead. */ |
| 303 | typedef struct salitem_S |
| 304 | { |
| 305 | char_u *sm_lead; /* leading letters */ |
| 306 | int sm_leadlen; /* length of "sm_lead" */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 307 | char_u *sm_oneof; /* letters from () or NULL */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 308 | char_u *sm_rules; /* rules like ^, $, priority */ |
| 309 | char_u *sm_to; /* replacement. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 310 | #ifdef FEAT_MBYTE |
| 311 | int *sm_lead_w; /* wide character copy of "sm_lead" */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 312 | int *sm_oneof_w; /* wide character copy of "sm_oneof" */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 313 | int *sm_to_w; /* wide character copy of "sm_to" */ |
| 314 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 315 | } salitem_T; |
| 316 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 317 | #ifdef FEAT_MBYTE |
| 318 | typedef int salfirst_T; |
| 319 | #else |
| 320 | typedef short salfirst_T; |
| 321 | #endif |
| 322 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 323 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 324 | * Structure used to store words and other info for one language, loaded from |
| 325 | * a .spl file. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 326 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 327 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 328 | * |
| 329 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 330 | * the number of possible bytes, sorted on byte value: |
| 331 | * <len> <byte1> <byte2> ... |
| 332 | * The "idxs" array stores the index of the child node corresponding to the |
| 333 | * byte in "byts". |
| 334 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 335 | * the flags, region mask and prefixID for the word. There may be several |
| 336 | * zeros in sequence for alternative flag/region combinations. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 337 | */ |
| 338 | typedef struct slang_S slang_T; |
| 339 | struct slang_S |
| 340 | { |
| 341 | slang_T *sl_next; /* next language */ |
| 342 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 343 | char_u *sl_fname; /* name of .spl file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 344 | int sl_add; /* TRUE if it's a .add file. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 345 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 346 | char_u *sl_fbyts; /* case-folded word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 347 | idx_T *sl_fidxs; /* case-folded word indexes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 348 | char_u *sl_kbyts; /* keep-case word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 349 | idx_T *sl_kidxs; /* keep-case word indexes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 350 | char_u *sl_pbyts; /* prefix tree word bytes */ |
| 351 | idx_T *sl_pidxs; /* prefix tree word indexes */ |
| 352 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 353 | 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] | 354 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 355 | char_u *sl_midword; /* MIDWORD string or NULL */ |
| 356 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 357 | int sl_prefixcnt; /* number of items in "sl_prefprog" */ |
| 358 | regprog_T **sl_prefprog; /* table with regprogs for prefixes */ |
| 359 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 360 | garray_T sl_rep; /* list of fromto_T entries from REP lines */ |
| 361 | short sl_rep_first[256]; /* indexes where byte first appears, -1 if |
| 362 | there is none */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 363 | garray_T sl_sal; /* list of salitem_T entries from SAL lines */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 364 | 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] | 365 | there is none */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 366 | int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items: |
| 367 | * "sl_sal_first" maps chars, when has_mbyte |
| 368 | * "sl_sal" is a list of wide char lists. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 369 | int sl_followup; /* SAL followup */ |
| 370 | int sl_collapse; /* SAL collapse_result */ |
| 371 | int sl_rem_accents; /* SAL remove_accents */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 372 | int sl_has_map; /* TRUE if there is a MAP line */ |
| 373 | #ifdef FEAT_MBYTE |
| 374 | hashtab_T sl_map_hash; /* MAP for multi-byte chars */ |
| 375 | int sl_map_array[256]; /* MAP for first 256 chars */ |
| 376 | #else |
| 377 | char_u sl_map_array[256]; /* MAP for first 256 chars */ |
| 378 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 381 | /* First language that is loaded, start of the linked list of loaded |
| 382 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 383 | static slang_T *first_lang = NULL; |
| 384 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 385 | /* Flags used in .spl file for soundsalike flags. */ |
| 386 | #define SAL_F0LLOWUP 1 |
| 387 | #define SAL_COLLAPSE 2 |
| 388 | #define SAL_REM_ACCENTS 4 |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 389 | #define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 390 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 391 | /* |
| 392 | * Structure used in "b_langp", filled from 'spelllang'. |
| 393 | */ |
| 394 | typedef struct langp_S |
| 395 | { |
| 396 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 397 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 398 | } langp_T; |
| 399 | |
| 400 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 401 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 402 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 403 | |
| 404 | /* Result values. Lower number is accepted over higher one. */ |
| 405 | #define SP_BANNED -1 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 406 | #define SP_OK 0 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 407 | #define SP_RARE 1 |
| 408 | #define SP_LOCAL 2 |
| 409 | #define SP_BAD 3 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 410 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 411 | #define VIMSPELLMAGIC "VIMspell09" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 412 | #define VIMSPELLMAGICL 10 |
| 413 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 414 | /* file used for "zG" and "zW" */ |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 415 | static char_u *int_wordlist = NULL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 416 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 417 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 418 | * Information used when looking for suggestions. |
| 419 | */ |
| 420 | typedef struct suginfo_S |
| 421 | { |
| 422 | garray_T su_ga; /* suggestions, contains "suggest_T" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 423 | int su_maxcount; /* max. number of suggestions displayed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 424 | int su_maxscore; /* maximum score for adding to su_ga */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 425 | garray_T su_sga; /* like su_ga, sound-folded scoring */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 426 | char_u *su_badptr; /* start of bad word in line */ |
| 427 | int su_badlen; /* length of detected bad word in line */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 428 | int su_badflags; /* caps flags for bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 429 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 430 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| 431 | hashtab_T su_banned; /* table with banned words */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 432 | } suginfo_T; |
| 433 | |
| 434 | /* One word suggestion. Used in "si_ga". */ |
| 435 | typedef struct suggest_S |
| 436 | { |
| 437 | char_u *st_word; /* suggested word, allocated string */ |
| 438 | int st_orglen; /* length of replaced text */ |
| 439 | int st_score; /* lower is better */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 440 | int st_altscore; /* used when st_score compares equal */ |
| 441 | int st_salscore; /* st_score is for soundalike */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 442 | int st_had_bonus; /* bonus already included in score */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 443 | } suggest_T; |
| 444 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 445 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 446 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 447 | /* Number of suggestions kept when cleaning up. When rescore_suggestions() is |
| 448 | * called the score may change, thus we need to keep more than what is |
| 449 | * displayed. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 450 | #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] | 451 | |
| 452 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 453 | * of suggestions that are not going to be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 454 | #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 455 | |
| 456 | /* score for various changes */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 457 | #define SCORE_SPLIT 149 /* split bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 458 | #define SCORE_ICASE 52 /* slightly different case */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 459 | #define SCORE_REGION 70 /* word is for different region */ |
| 460 | #define SCORE_RARE 180 /* rare word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 461 | #define SCORE_SWAP 90 /* swap two characters */ |
| 462 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| 463 | #define SCORE_REP 87 /* REP replacement */ |
| 464 | #define SCORE_SUBST 93 /* substitute a character */ |
| 465 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 466 | #define SCORE_DEL 94 /* delete a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 467 | #define SCORE_DELDUP 64 /* delete a duplicated character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 468 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 469 | #define SCORE_INSDUP 66 /* insert a duplicate character */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 470 | #define SCORE_NONWORD 103 /* change non-word to word char */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 471 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 472 | #define SCORE_FILE 30 /* suggestion from a file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 473 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 474 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 475 | |
| 476 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 477 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 478 | |
| 479 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 480 | * Structure to store info for word matching. |
| 481 | */ |
| 482 | typedef struct matchinf_S |
| 483 | { |
| 484 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 485 | |
| 486 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 487 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 488 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 489 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 490 | char_u *mi_cend; /* char after what was used for |
| 491 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 492 | |
| 493 | /* case-folded text */ |
| 494 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 495 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 496 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 497 | /* for when checking word after a prefix */ |
| 498 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 499 | prefixID/condition */ |
| 500 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 501 | int mi_prefixlen; /* byte length of prefix */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 502 | #ifdef FEAT_MBYTE |
| 503 | int mi_cprefixlen; /* byte length of prefix in original |
| 504 | case */ |
| 505 | #else |
| 506 | # define mi_cprefixlen mi_prefixlen /* it's the same value */ |
| 507 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 508 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 509 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 510 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 511 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 512 | buf_T *mi_buf; /* buffer being checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 513 | } matchinf_T; |
| 514 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 515 | /* |
| 516 | * The tables used for recognizing word characters according to spelling. |
| 517 | * These are only used for the first 256 characters of 'encoding'. |
| 518 | */ |
| 519 | typedef struct spelltab_S |
| 520 | { |
| 521 | char_u st_isw[256]; /* flags: is word char */ |
| 522 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 523 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 524 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 525 | } spelltab_T; |
| 526 | |
| 527 | static spelltab_T spelltab; |
| 528 | static int did_set_spelltab; |
| 529 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 530 | #define CF_WORD 0x01 |
| 531 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 532 | |
| 533 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 534 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 535 | static int spell_iswordp __ARGS((char_u *p, buf_T *buf)); |
| 536 | static int spell_iswordp_nmw __ARGS((char_u *p)); |
| 537 | #ifdef FEAT_MBYTE |
| 538 | static int spell_iswordp_w __ARGS((int *p, buf_T *buf)); |
| 539 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 540 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 541 | |
| 542 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 543 | * 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] | 544 | */ |
| 545 | typedef enum |
| 546 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 547 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 548 | * ends); if badword ends there is a match, otherwise |
| 549 | * try splitting word. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 550 | STATE_NOPREFIX, /* try without prefix */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 551 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 552 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 553 | STATE_PLAIN, /* Use each byte of the node. */ |
| 554 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 555 | STATE_INS, /* Insert a byte in the bad word. */ |
| 556 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 557 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 558 | STATE_SWAP3, /* Swap two characters over three. */ |
| 559 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 560 | STATE_UNROT3L, /* Undo rotate three characters left */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 561 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 562 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 563 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 564 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 565 | STATE_FINAL /* End of this node. */ |
| 566 | } state_T; |
| 567 | |
| 568 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 569 | * Struct to keep the state at each level in suggest_try_change(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 570 | */ |
| 571 | typedef struct trystate_S |
| 572 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 573 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 574 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 575 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 576 | short ts_curi; /* index in list of child nodes */ |
| 577 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 578 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 579 | char_u ts_twordlen; /* valid length of tword[] */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 580 | char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE |
| 581 | * or NOPREFIX */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 582 | #ifdef FEAT_MBYTE |
| 583 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 584 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 585 | char_u ts_isdiff; /* DIFF_ values */ |
| 586 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 587 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 588 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 589 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 590 | char_u ts_save_badflags; /* su_badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 591 | } trystate_T; |
| 592 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 593 | /* values for ts_isdiff */ |
| 594 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 595 | #define DIFF_YES 1 /* different byte found */ |
| 596 | #define DIFF_INSERT 2 /* inserting character */ |
| 597 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 598 | /* special values ts_prefixdepth */ |
| 599 | #define PREFIXTREE 0xfe /* walking through the prefix tree */ |
| 600 | #define NOPREFIX 0xff /* not using prefixes */ |
| 601 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 602 | /* mode values for find_word */ |
| 603 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 604 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 605 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 606 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 607 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 608 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 609 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 610 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 611 | static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 612 | static void find_prefix __ARGS((matchinf_T *mip)); |
| 613 | static int fold_more __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 614 | static int spell_valid_case __ARGS((int wordflags, int treeflags)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 615 | static int no_spell_checking __ARGS((void)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 616 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 617 | static char_u *spell_enc __ARGS((void)); |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 618 | static void int_wordlist_spl __ARGS((char_u *fname)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 619 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 620 | static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 621 | static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp)); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 622 | static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to)); |
| 623 | static void set_sal_first __ARGS((slang_T *lp)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 624 | #ifdef FEAT_MBYTE |
| 625 | static int *mb_str2wide __ARGS((char_u *s)); |
| 626 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 627 | 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] | 628 | static void clear_midword __ARGS((buf_T *buf)); |
| 629 | static void use_midword __ARGS((slang_T *lp, buf_T *buf)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 630 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 631 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 632 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 633 | 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] | 634 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 635 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 636 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 637 | static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 638 | #ifdef FEAT_EVAL |
| 639 | static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr)); |
| 640 | #endif |
| 641 | static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname)); |
| 642 | static void spell_suggest_intern __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 643 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 644 | 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] | 645 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 646 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 647 | static void suggest_try_change __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 648 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 649 | #ifdef FEAT_MBYTE |
| 650 | static int nofold_len __ARGS((char_u *fword, int flen, char_u *word)); |
| 651 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 652 | 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] | 653 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 654 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 655 | 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] | 656 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 657 | 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] | 658 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 659 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 660 | 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] | 661 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 662 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 663 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 664 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 665 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 666 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res)); |
| 667 | static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| 668 | 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] | 669 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 670 | 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] | 671 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 672 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 673 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 674 | static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum)); |
| 675 | 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] | 676 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 677 | /* |
| 678 | * Use our own character-case definitions, because the current locale may |
| 679 | * differ from what the .spl file uses. |
| 680 | * These must not be called with negative number! |
| 681 | */ |
| 682 | #ifndef FEAT_MBYTE |
| 683 | /* Non-multi-byte implementation. */ |
| 684 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 685 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 686 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 687 | #else |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 688 | # if defined(HAVE_WCHAR_H) |
| 689 | # include <wchar.h> /* for towupper() and towlower() */ |
| 690 | # endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 691 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 692 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 693 | * the "w" library function for characters above 255 if available. */ |
| 694 | # ifdef HAVE_TOWLOWER |
| 695 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 696 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 697 | # else |
| 698 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 699 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 700 | # endif |
| 701 | |
| 702 | # ifdef HAVE_TOWUPPER |
| 703 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 704 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 705 | # else |
| 706 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 707 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 708 | # endif |
| 709 | |
| 710 | # ifdef HAVE_ISWUPPER |
| 711 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 712 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 713 | # else |
| 714 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 715 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 716 | # endif |
| 717 | #endif |
| 718 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 719 | |
| 720 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 721 | static char *e_spell_trunc = N_("E758: Truncated spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 725 | * "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] | 726 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 727 | * or when it's OK it remains unchanged. |
| 728 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 729 | * |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 730 | * "capcol" is used to check for a Capitalised word after the end of a |
| 731 | * sentence. If it's zero then perform the check. Return the column where to |
| 732 | * check next, or -1 when no sentence end was found. If it's NULL then don't |
| 733 | * worry. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 734 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 735 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 736 | * caller can skip over the word. |
| 737 | */ |
| 738 | int |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 739 | spell_check(wp, ptr, attrp, capcol) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 740 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 741 | char_u *ptr; |
| 742 | int *attrp; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 743 | int *capcol; /* column to check for Capital */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 744 | { |
| 745 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 746 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 747 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 748 | int c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 749 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 750 | /* A word never starts at a space or a control character. Return quickly |
| 751 | * then, skipping over the character. */ |
| 752 | if (*ptr <= ' ') |
| 753 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 754 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 755 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 756 | * 0X99FF. But when a word character follows do check spelling to find |
| 757 | * "3GPP". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 758 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 759 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 760 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 761 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 762 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 763 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 764 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 765 | nrlen = mi.mi_end - ptr; |
| 766 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 767 | if (!spell_iswordp(mi.mi_end, wp->w_buffer)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 768 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 769 | |
| 770 | /* Try including the digits in the word. */ |
| 771 | mi.mi_fend = ptr + nrlen; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 772 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 773 | else |
| 774 | mi.mi_fend = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 775 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 776 | /* 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] | 777 | mi.mi_word = ptr; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 778 | if (spell_iswordp(mi.mi_fend, wp->w_buffer)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 779 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 780 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 781 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 782 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 783 | } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer)); |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 784 | |
| 785 | if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL) |
| 786 | { |
| 787 | /* Check word starting with capital letter. */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 788 | c = PTR2CHAR(ptr); |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 789 | if (!SPELL_ISUPPER(c)) |
| 790 | { |
| 791 | *attrp = highlight_attr[HLF_SPC]; |
| 792 | return (int)(mi.mi_fend - ptr); |
| 793 | } |
| 794 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 795 | } |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 796 | if (capcol != NULL) |
| 797 | *capcol = -1; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 798 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 799 | /* We always use the characters up to the next non-word character, |
| 800 | * also for bad words. */ |
| 801 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 802 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 803 | /* Check caps type later. */ |
| 804 | mi.mi_capflags = 0; |
| 805 | mi.mi_cend = NULL; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 806 | mi.mi_buf = wp->w_buffer; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 807 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 808 | /* Include one non-word character so that we can check for the |
| 809 | * word end. */ |
| 810 | if (*mi.mi_fend != NUL) |
| 811 | mb_ptr_adv(mi.mi_fend); |
| 812 | |
| 813 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 814 | MAXWLEN + 1); |
| 815 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 816 | |
| 817 | /* The word is bad unless we recognize it. */ |
| 818 | mi.mi_result = SP_BAD; |
| 819 | |
| 820 | /* |
| 821 | * Loop over the languages specified in 'spelllang'. |
| 822 | * We check them all, because a matching word may be longer than an |
| 823 | * already found matching word. |
| 824 | */ |
| 825 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 826 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 827 | { |
| 828 | /* Check for a matching word in case-folded words. */ |
| 829 | find_word(&mi, FIND_FOLDWORD); |
| 830 | |
| 831 | /* Check for a matching word in keep-case words. */ |
| 832 | find_word(&mi, FIND_KEEPWORD); |
| 833 | |
| 834 | /* Check for matching prefixes. */ |
| 835 | find_prefix(&mi); |
| 836 | } |
| 837 | |
| 838 | if (mi.mi_result != SP_OK) |
| 839 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 840 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 841 | * rare and local words, e.g., "3GPP". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 842 | if (nrlen > 0) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 843 | { |
| 844 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 845 | return nrlen; |
| 846 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 847 | |
| 848 | /* When we are at a non-word character there is no error, just |
| 849 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 850 | else if (!spell_iswordp_nmw(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 851 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 852 | if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL) |
| 853 | { |
| 854 | regmatch_T regmatch; |
| 855 | |
| 856 | /* Check for end of sentence. */ |
| 857 | regmatch.regprog = wp->w_buffer->b_cap_prog; |
| 858 | regmatch.rm_ic = FALSE; |
| 859 | if (vim_regexec(®match, ptr, 0)) |
| 860 | *capcol = (int)(regmatch.endp[0] - ptr); |
| 861 | } |
| 862 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 863 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 864 | if (has_mbyte) |
| 865 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 866 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 867 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 868 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 869 | |
| 870 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 871 | *attrp = highlight_attr[HLF_SPB]; |
| 872 | else if (mi.mi_result == SP_RARE) |
| 873 | *attrp = highlight_attr[HLF_SPR]; |
| 874 | else |
| 875 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 878 | return (int)(mi.mi_end - ptr); |
| 879 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 880 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 881 | /* |
| 882 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 883 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 884 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 885 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 886 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 887 | * |
| 888 | * For a match mip->mi_result is updated. |
| 889 | */ |
| 890 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 891 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 892 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 893 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 894 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 895 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 896 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 897 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 898 | int endidxcnt = 0; |
| 899 | int len; |
| 900 | int wlen = 0; |
| 901 | int flen; |
| 902 | int c; |
| 903 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 904 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 905 | #ifdef FEAT_MBYTE |
| 906 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 907 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 908 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 909 | int res = SP_BAD; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 910 | slang_T *slang = mip->mi_lp->lp_slang; |
| 911 | unsigned flags; |
| 912 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 913 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 914 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 915 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 916 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 917 | /* Check for word with matching case in keep-case tree. */ |
| 918 | ptr = mip->mi_word; |
| 919 | flen = 9999; /* no case folding, always enough bytes */ |
| 920 | byts = slang->sl_kbyts; |
| 921 | idxs = slang->sl_kidxs; |
| 922 | } |
| 923 | else |
| 924 | { |
| 925 | /* Check for case-folded in case-folded tree. */ |
| 926 | ptr = mip->mi_fword; |
| 927 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 928 | byts = slang->sl_fbyts; |
| 929 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 930 | |
| 931 | if (mode == FIND_PREFIX) |
| 932 | { |
| 933 | /* Skip over the prefix. */ |
| 934 | wlen = mip->mi_prefixlen; |
| 935 | flen -= mip->mi_prefixlen; |
| 936 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 939 | if (byts == NULL) |
| 940 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 941 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 942 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 943 | * Repeat advancing in the tree until: |
| 944 | * - there is a byte that doesn't match, |
| 945 | * - we reach the end of the tree, |
| 946 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 947 | */ |
| 948 | for (;;) |
| 949 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 950 | if (flen <= 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 951 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 952 | |
| 953 | len = byts[arridx++]; |
| 954 | |
| 955 | /* If the first possible byte is a zero the word could end here. |
| 956 | * Remember this index, we first check for the longest word. */ |
| 957 | if (byts[arridx] == 0) |
| 958 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 959 | if (endidxcnt == MAXWLEN) |
| 960 | { |
| 961 | /* Must be a corrupted spell file. */ |
| 962 | EMSG(_(e_format)); |
| 963 | return; |
| 964 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 965 | endlen[endidxcnt] = wlen; |
| 966 | endidx[endidxcnt++] = arridx++; |
| 967 | --len; |
| 968 | |
| 969 | /* Skip over the zeros, there can be several flag/region |
| 970 | * combinations. */ |
| 971 | while (len > 0 && byts[arridx] == 0) |
| 972 | { |
| 973 | ++arridx; |
| 974 | --len; |
| 975 | } |
| 976 | if (len == 0) |
| 977 | break; /* no children, word must end here */ |
| 978 | } |
| 979 | |
| 980 | /* Stop looking at end of the line. */ |
| 981 | if (ptr[wlen] == NUL) |
| 982 | break; |
| 983 | |
| 984 | /* Perform a binary search in the list of accepted bytes. */ |
| 985 | c = ptr[wlen]; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 986 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 987 | c = ' '; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 988 | lo = arridx; |
| 989 | hi = arridx + len - 1; |
| 990 | while (lo < hi) |
| 991 | { |
| 992 | m = (lo + hi) / 2; |
| 993 | if (byts[m] > c) |
| 994 | hi = m - 1; |
| 995 | else if (byts[m] < c) |
| 996 | lo = m + 1; |
| 997 | else |
| 998 | { |
| 999 | lo = hi = m; |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /* Stop if there is no matching byte. */ |
| 1005 | if (hi < lo || byts[lo] != c) |
| 1006 | break; |
| 1007 | |
| 1008 | /* Continue at the child (if there is one). */ |
| 1009 | arridx = idxs[lo]; |
| 1010 | ++wlen; |
| 1011 | --flen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* One space in the good word may stand for several spaces in the |
| 1014 | * checked word. */ |
| 1015 | if (c == ' ') |
| 1016 | { |
| 1017 | for (;;) |
| 1018 | { |
| 1019 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 1020 | flen = fold_more(mip); |
| 1021 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 1022 | break; |
| 1023 | ++wlen; |
| 1024 | --flen; |
| 1025 | } |
| 1026 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Verify that one of the possible endings is valid. Try the longest |
| 1031 | * first. |
| 1032 | */ |
| 1033 | while (endidxcnt > 0) |
| 1034 | { |
| 1035 | --endidxcnt; |
| 1036 | arridx = endidx[endidxcnt]; |
| 1037 | wlen = endlen[endidxcnt]; |
| 1038 | |
| 1039 | #ifdef FEAT_MBYTE |
| 1040 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 1041 | continue; /* not at first byte of character */ |
| 1042 | #endif |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1043 | if (spell_iswordp(ptr + wlen, mip->mi_buf)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1044 | continue; /* next char is a word character */ |
| 1045 | |
| 1046 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1047 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1048 | { |
| 1049 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1050 | * when folding case. This can be slow, take a shortcut when the |
| 1051 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1052 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1053 | if (STRNCMP(ptr, p, wlen) != 0) |
| 1054 | { |
| 1055 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 1056 | mb_ptr_adv(p); |
| 1057 | wlen = p - mip->mi_word; |
| 1058 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1059 | } |
| 1060 | #endif |
| 1061 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1062 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 1063 | * prefix ID. |
| 1064 | * Repeat this if there are more flags/region alternatives until there |
| 1065 | * is a match. */ |
| 1066 | res = SP_BAD; |
| 1067 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 1068 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1069 | { |
| 1070 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1071 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1072 | /* For the fold-case tree check that the case of the checked word |
| 1073 | * matches with what the word in the tree requires. |
| 1074 | * For keep-case tree the case is always right. For prefixes we |
| 1075 | * don't bother to check. */ |
| 1076 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1077 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1078 | if (mip->mi_cend != mip->mi_word + wlen) |
| 1079 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1080 | /* mi_capflags was set for a different word length, need |
| 1081 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1082 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1083 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1086 | if (mip->mi_capflags == WF_KEEPCAP |
| 1087 | || !spell_valid_case(mip->mi_capflags, flags)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1088 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1091 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 1092 | * 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] | 1093 | * mip->mi_prefarridx that find_prefix() filled. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1094 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1095 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1096 | /* The prefix ID is stored two bytes above the flags. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1097 | c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1098 | flags, |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1099 | mip->mi_word + mip->mi_cprefixlen, slang, |
| 1100 | FALSE); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1101 | if (c == 0) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1102 | continue; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1103 | |
| 1104 | /* Use the WF_RARE flag for a rare prefix. */ |
| 1105 | if (c & WF_RAREPFX) |
| 1106 | flags |= WF_RARE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | if (flags & WF_BANNED) |
| 1110 | res = SP_BANNED; |
| 1111 | else if (flags & WF_REGION) |
| 1112 | { |
| 1113 | /* Check region. */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1114 | if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1115 | res = SP_OK; |
| 1116 | else |
| 1117 | res = SP_LOCAL; |
| 1118 | } |
| 1119 | else if (flags & WF_RARE) |
| 1120 | res = SP_RARE; |
| 1121 | else |
| 1122 | res = SP_OK; |
| 1123 | |
| 1124 | /* Always use the longest match and the best result. */ |
| 1125 | if (mip->mi_result > res) |
| 1126 | { |
| 1127 | mip->mi_result = res; |
| 1128 | mip->mi_end = mip->mi_word + wlen; |
| 1129 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1130 | 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] | 1131 | mip->mi_end = mip->mi_word + wlen; |
| 1132 | |
| 1133 | if (res == SP_OK) |
| 1134 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1137 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1138 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1139 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1142 | /* |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1143 | * Return non-zero if the prefix indicated by "arridx" matches with the prefix |
| 1144 | * ID in "flags" for the word "word". |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1145 | * 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] | 1146 | */ |
| 1147 | static int |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1148 | valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1149 | int totprefcnt; /* nr of prefix IDs */ |
| 1150 | int arridx; /* idx in sl_pidxs[] */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1151 | int flags; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1152 | char_u *word; |
| 1153 | slang_T *slang; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1154 | int cond_req; /* only use prefixes with a condition */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1155 | { |
| 1156 | int prefcnt; |
| 1157 | int pidx; |
| 1158 | regprog_T *rp; |
| 1159 | regmatch_T regmatch; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1160 | int prefid; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1161 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1162 | prefid = (unsigned)flags >> 24; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1163 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1164 | { |
| 1165 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1166 | |
| 1167 | /* Check the prefix ID. */ |
| 1168 | if (prefid != (pidx & 0xff)) |
| 1169 | continue; |
| 1170 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1171 | /* Check if the prefix doesn't combine and the word already has a |
| 1172 | * suffix. */ |
| 1173 | if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) |
| 1174 | continue; |
| 1175 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1176 | /* Check the condition, if there is one. The condition index is |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1177 | * stored in the two bytes above the prefix ID byte. */ |
| 1178 | rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1179 | if (rp != NULL) |
| 1180 | { |
| 1181 | regmatch.regprog = rp; |
| 1182 | regmatch.rm_ic = FALSE; |
| 1183 | if (!vim_regexec(®match, word, 0)) |
| 1184 | continue; |
| 1185 | } |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1186 | else if (cond_req) |
| 1187 | continue; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1188 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1189 | /* It's a match! Return the WF_ flags. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1190 | return pidx; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1191 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1192 | return 0; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1196 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1197 | * If it does, then check the following word. |
| 1198 | * |
| 1199 | * For a match mip->mi_result is updated. |
| 1200 | */ |
| 1201 | static void |
| 1202 | find_prefix(mip) |
| 1203 | matchinf_T *mip; |
| 1204 | { |
| 1205 | idx_T arridx = 0; |
| 1206 | int len; |
| 1207 | int wlen = 0; |
| 1208 | int flen; |
| 1209 | int c; |
| 1210 | char_u *ptr; |
| 1211 | idx_T lo, hi, m; |
| 1212 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1213 | char_u *byts; |
| 1214 | idx_T *idxs; |
| 1215 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1216 | byts = slang->sl_pbyts; |
| 1217 | if (byts == NULL) |
| 1218 | return; /* array is empty */ |
| 1219 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1220 | /* We use the case-folded word here, since prefixes are always |
| 1221 | * case-folded. */ |
| 1222 | ptr = mip->mi_fword; |
| 1223 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1224 | idxs = slang->sl_pidxs; |
| 1225 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1226 | /* |
| 1227 | * Repeat advancing in the tree until: |
| 1228 | * - there is a byte that doesn't match, |
| 1229 | * - we reach the end of the tree, |
| 1230 | * - or we reach the end of the line. |
| 1231 | */ |
| 1232 | for (;;) |
| 1233 | { |
| 1234 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1235 | flen = fold_more(mip); |
| 1236 | |
| 1237 | len = byts[arridx++]; |
| 1238 | |
| 1239 | /* If the first possible byte is a zero the prefix could end here. |
| 1240 | * Check if the following word matches and supports the prefix. */ |
| 1241 | if (byts[arridx] == 0) |
| 1242 | { |
| 1243 | /* There can be several prefixes with different conditions. We |
| 1244 | * try them all, since we don't know which one will give the |
| 1245 | * longest match. The word is the same each time, pass the list |
| 1246 | * of possible prefixes to find_word(). */ |
| 1247 | mip->mi_prefarridx = arridx; |
| 1248 | mip->mi_prefcnt = len; |
| 1249 | while (len > 0 && byts[arridx] == 0) |
| 1250 | { |
| 1251 | ++arridx; |
| 1252 | --len; |
| 1253 | } |
| 1254 | mip->mi_prefcnt -= len; |
| 1255 | |
| 1256 | /* Find the word that comes after the prefix. */ |
| 1257 | mip->mi_prefixlen = wlen; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1258 | #ifdef FEAT_MBYTE |
| 1259 | if (has_mbyte) |
| 1260 | { |
| 1261 | /* Case-folded length may differ from original length. */ |
| 1262 | mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen, |
| 1263 | mip->mi_word); |
| 1264 | } |
| 1265 | else |
| 1266 | mip->mi_cprefixlen = wlen; |
| 1267 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1268 | find_word(mip, FIND_PREFIX); |
| 1269 | |
| 1270 | |
| 1271 | if (len == 0) |
| 1272 | break; /* no children, word must end here */ |
| 1273 | } |
| 1274 | |
| 1275 | /* Stop looking at end of the line. */ |
| 1276 | if (ptr[wlen] == NUL) |
| 1277 | break; |
| 1278 | |
| 1279 | /* Perform a binary search in the list of accepted bytes. */ |
| 1280 | c = ptr[wlen]; |
| 1281 | lo = arridx; |
| 1282 | hi = arridx + len - 1; |
| 1283 | while (lo < hi) |
| 1284 | { |
| 1285 | m = (lo + hi) / 2; |
| 1286 | if (byts[m] > c) |
| 1287 | hi = m - 1; |
| 1288 | else if (byts[m] < c) |
| 1289 | lo = m + 1; |
| 1290 | else |
| 1291 | { |
| 1292 | lo = hi = m; |
| 1293 | break; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /* Stop if there is no matching byte. */ |
| 1298 | if (hi < lo || byts[lo] != c) |
| 1299 | break; |
| 1300 | |
| 1301 | /* Continue at the child (if there is one). */ |
| 1302 | arridx = idxs[lo]; |
| 1303 | ++wlen; |
| 1304 | --flen; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * Need to fold at least one more character. Do until next non-word character |
| 1310 | * for efficiency. |
| 1311 | * Return the length of the folded chars in bytes. |
| 1312 | */ |
| 1313 | static int |
| 1314 | fold_more(mip) |
| 1315 | matchinf_T *mip; |
| 1316 | { |
| 1317 | int flen; |
| 1318 | char_u *p; |
| 1319 | |
| 1320 | p = mip->mi_fend; |
| 1321 | do |
| 1322 | { |
| 1323 | mb_ptr_adv(mip->mi_fend); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1324 | } 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] | 1325 | |
| 1326 | /* Include the non-word character so that we can check for the |
| 1327 | * word end. */ |
| 1328 | if (*mip->mi_fend != NUL) |
| 1329 | mb_ptr_adv(mip->mi_fend); |
| 1330 | |
| 1331 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1332 | mip->mi_fword + mip->mi_fwordlen, |
| 1333 | MAXWLEN - mip->mi_fwordlen); |
| 1334 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1335 | mip->mi_fwordlen += flen; |
| 1336 | return flen; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1340 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1341 | * case. |
| 1342 | */ |
| 1343 | static int |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1344 | spell_valid_case(wordflags, treeflags) |
| 1345 | int wordflags; /* flags for the checked word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1346 | int treeflags; /* flags for the word in the spell tree */ |
| 1347 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1348 | return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1349 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1350 | && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP))); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1353 | /* |
| 1354 | * Return TRUE if spell checking is not enabled. |
| 1355 | */ |
| 1356 | static int |
| 1357 | no_spell_checking() |
| 1358 | { |
| 1359 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 1360 | { |
| 1361 | EMSG(_("E756: Spell checking is not enabled")); |
| 1362 | return TRUE; |
| 1363 | } |
| 1364 | return FALSE; |
| 1365 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1366 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1367 | /* |
| 1368 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1369 | * "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] | 1370 | * Return OK if found, FAIL otherwise. |
| 1371 | */ |
| 1372 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1373 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1374 | int dir; /* FORWARD or BACKWARD */ |
| 1375 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1376 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1377 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1378 | linenr_T lnum; |
| 1379 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1380 | char_u *line; |
| 1381 | char_u *p; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1382 | char_u *endp; |
| 1383 | int attr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1384 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1385 | int has_syntax = syntax_present(curbuf); |
| 1386 | int col; |
| 1387 | int can_spell; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1388 | char_u *buf = NULL; |
| 1389 | int buflen = 0; |
| 1390 | int skip = 0; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1391 | int capcol = -1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1392 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1393 | if (no_spell_checking()) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1394 | return FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1395 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1396 | /* |
| 1397 | * 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] | 1398 | * 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] | 1399 | * |
| 1400 | * When searching backwards, we continue in the line to find the last |
| 1401 | * bad word (in the cursor line: before the cursor). |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1402 | * |
| 1403 | * We concatenate the start of the next line, so that wrapped words work |
| 1404 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 1405 | * though... |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1406 | */ |
| 1407 | lnum = curwin->w_cursor.lnum; |
| 1408 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1409 | |
| 1410 | while (!got_int) |
| 1411 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1412 | line = ml_get(lnum); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1413 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1414 | len = STRLEN(line); |
| 1415 | if (buflen < len + MAXWLEN + 2) |
| 1416 | { |
| 1417 | vim_free(buf); |
| 1418 | buflen = len + MAXWLEN + 2; |
| 1419 | buf = alloc(buflen); |
| 1420 | if (buf == NULL) |
| 1421 | break; |
| 1422 | } |
| 1423 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1424 | /* In first line check first word for Capital. */ |
| 1425 | if (lnum == 1) |
| 1426 | capcol = 0; |
| 1427 | |
| 1428 | /* For checking first word with a capital skip white space. */ |
| 1429 | if (capcol == 0) |
| 1430 | capcol = skipwhite(line) - line; |
| 1431 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1432 | /* Copy the line into "buf" and append the start of the next line if |
| 1433 | * possible. */ |
| 1434 | STRCPY(buf, line); |
| 1435 | if (lnum < curbuf->b_ml.ml_line_count) |
| 1436 | spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN); |
| 1437 | |
| 1438 | p = buf + skip; |
| 1439 | endp = buf + len; |
| 1440 | while (p < endp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1441 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1442 | /* When searching backward don't search after the cursor. */ |
| 1443 | if (dir == BACKWARD |
| 1444 | && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1445 | && (colnr_T)(p - buf) >= curwin->w_cursor.col) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1446 | break; |
| 1447 | |
| 1448 | /* start of word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1449 | attr = 0; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1450 | len = spell_check(curwin, p, &attr, &capcol); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1451 | |
| 1452 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1453 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1454 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1455 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1456 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1457 | /* When searching forward only accept a bad word after |
| 1458 | * the cursor. */ |
| 1459 | if (dir == BACKWARD |
| 1460 | || lnum > curwin->w_cursor.lnum |
| 1461 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1462 | && (colnr_T)(curline ? p - buf + len |
| 1463 | : p - buf) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1464 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1465 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1466 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1467 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1468 | col = p - buf; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1469 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1470 | FALSE, &can_spell); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1471 | } |
| 1472 | else |
| 1473 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1474 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1475 | if (can_spell) |
| 1476 | { |
| 1477 | found_pos.lnum = lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1478 | found_pos.col = p - buf; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1479 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1480 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1481 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1482 | if (dir == FORWARD) |
| 1483 | { |
| 1484 | /* No need to search further. */ |
| 1485 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1486 | vim_free(buf); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1487 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1488 | } |
| 1489 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1490 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1491 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1494 | /* advance to character after the word */ |
| 1495 | p += len; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1496 | capcol -= len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1499 | if (curline) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1500 | break; /* only check cursor line */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1501 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1502 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1503 | if (dir == BACKWARD) |
| 1504 | { |
| 1505 | if (found_pos.lnum != 0) |
| 1506 | { |
| 1507 | /* Use the last match in the line. */ |
| 1508 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1509 | vim_free(buf); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1510 | return OK; |
| 1511 | } |
| 1512 | if (lnum == 1) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1513 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1514 | --lnum; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1515 | capcol = -1; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1516 | } |
| 1517 | else |
| 1518 | { |
| 1519 | if (lnum == curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1520 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1521 | ++lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1522 | |
| 1523 | /* Skip the characters at the start of the next line that were |
| 1524 | * included in a match crossing line boundaries. */ |
| 1525 | if (attr == 0) |
| 1526 | skip = p - endp; |
| 1527 | else |
| 1528 | skip = 0; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1529 | |
| 1530 | /* Capscol skips over the inserted space. */ |
| 1531 | --capcol; |
| 1532 | |
| 1533 | /* But after empty line check first word in next line */ |
| 1534 | if (*skipwhite(line) == NUL) |
| 1535 | capcol = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1536 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1537 | |
| 1538 | line_breakcheck(); |
| 1539 | } |
| 1540 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1541 | vim_free(buf); |
| 1542 | return FAIL; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * For spell checking: concatenate the start of the following line "line" into |
| 1547 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| 1548 | */ |
| 1549 | void |
| 1550 | spell_cat_line(buf, line, maxlen) |
| 1551 | char_u *buf; |
| 1552 | char_u *line; |
| 1553 | int maxlen; |
| 1554 | { |
| 1555 | char_u *p; |
| 1556 | int n; |
| 1557 | |
| 1558 | p = skipwhite(line); |
| 1559 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 1560 | p = skipwhite(p + 1); |
| 1561 | |
| 1562 | if (*p != NUL) |
| 1563 | { |
| 1564 | *buf = ' '; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1565 | vim_strncpy(buf + 1, line, maxlen - 2); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1566 | n = p - line; |
| 1567 | if (n >= maxlen) |
| 1568 | n = maxlen - 1; |
| 1569 | vim_memset(buf + 1, ' ', n); |
| 1570 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1574 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1575 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1576 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1577 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1578 | spell_load_lang(lang) |
| 1579 | char_u *lang; |
| 1580 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1581 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1582 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1583 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1584 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1585 | /* 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] | 1586 | * It's truncated when an error is detected. */ |
| 1587 | STRCPY(langcp, lang); |
| 1588 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1589 | /* |
| 1590 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1591 | */ |
| 1592 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1593 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1594 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1595 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1596 | if (r == FAIL && *langcp != NUL) |
| 1597 | { |
| 1598 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1599 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1600 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1601 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1604 | if (r == FAIL) |
| 1605 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1606 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1607 | else if (*langcp != NUL) |
| 1608 | { |
| 1609 | /* Load all the additions. */ |
| 1610 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1611 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1617 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1618 | */ |
| 1619 | static char_u * |
| 1620 | spell_enc() |
| 1621 | { |
| 1622 | |
| 1623 | #ifdef FEAT_MBYTE |
| 1624 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1625 | return p_enc; |
| 1626 | #endif |
| 1627 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | /* |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1631 | * Get the name of the .spl file for the internal wordlist into |
| 1632 | * "fname[MAXPATHL]". |
| 1633 | */ |
| 1634 | static void |
| 1635 | int_wordlist_spl(fname) |
| 1636 | char_u *fname; |
| 1637 | { |
| 1638 | vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl", |
| 1639 | int_wordlist, spell_enc()); |
| 1640 | } |
| 1641 | |
| 1642 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1643 | * Allocate a new slang_T. |
| 1644 | * Caller must fill "sl_next". |
| 1645 | */ |
| 1646 | static slang_T * |
| 1647 | slang_alloc(lang) |
| 1648 | char_u *lang; |
| 1649 | { |
| 1650 | slang_T *lp; |
| 1651 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1652 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1653 | if (lp != NULL) |
| 1654 | { |
| 1655 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1656 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1657 | } |
| 1658 | return lp; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * Free the contents of an slang_T and the structure itself. |
| 1663 | */ |
| 1664 | static void |
| 1665 | slang_free(lp) |
| 1666 | slang_T *lp; |
| 1667 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1668 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1669 | vim_free(lp->sl_fname); |
| 1670 | slang_clear(lp); |
| 1671 | vim_free(lp); |
| 1672 | } |
| 1673 | |
| 1674 | /* |
| 1675 | * Clear an slang_T so that the file can be reloaded. |
| 1676 | */ |
| 1677 | static void |
| 1678 | slang_clear(lp) |
| 1679 | slang_T *lp; |
| 1680 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1681 | garray_T *gap; |
| 1682 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1683 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1684 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1685 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1686 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1687 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1688 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1689 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1690 | vim_free(lp->sl_pbyts); |
| 1691 | lp->sl_pbyts = NULL; |
| 1692 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1693 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1694 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1695 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1696 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1697 | vim_free(lp->sl_pidxs); |
| 1698 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1699 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1700 | gap = &lp->sl_rep; |
| 1701 | while (gap->ga_len > 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1702 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1703 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1704 | vim_free(ftp->ft_from); |
| 1705 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1706 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1707 | ga_clear(gap); |
| 1708 | |
| 1709 | gap = &lp->sl_sal; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1710 | if (lp->sl_sofo) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1711 | { |
| 1712 | /* "ga_len" is set to 1 without adding an item for latin1 */ |
| 1713 | if (gap->ga_data != NULL) |
| 1714 | /* SOFOFROM and SOFOTO items: free lists of wide characters. */ |
| 1715 | for (i = 0; i < gap->ga_len; ++i) |
| 1716 | vim_free(((int **)gap->ga_data)[i]); |
| 1717 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1718 | else |
| 1719 | /* SAL items: free salitem_T items */ |
| 1720 | while (gap->ga_len > 0) |
| 1721 | { |
| 1722 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1723 | vim_free(smp->sm_lead); |
| 1724 | /* Don't free sm_oneof and sm_rules, they point into sm_lead. */ |
| 1725 | vim_free(smp->sm_to); |
| 1726 | #ifdef FEAT_MBYTE |
| 1727 | vim_free(smp->sm_lead_w); |
| 1728 | vim_free(smp->sm_oneof_w); |
| 1729 | vim_free(smp->sm_to_w); |
| 1730 | #endif |
| 1731 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1732 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1733 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1734 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1735 | vim_free(lp->sl_prefprog[i]); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1736 | lp->sl_prefixcnt = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1737 | vim_free(lp->sl_prefprog); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1738 | lp->sl_prefprog = NULL; |
| 1739 | |
| 1740 | vim_free(lp->sl_midword); |
| 1741 | lp->sl_midword = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1742 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1743 | #ifdef FEAT_MBYTE |
| 1744 | { |
| 1745 | int todo = lp->sl_map_hash.ht_used; |
| 1746 | hashitem_T *hi; |
| 1747 | |
| 1748 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1749 | if (!HASHITEM_EMPTY(hi)) |
| 1750 | { |
| 1751 | --todo; |
| 1752 | vim_free(hi->hi_key); |
| 1753 | } |
| 1754 | } |
| 1755 | hash_clear(&lp->sl_map_hash); |
| 1756 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1760 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1761 | * Invoked through do_in_runtimepath(). |
| 1762 | */ |
| 1763 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1764 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1765 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1766 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1767 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1768 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /* |
| 1772 | * Load one spell file and store the info into a slang_T. |
| 1773 | * |
| 1774 | * This is invoked in two ways: |
| 1775 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1776 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1777 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1778 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1779 | * 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] | 1780 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1781 | static slang_T * |
| 1782 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1783 | char_u *fname; |
| 1784 | char_u *lang; |
| 1785 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1786 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1787 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1788 | FILE *fd; |
| 1789 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1790 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1791 | char_u *bp; |
| 1792 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1793 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1794 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1795 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1796 | int round; |
| 1797 | char_u *save_sourcing_name = sourcing_name; |
| 1798 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1799 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1800 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1801 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1802 | garray_T *gap; |
| 1803 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1804 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1805 | short *first; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1806 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1807 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1808 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1809 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1810 | if (fd == NULL) |
| 1811 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1812 | if (!silent) |
| 1813 | EMSG2(_(e_notopen), fname); |
| 1814 | else if (p_verbose > 2) |
| 1815 | { |
| 1816 | verbose_enter(); |
| 1817 | smsg((char_u *)e_notopen, fname); |
| 1818 | verbose_leave(); |
| 1819 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1820 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1821 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1822 | if (p_verbose > 2) |
| 1823 | { |
| 1824 | verbose_enter(); |
| 1825 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1826 | verbose_leave(); |
| 1827 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1828 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1829 | if (old_lp == NULL) |
| 1830 | { |
| 1831 | lp = slang_alloc(lang); |
| 1832 | if (lp == NULL) |
| 1833 | goto endFAIL; |
| 1834 | |
| 1835 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1836 | lp->sl_fname = vim_strsave(fname); |
| 1837 | if (lp->sl_fname == NULL) |
| 1838 | goto endFAIL; |
| 1839 | |
| 1840 | /* Check for .add.spl. */ |
| 1841 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1842 | } |
| 1843 | else |
| 1844 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1845 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1846 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1847 | sourcing_name = fname; |
| 1848 | sourcing_lnum = 0; |
| 1849 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1850 | /* <HEADER>: <fileID> |
| 1851 | * <regioncnt> <regionname> ... |
| 1852 | * <charflagslen> <charflags> |
| 1853 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1854 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1855 | * <prefcondcnt> <prefcond> ... |
| 1856 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1857 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1858 | buf[i] = getc(fd); /* <fileID> */ |
| 1859 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1860 | { |
| 1861 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1862 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1866 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1867 | { |
| 1868 | truncerr: |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1869 | EMSG(_(e_spell_trunc)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1870 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1871 | } |
| 1872 | if (cnt > 8) |
| 1873 | { |
| 1874 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1875 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1876 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1877 | } |
| 1878 | for (i = 0; i < cnt; ++i) |
| 1879 | { |
| 1880 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1881 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1882 | } |
| 1883 | lp->sl_regions[cnt * 2] = NUL; |
| 1884 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1885 | /* <charflagslen> <charflags> */ |
| 1886 | p = read_cnt_string(fd, 1, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1887 | if (cnt < 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1888 | goto endFAIL; |
| 1889 | |
| 1890 | /* <fcharslen> <fchars> */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1891 | fol = read_cnt_string(fd, 2, &ccnt); |
| 1892 | if (ccnt < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1893 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1894 | vim_free(p); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1895 | goto endFAIL; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1896 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1897 | |
| 1898 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
| 1899 | if (p != NULL && fol != NULL) |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1900 | i = set_spell_charflags(p, cnt, fol); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1901 | |
| 1902 | vim_free(p); |
| 1903 | vim_free(fol); |
| 1904 | |
| 1905 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1906 | if ((p == NULL) != (fol == NULL)) |
| 1907 | goto formerr; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1908 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1909 | /* <midwordlen> <midword> */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1910 | lp->sl_midword = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1911 | if (cnt < 0) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1912 | goto endFAIL; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1913 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1914 | /* <prefcondcnt> <prefcond> ... */ |
| 1915 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1916 | if (cnt > 0) |
| 1917 | { |
| 1918 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1919 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1920 | if (lp->sl_prefprog == NULL) |
| 1921 | goto endFAIL; |
| 1922 | lp->sl_prefixcnt = cnt; |
| 1923 | |
| 1924 | for (i = 0; i < cnt; ++i) |
| 1925 | { |
| 1926 | /* <prefcond> : <condlen> <condstr> */ |
| 1927 | n = getc(fd); /* <condlen> */ |
| 1928 | if (n < 0) |
| 1929 | goto formerr; |
| 1930 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1931 | * compile the regexp program used to check for the condition. */ |
| 1932 | if (n > 0) |
| 1933 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1934 | buf[0] = '^'; /* always match at one position only */ |
| 1935 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1936 | while (n-- > 0) |
| 1937 | *p++ = getc(fd); /* <condstr> */ |
| 1938 | *p = NUL; |
| 1939 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1945 | /* <SUGGEST> : <repcount> <rep> ... |
| 1946 | * <salflags> <salcount> <sal> ... |
| 1947 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1948 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1949 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1950 | if (cnt < 0) |
| 1951 | goto formerr; |
| 1952 | |
| 1953 | gap = &lp->sl_rep; |
| 1954 | if (ga_grow(gap, cnt) == FAIL) |
| 1955 | goto endFAIL; |
| 1956 | |
| 1957 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1958 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1959 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1960 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1961 | ftp->ft_from = read_cnt_string(fd, 1, &i); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1962 | if (i <= 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1963 | goto endFAIL; |
| 1964 | ftp->ft_to = read_cnt_string(fd, 1, &i); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1965 | if (i <= 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1966 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1967 | vim_free(ftp->ft_from); |
| 1968 | goto endFAIL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | /* Fill the first-index table. */ |
| 1973 | first = lp->sl_rep_first; |
| 1974 | for (i = 0; i < 256; ++i) |
| 1975 | first[i] = -1; |
| 1976 | for (i = 0; i < gap->ga_len; ++i) |
| 1977 | { |
| 1978 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1979 | if (first[*ftp->ft_from] == -1) |
| 1980 | first[*ftp->ft_from] = i; |
| 1981 | } |
| 1982 | |
| 1983 | i = getc(fd); /* <salflags> */ |
| 1984 | if (i & SAL_F0LLOWUP) |
| 1985 | lp->sl_followup = TRUE; |
| 1986 | if (i & SAL_COLLAPSE) |
| 1987 | lp->sl_collapse = TRUE; |
| 1988 | if (i & SAL_REM_ACCENTS) |
| 1989 | lp->sl_rem_accents = TRUE; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1990 | if (i & SAL_SOFO) |
| 1991 | lp->sl_sofo = TRUE; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 1992 | else |
| 1993 | lp->sl_sofo = FALSE; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1994 | |
| 1995 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1996 | if (cnt < 0) |
| 1997 | goto formerr; |
| 1998 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1999 | if (lp->sl_sofo) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2000 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2001 | /* |
| 2002 | * SOFOFROM and SOFOTO items come in one <salfrom> and <salto> |
| 2003 | */ |
| 2004 | if (cnt != 1) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2005 | goto formerr; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2006 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2007 | /* <salfromlen> <salfrom> */ |
| 2008 | bp = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2009 | if (cnt < 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2010 | goto endFAIL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2011 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2012 | /* <saltolen> <salto> */ |
| 2013 | fol = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2014 | if (cnt < 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2015 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2016 | vim_free(bp); |
| 2017 | goto endFAIL; |
| 2018 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2019 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2020 | /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2021 | if (bp != NULL && fol != NULL) |
| 2022 | i = set_sofo(lp, bp, fol); |
| 2023 | else if (bp != NULL || fol != NULL) |
| 2024 | i = FAIL; /* only one of two strings is an error */ |
| 2025 | else |
| 2026 | i = OK; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2027 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2028 | vim_free(bp); |
| 2029 | vim_free(fol); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2030 | if (i == FAIL) |
| 2031 | goto formerr; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2032 | } |
| 2033 | else |
| 2034 | { |
| 2035 | /* |
| 2036 | * SAL items |
| 2037 | */ |
| 2038 | gap = &lp->sl_sal; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2039 | ga_init2(gap, sizeof(salitem_T), 10); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2040 | if (ga_grow(gap, cnt) == FAIL) |
| 2041 | goto endFAIL; |
| 2042 | |
| 2043 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 2044 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 2045 | { |
| 2046 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 2047 | ccnt = getc(fd); /* <salfromlen> */ |
| 2048 | if (ccnt < 0) |
| 2049 | goto formerr; |
| 2050 | if ((p = alloc(ccnt + 2)) == NULL) |
| 2051 | goto endFAIL; |
| 2052 | smp->sm_lead = p; |
| 2053 | |
| 2054 | /* Read up to the first special char into sm_lead. */ |
| 2055 | for (i = 0; i < ccnt; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2056 | { |
| 2057 | c = getc(fd); /* <salfrom> */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2058 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2059 | break; |
| 2060 | *p++ = c; |
| 2061 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2062 | smp->sm_leadlen = p - smp->sm_lead; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2063 | *p++ = NUL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2064 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2065 | /* Put (abc) chars in sm_oneof, if any. */ |
| 2066 | if (c == '(') |
| 2067 | { |
| 2068 | smp->sm_oneof = p; |
| 2069 | for (++i; i < ccnt; ++i) |
| 2070 | { |
| 2071 | c = getc(fd); /* <salfrom> */ |
| 2072 | if (c == ')') |
| 2073 | break; |
| 2074 | *p++ = c; |
| 2075 | } |
| 2076 | *p++ = NUL; |
| 2077 | if (++i < ccnt) |
| 2078 | c = getc(fd); |
| 2079 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2080 | else |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2081 | smp->sm_oneof = NULL; |
| 2082 | |
| 2083 | /* Any following chars go in sm_rules. */ |
| 2084 | smp->sm_rules = p; |
| 2085 | if (i < ccnt) |
| 2086 | /* store the char we got while checking for end of sm_lead */ |
| 2087 | *p++ = c; |
| 2088 | for (++i; i < ccnt; ++i) |
| 2089 | *p++ = getc(fd); /* <salfrom> */ |
| 2090 | *p++ = NUL; |
| 2091 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2092 | /* <saltolen> <salto> */ |
| 2093 | smp->sm_to = read_cnt_string(fd, 1, &ccnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2094 | if (ccnt < 0) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2095 | { |
| 2096 | vim_free(smp->sm_lead); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2097 | goto formerr; |
| 2098 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2099 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2100 | #ifdef FEAT_MBYTE |
| 2101 | if (has_mbyte) |
| 2102 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2103 | /* convert the multi-byte strings to wide char strings */ |
| 2104 | smp->sm_lead_w = mb_str2wide(smp->sm_lead); |
| 2105 | smp->sm_leadlen = mb_charlen(smp->sm_lead); |
| 2106 | if (smp->sm_oneof == NULL) |
| 2107 | smp->sm_oneof_w = NULL; |
| 2108 | else |
| 2109 | smp->sm_oneof_w = mb_str2wide(smp->sm_oneof); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2110 | if (smp->sm_to == NULL) |
| 2111 | smp->sm_to_w = NULL; |
| 2112 | else |
| 2113 | smp->sm_to_w = mb_str2wide(smp->sm_to); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2114 | if (smp->sm_lead_w == NULL |
| 2115 | || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL) |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2116 | || (smp->sm_to_w == NULL && smp->sm_to != NULL)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2117 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2118 | vim_free(smp->sm_lead); |
| 2119 | vim_free(smp->sm_to); |
| 2120 | vim_free(smp->sm_lead_w); |
| 2121 | vim_free(smp->sm_oneof_w); |
| 2122 | vim_free(smp->sm_to_w); |
| 2123 | goto endFAIL; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2124 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2125 | } |
| 2126 | #endif |
| 2127 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2128 | |
| 2129 | /* Fill the first-index table. */ |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2130 | set_sal_first(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2133 | /* <maplen> <mapstr> */ |
| 2134 | p = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2135 | if (cnt < 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2136 | goto endFAIL; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2137 | if (p != NULL) |
| 2138 | { |
| 2139 | set_map_str(lp, p); |
| 2140 | vim_free(p); |
| 2141 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2142 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2143 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2144 | * round 2: <KWORDTREE> |
| 2145 | * round 3: <PREFIXTREE> */ |
| 2146 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2147 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2148 | /* The tree size was computed when writing the file, so that we can |
| 2149 | * allocate it as one long block. <nodecount> */ |
| 2150 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2151 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2152 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2153 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2154 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2155 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2156 | bp = lalloc((long_u)len, TRUE); |
| 2157 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2158 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2159 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2160 | lp->sl_fbyts = bp; |
| 2161 | else if (round == 2) |
| 2162 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2163 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2164 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2165 | |
| 2166 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2167 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 2168 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2169 | goto endFAIL; |
| 2170 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2171 | lp->sl_fidxs = ip; |
| 2172 | else if (round == 2) |
| 2173 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2174 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2175 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2176 | |
| 2177 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2178 | 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] | 2179 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2180 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2181 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2182 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2183 | } |
| 2184 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2185 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2186 | /* For a new file link it in the list of spell files. */ |
| 2187 | if (old_lp == NULL) |
| 2188 | { |
| 2189 | lp->sl_next = first_lang; |
| 2190 | first_lang = lp; |
| 2191 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2192 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2193 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2194 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2195 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2196 | if (lang != NULL) |
| 2197 | /* truncating the name signals the error to spell_load_lang() */ |
| 2198 | *lang = NUL; |
| 2199 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2200 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2201 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2202 | lp = NULL; |
| 2203 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2204 | |
| 2205 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2206 | if (fd != NULL) |
| 2207 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2208 | sourcing_name = save_sourcing_name; |
| 2209 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2210 | |
| 2211 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2214 | /* |
| 2215 | * Read a length field from "fd" in "cnt_bytes" bytes. |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2216 | * Allocate memory, read the string into it and add a NUL at the end. |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2217 | * Returns NULL when the count is zero. |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2218 | * Sets "*cntp" to -1 when there is an error, length of the result otherwise. |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2219 | */ |
| 2220 | static char_u * |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2221 | read_cnt_string(fd, cnt_bytes, cntp) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2222 | FILE *fd; |
| 2223 | int cnt_bytes; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2224 | int *cntp; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2225 | { |
| 2226 | int cnt = 0; |
| 2227 | int i; |
| 2228 | char_u *str; |
| 2229 | |
| 2230 | /* read the length bytes, MSB first */ |
| 2231 | for (i = 0; i < cnt_bytes; ++i) |
| 2232 | cnt = (cnt << 8) + getc(fd); |
| 2233 | if (cnt < 0) |
| 2234 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2235 | EMSG(_(e_spell_trunc)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2236 | *cntp = -1; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2237 | return NULL; |
| 2238 | } |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2239 | *cntp = cnt; |
| 2240 | if (cnt == 0) |
| 2241 | return NULL; /* nothing to read, return NULL */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2242 | |
| 2243 | /* allocate memory */ |
| 2244 | str = alloc((unsigned)cnt + 1); |
| 2245 | if (str == NULL) |
| 2246 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2247 | *cntp = -1; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2248 | return NULL; |
| 2249 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2250 | |
| 2251 | /* Read the string. Doesn't check for truncated file. */ |
| 2252 | for (i = 0; i < cnt; ++i) |
| 2253 | str[i] = getc(fd); |
| 2254 | str[i] = NUL; |
| 2255 | |
| 2256 | return str; |
| 2257 | } |
| 2258 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2259 | /* |
| 2260 | * Set the SOFOFROM and SOFOTO items in language "lp". |
| 2261 | * Returns FAIL when there is something wrong. |
| 2262 | */ |
| 2263 | static int |
| 2264 | set_sofo(lp, from, to) |
| 2265 | slang_T *lp; |
| 2266 | char_u *from; |
| 2267 | char_u *to; |
| 2268 | { |
| 2269 | int i; |
| 2270 | |
| 2271 | #ifdef FEAT_MBYTE |
| 2272 | garray_T *gap; |
| 2273 | char_u *s; |
| 2274 | char_u *p; |
| 2275 | int c; |
| 2276 | int *inp; |
| 2277 | |
| 2278 | if (has_mbyte) |
| 2279 | { |
| 2280 | /* Use "sl_sal" as an array with 256 pointers to a list of wide |
| 2281 | * characters. The index is the low byte of the character. |
| 2282 | * The list contains from-to pairs with a terminating NUL. |
| 2283 | * sl_sal_first[] is used for latin1 "from" characters. */ |
| 2284 | gap = &lp->sl_sal; |
| 2285 | ga_init2(gap, sizeof(int *), 1); |
| 2286 | if (ga_grow(gap, 256) == FAIL) |
| 2287 | return FAIL; |
| 2288 | vim_memset(gap->ga_data, 0, sizeof(int *) * 256); |
| 2289 | gap->ga_len = 256; |
| 2290 | |
| 2291 | /* First count the number of items for each list. Temporarily use |
| 2292 | * sl_sal_first[] for this. */ |
| 2293 | for (p = from, s = to; *p != NUL && *s != NUL; ) |
| 2294 | { |
| 2295 | c = mb_ptr2char_adv(&p); |
| 2296 | mb_ptr_adv(s); |
| 2297 | if (c >= 256) |
| 2298 | ++lp->sl_sal_first[c & 0xff]; |
| 2299 | } |
| 2300 | if (*p != NUL || *s != NUL) /* lengths differ */ |
| 2301 | return FAIL; |
| 2302 | |
| 2303 | /* Allocate the lists. */ |
| 2304 | for (i = 0; i < 256; ++i) |
| 2305 | if (lp->sl_sal_first[i] > 0) |
| 2306 | { |
| 2307 | p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1)); |
| 2308 | if (p == NULL) |
| 2309 | return FAIL; |
| 2310 | ((int **)gap->ga_data)[i] = (int *)p; |
| 2311 | *(int *)p = 0; |
| 2312 | } |
| 2313 | |
| 2314 | /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal |
| 2315 | * list. */ |
| 2316 | vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256); |
| 2317 | for (p = from, s = to; *p != NUL && *s != NUL; ) |
| 2318 | { |
| 2319 | c = mb_ptr2char_adv(&p); |
| 2320 | i = mb_ptr2char_adv(&s); |
| 2321 | if (c >= 256) |
| 2322 | { |
| 2323 | /* Append the from-to chars at the end of the list with |
| 2324 | * the low byte. */ |
| 2325 | inp = ((int **)gap->ga_data)[c & 0xff]; |
| 2326 | while (*inp != 0) |
| 2327 | ++inp; |
| 2328 | *inp++ = c; /* from char */ |
| 2329 | *inp++ = i; /* to char */ |
| 2330 | *inp++ = NUL; /* NUL at the end */ |
| 2331 | } |
| 2332 | else |
| 2333 | /* mapping byte to char is done in sl_sal_first[] */ |
| 2334 | lp->sl_sal_first[c] = i; |
| 2335 | } |
| 2336 | } |
| 2337 | else |
| 2338 | #endif |
| 2339 | { |
| 2340 | /* mapping bytes to bytes is done in sl_sal_first[] */ |
| 2341 | if (STRLEN(from) != STRLEN(to)) |
| 2342 | return FAIL; |
| 2343 | |
| 2344 | for (i = 0; to[i] != NUL; ++i) |
| 2345 | lp->sl_sal_first[from[i]] = to[i]; |
| 2346 | lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */ |
| 2347 | } |
| 2348 | |
| 2349 | return OK; |
| 2350 | } |
| 2351 | |
| 2352 | /* |
| 2353 | * Fill the first-index table for "lp". |
| 2354 | */ |
| 2355 | static void |
| 2356 | set_sal_first(lp) |
| 2357 | slang_T *lp; |
| 2358 | { |
| 2359 | salfirst_T *sfirst; |
| 2360 | int i; |
| 2361 | salitem_T *smp; |
| 2362 | int c; |
| 2363 | garray_T *gap = &lp->sl_sal; |
| 2364 | |
| 2365 | sfirst = lp->sl_sal_first; |
| 2366 | for (i = 0; i < 256; ++i) |
| 2367 | sfirst[i] = -1; |
| 2368 | smp = (salitem_T *)gap->ga_data; |
| 2369 | for (i = 0; i < gap->ga_len; ++i) |
| 2370 | { |
| 2371 | #ifdef FEAT_MBYTE |
| 2372 | if (has_mbyte) |
| 2373 | /* Use the lowest byte of the first character. For latin1 it's |
| 2374 | * the character, for other encodings it should differ for most |
| 2375 | * characters. */ |
| 2376 | c = *smp[i].sm_lead_w & 0xff; |
| 2377 | else |
| 2378 | #endif |
| 2379 | c = *smp[i].sm_lead; |
| 2380 | if (sfirst[c] == -1) |
| 2381 | { |
| 2382 | sfirst[c] = i; |
| 2383 | #ifdef FEAT_MBYTE |
| 2384 | if (has_mbyte) |
| 2385 | { |
| 2386 | int n; |
| 2387 | |
| 2388 | /* Make sure all entries with this byte are following each |
| 2389 | * other. Move the ones that are in the wrong position. Do |
| 2390 | * keep the same ordering! */ |
| 2391 | while (i + 1 < gap->ga_len |
| 2392 | && (*smp[i + 1].sm_lead_w & 0xff) == c) |
| 2393 | /* Skip over entry with same index byte. */ |
| 2394 | ++i; |
| 2395 | |
| 2396 | for (n = 1; i + n < gap->ga_len; ++n) |
| 2397 | if ((*smp[i + n].sm_lead_w & 0xff) == c) |
| 2398 | { |
| 2399 | salitem_T tsal; |
| 2400 | |
| 2401 | /* Move entry with same index byte after the entries |
| 2402 | * we already found. */ |
| 2403 | ++i; |
| 2404 | --n; |
| 2405 | tsal = smp[i + n]; |
| 2406 | mch_memmove(smp + i + 1, smp + i, |
| 2407 | sizeof(salitem_T) * n); |
| 2408 | smp[i] = tsal; |
| 2409 | } |
| 2410 | } |
| 2411 | #endif |
| 2412 | } |
| 2413 | } |
| 2414 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2415 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2416 | #ifdef FEAT_MBYTE |
| 2417 | /* |
| 2418 | * Turn a multi-byte string into a wide character string. |
| 2419 | * Return it in allocated memory (NULL for out-of-memory) |
| 2420 | */ |
| 2421 | static int * |
| 2422 | mb_str2wide(s) |
| 2423 | char_u *s; |
| 2424 | { |
| 2425 | int *res; |
| 2426 | char_u *p; |
| 2427 | int i = 0; |
| 2428 | |
| 2429 | res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1)); |
| 2430 | if (res != NULL) |
| 2431 | { |
| 2432 | for (p = s; *p != NUL; ) |
| 2433 | res[i++] = mb_ptr2char_adv(&p); |
| 2434 | res[i] = NUL; |
| 2435 | } |
| 2436 | return res; |
| 2437 | } |
| 2438 | #endif |
| 2439 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2440 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2441 | * Read one row of siblings from the spell file and store it in the byte array |
| 2442 | * "byts" and index array "idxs". Recursively read the children. |
| 2443 | * |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2444 | * NOTE: The code here must match put_node(). |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2445 | * |
| 2446 | * Returns the index follosing the siblings. |
| 2447 | * Returns -1 if the file is shorter than expected. |
| 2448 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2449 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2450 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2451 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2452 | FILE *fd; |
| 2453 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2454 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2455 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2456 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2457 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 2458 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2459 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2460 | int len; |
| 2461 | int i; |
| 2462 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2463 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2464 | int c; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2465 | int c2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2466 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2467 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2468 | len = getc(fd); /* <siblingcount> */ |
| 2469 | if (len <= 0) |
| 2470 | return -1; |
| 2471 | |
| 2472 | if (startidx + len >= maxidx) |
| 2473 | return -2; |
| 2474 | byts[idx++] = len; |
| 2475 | |
| 2476 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 2477 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2478 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2479 | c = getc(fd); /* <byte> */ |
| 2480 | if (c < 0) |
| 2481 | return -1; |
| 2482 | if (c <= BY_SPECIAL) |
| 2483 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2484 | if (c == BY_NOFLAGS && !prefixtree) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2485 | { |
| 2486 | /* No flags, all regions. */ |
| 2487 | idxs[idx] = 0; |
| 2488 | c = 0; |
| 2489 | } |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2490 | else if (c != BY_INDEX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2491 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2492 | if (prefixtree) |
| 2493 | { |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 2494 | /* Read the optional pflags byte, the prefix ID and the |
| 2495 | * condition nr. In idxs[] store the prefix ID in the low |
| 2496 | * byte, the condition index shifted up 8 bits, the flags |
| 2497 | * shifted up 24 bits. */ |
| 2498 | if (c == BY_FLAGS) |
| 2499 | c = getc(fd) << 24; /* <pflags> */ |
| 2500 | else |
| 2501 | c = 0; |
| 2502 | |
| 2503 | c |= getc(fd); /* <prefixID> */ |
| 2504 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2505 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 2506 | if (n >= maxprefcondnr) |
| 2507 | return -2; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 2508 | c |= (n << 8); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2509 | } |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2510 | else /* c must be BY_FLAGS or BY_FLAGS2 */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2511 | { |
| 2512 | /* Read flags and optional region and prefix ID. In |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2513 | * idxs[] the flags go in the low two bytes, region above |
| 2514 | * that and prefix ID above the region. */ |
| 2515 | c2 = c; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2516 | c = getc(fd); /* <flags> */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2517 | if (c2 == BY_FLAGS2) |
| 2518 | c = (getc(fd) << 8) + c; /* <flags2> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2519 | if (c & WF_REGION) |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2520 | c = (getc(fd) << 16) + c; /* <region> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2521 | if (c & WF_PFX) |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 2522 | c = (getc(fd) << 24) + c; /* <prefixID> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2525 | idxs[idx] = c; |
| 2526 | c = 0; |
| 2527 | } |
| 2528 | else /* c == BY_INDEX */ |
| 2529 | { |
| 2530 | /* <nodeidx> */ |
| 2531 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2532 | if (n < 0 || n >= maxidx) |
| 2533 | return -2; |
| 2534 | idxs[idx] = n + SHARED_MASK; |
| 2535 | c = getc(fd); /* <xbyte> */ |
| 2536 | } |
| 2537 | } |
| 2538 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2541 | /* Recursively read the children for non-shared siblings. |
| 2542 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 2543 | * remove SHARED_MASK) */ |
| 2544 | for (i = 1; i <= len; ++i) |
| 2545 | if (byts[startidx + i] != 0) |
| 2546 | { |
| 2547 | if (idxs[startidx + i] & SHARED_MASK) |
| 2548 | idxs[startidx + i] &= ~SHARED_MASK; |
| 2549 | else |
| 2550 | { |
| 2551 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2552 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 2553 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2554 | if (idx < 0) |
| 2555 | break; |
| 2556 | } |
| 2557 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2558 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2559 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * Parse 'spelllang' and set buf->b_langp accordingly. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2564 | * Returns NULL if it's OK, an error message otherwise. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2565 | */ |
| 2566 | char_u * |
| 2567 | did_set_spelllang(buf) |
| 2568 | buf_T *buf; |
| 2569 | { |
| 2570 | garray_T ga; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2571 | char_u *splp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2572 | char_u *region; |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 2573 | char_u region_cp[3]; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2574 | int filename; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2575 | int region_mask; |
| 2576 | slang_T *lp; |
| 2577 | int c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2578 | char_u lang[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2579 | char_u spf_name[MAXPATHL]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2580 | int len; |
| 2581 | char_u *p; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2582 | int round; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2583 | char_u *spf; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2584 | char_u *use_region = NULL; |
| 2585 | int dont_use_region = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2586 | |
| 2587 | ga_init2(&ga, sizeof(langp_T), 2); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2588 | clear_midword(buf); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2589 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2590 | /* loop over comma separated language names. */ |
| 2591 | for (splp = buf->b_p_spl; *splp != NUL; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2592 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2593 | /* Get one language name. */ |
| 2594 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 2595 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2596 | region = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2597 | len = STRLEN(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2598 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2599 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 2600 | * If there is a region name let "region" point to it and remove it |
| 2601 | * from the name. */ |
| 2602 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 2603 | { |
| 2604 | filename = TRUE; |
| 2605 | |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 2606 | /* Locate a region and remove it from the file name. */ |
| 2607 | p = vim_strchr(gettail(lang), '_'); |
| 2608 | if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2]) |
| 2609 | && !ASCII_ISALPHA(p[3])) |
| 2610 | { |
| 2611 | vim_strncpy(region_cp, p + 1, 2); |
| 2612 | mch_memmove(p, p + 3, len - (p - lang) - 2); |
| 2613 | len -= 3; |
| 2614 | region = region_cp; |
| 2615 | } |
| 2616 | else |
| 2617 | dont_use_region = TRUE; |
| 2618 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2619 | /* Check if we loaded this language before. */ |
| 2620 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2621 | if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME) |
| 2622 | break; |
| 2623 | } |
| 2624 | else |
| 2625 | { |
| 2626 | filename = FALSE; |
| 2627 | if (len > 3 && lang[len - 3] == '_') |
| 2628 | { |
| 2629 | region = lang + len - 2; |
| 2630 | len -= 3; |
| 2631 | lang[len] = NUL; |
| 2632 | } |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2633 | else |
| 2634 | dont_use_region = TRUE; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2635 | |
| 2636 | /* Check if we loaded this language before. */ |
| 2637 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2638 | if (STRICMP(lang, lp->sl_name) == 0) |
| 2639 | break; |
| 2640 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2641 | |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 2642 | if (region != NULL) |
| 2643 | { |
| 2644 | /* If the region differs from what was used before then don't |
| 2645 | * use it for 'spellfile'. */ |
| 2646 | if (use_region != NULL && STRCMP(region, use_region) != 0) |
| 2647 | dont_use_region = TRUE; |
| 2648 | use_region = region; |
| 2649 | } |
| 2650 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2651 | /* If not found try loading the language now. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2652 | if (lp == NULL) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2653 | { |
| 2654 | if (filename) |
| 2655 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 2656 | else |
| 2657 | spell_load_lang(lang); |
| 2658 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2659 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2660 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2661 | * Loop over the languages, there can be several files for "lang". |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2662 | */ |
| 2663 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2664 | if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME |
| 2665 | : STRICMP(lang, lp->sl_name) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2666 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2667 | region_mask = REGION_ALL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2668 | if (!filename && region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2669 | { |
| 2670 | /* find region in sl_regions */ |
| 2671 | c = find_region(lp->sl_regions, region); |
| 2672 | if (c == REGION_ALL) |
| 2673 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2674 | if (lp->sl_add) |
| 2675 | { |
| 2676 | if (*lp->sl_regions != NUL) |
| 2677 | /* This addition file is for other regions. */ |
| 2678 | region_mask = 0; |
| 2679 | } |
| 2680 | else |
| 2681 | /* This is probably an error. Give a warning and |
| 2682 | * accept the words anyway. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2683 | smsg((char_u *) |
| 2684 | _("Warning: region %s not supported"), |
| 2685 | region); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2686 | } |
| 2687 | else |
| 2688 | region_mask = 1 << c; |
| 2689 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2690 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2691 | if (region_mask != 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2692 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2693 | if (ga_grow(&ga, 1) == FAIL) |
| 2694 | { |
| 2695 | ga_clear(&ga); |
| 2696 | return e_outofmem; |
| 2697 | } |
| 2698 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2699 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2700 | ++ga.ga_len; |
| 2701 | use_midword(lp, buf); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2702 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2703 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2706 | /* round 0: load int_wordlist, if possible. |
| 2707 | * round 1: load first name in 'spellfile'. |
| 2708 | * round 2: load second name in 'spellfile. |
| 2709 | * etc. */ |
| 2710 | spf = curbuf->b_p_spf; |
| 2711 | for (round = 0; round == 0 || *spf != NUL; ++round) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2712 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2713 | if (round == 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2714 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2715 | /* Internal wordlist, if there is one. */ |
| 2716 | if (int_wordlist == NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2717 | continue; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2718 | int_wordlist_spl(spf_name); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2719 | } |
| 2720 | else |
| 2721 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2722 | /* One entry in 'spellfile'. */ |
| 2723 | copy_option_part(&spf, spf_name, MAXPATHL - 5, ","); |
| 2724 | STRCAT(spf_name, ".spl"); |
| 2725 | |
| 2726 | /* If it was already found above then skip it. */ |
| 2727 | for (c = 0; c < ga.ga_len; ++c) |
| 2728 | if (fullpathcmp(spf_name, |
| 2729 | LANGP_ENTRY(ga, c)->lp_slang->sl_fname, |
| 2730 | FALSE) == FPC_SAME) |
| 2731 | break; |
| 2732 | if (c < ga.ga_len) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2733 | continue; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2736 | /* Check if it was loaded already. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2737 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2738 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2739 | break; |
| 2740 | if (lp == NULL) |
| 2741 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2742 | /* Not loaded, try loading it now. The language name includes the |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2743 | * region name, the region is ignored otherwise. for int_wordlist |
| 2744 | * use an arbitrary name. */ |
| 2745 | if (round == 0) |
| 2746 | STRCPY(lang, "internal wordlist"); |
| 2747 | else |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2748 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2749 | vim_strncpy(lang, gettail(spf_name), MAXWLEN); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2750 | p = vim_strchr(lang, '.'); |
| 2751 | if (p != NULL) |
| 2752 | *p = NUL; /* truncate at ".encoding.add" */ |
| 2753 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2754 | lp = spell_load_file(spf_name, lang, NULL, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2755 | } |
| 2756 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2757 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2758 | region_mask = REGION_ALL; |
| 2759 | if (use_region != NULL && !dont_use_region) |
| 2760 | { |
| 2761 | /* find region in sl_regions */ |
| 2762 | c = find_region(lp->sl_regions, use_region); |
| 2763 | if (c != REGION_ALL) |
| 2764 | region_mask = 1 << c; |
| 2765 | else if (*lp->sl_regions != NUL) |
| 2766 | /* This spell file is for other regions. */ |
| 2767 | region_mask = 0; |
| 2768 | } |
| 2769 | |
| 2770 | if (region_mask != 0) |
| 2771 | { |
| 2772 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2773 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2774 | ++ga.ga_len; |
| 2775 | use_midword(lp, buf); |
| 2776 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2780 | /* Add a NULL entry to mark the end of the list. */ |
| 2781 | if (ga_grow(&ga, 1) == FAIL) |
| 2782 | { |
| 2783 | ga_clear(&ga); |
| 2784 | return e_outofmem; |
| 2785 | } |
| 2786 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2787 | ++ga.ga_len; |
| 2788 | |
| 2789 | /* Everything is fine, store the new b_langp value. */ |
| 2790 | ga_clear(&buf->b_langp); |
| 2791 | buf->b_langp = ga; |
| 2792 | |
| 2793 | return NULL; |
| 2794 | } |
| 2795 | |
| 2796 | /* |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2797 | * Clear the midword characters for buffer "buf". |
| 2798 | */ |
| 2799 | static void |
| 2800 | clear_midword(buf) |
| 2801 | buf_T *buf; |
| 2802 | { |
| 2803 | vim_memset(buf->b_spell_ismw, 0, 256); |
| 2804 | #ifdef FEAT_MBYTE |
| 2805 | vim_free(buf->b_spell_ismw_mb); |
| 2806 | buf->b_spell_ismw_mb = NULL; |
| 2807 | #endif |
| 2808 | } |
| 2809 | |
| 2810 | /* |
| 2811 | * Use the "sl_midword" field of language "lp" for buffer "buf". |
| 2812 | * They add up to any currently used midword characters. |
| 2813 | */ |
| 2814 | static void |
| 2815 | use_midword(lp, buf) |
| 2816 | slang_T *lp; |
| 2817 | buf_T *buf; |
| 2818 | { |
| 2819 | char_u *p; |
| 2820 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2821 | if (lp->sl_midword == NULL) /* there aren't any */ |
| 2822 | return; |
| 2823 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2824 | for (p = lp->sl_midword; *p != NUL; ) |
| 2825 | #ifdef FEAT_MBYTE |
| 2826 | if (has_mbyte) |
| 2827 | { |
| 2828 | int c, l, n; |
| 2829 | char_u *bp; |
| 2830 | |
| 2831 | c = mb_ptr2char(p); |
| 2832 | l = mb_ptr2len_check(p); |
| 2833 | if (c < 256) |
| 2834 | buf->b_spell_ismw[c] = TRUE; |
| 2835 | else if (buf->b_spell_ismw_mb == NULL) |
| 2836 | /* First multi-byte char in "b_spell_ismw_mb". */ |
| 2837 | buf->b_spell_ismw_mb = vim_strnsave(p, l); |
| 2838 | else |
| 2839 | { |
| 2840 | /* Append multi-byte chars to "b_spell_ismw_mb". */ |
| 2841 | n = STRLEN(buf->b_spell_ismw_mb); |
| 2842 | bp = vim_strnsave(buf->b_spell_ismw_mb, n + l); |
| 2843 | if (bp != NULL) |
| 2844 | { |
| 2845 | vim_free(buf->b_spell_ismw_mb); |
| 2846 | buf->b_spell_ismw_mb = bp; |
| 2847 | vim_strncpy(bp + n, p, l); |
| 2848 | } |
| 2849 | } |
| 2850 | p += l; |
| 2851 | } |
| 2852 | else |
| 2853 | #endif |
| 2854 | buf->b_spell_ismw[*p++] = TRUE; |
| 2855 | } |
| 2856 | |
| 2857 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2858 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2859 | * Each region is simply stored as the two characters of it's name. |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2860 | * Returns the index if found (first is 0), REGION_ALL if not found. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2861 | */ |
| 2862 | static int |
| 2863 | find_region(rp, region) |
| 2864 | char_u *rp; |
| 2865 | char_u *region; |
| 2866 | { |
| 2867 | int i; |
| 2868 | |
| 2869 | for (i = 0; ; i += 2) |
| 2870 | { |
| 2871 | if (rp[i] == NUL) |
| 2872 | return REGION_ALL; |
| 2873 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2874 | break; |
| 2875 | } |
| 2876 | return i / 2; |
| 2877 | } |
| 2878 | |
| 2879 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2880 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2881 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2882 | * Word WF_ONECAP |
| 2883 | * W WORD WF_ALLCAP |
| 2884 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2885 | */ |
| 2886 | static int |
| 2887 | captype(word, end) |
| 2888 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2889 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2890 | { |
| 2891 | char_u *p; |
| 2892 | int c; |
| 2893 | int firstcap; |
| 2894 | int allcap; |
| 2895 | int past_second = FALSE; /* past second word char */ |
| 2896 | |
| 2897 | /* find first letter */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2898 | for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2899 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2900 | return 0; /* only non-word characters, illegal word */ |
| 2901 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2902 | if (has_mbyte) |
| 2903 | c = mb_ptr2char_adv(&p); |
| 2904 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2905 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2906 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2907 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2908 | |
| 2909 | /* |
| 2910 | * Need to check all letters to find a word with mixed upper/lower. |
| 2911 | * But a word with an upper char only at start is a ONECAP. |
| 2912 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2913 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2914 | if (spell_iswordp_nmw(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2915 | { |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 2916 | c = PTR2CHAR(p); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2917 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2918 | { |
| 2919 | /* UUl -> KEEPCAP */ |
| 2920 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2921 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2922 | allcap = FALSE; |
| 2923 | } |
| 2924 | else if (!allcap) |
| 2925 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2926 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2927 | past_second = TRUE; |
| 2928 | } |
| 2929 | |
| 2930 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2931 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2932 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2933 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2934 | return 0; |
| 2935 | } |
| 2936 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2937 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 2938 | /* |
| 2939 | * Free all languages. |
| 2940 | */ |
| 2941 | void |
| 2942 | spell_free_all() |
| 2943 | { |
| 2944 | slang_T *lp; |
| 2945 | buf_T *buf; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2946 | char_u fname[MAXPATHL]; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2947 | |
| 2948 | /* Go through all buffers and handle 'spelllang'. */ |
| 2949 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2950 | ga_clear(&buf->b_langp); |
| 2951 | |
| 2952 | while (first_lang != NULL) |
| 2953 | { |
| 2954 | lp = first_lang; |
| 2955 | first_lang = lp->sl_next; |
| 2956 | slang_free(lp); |
| 2957 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2958 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2959 | if (int_wordlist != NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2960 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2961 | /* Delete the internal wordlist and its .spl file */ |
| 2962 | mch_remove(int_wordlist); |
| 2963 | int_wordlist_spl(fname); |
| 2964 | mch_remove(fname); |
| 2965 | vim_free(int_wordlist); |
| 2966 | int_wordlist = NULL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2969 | init_spell_chartab(); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2970 | } |
| 2971 | # endif |
| 2972 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2973 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2974 | /* |
| 2975 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2976 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2977 | */ |
| 2978 | void |
| 2979 | spell_reload() |
| 2980 | { |
| 2981 | buf_T *buf; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2982 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2983 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2984 | /* Initialize the table for spell_iswordp(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2985 | init_spell_chartab(); |
| 2986 | |
| 2987 | /* Unload all allocated memory. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2988 | spell_free_all(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2989 | |
| 2990 | /* Go through all buffers and handle 'spelllang'. */ |
| 2991 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2992 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2993 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2994 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2995 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2996 | { |
| 2997 | FOR_ALL_WINDOWS(wp) |
| 2998 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2999 | { |
| 3000 | (void)did_set_spelllang(buf); |
| 3001 | # ifdef FEAT_WINDOWS |
| 3002 | break; |
| 3003 | # endif |
| 3004 | } |
| 3005 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3006 | } |
| 3007 | } |
| 3008 | # endif |
| 3009 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3010 | /* |
| 3011 | * Reload the spell file "fname" if it's loaded. |
| 3012 | */ |
| 3013 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3014 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3015 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3016 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3017 | { |
| 3018 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3019 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3020 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3021 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 3022 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 3023 | { |
| 3024 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3025 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3026 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3027 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3028 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3029 | |
| 3030 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 3031 | * 'spelllang' to get it loaded. */ |
| 3032 | if (added_word && !didit) |
| 3033 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3037 | /* |
| 3038 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3039 | */ |
| 3040 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3041 | #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] | 3042 | and .dic file. */ |
| 3043 | /* |
| 3044 | * Main structure to store the contents of a ".aff" file. |
| 3045 | */ |
| 3046 | typedef struct afffile_S |
| 3047 | { |
| 3048 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3049 | int af_rar; /* RAR ID for rare word */ |
| 3050 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3051 | int af_bad; /* BAD ID for banned word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3052 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3053 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 3054 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3055 | } afffile_T; |
| 3056 | |
| 3057 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3058 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 3059 | struct affentry_S |
| 3060 | { |
| 3061 | affentry_T *ae_next; /* next affix with same name/number */ |
| 3062 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 3063 | 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] | 3064 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 3065 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3066 | int ae_rare; /* rare affix */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3067 | }; |
| 3068 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3069 | #define AH_KEY_LEN 10 |
| 3070 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3071 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 3072 | typedef struct affheader_S |
| 3073 | { |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3074 | /* key for hashtable == name of affix entry */ |
| 3075 | #ifdef FEAT_MBYTE |
| 3076 | char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */ |
| 3077 | #else |
| 3078 | char_u ah_key[2]; /* one byte char plus NUL */ |
| 3079 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3080 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3081 | int ah_combine; /* suffix may combine with prefix */ |
| 3082 | affentry_T *ah_first; /* first affix entry */ |
| 3083 | } affheader_T; |
| 3084 | |
| 3085 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 3086 | |
| 3087 | /* |
| 3088 | * Structure that is used to store the items in the word tree. This avoids |
| 3089 | * the need to keep track of each allocated thing, it's freed all at once |
| 3090 | * after ":mkspell" is done. |
| 3091 | */ |
| 3092 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 3093 | typedef struct sblock_S sblock_T; |
| 3094 | struct sblock_S |
| 3095 | { |
| 3096 | sblock_T *sb_next; /* next block in list */ |
| 3097 | int sb_used; /* nr of bytes already in use */ |
| 3098 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3099 | }; |
| 3100 | |
| 3101 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3102 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3103 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3104 | typedef struct wordnode_S wordnode_T; |
| 3105 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3106 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3107 | union /* shared to save space */ |
| 3108 | { |
| 3109 | char_u hashkey[6]; /* room for the hash key */ |
| 3110 | int index; /* index in written nodes (valid after first |
| 3111 | round) */ |
| 3112 | } wn_u1; |
| 3113 | union /* shared to save space */ |
| 3114 | { |
| 3115 | wordnode_T *next; /* next node with same hash key */ |
| 3116 | wordnode_T *wnode; /* parent node that will write this node */ |
| 3117 | } wn_u2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3118 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 3119 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 3120 | always sorted) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3121 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 3122 | short_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3123 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 3124 | PREFIXTREE it's the prefcondnr */ |
| 3125 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3126 | }; |
| 3127 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 3128 | #define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */ |
| 3129 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3130 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3131 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3132 | /* |
| 3133 | * Info used while reading the spell files. |
| 3134 | */ |
| 3135 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3136 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3137 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3138 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3139 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3140 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3141 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3142 | sblock_T *si_blocks; /* memory blocks used */ |
| 3143 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3144 | int si_add; /* addition file */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3145 | int si_clear_chartab; /* when TRUE clear char tables */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3146 | int si_region; /* region mask */ |
| 3147 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3148 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3149 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3150 | int si_region_count; /* number of regions supported (1 when there |
| 3151 | are no regions) */ |
| 3152 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3153 | |
| 3154 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 3155 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3156 | char_u *si_sofofr; /* SOFOFROM text */ |
| 3157 | char_u *si_sofoto; /* SOFOTO text */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3158 | int si_followup; /* soundsalike: ? */ |
| 3159 | int si_collapse; /* soundsalike: ? */ |
| 3160 | int si_rem_accents; /* soundsalike: remove accents */ |
| 3161 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3162 | char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3163 | garray_T si_prefcond; /* table with conditions for postponed |
| 3164 | * prefixes, each stored as a string */ |
| 3165 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3166 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3167 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3168 | 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] | 3169 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3170 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 3171 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3172 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3173 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 3174 | 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] | 3175 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 3176 | 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] | 3177 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 3178 | static void *getroom __ARGS((sblock_T **blp, size_t len, int align)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3179 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 3180 | static void free_blocks __ARGS((sblock_T *bl)); |
| 3181 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3182 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 3183 | 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] | 3184 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3185 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 3186 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3187 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3188 | static void clear_node __ARGS((wordnode_T *node)); |
| 3189 | 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] | 3190 | 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] | 3191 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3192 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3193 | /* In the postponed prefixes tree wn_flags is used to store the WFP_ flags, |
| 3194 | * but it must be negative to indicate the prefix tree to tree_add_word(). |
| 3195 | * Use a negative number with the lower 8 bits zero. */ |
| 3196 | #define PFX_FLAGS -256 |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 3197 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3198 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3199 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3200 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3201 | */ |
| 3202 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3203 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3204 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3205 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3206 | { |
| 3207 | FILE *fd; |
| 3208 | afffile_T *aff; |
| 3209 | char_u rline[MAXLINELEN]; |
| 3210 | char_u *line; |
| 3211 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3212 | #define MAXITEMCNT 7 |
| 3213 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3214 | int itemcnt; |
| 3215 | char_u *p; |
| 3216 | int lnum = 0; |
| 3217 | affheader_T *cur_aff = NULL; |
| 3218 | int aff_todo = 0; |
| 3219 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3220 | char_u *low = NULL; |
| 3221 | char_u *fol = NULL; |
| 3222 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3223 | 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] | 3224 | int do_rep; |
| 3225 | int do_sal; |
| 3226 | int do_map; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3227 | int do_midword; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3228 | int do_sofo; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3229 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3230 | hashitem_T *hi; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3231 | int l; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3232 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3233 | /* |
| 3234 | * Open the file. |
| 3235 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3236 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3237 | if (fd == NULL) |
| 3238 | { |
| 3239 | EMSG2(_(e_notopen), fname); |
| 3240 | return NULL; |
| 3241 | } |
| 3242 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3243 | if (spin->si_verbose || p_verbose > 2) |
| 3244 | { |
| 3245 | if (!spin->si_verbose) |
| 3246 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3247 | smsg((char_u *)_("Reading affix file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3248 | out_flush(); |
| 3249 | if (!spin->si_verbose) |
| 3250 | verbose_leave(); |
| 3251 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3252 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3253 | /* Only do REP lines when not done in another .aff file already. */ |
| 3254 | do_rep = spin->si_rep.ga_len == 0; |
| 3255 | |
| 3256 | /* Only do SAL lines when not done in another .aff file already. */ |
| 3257 | do_sal = spin->si_sal.ga_len == 0; |
| 3258 | |
| 3259 | /* Only do MAP lines when not done in another .aff file already. */ |
| 3260 | do_map = spin->si_map.ga_len == 0; |
| 3261 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3262 | /* Only do MIDWORD line when not done in another .aff file already */ |
| 3263 | do_midword = spin->si_midword == NULL; |
| 3264 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3265 | /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */ |
| 3266 | do_sofo = spin->si_sofofr == NULL; |
| 3267 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3268 | /* |
| 3269 | * Allocate and init the afffile_T structure. |
| 3270 | */ |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 3271 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T), TRUE); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3272 | if (aff == NULL) |
| 3273 | return NULL; |
| 3274 | hash_init(&aff->af_pref); |
| 3275 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3276 | |
| 3277 | /* |
| 3278 | * Read all the lines in the file one by one. |
| 3279 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3280 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3281 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3282 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3283 | ++lnum; |
| 3284 | |
| 3285 | /* Skip comment lines. */ |
| 3286 | if (*rline == '#') |
| 3287 | continue; |
| 3288 | |
| 3289 | /* Convert from "SET" to 'encoding' when needed. */ |
| 3290 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3291 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3292 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3293 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3294 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3295 | if (pc == NULL) |
| 3296 | { |
| 3297 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3298 | fname, lnum, rline); |
| 3299 | continue; |
| 3300 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3301 | line = pc; |
| 3302 | } |
| 3303 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3304 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3305 | { |
| 3306 | pc = NULL; |
| 3307 | line = rline; |
| 3308 | } |
| 3309 | |
| 3310 | /* Split the line up in white separated items. Put a NUL after each |
| 3311 | * item. */ |
| 3312 | itemcnt = 0; |
| 3313 | for (p = line; ; ) |
| 3314 | { |
| 3315 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 3316 | ++p; |
| 3317 | if (*p == NUL) |
| 3318 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3319 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3320 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3321 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3322 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3323 | ++p; |
| 3324 | if (*p == NUL) |
| 3325 | break; |
| 3326 | *p++ = NUL; |
| 3327 | } |
| 3328 | |
| 3329 | /* Handle non-empty lines. */ |
| 3330 | if (itemcnt > 0) |
| 3331 | { |
| 3332 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 3333 | && aff->af_enc == NULL) |
| 3334 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3335 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3336 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 3337 | aff->af_enc = enc_canonize(items[1]); |
| 3338 | if (aff->af_enc != NULL && !spin->si_ascii |
| 3339 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 3340 | p_enc) == FAIL) |
| 3341 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 3342 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3343 | spin->si_conv.vc_fail = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3344 | #else |
| 3345 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3346 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3347 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3348 | else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2) |
| 3349 | { |
| 3350 | if (do_midword) |
| 3351 | spin->si_midword = vim_strsave(items[1]); |
| 3352 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3353 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 3354 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3355 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3356 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3357 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3358 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3359 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3360 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3361 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 3362 | && aff->af_rar == 0) |
| 3363 | { |
| 3364 | aff->af_rar = items[1][0]; |
| 3365 | if (items[1][1] != NUL) |
| 3366 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3367 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3368 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 3369 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3370 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3371 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3372 | if (items[1][1] != NUL) |
| 3373 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3374 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3375 | else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2 |
| 3376 | && aff->af_bad == 0) |
| 3377 | { |
| 3378 | aff->af_bad = items[1][0]; |
| 3379 | if (items[1][1] != NUL) |
| 3380 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3381 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3382 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 3383 | { |
| 3384 | aff->af_pfxpostpone = TRUE; |
| 3385 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3386 | else if ((STRCMP(items[0], "PFX") == 0 |
| 3387 | || STRCMP(items[0], "SFX") == 0) |
| 3388 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3389 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3390 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3391 | /* Myspell allows extra text after the item, but that might |
| 3392 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 3393 | if (itemcnt > 4 && *items[4] != '#') |
| 3394 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 3395 | fname, lnum, items[4]); |
| 3396 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3397 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3398 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 3399 | sizeof(affheader_T), TRUE); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3400 | if (cur_aff == NULL) |
| 3401 | break; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3402 | #ifdef FEAT_MBYTE |
| 3403 | if (has_mbyte) |
| 3404 | { |
| 3405 | l = mb_ptr2len_check(items[1]); |
| 3406 | if (l >= AH_KEY_LEN) |
| 3407 | l = 1; /* too long, must be an overlong sequence */ |
| 3408 | else |
| 3409 | mch_memmove(cur_aff->ah_key, items[1], l); |
| 3410 | } |
| 3411 | else |
| 3412 | #endif |
| 3413 | { |
| 3414 | *cur_aff->ah_key = *items[1]; |
| 3415 | l = 1; |
| 3416 | } |
| 3417 | cur_aff->ah_key[l] = NUL; |
| 3418 | if (items[1][l] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3419 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3420 | if (*items[2] == 'Y') |
| 3421 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3422 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3423 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 3424 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3425 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3426 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3427 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3428 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3429 | /* Use a new number in the .spl file later, to be able to |
| 3430 | * handle multiple .aff files. */ |
| 3431 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3432 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3433 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3434 | else |
| 3435 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3436 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3437 | hi = hash_find(tp, cur_aff->ah_key); |
| 3438 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3439 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3440 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 3441 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3442 | aff_todo = 0; |
| 3443 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3444 | else |
| 3445 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3446 | } |
| 3447 | else if ((STRCMP(items[0], "PFX") == 0 |
| 3448 | || STRCMP(items[0], "SFX") == 0) |
| 3449 | && aff_todo > 0 |
| 3450 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3451 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3452 | { |
| 3453 | affentry_T *aff_entry; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3454 | int rare = FALSE; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3455 | int upper = FALSE; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3456 | int lasti = 5; |
| 3457 | |
| 3458 | /* Check for "rare" after the other info. */ |
| 3459 | if (itemcnt > 5 && STRICMP(items[5], "rare") == 0) |
| 3460 | { |
| 3461 | rare = TRUE; |
| 3462 | lasti = 6; |
| 3463 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3464 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3465 | /* Myspell allows extra text after the item, but that might |
| 3466 | * mean mistakes go unnoticed. Require a comment-starter. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3467 | if (itemcnt > lasti && *items[lasti] != '#') |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3468 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3469 | fname, lnum, items[lasti]); |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3470 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3471 | /* New item for an affix letter. */ |
| 3472 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3473 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 3474 | sizeof(affentry_T), TRUE); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3475 | if (aff_entry == NULL) |
| 3476 | break; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3477 | aff_entry->ae_rare = rare; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3478 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3479 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3480 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 3481 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3482 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3483 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 3484 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3485 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3486 | /* Don't use an affix entry with non-ASCII characters when |
| 3487 | * "spin->si_ascii" is TRUE. */ |
| 3488 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3489 | || has_non_ascii(aff_entry->ae_add))) |
| 3490 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3491 | aff_entry->ae_next = cur_aff->ah_first; |
| 3492 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3493 | |
| 3494 | if (STRCMP(items[4], ".") != 0) |
| 3495 | { |
| 3496 | char_u buf[MAXLINELEN]; |
| 3497 | |
| 3498 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 3499 | items[4]); |
| 3500 | if (*items[0] == 'P') |
| 3501 | sprintf((char *)buf, "^%s", items[4]); |
| 3502 | else |
| 3503 | sprintf((char *)buf, "%s$", items[4]); |
| 3504 | aff_entry->ae_prog = vim_regcomp(buf, |
| 3505 | RE_MAGIC + RE_STRING); |
| 3506 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3507 | |
| 3508 | /* For postponed prefixes we need an entry in si_prefcond |
| 3509 | * for the condition. Use an existing one if possible. */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3510 | if (*items[0] == 'P' && aff->af_pfxpostpone) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3511 | { |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3512 | /* When the chop string is one lower-case letter and |
| 3513 | * the add string ends in the upper-case letter we set |
| 3514 | * the "upper" flag, clear "ae_chop" and remove the |
| 3515 | * letters from "ae_add". The condition must either |
| 3516 | * be empty or start with the same letter. */ |
| 3517 | if (aff_entry->ae_chop != NULL |
| 3518 | && aff_entry->ae_add != NULL |
| 3519 | #ifdef FEAT_MBYTE |
| 3520 | && aff_entry->ae_chop[mb_ptr2len_check( |
| 3521 | aff_entry->ae_chop)] == NUL |
| 3522 | #else |
| 3523 | && aff_entry->ae_chop[1] == NUL |
| 3524 | #endif |
| 3525 | ) |
| 3526 | { |
| 3527 | int c, c_up; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3528 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3529 | c = PTR2CHAR(aff_entry->ae_chop); |
| 3530 | c_up = SPELL_TOUPPER(c); |
| 3531 | if (c_up != c |
| 3532 | && (aff_entry->ae_cond == NULL |
| 3533 | || PTR2CHAR(aff_entry->ae_cond) == c)) |
| 3534 | { |
| 3535 | p = aff_entry->ae_add |
| 3536 | + STRLEN(aff_entry->ae_add); |
| 3537 | mb_ptr_back(aff_entry->ae_add, p); |
| 3538 | if (PTR2CHAR(p) == c_up) |
| 3539 | { |
| 3540 | upper = TRUE; |
| 3541 | aff_entry->ae_chop = NULL; |
| 3542 | *p = NUL; |
| 3543 | |
| 3544 | /* The condition is matched with the |
| 3545 | * actual word, thus must check for the |
| 3546 | * upper-case letter. */ |
| 3547 | if (aff_entry->ae_cond != NULL) |
| 3548 | { |
| 3549 | char_u buf[MAXLINELEN]; |
| 3550 | #ifdef FEAT_MBYTE |
| 3551 | if (has_mbyte) |
| 3552 | { |
| 3553 | onecap_copy(items[4], buf, TRUE); |
| 3554 | aff_entry->ae_cond = getroom_save( |
| 3555 | &spin->si_blocks, buf); |
| 3556 | } |
| 3557 | else |
| 3558 | #endif |
| 3559 | *aff_entry->ae_cond = c_up; |
| 3560 | if (aff_entry->ae_cond != NULL) |
| 3561 | { |
| 3562 | sprintf((char *)buf, "^%s", |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3563 | aff_entry->ae_cond); |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3564 | vim_free(aff_entry->ae_prog); |
| 3565 | aff_entry->ae_prog = vim_regcomp( |
| 3566 | buf, RE_MAGIC + RE_STRING); |
| 3567 | } |
| 3568 | } |
| 3569 | } |
| 3570 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3573 | if (aff_entry->ae_chop == NULL) |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 3574 | { |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3575 | int idx; |
| 3576 | char_u **pp; |
| 3577 | int n; |
| 3578 | |
| 3579 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 3580 | --idx) |
| 3581 | { |
| 3582 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 3583 | if (str_equal(p, aff_entry->ae_cond)) |
| 3584 | break; |
| 3585 | } |
| 3586 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 3587 | { |
| 3588 | /* Not found, add a new condition. */ |
| 3589 | idx = spin->si_prefcond.ga_len++; |
| 3590 | pp = ((char_u **)spin->si_prefcond.ga_data) |
| 3591 | + idx; |
| 3592 | if (aff_entry->ae_cond == NULL) |
| 3593 | *pp = NULL; |
| 3594 | else |
| 3595 | *pp = getroom_save(&spin->si_blocks, |
| 3596 | aff_entry->ae_cond); |
| 3597 | } |
| 3598 | |
| 3599 | /* Add the prefix to the prefix tree. */ |
| 3600 | if (aff_entry->ae_add == NULL) |
| 3601 | p = (char_u *)""; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 3602 | else |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3603 | p = aff_entry->ae_add; |
| 3604 | /* PFX_FLAGS is a negative number, so that |
| 3605 | * tree_add_word() knows this is the prefix tree. */ |
| 3606 | n = PFX_FLAGS; |
| 3607 | if (rare) |
| 3608 | n |= WFP_RARE; |
| 3609 | if (!cur_aff->ah_combine) |
| 3610 | n |= WFP_NC; |
| 3611 | if (upper) |
| 3612 | n |= WFP_UP; |
| 3613 | tree_add_word(p, spin->si_prefroot, n, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3614 | idx, cur_aff->ah_newID, &spin->si_blocks); |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3615 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3616 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3617 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3618 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3619 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 3620 | { |
| 3621 | if (fol != NULL) |
| 3622 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 3623 | fname, lnum); |
| 3624 | else |
| 3625 | fol = vim_strsave(items[1]); |
| 3626 | } |
| 3627 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 3628 | { |
| 3629 | if (low != NULL) |
| 3630 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 3631 | fname, lnum); |
| 3632 | else |
| 3633 | low = vim_strsave(items[1]); |
| 3634 | } |
| 3635 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 3636 | { |
| 3637 | if (upp != NULL) |
| 3638 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 3639 | fname, lnum); |
| 3640 | else |
| 3641 | upp = vim_strsave(items[1]); |
| 3642 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3643 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3644 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3645 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3646 | if (!isdigit(*items[1])) |
| 3647 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 3648 | fname, lnum); |
| 3649 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3650 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 3651 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3652 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3653 | if (do_rep) |
| 3654 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 3655 | } |
| 3656 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 3657 | { |
| 3658 | /* MAP item or count */ |
| 3659 | if (!found_map) |
| 3660 | { |
| 3661 | /* First line contains the count. */ |
| 3662 | found_map = TRUE; |
| 3663 | if (!isdigit(*items[1])) |
| 3664 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 3665 | fname, lnum); |
| 3666 | } |
| 3667 | else if (do_map) |
| 3668 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3669 | int c; |
| 3670 | |
| 3671 | /* Check that every character appears only once. */ |
| 3672 | for (p = items[1]; *p != NUL; ) |
| 3673 | { |
| 3674 | #ifdef FEAT_MBYTE |
| 3675 | c = mb_ptr2char_adv(&p); |
| 3676 | #else |
| 3677 | c = *p++; |
| 3678 | #endif |
| 3679 | if ((spin->si_map.ga_len > 0 |
| 3680 | && vim_strchr(spin->si_map.ga_data, c) |
| 3681 | != NULL) |
| 3682 | || vim_strchr(p, c) != NULL) |
| 3683 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 3684 | fname, lnum); |
| 3685 | } |
| 3686 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3687 | /* We simply concatenate all the MAP strings, separated by |
| 3688 | * slashes. */ |
| 3689 | ga_concat(&spin->si_map, items[1]); |
| 3690 | ga_append(&spin->si_map, '/'); |
| 3691 | } |
| 3692 | } |
| 3693 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 3694 | { |
| 3695 | if (do_sal) |
| 3696 | { |
| 3697 | /* SAL item (sounds-a-like) |
| 3698 | * Either one of the known keys or a from-to pair. */ |
| 3699 | if (STRCMP(items[1], "followup") == 0) |
| 3700 | spin->si_followup = sal_to_bool(items[2]); |
| 3701 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 3702 | spin->si_collapse = sal_to_bool(items[2]); |
| 3703 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 3704 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 3705 | else |
| 3706 | /* when "to" is "_" it means empty */ |
| 3707 | add_fromto(spin, &spin->si_sal, items[1], |
| 3708 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 3709 | : items[2]); |
| 3710 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3711 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3712 | else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2 |
| 3713 | && (!do_sofo || spin->si_sofofr == NULL)) |
| 3714 | { |
| 3715 | if (do_sofo) |
| 3716 | spin->si_sofofr = vim_strsave(items[1]); |
| 3717 | } |
| 3718 | else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2 |
| 3719 | && (!do_sofo || spin->si_sofoto == NULL)) |
| 3720 | { |
| 3721 | if (do_sofo) |
| 3722 | spin->si_sofoto = vim_strsave(items[1]); |
| 3723 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3724 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3725 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 3726 | fname, lnum, items[0]); |
| 3727 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3730 | if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL)) |
| 3731 | smsg((char_u *)_("Missing SOFO%s line in %s"), |
| 3732 | spin->si_sofofr == NULL ? "FROM" : "TO", fname); |
| 3733 | if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0) |
| 3734 | smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname); |
| 3735 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3736 | if (fol != NULL || low != NULL || upp != NULL) |
| 3737 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3738 | if (spin->si_clear_chartab) |
| 3739 | { |
| 3740 | /* Clear the char type tables, don't want to use any of the |
| 3741 | * currently used spell properties. */ |
| 3742 | init_spell_chartab(); |
| 3743 | spin->si_clear_chartab = FALSE; |
| 3744 | } |
| 3745 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3746 | /* |
| 3747 | * Don't write a word table for an ASCII file, so that we don't check |
| 3748 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3749 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3750 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 3751 | */ |
| 3752 | if (!spin->si_ascii |
| 3753 | #ifdef FEAT_MBYTE |
| 3754 | && !enc_utf8 |
| 3755 | #endif |
| 3756 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3757 | { |
| 3758 | if (fol == NULL || low == NULL || upp == NULL) |
| 3759 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 3760 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3761 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3762 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3763 | |
| 3764 | vim_free(fol); |
| 3765 | vim_free(low); |
| 3766 | vim_free(upp); |
| 3767 | } |
| 3768 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3769 | vim_free(pc); |
| 3770 | fclose(fd); |
| 3771 | return aff; |
| 3772 | } |
| 3773 | |
| 3774 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3775 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 3776 | * NULL as equal. |
| 3777 | */ |
| 3778 | static int |
| 3779 | str_equal(s1, s2) |
| 3780 | char_u *s1; |
| 3781 | char_u *s2; |
| 3782 | { |
| 3783 | if (s1 == NULL || s2 == NULL) |
| 3784 | return s1 == s2; |
| 3785 | return STRCMP(s1, s2) == 0; |
| 3786 | } |
| 3787 | |
| 3788 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3789 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 3790 | * They are stored case-folded. |
| 3791 | */ |
| 3792 | static void |
| 3793 | add_fromto(spin, gap, from, to) |
| 3794 | spellinfo_T *spin; |
| 3795 | garray_T *gap; |
| 3796 | char_u *from; |
| 3797 | char_u *to; |
| 3798 | { |
| 3799 | fromto_T *ftp; |
| 3800 | char_u word[MAXWLEN]; |
| 3801 | |
| 3802 | if (ga_grow(gap, 1) == OK) |
| 3803 | { |
| 3804 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 3805 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 3806 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 3807 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 3808 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 3809 | ++gap->ga_len; |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | /* |
| 3814 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 3815 | */ |
| 3816 | static int |
| 3817 | sal_to_bool(s) |
| 3818 | char_u *s; |
| 3819 | { |
| 3820 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 3821 | } |
| 3822 | |
| 3823 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3824 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 3825 | * When "s" is NULL FALSE is returned. |
| 3826 | */ |
| 3827 | static int |
| 3828 | has_non_ascii(s) |
| 3829 | char_u *s; |
| 3830 | { |
| 3831 | char_u *p; |
| 3832 | |
| 3833 | if (s != NULL) |
| 3834 | for (p = s; *p != NUL; ++p) |
| 3835 | if (*p >= 128) |
| 3836 | return TRUE; |
| 3837 | return FALSE; |
| 3838 | } |
| 3839 | |
| 3840 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3841 | * Free the structure filled by spell_read_aff(). |
| 3842 | */ |
| 3843 | static void |
| 3844 | spell_free_aff(aff) |
| 3845 | afffile_T *aff; |
| 3846 | { |
| 3847 | hashtab_T *ht; |
| 3848 | hashitem_T *hi; |
| 3849 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3850 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3851 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3852 | |
| 3853 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3854 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3855 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3856 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 3857 | { |
| 3858 | todo = ht->ht_used; |
| 3859 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3860 | { |
| 3861 | if (!HASHITEM_EMPTY(hi)) |
| 3862 | { |
| 3863 | --todo; |
| 3864 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3865 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3866 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | if (ht == &aff->af_suff) |
| 3870 | break; |
| 3871 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3872 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3873 | hash_clear(&aff->af_pref); |
| 3874 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3878 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3879 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3880 | */ |
| 3881 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3882 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3883 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3884 | spellinfo_T *spin; |
| 3885 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3886 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3887 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3888 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3889 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3890 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3891 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3892 | char_u *pc; |
| 3893 | char_u *w; |
| 3894 | int l; |
| 3895 | hash_T hash; |
| 3896 | hashitem_T *hi; |
| 3897 | FILE *fd; |
| 3898 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3899 | int non_ascii = 0; |
| 3900 | int retval = OK; |
| 3901 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3902 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3903 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3904 | /* |
| 3905 | * Open the file. |
| 3906 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3907 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3908 | if (fd == NULL) |
| 3909 | { |
| 3910 | EMSG2(_(e_notopen), fname); |
| 3911 | return FAIL; |
| 3912 | } |
| 3913 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3914 | /* The hashtable is only used to detect duplicated words. */ |
| 3915 | hash_init(&ht); |
| 3916 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3917 | spin->si_foldwcount = 0; |
| 3918 | spin->si_keepwcount = 0; |
| 3919 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3920 | if (spin->si_verbose || p_verbose > 2) |
| 3921 | { |
| 3922 | if (!spin->si_verbose) |
| 3923 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3924 | smsg((char_u *)_("Reading dictionary file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3925 | out_flush(); |
| 3926 | if (!spin->si_verbose) |
| 3927 | verbose_leave(); |
| 3928 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3929 | |
| 3930 | /* Read and ignore the first line: word count. */ |
| 3931 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3932 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3933 | EMSG2(_("E760: No word count in %s"), fname); |
| 3934 | |
| 3935 | /* |
| 3936 | * Read all the lines in the file one by one. |
| 3937 | * The words are converted to 'encoding' here, before being added to |
| 3938 | * the hashtable. |
| 3939 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3940 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3941 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3942 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3943 | ++lnum; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 3944 | if (line[0] == '#' || line[0] == '/') |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3945 | continue; /* comment line */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3946 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3947 | /* Remove CR, LF and white space from the end. White space halfway |
| 3948 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3949 | l = STRLEN(line); |
| 3950 | while (l > 0 && line[l - 1] <= ' ') |
| 3951 | --l; |
| 3952 | if (l == 0) |
| 3953 | continue; /* empty line */ |
| 3954 | line[l] = NUL; |
| 3955 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3956 | /* Find the optional affix names. */ |
| 3957 | afflist = vim_strchr(line, '/'); |
| 3958 | if (afflist != NULL) |
| 3959 | *afflist++ = NUL; |
| 3960 | |
| 3961 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3962 | if (spin->si_ascii && has_non_ascii(line)) |
| 3963 | { |
| 3964 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3965 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3966 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3967 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3968 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3969 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3970 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3971 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3972 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3973 | if (pc == NULL) |
| 3974 | { |
| 3975 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3976 | fname, lnum, line); |
| 3977 | continue; |
| 3978 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3979 | w = pc; |
| 3980 | } |
| 3981 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3982 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3983 | { |
| 3984 | pc = NULL; |
| 3985 | w = line; |
| 3986 | } |
| 3987 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3988 | /* This takes time, print a message now and then. */ |
| 3989 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3990 | { |
| 3991 | vim_snprintf((char *)message, sizeof(message), |
| 3992 | _("line %6d, word %6d - %s"), |
| 3993 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3994 | msg_start(); |
| 3995 | msg_puts_long_attr(message, 0); |
| 3996 | msg_clr_eos(); |
| 3997 | msg_didout = FALSE; |
| 3998 | msg_col = 0; |
| 3999 | out_flush(); |
| 4000 | } |
| 4001 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4002 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 4003 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4004 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4005 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4006 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4007 | if (retval == FAIL) |
| 4008 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4009 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4010 | hash = hash_hash(dw); |
| 4011 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4012 | if (!HASHITEM_EMPTY(hi)) |
| 4013 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4014 | fname, lnum, dw); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4015 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4016 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4017 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4018 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4019 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4020 | if (afflist != NULL) |
| 4021 | { |
| 4022 | /* Check for affix name that stands for keep-case word and stands |
| 4023 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4024 | if (affile->af_kep != NUL |
| 4025 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4026 | flags |= WF_KEEPCAP | WF_FIXCAP; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4027 | if (affile->af_rar != NUL |
| 4028 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 4029 | flags |= WF_RARE; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4030 | if (affile->af_bad != NUL |
| 4031 | && vim_strchr(afflist, affile->af_bad) != NULL) |
| 4032 | flags |= WF_BANNED; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4033 | |
| 4034 | if (affile->af_pfxpostpone) |
| 4035 | /* Need to store the list of prefix IDs with the word. */ |
| 4036 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4039 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4040 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4041 | retval = FAIL; |
| 4042 | |
| 4043 | if (afflist != NULL) |
| 4044 | { |
| 4045 | /* Find all matching suffixes and add the resulting words. |
| 4046 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4047 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4048 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4049 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4050 | retval = FAIL; |
| 4051 | |
| 4052 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4053 | if (store_aff_word(dw, spin, afflist, affile, |
| 4054 | &affile->af_pref, NULL, |
| 4055 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4056 | retval = FAIL; |
| 4057 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4060 | if (spin->si_ascii && non_ascii > 0) |
| 4061 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 4062 | non_ascii); |
| 4063 | hash_clear(&ht); |
| 4064 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4065 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4066 | return retval; |
| 4067 | } |
| 4068 | |
| 4069 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4070 | * Get the list of prefix IDs from the affix list "afflist". |
| 4071 | * Used for PFXPOSTPONE. |
| 4072 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 4073 | * or when out of memory. |
| 4074 | */ |
| 4075 | static char_u * |
| 4076 | get_pfxlist(affile, afflist, blp) |
| 4077 | afffile_T *affile; |
| 4078 | char_u *afflist; |
| 4079 | sblock_T **blp; |
| 4080 | { |
| 4081 | char_u *p; |
| 4082 | int cnt; |
| 4083 | int round; |
| 4084 | char_u *res = NULL; |
| 4085 | char_u key[2]; |
| 4086 | hashitem_T *hi; |
| 4087 | |
| 4088 | key[1] = NUL; |
| 4089 | |
| 4090 | /* round 1: count the number of prefix IDs. |
| 4091 | * round 2: move prefix IDs to "res" */ |
| 4092 | for (round = 1; round <= 2; ++round) |
| 4093 | { |
| 4094 | cnt = 0; |
| 4095 | for (p = afflist; *p != NUL; ++p) |
| 4096 | { |
| 4097 | key[0] = *p; |
| 4098 | hi = hash_find(&affile->af_pref, key); |
| 4099 | if (!HASHITEM_EMPTY(hi)) |
| 4100 | { |
| 4101 | /* This is a prefix ID, use the new number. */ |
| 4102 | if (round == 2) |
| 4103 | res[cnt] = HI2AH(hi)->ah_newID; |
| 4104 | ++cnt; |
| 4105 | } |
| 4106 | } |
| 4107 | if (round == 1 && cnt > 0) |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4108 | res = getroom(blp, cnt + 1, FALSE); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4109 | if (res == NULL) |
| 4110 | break; |
| 4111 | } |
| 4112 | |
| 4113 | if (res != NULL) |
| 4114 | res[cnt] = NUL; |
| 4115 | return res; |
| 4116 | } |
| 4117 | |
| 4118 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4119 | * Apply affixes to a word and store the resulting words. |
| 4120 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 4121 | * prefixes or suffixes. |
| 4122 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 4123 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4124 | * |
| 4125 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4126 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4127 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4128 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4129 | char_u *word; /* basic word start */ |
| 4130 | spellinfo_T *spin; /* spell info */ |
| 4131 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4132 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4133 | hashtab_T *ht; |
| 4134 | hashtab_T *xht; |
| 4135 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4136 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4137 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4138 | { |
| 4139 | int todo; |
| 4140 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4141 | affheader_T *ah; |
| 4142 | affentry_T *ae; |
| 4143 | regmatch_T regmatch; |
| 4144 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4145 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4146 | int i; |
| 4147 | char_u *p; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4148 | int use_flags; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4149 | char_u *use_pfxlist; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4150 | int c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4151 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4152 | todo = ht->ht_used; |
| 4153 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4154 | { |
| 4155 | if (!HASHITEM_EMPTY(hi)) |
| 4156 | { |
| 4157 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4158 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4159 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4160 | /* Check that the affix combines, if required, and that the word |
| 4161 | * supports this affix. */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4162 | c = PTR2CHAR(ah->ah_key); |
| 4163 | if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4164 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4165 | /* Loop over all affix entries with this name. */ |
| 4166 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4167 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4168 | /* Check the condition. It's not logical to match case |
| 4169 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4170 | * Myspell. |
| 4171 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 4172 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4173 | regmatch.regprog = ae->ae_prog; |
| 4174 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4175 | if ((xht != NULL || !affile->af_pfxpostpone |
| 4176 | || ae->ae_chop != NULL) |
| 4177 | && (ae->ae_prog == NULL |
| 4178 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4179 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4180 | /* Match. Remove the chop and add the affix. */ |
| 4181 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4182 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4183 | /* prefix: chop/add at the start of the word */ |
| 4184 | if (ae->ae_add == NULL) |
| 4185 | *newword = NUL; |
| 4186 | else |
| 4187 | STRCPY(newword, ae->ae_add); |
| 4188 | p = word; |
| 4189 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4190 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4191 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4192 | #ifdef FEAT_MBYTE |
| 4193 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4194 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4195 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4196 | for ( ; i > 0; --i) |
| 4197 | mb_ptr_adv(p); |
| 4198 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4199 | else |
| 4200 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4201 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4202 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4203 | STRCAT(newword, p); |
| 4204 | } |
| 4205 | else |
| 4206 | { |
| 4207 | /* suffix: chop/add at the end of the word */ |
| 4208 | STRCPY(newword, word); |
| 4209 | if (ae->ae_chop != NULL) |
| 4210 | { |
| 4211 | /* Remove chop string. */ |
| 4212 | p = newword + STRLEN(newword); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4213 | i = MB_CHARLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4214 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4215 | mb_ptr_back(newword, p); |
| 4216 | *p = NUL; |
| 4217 | } |
| 4218 | if (ae->ae_add != NULL) |
| 4219 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4222 | /* Obey the "rare" flag of the affix. */ |
| 4223 | if (ae->ae_rare) |
| 4224 | use_flags = flags | WF_RARE; |
| 4225 | else |
| 4226 | use_flags = flags; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4227 | use_pfxlist = pfxlist; |
| 4228 | |
| 4229 | /* When there are postponed prefixes... */ |
Bram Moolenaar | 551f84f | 2005-07-06 22:29:20 +0000 | [diff] [blame] | 4230 | if (spin->si_prefroot != NULL |
| 4231 | && spin->si_prefroot->wn_sibling != NULL) |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4232 | { |
| 4233 | /* ... add a flag to indicate an affix was used. */ |
| 4234 | use_flags |= WF_HAS_AFF; |
| 4235 | |
| 4236 | /* ... don't use a prefix list if combining |
| 4237 | * affixes is not allowed */ |
| 4238 | if (!ah->ah_combine || comb) |
| 4239 | use_pfxlist = NULL; |
| 4240 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4241 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4242 | /* Store the modified word. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4243 | if (store_word(newword, spin, use_flags, |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4244 | spin->si_region, use_pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4245 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4246 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4247 | /* When added a suffix and combining is allowed also |
| 4248 | * try adding prefixes additionally. */ |
| 4249 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4250 | if (store_aff_word(newword, spin, afflist, affile, |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4251 | xht, NULL, TRUE, |
| 4252 | use_flags, use_pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4253 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4254 | } |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | } |
| 4259 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4260 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4264 | * Read a file with a list of words. |
| 4265 | */ |
| 4266 | static int |
| 4267 | spell_read_wordfile(fname, spin) |
| 4268 | char_u *fname; |
| 4269 | spellinfo_T *spin; |
| 4270 | { |
| 4271 | FILE *fd; |
| 4272 | long lnum = 0; |
| 4273 | char_u rline[MAXLINELEN]; |
| 4274 | char_u *line; |
| 4275 | char_u *pc = NULL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4276 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4277 | int l; |
| 4278 | int retval = OK; |
| 4279 | int did_word = FALSE; |
| 4280 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4281 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4282 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4283 | |
| 4284 | /* |
| 4285 | * Open the file. |
| 4286 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4287 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4288 | if (fd == NULL) |
| 4289 | { |
| 4290 | EMSG2(_(e_notopen), fname); |
| 4291 | return FAIL; |
| 4292 | } |
| 4293 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4294 | if (spin->si_verbose || p_verbose > 2) |
| 4295 | { |
| 4296 | if (!spin->si_verbose) |
| 4297 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4298 | smsg((char_u *)_("Reading word file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4299 | out_flush(); |
| 4300 | if (!spin->si_verbose) |
| 4301 | verbose_leave(); |
| 4302 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4303 | |
| 4304 | /* |
| 4305 | * Read all the lines in the file one by one. |
| 4306 | */ |
| 4307 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 4308 | { |
| 4309 | line_breakcheck(); |
| 4310 | ++lnum; |
| 4311 | |
| 4312 | /* Skip comment lines. */ |
| 4313 | if (*rline == '#') |
| 4314 | continue; |
| 4315 | |
| 4316 | /* Remove CR, LF and white space from the end. */ |
| 4317 | l = STRLEN(rline); |
| 4318 | while (l > 0 && rline[l - 1] <= ' ') |
| 4319 | --l; |
| 4320 | if (l == 0) |
| 4321 | continue; /* empty or blank line */ |
| 4322 | rline[l] = NUL; |
| 4323 | |
| 4324 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 4325 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4326 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4327 | if (spin->si_conv.vc_type != CONV_NONE) |
| 4328 | { |
| 4329 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 4330 | if (pc == NULL) |
| 4331 | { |
| 4332 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 4333 | fname, lnum, rline); |
| 4334 | continue; |
| 4335 | } |
| 4336 | line = pc; |
| 4337 | } |
| 4338 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4339 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4340 | { |
| 4341 | pc = NULL; |
| 4342 | line = rline; |
| 4343 | } |
| 4344 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4345 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4346 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4347 | ++line; |
| 4348 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4349 | { |
| 4350 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4351 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 4352 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4353 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4354 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 4355 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4356 | else |
| 4357 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4358 | #ifdef FEAT_MBYTE |
| 4359 | char_u *enc; |
| 4360 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4361 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4362 | line += 10; |
| 4363 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4364 | if (enc != NULL && !spin->si_ascii |
| 4365 | && convert_setup(&spin->si_conv, enc, |
| 4366 | p_enc) == FAIL) |
| 4367 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4368 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4369 | vim_free(enc); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4370 | spin->si_conv.vc_fail = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4371 | #else |
| 4372 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 4373 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4374 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4375 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4376 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4377 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4378 | if (STRNCMP(line, "regions=", 8) == 0) |
| 4379 | { |
| 4380 | if (spin->si_region_count > 1) |
| 4381 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 4382 | fname, lnum, line); |
| 4383 | else |
| 4384 | { |
| 4385 | line += 8; |
| 4386 | if (STRLEN(line) > 16) |
| 4387 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 4388 | fname, lnum, line); |
| 4389 | else |
| 4390 | { |
| 4391 | spin->si_region_count = STRLEN(line) / 2; |
| 4392 | STRCPY(spin->si_region_name, line); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4393 | |
| 4394 | /* Adjust the mask for a word valid in all regions. */ |
| 4395 | spin->si_region = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | continue; |
| 4399 | } |
| 4400 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4401 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
| 4402 | fname, lnum, line - 1); |
| 4403 | continue; |
| 4404 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4405 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4406 | flags = 0; |
| 4407 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4408 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4409 | /* Check for flags and region after a slash. */ |
| 4410 | p = vim_strchr(line, '/'); |
| 4411 | if (p != NULL) |
| 4412 | { |
| 4413 | *p++ = NUL; |
| 4414 | while (*p != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4415 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4416 | if (*p == '=') /* keep-case word */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4417 | flags |= WF_KEEPCAP | WF_FIXCAP; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4418 | else if (*p == '!') /* Bad, bad, wicked word. */ |
| 4419 | flags |= WF_BANNED; |
| 4420 | else if (*p == '?') /* Rare word. */ |
| 4421 | flags |= WF_RARE; |
| 4422 | else if (VIM_ISDIGIT(*p)) /* region number(s) */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4423 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4424 | if ((flags & WF_REGION) == 0) /* first one */ |
| 4425 | regionmask = 0; |
| 4426 | flags |= WF_REGION; |
| 4427 | |
| 4428 | l = *p - '0'; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4429 | if (l > spin->si_region_count) |
| 4430 | { |
| 4431 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4432 | fname, lnum, p); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4433 | break; |
| 4434 | } |
| 4435 | regionmask |= 1 << (l - 1); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4436 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4437 | else |
| 4438 | { |
| 4439 | smsg((char_u *)_("Unrecognized flags in %s line %d: %s"), |
| 4440 | fname, lnum, p); |
| 4441 | break; |
| 4442 | } |
| 4443 | ++p; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4444 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 4448 | if (spin->si_ascii && has_non_ascii(line)) |
| 4449 | { |
| 4450 | ++non_ascii; |
| 4451 | continue; |
| 4452 | } |
| 4453 | |
| 4454 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4455 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4456 | { |
| 4457 | retval = FAIL; |
| 4458 | break; |
| 4459 | } |
| 4460 | did_word = TRUE; |
| 4461 | } |
| 4462 | |
| 4463 | vim_free(pc); |
| 4464 | fclose(fd); |
| 4465 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4466 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 4467 | { |
| 4468 | if (p_verbose > 2) |
| 4469 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4470 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 4471 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4472 | if (p_verbose > 2) |
| 4473 | verbose_leave(); |
| 4474 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4475 | return retval; |
| 4476 | } |
| 4477 | |
| 4478 | /* |
| 4479 | * Get part of an sblock_T, "len" bytes long. |
| 4480 | * This avoids calling free() for every little struct we use. |
| 4481 | * The memory is cleared to all zeros. |
| 4482 | * Returns NULL when out of memory. |
| 4483 | */ |
| 4484 | static void * |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4485 | getroom(blp, len, align) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4486 | sblock_T **blp; |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4487 | size_t len; /* length needed */ |
| 4488 | int align; /* align for pointer */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4489 | { |
| 4490 | char_u *p; |
| 4491 | sblock_T *bl = *blp; |
| 4492 | |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4493 | if (align && bl != NULL) |
| 4494 | /* Round size up for alignment. On some systems structures need to be |
| 4495 | * aligned to the size of a pointer (e.g., SPARC). */ |
| 4496 | bl->sb_used = (bl->sb_used + sizeof(char *) - 1) |
| 4497 | & ~(sizeof(char *) - 1); |
| 4498 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4499 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 4500 | { |
| 4501 | /* Allocate a block of memory. This is not freed until much later. */ |
| 4502 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 4503 | if (bl == NULL) |
| 4504 | return NULL; |
| 4505 | bl->sb_next = *blp; |
| 4506 | *blp = bl; |
| 4507 | bl->sb_used = 0; |
| 4508 | } |
| 4509 | |
| 4510 | p = bl->sb_data + bl->sb_used; |
| 4511 | bl->sb_used += len; |
| 4512 | |
| 4513 | return p; |
| 4514 | } |
| 4515 | |
| 4516 | /* |
| 4517 | * Make a copy of a string into memory allocated with getroom(). |
| 4518 | */ |
| 4519 | static char_u * |
| 4520 | getroom_save(blp, s) |
| 4521 | sblock_T **blp; |
| 4522 | char_u *s; |
| 4523 | { |
| 4524 | char_u *sc; |
| 4525 | |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4526 | sc = (char_u *)getroom(blp, STRLEN(s) + 1, FALSE); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4527 | if (sc != NULL) |
| 4528 | STRCPY(sc, s); |
| 4529 | return sc; |
| 4530 | } |
| 4531 | |
| 4532 | |
| 4533 | /* |
| 4534 | * Free the list of allocated sblock_T. |
| 4535 | */ |
| 4536 | static void |
| 4537 | free_blocks(bl) |
| 4538 | sblock_T *bl; |
| 4539 | { |
| 4540 | sblock_T *next; |
| 4541 | |
| 4542 | while (bl != NULL) |
| 4543 | { |
| 4544 | next = bl->sb_next; |
| 4545 | vim_free(bl); |
| 4546 | bl = next; |
| 4547 | } |
| 4548 | } |
| 4549 | |
| 4550 | /* |
| 4551 | * Allocate the root of a word tree. |
| 4552 | */ |
| 4553 | static wordnode_T * |
| 4554 | wordtree_alloc(blp) |
| 4555 | sblock_T **blp; |
| 4556 | { |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4557 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /* |
| 4561 | * Store a word in the tree(s). |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4562 | * Always store it in the case-folded tree. For a keep-case word this is |
| 4563 | * useful when the word can also be used with all caps (no WF_FIXCAP flag) and |
| 4564 | * used to find suggestions. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4565 | * For a keep-case word also store it in the keep-case tree. |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4566 | * When "pfxlist" is not NULL store the word for each postponed prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4567 | */ |
| 4568 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4569 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4570 | char_u *word; |
| 4571 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4572 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4573 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4574 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4575 | { |
| 4576 | int len = STRLEN(word); |
| 4577 | int ct = captype(word, word + len); |
| 4578 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4579 | int res = OK; |
| 4580 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4581 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4582 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4583 | for (p = pfxlist; res == OK; ++p) |
| 4584 | { |
| 4585 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 4586 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4587 | if (p == NULL || *p == NUL) |
| 4588 | break; |
| 4589 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4590 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4591 | |
| 4592 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4593 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4594 | for (p = pfxlist; res == OK; ++p) |
| 4595 | { |
| 4596 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 4597 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4598 | if (p == NULL || *p == NUL) |
| 4599 | break; |
| 4600 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4601 | ++spin->si_keepwcount; |
| 4602 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4603 | return res; |
| 4604 | } |
| 4605 | |
| 4606 | /* |
| 4607 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4608 | * When "flags" < 0 we are adding to the prefix tree where flags is used for |
| 4609 | * "rare" and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4610 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4611 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4612 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4613 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4614 | char_u *word; |
| 4615 | wordnode_T *root; |
| 4616 | int flags; |
| 4617 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4618 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4619 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4620 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4621 | wordnode_T *node = root; |
| 4622 | wordnode_T *np; |
| 4623 | wordnode_T **prev = NULL; |
| 4624 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4625 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4626 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 4627 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4628 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4629 | /* Look for the sibling that has the same character. They are sorted |
| 4630 | * 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] | 4631 | * higher byte value. For zero bytes (end of word) the sorting is |
| 4632 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4633 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4634 | while (node != NULL |
| 4635 | && (node->wn_byte < word[i] |
| 4636 | || (node->wn_byte == NUL |
| 4637 | && (flags < 0 |
| 4638 | ? node->wn_prefixID < prefixID |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4639 | : node->wn_flags < (flags & WN_MASK) |
| 4640 | || (node->wn_flags == (flags & WN_MASK) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4641 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4642 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4643 | prev = &node->wn_sibling; |
| 4644 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4645 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4646 | if (node == NULL |
| 4647 | || node->wn_byte != word[i] |
| 4648 | || (word[i] == NUL |
| 4649 | && (flags < 0 |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4650 | || node->wn_flags != (flags & WN_MASK) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4651 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4652 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4653 | /* Allocate a new node. */ |
Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 4654 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4655 | if (np == NULL) |
| 4656 | return FAIL; |
| 4657 | np->wn_byte = word[i]; |
| 4658 | *prev = np; |
| 4659 | np->wn_sibling = node; |
| 4660 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4661 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4662 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4663 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4664 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4665 | node->wn_flags = flags; |
| 4666 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4667 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4668 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4669 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4670 | prev = &node->wn_child; |
| 4671 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4678 | * Compress a tree: find tails that are identical and can be shared. |
| 4679 | */ |
| 4680 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4681 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4682 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4683 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4684 | { |
| 4685 | hashtab_T ht; |
| 4686 | int n; |
| 4687 | int tot = 0; |
| 4688 | |
| 4689 | if (root != NULL) |
| 4690 | { |
| 4691 | hash_init(&ht); |
| 4692 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4693 | if (spin->si_verbose || p_verbose > 2) |
| 4694 | { |
| 4695 | if (!spin->si_verbose) |
| 4696 | verbose_enter(); |
| 4697 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4698 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4699 | if (p_verbose > 2) |
| 4700 | verbose_leave(); |
| 4701 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4702 | hash_clear(&ht); |
| 4703 | } |
| 4704 | } |
| 4705 | |
| 4706 | /* |
| 4707 | * Compress a node, its siblings and its children, depth first. |
| 4708 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4709 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4710 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4711 | node_compress(node, ht, tot) |
| 4712 | wordnode_T *node; |
| 4713 | hashtab_T *ht; |
| 4714 | int *tot; /* total count of nodes before compressing, |
| 4715 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4716 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4717 | wordnode_T *np; |
| 4718 | wordnode_T *tp; |
| 4719 | wordnode_T *child; |
| 4720 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4721 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4722 | int len = 0; |
| 4723 | unsigned nr, n; |
| 4724 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4725 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4726 | /* |
| 4727 | * Go through the list of siblings. Compress each child and then try |
| 4728 | * finding an identical child to replace it. |
| 4729 | * Note that with "child" we mean not just the node that is pointed to, |
| 4730 | * but the whole list of siblings, of which the node is the first. |
| 4731 | */ |
| 4732 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4733 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4734 | ++len; |
| 4735 | if ((child = np->wn_child) != NULL) |
| 4736 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4737 | /* Compress the child. This fills hashkey. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4738 | compressed += node_compress(child, ht, tot); |
| 4739 | |
| 4740 | /* Try to find an identical child. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4741 | hash = hash_hash(child->wn_u1.hashkey); |
| 4742 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4743 | tp = NULL; |
| 4744 | if (!HASHITEM_EMPTY(hi)) |
| 4745 | { |
| 4746 | /* There are children with an identical hash value. Now check |
| 4747 | * if there is one that is really identical. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4748 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4749 | if (node_equal(child, tp)) |
| 4750 | { |
| 4751 | /* Found one! Now use that child in place of the |
| 4752 | * current one. This means the current child is |
| 4753 | * dropped from the tree. */ |
| 4754 | np->wn_child = tp; |
| 4755 | ++compressed; |
| 4756 | break; |
| 4757 | } |
| 4758 | if (tp == NULL) |
| 4759 | { |
| 4760 | /* No other child with this hash value equals the child of |
| 4761 | * the node, add it to the linked list after the first |
| 4762 | * item. */ |
| 4763 | tp = HI2WN(hi); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4764 | child->wn_u2.next = tp->wn_u2.next; |
| 4765 | tp->wn_u2.next = child; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4766 | } |
| 4767 | } |
| 4768 | else |
| 4769 | /* No other child has this hash value, add it to the |
| 4770 | * hashtable. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4771 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4772 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4773 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4774 | *tot += len; |
| 4775 | |
| 4776 | /* |
| 4777 | * Make a hash key for the node and its siblings, so that we can quickly |
| 4778 | * find a lookalike node. This must be done after compressing the sibling |
| 4779 | * list, otherwise the hash key would become invalid by the compression. |
| 4780 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4781 | node->wn_u1.hashkey[0] = len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4782 | nr = 0; |
| 4783 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4784 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4785 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4786 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 4787 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4788 | else |
| 4789 | /* byte node: use the byte value and the child pointer */ |
| 4790 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 4791 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4792 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4793 | |
| 4794 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 4795 | n = nr & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4796 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4797 | n = (nr >> 8) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4798 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4799 | n = (nr >> 16) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4800 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4801 | n = (nr >> 24) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4802 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 4803 | node->wn_u1.hashkey[5] = NUL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4804 | |
| 4805 | return compressed; |
| 4806 | } |
| 4807 | |
| 4808 | /* |
| 4809 | * Return TRUE when two nodes have identical siblings and children. |
| 4810 | */ |
| 4811 | static int |
| 4812 | node_equal(n1, n2) |
| 4813 | wordnode_T *n1; |
| 4814 | wordnode_T *n2; |
| 4815 | { |
| 4816 | wordnode_T *p1; |
| 4817 | wordnode_T *p2; |
| 4818 | |
| 4819 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 4820 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 4821 | if (p1->wn_byte != p2->wn_byte |
| 4822 | || (p1->wn_byte == NUL |
| 4823 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4824 | || p1->wn_region != p2->wn_region |
| 4825 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4826 | : (p1->wn_child != p2->wn_child))) |
| 4827 | break; |
| 4828 | |
| 4829 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /* |
| 4833 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 4834 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4835 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4836 | put_bytes(fd, nr, len) |
| 4837 | FILE *fd; |
| 4838 | long_u nr; |
| 4839 | int len; |
| 4840 | { |
| 4841 | int i; |
| 4842 | |
| 4843 | for (i = len - 1; i >= 0; --i) |
| 4844 | putc((int)(nr >> (i * 8)), fd); |
| 4845 | } |
| 4846 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4847 | static int |
| 4848 | #ifdef __BORLANDC__ |
| 4849 | _RTLENTRYF |
| 4850 | #endif |
| 4851 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 4852 | |
| 4853 | /* |
| 4854 | * Function given to qsort() to sort the REP items on "from" string. |
| 4855 | */ |
| 4856 | static int |
| 4857 | #ifdef __BORLANDC__ |
| 4858 | _RTLENTRYF |
| 4859 | #endif |
| 4860 | rep_compare(s1, s2) |
| 4861 | const void *s1; |
| 4862 | const void *s2; |
| 4863 | { |
| 4864 | fromto_T *p1 = (fromto_T *)s1; |
| 4865 | fromto_T *p2 = (fromto_T *)s2; |
| 4866 | |
| 4867 | return STRCMP(p1->ft_from, p2->ft_from); |
| 4868 | } |
| 4869 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4870 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4871 | * Write the Vim spell file "fname". |
| 4872 | */ |
| 4873 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4874 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4875 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4876 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4877 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4878 | FILE *fd; |
| 4879 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4880 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4881 | wordnode_T *tree; |
| 4882 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4883 | int i; |
| 4884 | int l; |
| 4885 | garray_T *gap; |
| 4886 | fromto_T *ftp; |
| 4887 | char_u *p; |
| 4888 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4889 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4890 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4891 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4892 | { |
| 4893 | EMSG2(_(e_notopen), fname); |
| 4894 | return; |
| 4895 | } |
| 4896 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4897 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4898 | * <charflagslen> <charflags> |
| 4899 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4900 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4901 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4902 | |
| 4903 | /* <fileID> */ |
| 4904 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 4905 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4906 | |
| 4907 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4908 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4909 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4910 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 4911 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 4912 | (size_t)1, fd); |
| 4913 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4914 | } |
| 4915 | else |
| 4916 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4917 | putc(0, fd); |
| 4918 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4921 | /* |
| 4922 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4923 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 4924 | * 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] | 4925 | * 'encoding'. |
| 4926 | * Also skip this for an .add.spl file, the main spell file must contain |
| 4927 | * the table (avoids that it conflicts). File is shorter too. |
| 4928 | */ |
| 4929 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4930 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4931 | putc(0, fd); |
| 4932 | putc(0, fd); |
| 4933 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4934 | } |
| 4935 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4936 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4937 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4938 | |
| 4939 | if (spin->si_midword == NULL) |
| 4940 | put_bytes(fd, 0L, 2); /* <midwordlen> */ |
| 4941 | else |
| 4942 | { |
| 4943 | i = STRLEN(spin->si_midword); |
| 4944 | put_bytes(fd, (long_u)i, 2); /* <midwordlen> */ |
| 4945 | fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */ |
| 4946 | } |
| 4947 | |
| 4948 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4949 | /* Write the prefix conditions. */ |
| 4950 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 4951 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4952 | /* <SUGGEST> : <repcount> <rep> ... |
| 4953 | * <salflags> <salcount> <sal> ... |
| 4954 | * <maplen> <mapstr> */ |
| 4955 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4956 | /* Sort the REP items. */ |
| 4957 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 4958 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4959 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4960 | /* round 1: REP items |
| 4961 | * round 2: SAL items (unless SOFO is used) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4962 | for (round = 1; round <= 2; ++round) |
| 4963 | { |
| 4964 | if (round == 1) |
| 4965 | gap = &spin->si_rep; |
| 4966 | else |
| 4967 | { |
| 4968 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4969 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4970 | i = 0; |
| 4971 | if (spin->si_followup) |
| 4972 | i |= SAL_F0LLOWUP; |
| 4973 | if (spin->si_collapse) |
| 4974 | i |= SAL_COLLAPSE; |
| 4975 | if (spin->si_rem_accents) |
| 4976 | i |= SAL_REM_ACCENTS; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4977 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 4978 | i |= SAL_SOFO; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4979 | putc(i, fd); /* <salflags> */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4980 | if (i & SAL_SOFO) |
| 4981 | break; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 4985 | for (i = 0; i < gap->ga_len; ++i) |
| 4986 | { |
| 4987 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 4988 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 4989 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 4990 | for (rr = 1; rr <= 2; ++rr) |
| 4991 | { |
| 4992 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 4993 | l = STRLEN(p); |
| 4994 | putc(l, fd); |
| 4995 | fwrite(p, l, (size_t)1, fd); |
| 4996 | } |
| 4997 | } |
| 4998 | } |
| 4999 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5000 | /* SOFOFROM and SOFOTO */ |
| 5001 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 5002 | { |
| 5003 | put_bytes(fd, 1L, 2); /* <salcount> */ |
| 5004 | |
| 5005 | l = STRLEN(spin->si_sofofr); |
| 5006 | put_bytes(fd, (long_u)l, 2); /* <salfromlen> */ |
| 5007 | fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */ |
| 5008 | |
| 5009 | l = STRLEN(spin->si_sofoto); |
| 5010 | put_bytes(fd, (long_u)l, 2); /* <saltolen> */ |
| 5011 | fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */ |
| 5012 | } |
| 5013 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5014 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 5015 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 5016 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 5017 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5018 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5019 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5020 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5021 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5022 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5023 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5024 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5025 | if (round == 1) |
| 5026 | tree = spin->si_foldroot; |
| 5027 | else if (round == 2) |
| 5028 | tree = spin->si_keeproot; |
| 5029 | else |
| 5030 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5031 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5032 | /* Clear the index and wnode fields in the tree. */ |
| 5033 | clear_node(tree); |
| 5034 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5035 | /* Count the number of nodes. Needed to be able to allocate the |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5036 | * memory when reading the nodes. Also fills in index for shared |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5037 | * nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5038 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5039 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5040 | /* number of nodes in 4 bytes */ |
| 5041 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5042 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5043 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5044 | /* Write the nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5045 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5048 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5052 | * Clear the index and wnode fields of "node", it siblings and its |
| 5053 | * children. This is needed because they are a union with other items to save |
| 5054 | * space. |
| 5055 | */ |
| 5056 | static void |
| 5057 | clear_node(node) |
| 5058 | wordnode_T *node; |
| 5059 | { |
| 5060 | wordnode_T *np; |
| 5061 | |
| 5062 | if (node != NULL) |
| 5063 | for (np = node; np != NULL; np = np->wn_sibling) |
| 5064 | { |
| 5065 | np->wn_u1.index = 0; |
| 5066 | np->wn_u2.wnode = NULL; |
| 5067 | |
| 5068 | if (np->wn_byte != NUL) |
| 5069 | clear_node(np->wn_child); |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | |
| 5074 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5075 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5076 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5077 | * This first writes the list of possible bytes (siblings). Then for each |
| 5078 | * byte recursively write the children. |
| 5079 | * |
| 5080 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 5081 | * are made about the indexes (so that we don't have to write them in the |
| 5082 | * file). |
| 5083 | * |
| 5084 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5085 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5086 | static int |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5087 | put_node(fd, node, index, regionmask, prefixtree) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5088 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5089 | wordnode_T *node; |
| 5090 | int index; |
| 5091 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5092 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5093 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5094 | int newindex = index; |
| 5095 | int siblingcount = 0; |
| 5096 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5097 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5098 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5099 | /* If "node" is zero the tree is empty. */ |
| 5100 | if (node == NULL) |
| 5101 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5102 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5103 | /* Store the index where this node is written. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5104 | node->wn_u1.index = index; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5105 | |
| 5106 | /* Count the number of siblings. */ |
| 5107 | for (np = node; np != NULL; np = np->wn_sibling) |
| 5108 | ++siblingcount; |
| 5109 | |
| 5110 | /* Write the sibling count. */ |
| 5111 | if (fd != NULL) |
| 5112 | putc(siblingcount, fd); /* <siblingcount> */ |
| 5113 | |
| 5114 | /* Write each sibling byte and optionally extra info. */ |
| 5115 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5116 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5117 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 5118 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5119 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5120 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5121 | /* For a NUL byte (end of word) write the flags etc. */ |
| 5122 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 5123 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5124 | /* In PREFIXTREE write the required prefixID and the |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5125 | * associated condition nr (stored in wn_region). The |
| 5126 | * byte value is misused to store the "rare" and "not |
| 5127 | * combining" flags */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5128 | if (np->wn_flags == (short_u)PFX_FLAGS) |
| 5129 | putc(BY_NOFLAGS, fd); /* <byte> */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5130 | else |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5131 | { |
| 5132 | putc(BY_FLAGS, fd); /* <byte> */ |
| 5133 | putc(np->wn_flags, fd); /* <pflags> */ |
| 5134 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5135 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 5136 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5137 | } |
| 5138 | else |
| 5139 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5140 | /* For word trees we write the flag/region items. */ |
| 5141 | flags = np->wn_flags; |
| 5142 | if (regionmask != 0 && np->wn_region != regionmask) |
| 5143 | flags |= WF_REGION; |
| 5144 | if (np->wn_prefixID != 0) |
| 5145 | flags |= WF_PFX; |
| 5146 | if (flags == 0) |
| 5147 | { |
| 5148 | /* word without flags or region */ |
| 5149 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 5150 | } |
| 5151 | else |
| 5152 | { |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5153 | if (np->wn_flags >= 0x100) |
| 5154 | { |
| 5155 | putc(BY_FLAGS2, fd); /* <byte> */ |
| 5156 | putc(flags, fd); /* <flags> */ |
| 5157 | putc((unsigned)flags >> 8, fd); /* <flags2> */ |
| 5158 | } |
| 5159 | else |
| 5160 | { |
| 5161 | putc(BY_FLAGS, fd); /* <byte> */ |
| 5162 | putc(flags, fd); /* <flags> */ |
| 5163 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5164 | if (flags & WF_REGION) |
| 5165 | putc(np->wn_region, fd); /* <region> */ |
| 5166 | if (flags & WF_PFX) |
| 5167 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 5168 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 5169 | } |
| 5170 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 5171 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5172 | else |
| 5173 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5174 | if (np->wn_child->wn_u1.index != 0 |
| 5175 | && np->wn_child->wn_u2.wnode != node) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5176 | { |
| 5177 | /* The child is written elsewhere, write the reference. */ |
| 5178 | if (fd != NULL) |
| 5179 | { |
| 5180 | putc(BY_INDEX, fd); /* <byte> */ |
| 5181 | /* <nodeidx> */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5182 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5183 | } |
| 5184 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5185 | else if (np->wn_child->wn_u2.wnode == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5186 | /* We will write the child below and give it an index. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5187 | np->wn_child->wn_u2.wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5188 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5189 | if (fd != NULL) |
| 5190 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 5191 | { |
| 5192 | EMSG(_(e_write)); |
| 5193 | return 0; |
| 5194 | } |
| 5195 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5196 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5197 | |
| 5198 | /* Space used in the array when reading: one for each sibling and one for |
| 5199 | * the count. */ |
| 5200 | newindex += siblingcount + 1; |
| 5201 | |
| 5202 | /* Recursively dump the children of each sibling. */ |
| 5203 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5204 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 5205 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5206 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5207 | |
| 5208 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | |
| 5212 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5213 | * ":mkspell [-ascii] outfile infile ..." |
| 5214 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5215 | */ |
| 5216 | void |
| 5217 | ex_mkspell(eap) |
| 5218 | exarg_T *eap; |
| 5219 | { |
| 5220 | int fcount; |
| 5221 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5222 | char_u *arg = eap->arg; |
| 5223 | int ascii = FALSE; |
| 5224 | |
| 5225 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 5226 | { |
| 5227 | ascii = TRUE; |
| 5228 | arg = skipwhite(arg + 6); |
| 5229 | } |
| 5230 | |
| 5231 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 5232 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 5233 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5234 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5235 | FreeWild(fcount, fnames); |
| 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | /* |
| 5240 | * Create a Vim spell file from one or more word lists. |
| 5241 | * "fnames[0]" is the output file name. |
| 5242 | * "fnames[fcount - 1]" is the last input file name. |
| 5243 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 5244 | * and ".spl" is appended to make the output file name. |
| 5245 | */ |
| 5246 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5247 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5248 | int fcount; |
| 5249 | char_u **fnames; |
| 5250 | int ascii; /* -ascii argument given */ |
| 5251 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5252 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5253 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5254 | char_u fname[MAXPATHL]; |
| 5255 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5256 | char_u **innames; |
| 5257 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5258 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5259 | int i; |
| 5260 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5261 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5262 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5263 | spellinfo_T spin; |
| 5264 | |
| 5265 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5266 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5267 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5268 | spin.si_followup = TRUE; |
| 5269 | spin.si_rem_accents = TRUE; |
| 5270 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 5271 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 5272 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5273 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5274 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5275 | /* default: fnames[0] is output file, following are input files */ |
| 5276 | innames = &fnames[1]; |
| 5277 | incount = fcount - 1; |
| 5278 | |
| 5279 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5280 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5281 | len = STRLEN(fnames[0]); |
| 5282 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 5283 | { |
| 5284 | /* For ":mkspell path/en.latin1.add" output file is |
| 5285 | * "path/en.latin1.add.spl". */ |
| 5286 | innames = &fnames[0]; |
| 5287 | incount = 1; |
| 5288 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 5289 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5290 | else if (fcount == 1) |
| 5291 | { |
| 5292 | /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */ |
| 5293 | innames = &fnames[0]; |
| 5294 | incount = 1; |
| 5295 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 5296 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 5297 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5298 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 5299 | { |
| 5300 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5301 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5302 | } |
| 5303 | else |
| 5304 | /* Name should be language, make the file name from it. */ |
| 5305 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 5306 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 5307 | |
| 5308 | /* Check for .ascii.spl. */ |
| 5309 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 5310 | spin.si_ascii = TRUE; |
| 5311 | |
| 5312 | /* Check for .add.spl. */ |
| 5313 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 5314 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5317 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5318 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5319 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 5320 | EMSG(_("E751: Output file name must not have region name")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5321 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5322 | EMSG(_("E754: Only up to 8 regions supported")); |
| 5323 | else |
| 5324 | { |
| 5325 | /* Check for overwriting before doing things that may take a lot of |
| 5326 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5327 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5328 | { |
| 5329 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5330 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5331 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5332 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5333 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5334 | EMSG2(_(e_isadir2), wfname); |
| 5335 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | /* |
| 5339 | * Init the aff and dic pointers. |
| 5340 | * Get the region names if there are more than 2 arguments. |
| 5341 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5342 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5343 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5344 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5345 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5346 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5347 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5348 | len = STRLEN(innames[i]); |
| 5349 | if (STRLEN(gettail(innames[i])) < 5 |
| 5350 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5351 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5352 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 5353 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5354 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5355 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 5356 | spin.si_region_name[i * 2 + 1] = |
| 5357 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5360 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5361 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5362 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 5363 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5364 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 5365 | if (spin.si_foldroot == NULL |
| 5366 | || spin.si_keeproot == NULL |
| 5367 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5368 | { |
| 5369 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5370 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5373 | /* When not producing a .add.spl file clear the character table when |
| 5374 | * we encounter one in the .aff file. This means we dump the current |
| 5375 | * one in the .spl file if the .aff file doesn't define one. That's |
| 5376 | * better than guessing the contents, the table will match a |
| 5377 | * previously loaded spell file. */ |
| 5378 | if (!spin.si_add) |
| 5379 | spin.si_clear_chartab = TRUE; |
| 5380 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5381 | /* |
| 5382 | * Read all the .aff and .dic files. |
| 5383 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5384 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5385 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5386 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5387 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5388 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5389 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5390 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5391 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5392 | if (mch_stat((char *)fname, &st) >= 0) |
| 5393 | { |
| 5394 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 5395 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5396 | afile[i] = spell_read_aff(fname, &spin); |
| 5397 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5398 | error = TRUE; |
| 5399 | else |
| 5400 | { |
| 5401 | /* Read the .dic file and store the words in the trees. */ |
| 5402 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5403 | innames[i]); |
| 5404 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5405 | error = TRUE; |
| 5406 | } |
| 5407 | } |
| 5408 | else |
| 5409 | { |
| 5410 | /* No .aff file, try reading the file as a word list. Store |
| 5411 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5412 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5413 | error = TRUE; |
| 5414 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5415 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5416 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5417 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5418 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5419 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5422 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5423 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5424 | /* |
| 5425 | * Remove the dummy NUL from the start of the tree root. |
| 5426 | */ |
| 5427 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 5428 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5429 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5430 | |
| 5431 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5432 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5433 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5434 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5435 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5436 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5437 | verbose_enter(); |
| 5438 | MSG(_("Compressing word tree...")); |
| 5439 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5440 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5441 | verbose_leave(); |
| 5442 | } |
| 5443 | wordtree_compress(spin.si_foldroot, &spin); |
| 5444 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5445 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5448 | if (!error) |
| 5449 | { |
| 5450 | /* |
| 5451 | * Write the info in the spell file. |
| 5452 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5453 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5454 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5455 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5456 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5457 | smsg((char_u *)_("Writing spell file %s ..."), wfname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5458 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5459 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5460 | verbose_leave(); |
| 5461 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5462 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5463 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5464 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5465 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5466 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5467 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5468 | verbose_enter(); |
| 5469 | MSG(_("Done!")); |
| 5470 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5471 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5472 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5473 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5474 | verbose_leave(); |
| 5475 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5476 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5477 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5478 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5482 | ga_clear(&spin.si_rep); |
| 5483 | ga_clear(&spin.si_sal); |
| 5484 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5485 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5486 | vim_free(spin.si_midword); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5487 | vim_free(spin.si_sofofr); |
| 5488 | vim_free(spin.si_sofoto); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5489 | |
| 5490 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5491 | for (i = 0; i < incount; ++i) |
| 5492 | if (afile[i] != NULL) |
| 5493 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5494 | |
| 5495 | /* Free all the bits and pieces at once. */ |
| 5496 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5497 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5500 | |
| 5501 | /* |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5502 | * ":[count]spellgood {word}" |
| 5503 | * ":[count]spellwrong {word}" |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5504 | */ |
| 5505 | void |
| 5506 | ex_spell(eap) |
| 5507 | exarg_T *eap; |
| 5508 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5509 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong, |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5510 | eap->forceit ? 0 : (int)eap->line2); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | /* |
| 5514 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 5515 | */ |
| 5516 | void |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5517 | spell_add_word(word, len, bad, index) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5518 | char_u *word; |
| 5519 | int len; |
| 5520 | int bad; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5521 | int index; /* "zG" and "zW": zero, otherwise index in |
| 5522 | 'spellfile' */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5523 | { |
| 5524 | FILE *fd; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5525 | buf_T *buf = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5526 | int new_spf = FALSE; |
| 5527 | struct stat st; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5528 | char_u *fname; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5529 | char_u fnamebuf[MAXPATHL]; |
| 5530 | char_u line[MAXWLEN * 2]; |
| 5531 | long fpos, fpos_next = 0; |
| 5532 | int i; |
| 5533 | char_u *spf; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5534 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5535 | if (index == 0) /* use internal wordlist */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5536 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5537 | if (int_wordlist == NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5538 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5539 | int_wordlist = vim_tempname('s'); |
| 5540 | if (int_wordlist == NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5541 | return; |
| 5542 | } |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5543 | fname = int_wordlist; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5544 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5545 | else |
| 5546 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5547 | /* If 'spellfile' isn't set figure out a good default value. */ |
| 5548 | if (*curbuf->b_p_spf == NUL) |
| 5549 | { |
| 5550 | init_spellfile(); |
| 5551 | new_spf = TRUE; |
| 5552 | } |
| 5553 | |
| 5554 | if (*curbuf->b_p_spf == NUL) |
| 5555 | { |
| 5556 | EMSG(_("E764: 'spellfile' is not set")); |
| 5557 | return; |
| 5558 | } |
| 5559 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5560 | for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i) |
| 5561 | { |
| 5562 | copy_option_part(&spf, fnamebuf, MAXPATHL, ","); |
| 5563 | if (i == index) |
| 5564 | break; |
| 5565 | if (*spf == NUL) |
| 5566 | { |
| 5567 | EMSGN(_("E765: 'spellfile' does not have %ld enties"), index); |
| 5568 | return; |
| 5569 | } |
| 5570 | } |
| 5571 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5572 | /* Check that the user isn't editing the .add file somewhere. */ |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5573 | buf = buflist_findname_exp(fnamebuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5574 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 5575 | buf = NULL; |
| 5576 | if (buf != NULL && bufIsChanged(buf)) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5577 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5578 | EMSG(_(e_bufloaded)); |
| 5579 | return; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5580 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5581 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5582 | fname = fnamebuf; |
| 5583 | } |
| 5584 | |
| 5585 | if (bad) |
| 5586 | { |
| 5587 | /* When the word also appears as good word we need to remove that one, |
| 5588 | * since its flags sort before the one with WF_BANNED. */ |
| 5589 | fd = mch_fopen((char *)fname, "r"); |
| 5590 | if (fd != NULL) |
| 5591 | { |
| 5592 | while (!vim_fgets(line, MAXWLEN * 2, fd)) |
| 5593 | { |
| 5594 | fpos = fpos_next; |
| 5595 | fpos_next = ftell(fd); |
| 5596 | if (STRNCMP(word, line, len) == 0 |
| 5597 | && (line[len] == '/' || line[len] < ' ')) |
| 5598 | { |
| 5599 | /* Found duplicate word. Remove it by writing a '#' at |
| 5600 | * the start of the line. Mixing reading and writing |
| 5601 | * doesn't work for all systems, close the file first. */ |
| 5602 | fclose(fd); |
| 5603 | fd = mch_fopen((char *)fname, "r+"); |
| 5604 | if (fd == NULL) |
| 5605 | break; |
| 5606 | if (fseek(fd, fpos, SEEK_SET) == 0) |
| 5607 | fputc('#', fd); |
| 5608 | fseek(fd, fpos_next, SEEK_SET); |
| 5609 | } |
| 5610 | } |
| 5611 | fclose(fd); |
| 5612 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | fd = mch_fopen((char *)fname, "a"); |
| 5616 | if (fd == NULL && new_spf) |
| 5617 | { |
| 5618 | /* We just initialized the 'spellfile' option and can't open the file. |
| 5619 | * We may need to create the "spell" directory first. We already |
| 5620 | * checked the runtime directory is writable in init_spellfile(). */ |
| 5621 | STRCPY(NameBuff, fname); |
| 5622 | *gettail_sep(NameBuff) = NUL; |
| 5623 | if (mch_stat((char *)NameBuff, &st) < 0) |
| 5624 | { |
| 5625 | /* The directory doesn't exist. Try creating it and opening the |
| 5626 | * file again. */ |
| 5627 | vim_mkdir(NameBuff, 0755); |
| 5628 | fd = mch_fopen((char *)fname, "a"); |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | if (fd == NULL) |
| 5633 | EMSG2(_(e_notopen), fname); |
| 5634 | else |
| 5635 | { |
| 5636 | if (bad) |
| 5637 | fprintf(fd, "%.*s/!\n", len, word); |
| 5638 | else |
| 5639 | fprintf(fd, "%.*s\n", len, word); |
| 5640 | fclose(fd); |
| 5641 | |
| 5642 | /* Update the .add.spl file. */ |
| 5643 | mkspell(1, &fname, FALSE, TRUE, TRUE); |
| 5644 | |
| 5645 | /* If the .add file is edited somewhere, reload it. */ |
| 5646 | if (buf != NULL) |
| 5647 | buf_reload(buf); |
| 5648 | |
| 5649 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | /* |
| 5654 | * Initialize 'spellfile' for the current buffer. |
| 5655 | */ |
| 5656 | static void |
| 5657 | init_spellfile() |
| 5658 | { |
| 5659 | char_u buf[MAXPATHL]; |
| 5660 | int l; |
| 5661 | slang_T *sl; |
| 5662 | char_u *rtp; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5663 | char_u *lend; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5664 | |
| 5665 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 5666 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5667 | /* Find the end of the language name. Exclude the region. */ |
| 5668 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 5669 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| 5670 | ; |
| 5671 | |
| 5672 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 5673 | * are allowed to write. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5674 | rtp = p_rtp; |
| 5675 | while (*rtp != NUL) |
| 5676 | { |
| 5677 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 5678 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 5679 | if (filewritable(buf) == 2) |
| 5680 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5681 | /* Use the first language name from 'spelllang' and the |
| 5682 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5683 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 5684 | l = STRLEN(buf); |
| 5685 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5686 | "/spell/%.*s.%s.add", |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5687 | (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5688 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 5689 | ? (char_u *)"ascii" : spell_enc()); |
| 5690 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 5691 | break; |
| 5692 | } |
| 5693 | } |
| 5694 | } |
| 5695 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5696 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5697 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5698 | /* |
| 5699 | * Init the chartab used for spelling for ASCII. |
| 5700 | * EBCDIC is not supported! |
| 5701 | */ |
| 5702 | static void |
| 5703 | clear_spell_chartab(sp) |
| 5704 | spelltab_T *sp; |
| 5705 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5706 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5707 | |
| 5708 | /* Init everything to FALSE. */ |
| 5709 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 5710 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 5711 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5712 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5713 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5714 | sp->st_upper[i] = i; |
| 5715 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5716 | |
| 5717 | /* We include digits. A word shouldn't start with a digit, but handling |
| 5718 | * that is done separately. */ |
| 5719 | for (i = '0'; i <= '9'; ++i) |
| 5720 | sp->st_isw[i] = TRUE; |
| 5721 | for (i = 'A'; i <= 'Z'; ++i) |
| 5722 | { |
| 5723 | sp->st_isw[i] = TRUE; |
| 5724 | sp->st_isu[i] = TRUE; |
| 5725 | sp->st_fold[i] = i + 0x20; |
| 5726 | } |
| 5727 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5728 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5729 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5730 | sp->st_upper[i] = i - 0x20; |
| 5731 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
| 5734 | /* |
| 5735 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 5736 | * Called once while starting up and when 'encoding' changes. |
| 5737 | * The default is to use isalpha(), but the spell file should define the word |
| 5738 | * characters to make it possible that 'encoding' differs from the current |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5739 | * locale. For utf-8 we don't use isalpha() but our own functions. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5740 | */ |
| 5741 | void |
| 5742 | init_spell_chartab() |
| 5743 | { |
| 5744 | int i; |
| 5745 | |
| 5746 | did_set_spelltab = FALSE; |
| 5747 | clear_spell_chartab(&spelltab); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5748 | #ifdef FEAT_MBYTE |
| 5749 | if (enc_dbcs) |
| 5750 | { |
| 5751 | /* DBCS: assume double-wide characters are word characters. */ |
| 5752 | for (i = 128; i <= 255; ++i) |
| 5753 | if (MB_BYTE2LEN(i) == 2) |
| 5754 | spelltab.st_isw[i] = TRUE; |
| 5755 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5756 | else if (enc_utf8) |
| 5757 | { |
| 5758 | for (i = 128; i < 256; ++i) |
| 5759 | { |
| 5760 | spelltab.st_isu[i] = utf_isupper(i); |
| 5761 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 5762 | spelltab.st_fold[i] = utf_fold(i); |
| 5763 | spelltab.st_upper[i] = utf_toupper(i); |
| 5764 | } |
| 5765 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5766 | else |
| 5767 | #endif |
| 5768 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5769 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5770 | for (i = 128; i < 256; ++i) |
| 5771 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5772 | if (MB_ISUPPER(i)) |
| 5773 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5774 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5775 | spelltab.st_isu[i] = TRUE; |
| 5776 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 5777 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5778 | else if (MB_ISLOWER(i)) |
| 5779 | { |
| 5780 | spelltab.st_isw[i] = TRUE; |
| 5781 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 5782 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | } |
| 5786 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5787 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 5788 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 5789 | |
| 5790 | /* |
| 5791 | * Set the spell character tables from strings in the affix file. |
| 5792 | */ |
| 5793 | static int |
| 5794 | set_spell_chartab(fol, low, upp) |
| 5795 | char_u *fol; |
| 5796 | char_u *low; |
| 5797 | char_u *upp; |
| 5798 | { |
| 5799 | /* We build the new tables here first, so that we can compare with the |
| 5800 | * previous one. */ |
| 5801 | spelltab_T new_st; |
| 5802 | char_u *pf = fol, *pl = low, *pu = upp; |
| 5803 | int f, l, u; |
| 5804 | |
| 5805 | clear_spell_chartab(&new_st); |
| 5806 | |
| 5807 | while (*pf != NUL) |
| 5808 | { |
| 5809 | if (*pl == NUL || *pu == NUL) |
| 5810 | { |
| 5811 | EMSG(_(e_affform)); |
| 5812 | return FAIL; |
| 5813 | } |
| 5814 | #ifdef FEAT_MBYTE |
| 5815 | f = mb_ptr2char_adv(&pf); |
| 5816 | l = mb_ptr2char_adv(&pl); |
| 5817 | u = mb_ptr2char_adv(&pu); |
| 5818 | #else |
| 5819 | f = *pf++; |
| 5820 | l = *pl++; |
| 5821 | u = *pu++; |
| 5822 | #endif |
| 5823 | /* Every character that appears is a word character. */ |
| 5824 | if (f < 256) |
| 5825 | new_st.st_isw[f] = TRUE; |
| 5826 | if (l < 256) |
| 5827 | new_st.st_isw[l] = TRUE; |
| 5828 | if (u < 256) |
| 5829 | new_st.st_isw[u] = TRUE; |
| 5830 | |
| 5831 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 5832 | * case-folding */ |
| 5833 | if (l < 256 && l != f) |
| 5834 | { |
| 5835 | if (f >= 256) |
| 5836 | { |
| 5837 | EMSG(_(e_affrange)); |
| 5838 | return FAIL; |
| 5839 | } |
| 5840 | new_st.st_fold[l] = f; |
| 5841 | } |
| 5842 | |
| 5843 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5844 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 5845 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5846 | if (u < 256 && u != f) |
| 5847 | { |
| 5848 | if (f >= 256) |
| 5849 | { |
| 5850 | EMSG(_(e_affrange)); |
| 5851 | return FAIL; |
| 5852 | } |
| 5853 | new_st.st_fold[u] = f; |
| 5854 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5855 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5856 | } |
| 5857 | } |
| 5858 | |
| 5859 | if (*pl != NUL || *pu != NUL) |
| 5860 | { |
| 5861 | EMSG(_(e_affform)); |
| 5862 | return FAIL; |
| 5863 | } |
| 5864 | |
| 5865 | return set_spell_finish(&new_st); |
| 5866 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5867 | |
| 5868 | /* |
| 5869 | * Set the spell character tables from strings in the .spl file. |
| 5870 | */ |
| 5871 | static int |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5872 | set_spell_charflags(flags, cnt, fol) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5873 | char_u *flags; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5874 | int cnt; /* length of "flags" */ |
| 5875 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5876 | { |
| 5877 | /* We build the new tables here first, so that we can compare with the |
| 5878 | * previous one. */ |
| 5879 | spelltab_T new_st; |
| 5880 | int i; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5881 | char_u *p = fol; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5882 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5883 | |
| 5884 | clear_spell_chartab(&new_st); |
| 5885 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5886 | for (i = 0; i < 128; ++i) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5887 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5888 | if (i < cnt) |
| 5889 | { |
| 5890 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 5891 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
| 5892 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5893 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5894 | if (*p != NUL) |
| 5895 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5896 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5897 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5898 | #else |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5899 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5900 | #endif |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 5901 | new_st.st_fold[i + 128] = c; |
| 5902 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 5903 | new_st.st_upper[c] = i + 128; |
| 5904 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | return set_spell_finish(&new_st); |
| 5908 | } |
| 5909 | |
| 5910 | static int |
| 5911 | set_spell_finish(new_st) |
| 5912 | spelltab_T *new_st; |
| 5913 | { |
| 5914 | int i; |
| 5915 | |
| 5916 | if (did_set_spelltab) |
| 5917 | { |
| 5918 | /* check that it's the same table */ |
| 5919 | for (i = 0; i < 256; ++i) |
| 5920 | { |
| 5921 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 5922 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5923 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 5924 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5925 | { |
| 5926 | EMSG(_("E763: Word characters differ between spell files")); |
| 5927 | return FAIL; |
| 5928 | } |
| 5929 | } |
| 5930 | } |
| 5931 | else |
| 5932 | { |
| 5933 | /* copy the new spelltab into the one being used */ |
| 5934 | spelltab = *new_st; |
| 5935 | did_set_spelltab = TRUE; |
| 5936 | } |
| 5937 | |
| 5938 | return OK; |
| 5939 | } |
| 5940 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5941 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5942 | * Return TRUE if "p" points to a word character. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5943 | * 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] | 5944 | * 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] | 5945 | * 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] | 5946 | */ |
| 5947 | static int |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5948 | spell_iswordp(p, buf) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5949 | char_u *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5950 | buf_T *buf; /* buffer used */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5951 | { |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5952 | #ifdef FEAT_MBYTE |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5953 | char_u *s; |
| 5954 | int l; |
| 5955 | int c; |
| 5956 | |
| 5957 | if (has_mbyte) |
| 5958 | { |
| 5959 | l = MB_BYTE2LEN(*p); |
| 5960 | s = p; |
| 5961 | if (l == 1) |
| 5962 | { |
| 5963 | /* be quick for ASCII */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5964 | if (buf->b_spell_ismw[*p]) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5965 | { |
| 5966 | s = p + 1; /* skip a mid-word character */ |
| 5967 | l = MB_BYTE2LEN(*s); |
| 5968 | } |
| 5969 | } |
| 5970 | else |
| 5971 | { |
| 5972 | c = mb_ptr2char(p); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5973 | if (c < 256 ? buf->b_spell_ismw[c] |
| 5974 | : (buf->b_spell_ismw_mb != NULL |
| 5975 | && vim_strchr(buf->b_spell_ismw_mb, c) != NULL)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5976 | { |
| 5977 | s = p + l; |
| 5978 | l = MB_BYTE2LEN(*s); |
| 5979 | } |
| 5980 | } |
| 5981 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5982 | c = mb_ptr2char(s); |
| 5983 | if (c > 255) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5984 | return mb_get_class(s) >= 2; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5985 | return spelltab.st_isw[c]; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5986 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5987 | #endif |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5988 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5989 | return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]]; |
| 5990 | } |
| 5991 | |
| 5992 | /* |
| 5993 | * Return TRUE if "p" points to a word character. |
| 5994 | * Unlike spell_iswordp() this doesn't check for "midword" characters. |
| 5995 | */ |
| 5996 | static int |
| 5997 | spell_iswordp_nmw(p) |
| 5998 | char_u *p; |
| 5999 | { |
| 6000 | #ifdef FEAT_MBYTE |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6001 | int c; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6002 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6003 | if (has_mbyte) |
| 6004 | { |
| 6005 | c = mb_ptr2char(p); |
| 6006 | if (c > 255) |
| 6007 | return mb_get_class(p) >= 2; |
| 6008 | return spelltab.st_isw[c]; |
| 6009 | } |
| 6010 | #endif |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6011 | return spelltab.st_isw[*p]; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6014 | #ifdef FEAT_MBYTE |
| 6015 | /* |
| 6016 | * Return TRUE if "p" points to a word character. |
| 6017 | * Wide version of spell_iswordp(). |
| 6018 | */ |
| 6019 | static int |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6020 | spell_iswordp_w(p, buf) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6021 | int *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6022 | buf_T *buf; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6023 | { |
| 6024 | int *s; |
| 6025 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6026 | if (*p < 256 ? buf->b_spell_ismw[*p] |
| 6027 | : (buf->b_spell_ismw_mb != NULL |
| 6028 | && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6029 | s = p + 1; |
| 6030 | else |
| 6031 | s = p; |
| 6032 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6033 | if (*s > 255) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6034 | { |
| 6035 | if (enc_utf8) |
| 6036 | return utf_class(*s) >= 2; |
| 6037 | if (enc_dbcs) |
| 6038 | return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2; |
| 6039 | return 0; |
| 6040 | } |
| 6041 | return spelltab.st_isw[*s]; |
| 6042 | } |
| 6043 | #endif |
| 6044 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6045 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6046 | * Write the table with prefix conditions to the .spl file. |
| 6047 | */ |
| 6048 | static void |
| 6049 | write_spell_prefcond(fd, gap) |
| 6050 | FILE *fd; |
| 6051 | garray_T *gap; |
| 6052 | { |
| 6053 | int i; |
| 6054 | char_u *p; |
| 6055 | int len; |
| 6056 | |
| 6057 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 6058 | |
| 6059 | for (i = 0; i < gap->ga_len; ++i) |
| 6060 | { |
| 6061 | /* <prefcond> : <condlen> <condstr> */ |
| 6062 | p = ((char_u **)gap->ga_data)[i]; |
| 6063 | if (p == NULL) |
| 6064 | fputc(0, fd); |
| 6065 | else |
| 6066 | { |
| 6067 | len = STRLEN(p); |
| 6068 | fputc(len, fd); |
| 6069 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6075 | * Write the current tables into the .spl file. |
| 6076 | * This makes sure the same characters are recognized as word characters when |
| 6077 | * generating an when using a spell file. |
| 6078 | */ |
| 6079 | static void |
| 6080 | write_spell_chartab(fd) |
| 6081 | FILE *fd; |
| 6082 | { |
| 6083 | char_u charbuf[256 * 4]; |
| 6084 | int len = 0; |
| 6085 | int flags; |
| 6086 | int i; |
| 6087 | |
| 6088 | fputc(128, fd); /* <charflagslen> */ |
| 6089 | for (i = 128; i < 256; ++i) |
| 6090 | { |
| 6091 | flags = 0; |
| 6092 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6093 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6094 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6095 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6096 | fputc(flags, fd); /* <charflags> */ |
| 6097 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 6098 | #ifdef FEAT_MBYTE |
| 6099 | if (has_mbyte) |
| 6100 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 6101 | else |
| 6102 | #endif |
| 6103 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
| 6106 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 6107 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 6108 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6109 | |
| 6110 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6111 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 6112 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6113 | * When using a multi-byte 'encoding' the length may change! |
| 6114 | * Returns FAIL when something wrong. |
| 6115 | */ |
| 6116 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6117 | spell_casefold(str, len, buf, buflen) |
| 6118 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6119 | int len; |
| 6120 | char_u *buf; |
| 6121 | int buflen; |
| 6122 | { |
| 6123 | int i; |
| 6124 | |
| 6125 | if (len >= buflen) |
| 6126 | { |
| 6127 | buf[0] = NUL; |
| 6128 | return FAIL; /* result will not fit */ |
| 6129 | } |
| 6130 | |
| 6131 | #ifdef FEAT_MBYTE |
| 6132 | if (has_mbyte) |
| 6133 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6134 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6135 | char_u *p; |
| 6136 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6137 | |
| 6138 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6139 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6140 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6141 | if (outi + MB_MAXBYTES > buflen) |
| 6142 | { |
| 6143 | buf[outi] = NUL; |
| 6144 | return FAIL; |
| 6145 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6146 | c = mb_ptr2char_adv(&p); |
| 6147 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6148 | } |
| 6149 | buf[outi] = NUL; |
| 6150 | } |
| 6151 | else |
| 6152 | #endif |
| 6153 | { |
| 6154 | /* Be quick for non-multibyte encodings. */ |
| 6155 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6156 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6157 | buf[i] = NUL; |
| 6158 | } |
| 6159 | |
| 6160 | return OK; |
| 6161 | } |
| 6162 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6163 | #define SPS_BEST 1 |
| 6164 | #define SPS_FAST 2 |
| 6165 | #define SPS_DOUBLE 4 |
| 6166 | |
| 6167 | static int sps_flags = SPS_BEST; |
| 6168 | |
| 6169 | /* |
| 6170 | * Check the 'spellsuggest' option. Return FAIL if it's wrong. |
| 6171 | * Sets "sps_flags". |
| 6172 | */ |
| 6173 | int |
| 6174 | spell_check_sps() |
| 6175 | { |
| 6176 | char_u *p; |
| 6177 | char_u buf[MAXPATHL]; |
| 6178 | int f; |
| 6179 | |
| 6180 | sps_flags = 0; |
| 6181 | |
| 6182 | for (p = p_sps; *p != NUL; ) |
| 6183 | { |
| 6184 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 6185 | |
| 6186 | f = 0; |
| 6187 | if (STRCMP(buf, "best") == 0) |
| 6188 | f = SPS_BEST; |
| 6189 | else if (STRCMP(buf, "fast") == 0) |
| 6190 | f = SPS_FAST; |
| 6191 | else if (STRCMP(buf, "double") == 0) |
| 6192 | f = SPS_DOUBLE; |
| 6193 | else if (STRNCMP(buf, "expr:", 5) != 0 |
| 6194 | && STRNCMP(buf, "file:", 5) != 0) |
| 6195 | f = -1; |
| 6196 | |
| 6197 | if (f == -1 || (sps_flags != 0 && f != 0)) |
| 6198 | { |
| 6199 | sps_flags = SPS_BEST; |
| 6200 | return FAIL; |
| 6201 | } |
| 6202 | if (f != 0) |
| 6203 | sps_flags = f; |
| 6204 | } |
| 6205 | |
| 6206 | if (sps_flags == 0) |
| 6207 | sps_flags = SPS_BEST; |
| 6208 | |
| 6209 | return OK; |
| 6210 | } |
| 6211 | |
| 6212 | /* Remember what "z?" replaced. */ |
| 6213 | static char_u *repl_from = NULL; |
| 6214 | static char_u *repl_to = NULL; |
| 6215 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6216 | /* |
| 6217 | * "z?": Find badly spelled word under or after the cursor. |
| 6218 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6219 | */ |
| 6220 | void |
| 6221 | spell_suggest() |
| 6222 | { |
| 6223 | char_u *line; |
| 6224 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6225 | char_u wcopy[MAXWLEN + 2]; |
| 6226 | char_u *p; |
| 6227 | int i; |
| 6228 | int c; |
| 6229 | suginfo_T sug; |
| 6230 | suggest_T *stp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6231 | int mouse_used; |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6232 | int need_cap; |
| 6233 | regmatch_T regmatch; |
| 6234 | int endcol; |
| 6235 | char_u *line_copy = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6236 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6237 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6238 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL |
| 6239 | || curwin->w_cursor.col > prev_cursor.col) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6240 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6241 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 6242 | return; |
| 6243 | |
| 6244 | /* No bad word or it starts after the cursor: use the word under the |
| 6245 | * cursor. */ |
| 6246 | curwin->w_cursor = prev_cursor; |
| 6247 | line = ml_get_curline(); |
| 6248 | p = line + curwin->w_cursor.col; |
| 6249 | /* Backup to before start of word. */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6250 | while (p > line && spell_iswordp_nmw(p)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6251 | mb_ptr_back(line, p); |
| 6252 | /* Forward to start of word. */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6253 | while (*p != NUL && !spell_iswordp_nmw(p)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6254 | mb_ptr_adv(p); |
| 6255 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6256 | if (!spell_iswordp_nmw(p)) /* No word found. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6257 | { |
| 6258 | beep_flush(); |
| 6259 | return; |
| 6260 | } |
| 6261 | curwin->w_cursor.col = p - line; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6262 | } |
| 6263 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6264 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6265 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6266 | |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6267 | /* Figure out if the word should be capitalised. */ |
| 6268 | need_cap = FALSE; |
| 6269 | if (curbuf->b_cap_prog != NULL) |
| 6270 | { |
| 6271 | endcol = 0; |
Bram Moolenaar | 97409f1 | 2005-07-08 22:17:29 +0000 | [diff] [blame] | 6272 | if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col) |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6273 | { |
| 6274 | /* At start of line, check if previous line is empty or sentence |
| 6275 | * ends there. */ |
| 6276 | if (curwin->w_cursor.lnum == 1) |
| 6277 | need_cap = TRUE; |
| 6278 | else |
| 6279 | { |
| 6280 | line = ml_get(curwin->w_cursor.lnum - 1); |
| 6281 | if (*skipwhite(line) == NUL) |
| 6282 | need_cap = TRUE; |
| 6283 | else |
| 6284 | { |
| 6285 | /* Append a space in place of the line break. */ |
| 6286 | line_copy = concat_str(line, (char_u *)" "); |
| 6287 | line = line_copy; |
| 6288 | endcol = STRLEN(line); |
| 6289 | } |
| 6290 | } |
| 6291 | } |
| 6292 | else |
| 6293 | endcol = curwin->w_cursor.col; |
| 6294 | |
| 6295 | if (endcol > 0) |
| 6296 | { |
| 6297 | /* Check if sentence ends before the bad word. */ |
| 6298 | regmatch.regprog = curbuf->b_cap_prog; |
| 6299 | regmatch.rm_ic = FALSE; |
| 6300 | p = line + endcol; |
| 6301 | for (;;) |
| 6302 | { |
| 6303 | mb_ptr_back(line, p); |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6304 | if (p == line || spell_iswordp_nmw(p)) |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6305 | break; |
| 6306 | if (vim_regexec(®match, p, 0) |
| 6307 | && regmatch.endp[0] == line + endcol) |
| 6308 | { |
| 6309 | need_cap = TRUE; |
| 6310 | break; |
| 6311 | } |
| 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | /* get the line again, we may have been using the previous one */ |
| 6316 | line = ml_get_curline(); |
| 6317 | vim_free(line_copy); |
| 6318 | } |
| 6319 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6320 | /* Get the list of suggestions */ |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6321 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, |
| 6322 | TRUE, need_cap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6323 | |
| 6324 | if (sug.su_ga.ga_len == 0) |
| 6325 | MSG(_("Sorry, no suggestions")); |
| 6326 | else |
| 6327 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6328 | vim_free(repl_from); |
| 6329 | repl_from = NULL; |
| 6330 | vim_free(repl_to); |
| 6331 | repl_to = NULL; |
| 6332 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6333 | /* List the suggestions. */ |
| 6334 | msg_start(); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6335 | lines_left = Rows; /* avoid more prompt */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6336 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 6337 | sug.su_badlen, sug.su_badptr); |
| 6338 | msg_puts(IObuff); |
| 6339 | msg_clr_eos(); |
| 6340 | msg_putchar('\n'); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6341 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6342 | msg_scroll = TRUE; |
| 6343 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 6344 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6345 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6346 | |
| 6347 | /* The suggested word may replace only part of the bad word, add |
| 6348 | * the not replaced part. */ |
| 6349 | STRCPY(wcopy, stp->st_word); |
| 6350 | if (sug.su_badlen > stp->st_orglen) |
| 6351 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 6352 | sug.su_badptr + stp->st_orglen, |
| 6353 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6354 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy); |
| 6355 | msg_puts(IObuff); |
| 6356 | |
| 6357 | /* The word may replace more than "su_badlen". */ |
| 6358 | if (sug.su_badlen < stp->st_orglen) |
| 6359 | { |
| 6360 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 6361 | stp->st_orglen, sug.su_badptr); |
| 6362 | msg_puts(IObuff); |
| 6363 | } |
| 6364 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6365 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6366 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6367 | /* Add the score. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6368 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6369 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"), |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6370 | stp->st_salscore ? "s " : "", |
| 6371 | stp->st_score, stp->st_altscore); |
| 6372 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6373 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"), |
| 6374 | stp->st_score); |
| 6375 | msg_advance(30); |
| 6376 | msg_puts(IObuff); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6377 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6378 | msg_putchar('\n'); |
| 6379 | } |
| 6380 | |
| 6381 | /* Ask for choice. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6382 | i = prompt_for_number(&mouse_used); |
| 6383 | if (mouse_used) |
| 6384 | i -= lines_left; |
| 6385 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6386 | 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] | 6387 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6388 | /* Save the from and to text for :spellrepall. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6389 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6390 | repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); |
| 6391 | repl_to = vim_strsave(stp->st_word); |
| 6392 | |
| 6393 | /* Replace the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6394 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 6395 | if (p != NULL) |
| 6396 | { |
| 6397 | c = sug.su_badptr - line; |
| 6398 | mch_memmove(p, line, c); |
| 6399 | STRCPY(p + c, stp->st_word); |
| 6400 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 6401 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 6402 | curwin->w_cursor.col = c; |
| 6403 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6404 | |
| 6405 | /* For redo we use a change-word command. */ |
| 6406 | ResetRedobuff(); |
| 6407 | AppendToRedobuff((char_u *)"ciw"); |
| 6408 | AppendToRedobuff(stp->st_word); |
| 6409 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6410 | } |
| 6411 | } |
| 6412 | else |
| 6413 | curwin->w_cursor = prev_cursor; |
| 6414 | } |
| 6415 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6416 | spell_find_cleanup(&sug); |
| 6417 | } |
| 6418 | |
| 6419 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6420 | * ":spellrepall" |
| 6421 | */ |
| 6422 | /*ARGSUSED*/ |
| 6423 | void |
| 6424 | ex_spellrepall(eap) |
| 6425 | exarg_T *eap; |
| 6426 | { |
| 6427 | pos_T pos = curwin->w_cursor; |
| 6428 | char_u *frompat; |
| 6429 | int addlen; |
| 6430 | char_u *line; |
| 6431 | char_u *p; |
| 6432 | int didone = FALSE; |
| 6433 | int save_ws = p_ws; |
| 6434 | |
| 6435 | if (repl_from == NULL || repl_to == NULL) |
| 6436 | { |
| 6437 | EMSG(_("E752: No previous spell replacement")); |
| 6438 | return; |
| 6439 | } |
| 6440 | addlen = STRLEN(repl_to) - STRLEN(repl_from); |
| 6441 | |
| 6442 | frompat = alloc(STRLEN(repl_from) + 7); |
| 6443 | if (frompat == NULL) |
| 6444 | return; |
| 6445 | sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); |
| 6446 | p_ws = FALSE; |
| 6447 | |
| 6448 | curwin->w_cursor.lnum = 0; |
| 6449 | while (!got_int) |
| 6450 | { |
| 6451 | if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0 |
| 6452 | || u_save_cursor() == FAIL) |
| 6453 | break; |
| 6454 | |
| 6455 | /* Only replace when the right word isn't there yet. This happens |
| 6456 | * when changing "etc" to "etc.". */ |
| 6457 | line = ml_get_curline(); |
| 6458 | if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, |
| 6459 | repl_to, STRLEN(repl_to)) != 0) |
| 6460 | { |
| 6461 | p = alloc(STRLEN(line) + addlen + 1); |
| 6462 | if (p == NULL) |
| 6463 | break; |
| 6464 | mch_memmove(p, line, curwin->w_cursor.col); |
| 6465 | STRCPY(p + curwin->w_cursor.col, repl_to); |
| 6466 | STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); |
| 6467 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 6468 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 6469 | didone = TRUE; |
| 6470 | } |
| 6471 | curwin->w_cursor.col += STRLEN(repl_to); |
| 6472 | } |
| 6473 | |
| 6474 | p_ws = save_ws; |
| 6475 | curwin->w_cursor = pos; |
| 6476 | vim_free(frompat); |
| 6477 | |
| 6478 | if (!didone) |
| 6479 | EMSG2(_("E753: Not found: %s"), repl_from); |
| 6480 | } |
| 6481 | |
| 6482 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6483 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 6484 | * a list of allocated strings. |
| 6485 | */ |
| 6486 | void |
| 6487 | spell_suggest_list(gap, word, maxcount) |
| 6488 | garray_T *gap; |
| 6489 | char_u *word; |
| 6490 | int maxcount; /* maximum nr of suggestions */ |
| 6491 | { |
| 6492 | suginfo_T sug; |
| 6493 | int i; |
| 6494 | suggest_T *stp; |
| 6495 | char_u *wcopy; |
| 6496 | |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6497 | spell_find_suggest(word, &sug, maxcount, FALSE, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6498 | |
| 6499 | /* Make room in "gap". */ |
| 6500 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 6501 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 6502 | return; |
| 6503 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6504 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6505 | { |
| 6506 | stp = &SUG(sug.su_ga, i); |
| 6507 | |
| 6508 | /* The suggested word may replace only part of "word", add the not |
| 6509 | * replaced part. */ |
| 6510 | wcopy = alloc(STRLEN(stp->st_word) |
| 6511 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 6512 | if (wcopy == NULL) |
| 6513 | break; |
| 6514 | STRCPY(wcopy, stp->st_word); |
| 6515 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 6516 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 6517 | } |
| 6518 | |
| 6519 | spell_find_cleanup(&sug); |
| 6520 | } |
| 6521 | |
| 6522 | /* |
| 6523 | * Find spell suggestions for the word at the start of "badptr". |
| 6524 | * Return the suggestions in "su->su_ga". |
| 6525 | * The maximum number of suggestions is "maxcount". |
| 6526 | * Note: does use info for the current window. |
| 6527 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 6528 | */ |
| 6529 | static void |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6530 | spell_find_suggest(badptr, su, maxcount, banbadword, need_cap) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6531 | char_u *badptr; |
| 6532 | suginfo_T *su; |
| 6533 | int maxcount; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6534 | int banbadword; /* don't include badword in suggestions */ |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6535 | int need_cap; /* word should start with capital */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6536 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6537 | int attr = 0; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6538 | char_u buf[MAXPATHL]; |
| 6539 | char_u *p; |
| 6540 | int do_combine = FALSE; |
| 6541 | char_u *sps_copy; |
| 6542 | #ifdef FEAT_EVAL |
| 6543 | static int expr_busy = FALSE; |
| 6544 | #endif |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6545 | int c; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6546 | |
| 6547 | /* |
| 6548 | * Set the info in "*su". |
| 6549 | */ |
| 6550 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 6551 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 6552 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 6553 | if (*badptr == NUL) |
| 6554 | return; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6555 | hash_init(&su->su_banned); |
| 6556 | |
| 6557 | su->su_badptr = badptr; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6558 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6559 | su->su_maxcount = maxcount; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6560 | su->su_maxscore = SCORE_MAXINIT; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6561 | |
| 6562 | if (su->su_badlen >= MAXWLEN) |
| 6563 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 6564 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 6565 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 6566 | su->su_fbadword, MAXWLEN); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6567 | /* get caps flags for bad word */ |
| 6568 | su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen); |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6569 | if (need_cap) |
| 6570 | su->su_badflags |= WF_ONECAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6571 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6572 | /* If the word is not capitalised and spell_check() doesn't consider the |
| 6573 | * word to be bad then it might need to be capitalised. Add a suggestion |
| 6574 | * for that. */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 6575 | c = PTR2CHAR(su->su_badptr); |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6576 | if (!SPELL_ISUPPER(c) && attr == 0) |
| 6577 | { |
| 6578 | make_case_word(su->su_badword, buf, WF_ONECAP); |
| 6579 | add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, |
| 6580 | 0, TRUE); |
| 6581 | } |
| 6582 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6583 | /* Ban the bad word itself. It may appear in another region. */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6584 | if (banbadword) |
| 6585 | add_banned(su, su->su_badword); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6586 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6587 | /* Make a copy of 'spellsuggest', because the expression may change it. */ |
| 6588 | sps_copy = vim_strsave(p_sps); |
| 6589 | if (sps_copy == NULL) |
| 6590 | return; |
| 6591 | |
| 6592 | /* Loop over the items in 'spellsuggest'. */ |
| 6593 | for (p = sps_copy; *p != NUL; ) |
| 6594 | { |
| 6595 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 6596 | |
| 6597 | if (STRNCMP(buf, "expr:", 5) == 0) |
| 6598 | { |
| 6599 | #ifdef FEAT_EVAL |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6600 | /* Evaluate an expression. Skip this when called recursively, |
| 6601 | * when using spellsuggest() in the expression. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6602 | if (!expr_busy) |
| 6603 | { |
| 6604 | expr_busy = TRUE; |
| 6605 | spell_suggest_expr(su, buf + 5); |
| 6606 | expr_busy = FALSE; |
| 6607 | } |
| 6608 | #endif |
| 6609 | } |
| 6610 | else if (STRNCMP(buf, "file:", 5) == 0) |
| 6611 | /* Use list of suggestions in a file. */ |
| 6612 | spell_suggest_file(su, buf + 5); |
| 6613 | else |
| 6614 | { |
| 6615 | /* Use internal method. */ |
| 6616 | spell_suggest_intern(su); |
| 6617 | if (sps_flags & SPS_DOUBLE) |
| 6618 | do_combine = TRUE; |
| 6619 | } |
| 6620 | } |
| 6621 | |
| 6622 | vim_free(sps_copy); |
| 6623 | |
| 6624 | if (do_combine) |
| 6625 | /* Combine the two list of suggestions. This must be done last, |
| 6626 | * because sorting changes the order again. */ |
| 6627 | score_combine(su); |
| 6628 | } |
| 6629 | |
| 6630 | #ifdef FEAT_EVAL |
| 6631 | /* |
| 6632 | * Find suggestions by evaluating expression "expr". |
| 6633 | */ |
| 6634 | static void |
| 6635 | spell_suggest_expr(su, expr) |
| 6636 | suginfo_T *su; |
| 6637 | char_u *expr; |
| 6638 | { |
| 6639 | list_T *list; |
| 6640 | listitem_T *li; |
| 6641 | int score; |
| 6642 | char_u *p; |
| 6643 | |
| 6644 | /* The work is split up in a few parts to avoid having to export |
| 6645 | * suginfo_T. |
| 6646 | * First evaluate the expression and get the resulting list. */ |
| 6647 | list = eval_spell_expr(su->su_badword, expr); |
| 6648 | if (list != NULL) |
| 6649 | { |
| 6650 | /* Loop over the items in the list. */ |
| 6651 | for (li = list->lv_first; li != NULL; li = li->li_next) |
| 6652 | if (li->li_tv.v_type == VAR_LIST) |
| 6653 | { |
| 6654 | /* Get the word and the score from the items. */ |
| 6655 | score = get_spellword(li->li_tv.vval.v_list, &p); |
| 6656 | if (score >= 0) |
| 6657 | add_suggestion(su, &su->su_ga, p, |
| 6658 | su->su_badlen, score, 0, TRUE); |
| 6659 | } |
| 6660 | list_unref(list); |
| 6661 | } |
| 6662 | |
| 6663 | /* Sort the suggestions and truncate at "maxcount". */ |
| 6664 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6665 | } |
| 6666 | #endif |
| 6667 | |
| 6668 | /* |
| 6669 | * Find suggestions a file "fname". |
| 6670 | */ |
| 6671 | static void |
| 6672 | spell_suggest_file(su, fname) |
| 6673 | suginfo_T *su; |
| 6674 | char_u *fname; |
| 6675 | { |
| 6676 | FILE *fd; |
| 6677 | char_u line[MAXWLEN * 2]; |
| 6678 | char_u *p; |
| 6679 | int len; |
| 6680 | char_u cword[MAXWLEN]; |
| 6681 | |
| 6682 | /* Open the file. */ |
| 6683 | fd = mch_fopen((char *)fname, "r"); |
| 6684 | if (fd == NULL) |
| 6685 | { |
| 6686 | EMSG2(_(e_notopen), fname); |
| 6687 | return; |
| 6688 | } |
| 6689 | |
| 6690 | /* Read it line by line. */ |
| 6691 | while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) |
| 6692 | { |
| 6693 | line_breakcheck(); |
| 6694 | |
| 6695 | p = vim_strchr(line, '/'); |
| 6696 | if (p == NULL) |
| 6697 | continue; /* No Tab found, just skip the line. */ |
| 6698 | *p++ = NUL; |
| 6699 | if (STRICMP(su->su_badword, line) == 0) |
| 6700 | { |
| 6701 | /* Match! Isolate the good word, until CR or NL. */ |
| 6702 | for (len = 0; p[len] >= ' '; ++len) |
| 6703 | ; |
| 6704 | p[len] = NUL; |
| 6705 | |
| 6706 | /* If the suggestion doesn't have specific case duplicate the case |
| 6707 | * of the bad word. */ |
| 6708 | if (captype(p, NULL) == 0) |
| 6709 | { |
| 6710 | make_case_word(p, cword, su->su_badflags); |
| 6711 | p = cword; |
| 6712 | } |
| 6713 | |
| 6714 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| 6715 | SCORE_FILE, 0, TRUE); |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | fclose(fd); |
| 6720 | |
| 6721 | /* Sort the suggestions and truncate at "maxcount". */ |
| 6722 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6723 | } |
| 6724 | |
| 6725 | /* |
| 6726 | * Find suggestions for the internal method indicated by "sps_flags". |
| 6727 | */ |
| 6728 | static void |
| 6729 | spell_suggest_intern(su) |
| 6730 | suginfo_T *su; |
| 6731 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6732 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6733 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6734 | * |
| 6735 | * Set a maximum score to limit the combination of operations that is |
| 6736 | * tried. |
| 6737 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6738 | suggest_try_special(su); |
| 6739 | |
| 6740 | /* |
| 6741 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 6742 | * from the .aff file and inserting a space (split the word). |
| 6743 | */ |
| 6744 | suggest_try_change(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6745 | |
| 6746 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 6747 | if (sps_flags & SPS_DOUBLE) |
| 6748 | score_comp_sal(su); |
| 6749 | |
| 6750 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6751 | * 3. Try finding sound-a-like words. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6752 | * |
| 6753 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 6754 | * very slow and often doesn't find new suggestions. |
| 6755 | */ |
| 6756 | if ((sps_flags & SPS_DOUBLE) |
| 6757 | || (!(sps_flags & SPS_FAST) |
| 6758 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 6759 | { |
| 6760 | /* Allow a higher score now. */ |
| 6761 | su->su_maxscore = SCORE_MAXMAX; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6762 | suggest_try_soundalike(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
| 6765 | /* When CTRL-C was hit while searching do show the results. */ |
| 6766 | ui_breakcheck(); |
| 6767 | if (got_int) |
| 6768 | { |
| 6769 | (void)vgetc(); |
| 6770 | got_int = FALSE; |
| 6771 | } |
| 6772 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6773 | if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6774 | { |
| 6775 | if (sps_flags & SPS_BEST) |
| 6776 | /* Adjust the word score for how it sounds like. */ |
| 6777 | rescore_suggestions(su); |
| 6778 | |
| 6779 | /* Sort the suggestions and truncate at "maxcount". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6780 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6781 | } |
| 6782 | } |
| 6783 | |
| 6784 | /* |
| 6785 | * Free the info put in "*su" by spell_find_suggest(). |
| 6786 | */ |
| 6787 | static void |
| 6788 | spell_find_cleanup(su) |
| 6789 | suginfo_T *su; |
| 6790 | { |
| 6791 | int i; |
| 6792 | |
| 6793 | /* Free the suggestions. */ |
| 6794 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6795 | vim_free(SUG(su->su_ga, i).st_word); |
| 6796 | ga_clear(&su->su_ga); |
| 6797 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6798 | vim_free(SUG(su->su_sga, i).st_word); |
| 6799 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6800 | |
| 6801 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6802 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
| 6805 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6806 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 6807 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 6808 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6809 | */ |
| 6810 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6811 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6812 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6813 | char_u *wcopy; |
| 6814 | int upper; /* TRUE: first letter made upper case */ |
| 6815 | { |
| 6816 | char_u *p; |
| 6817 | int c; |
| 6818 | int l; |
| 6819 | |
| 6820 | p = word; |
| 6821 | #ifdef FEAT_MBYTE |
| 6822 | if (has_mbyte) |
| 6823 | c = mb_ptr2char_adv(&p); |
| 6824 | else |
| 6825 | #endif |
| 6826 | c = *p++; |
| 6827 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6828 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6829 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6830 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6831 | #ifdef FEAT_MBYTE |
| 6832 | if (has_mbyte) |
| 6833 | l = mb_char2bytes(c, wcopy); |
| 6834 | else |
| 6835 | #endif |
| 6836 | { |
| 6837 | l = 1; |
| 6838 | wcopy[0] = c; |
| 6839 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6840 | vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6844 | * Make a copy of "word" with all the letters upper cased into |
| 6845 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6846 | */ |
| 6847 | static void |
| 6848 | allcap_copy(word, wcopy) |
| 6849 | char_u *word; |
| 6850 | char_u *wcopy; |
| 6851 | { |
| 6852 | char_u *s; |
| 6853 | char_u *d; |
| 6854 | int c; |
| 6855 | |
| 6856 | d = wcopy; |
| 6857 | for (s = word; *s != NUL; ) |
| 6858 | { |
| 6859 | #ifdef FEAT_MBYTE |
| 6860 | if (has_mbyte) |
| 6861 | c = mb_ptr2char_adv(&s); |
| 6862 | else |
| 6863 | #endif |
| 6864 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6865 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6866 | |
| 6867 | #ifdef FEAT_MBYTE |
| 6868 | if (has_mbyte) |
| 6869 | { |
| 6870 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 6871 | break; |
| 6872 | d += mb_char2bytes(c, d); |
| 6873 | } |
| 6874 | else |
| 6875 | #endif |
| 6876 | { |
| 6877 | if (d - wcopy >= MAXWLEN - 1) |
| 6878 | break; |
| 6879 | *d++ = c; |
| 6880 | } |
| 6881 | } |
| 6882 | *d = NUL; |
| 6883 | } |
| 6884 | |
| 6885 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6886 | * Try finding suggestions by recognizing specific situations. |
| 6887 | */ |
| 6888 | static void |
| 6889 | suggest_try_special(su) |
| 6890 | suginfo_T *su; |
| 6891 | { |
| 6892 | char_u *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6893 | size_t len; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6894 | int c; |
| 6895 | char_u word[MAXWLEN]; |
| 6896 | |
| 6897 | /* |
| 6898 | * Recognize a word that is repeated: "the the". |
| 6899 | */ |
| 6900 | p = skiptowhite(su->su_fbadword); |
| 6901 | len = p - su->su_fbadword; |
| 6902 | p = skipwhite(p); |
| 6903 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 6904 | { |
| 6905 | /* Include badflags: if the badword is onecap or allcap |
| 6906 | * use that for the goodword too: "The the" -> "The". */ |
| 6907 | c = su->su_fbadword[len]; |
| 6908 | su->su_fbadword[len] = NUL; |
| 6909 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 6910 | su->su_fbadword[len] = c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6911 | 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] | 6912 | } |
| 6913 | } |
| 6914 | |
| 6915 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6916 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6917 | * |
| 6918 | * This uses a state machine. At each node in the tree we try various |
| 6919 | * operations. When trying if an operation work "depth" is increased and the |
| 6920 | * stack[] is used to store info. This allows combinations, thus insert one |
| 6921 | * character, replace one and delete another. The number of changes is |
| 6922 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6923 | */ |
| 6924 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6925 | suggest_try_change(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6926 | suginfo_T *su; |
| 6927 | { |
| 6928 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 6929 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 6930 | trystate_T stack[MAXWLEN]; |
| 6931 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 6932 | * to for word split) */ |
| 6933 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 6934 | int splitoff = 0; /* index in tword after last split */ |
| 6935 | trystate_T *sp; |
| 6936 | int newscore; |
| 6937 | langp_T *lp; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6938 | char_u *byts, *fbyts, *pbyts; |
| 6939 | idx_T *idxs, *fidxs, *pidxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6940 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6941 | int c, c2, c3; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6942 | int n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6943 | int flags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6944 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6945 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6946 | int len; |
| 6947 | char_u *p; |
| 6948 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6949 | int fl = 0, tl; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6950 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6951 | |
| 6952 | /* 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] | 6953 | * to find matches (esp. REP items). Append some more text, changing |
| 6954 | * chars after the bad word may help. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6955 | STRCPY(fword, su->su_fbadword); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6956 | n = STRLEN(fword); |
| 6957 | p = su->su_badptr + su->su_badlen; |
| 6958 | (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6959 | |
| 6960 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6961 | lp->lp_slang != NULL; ++lp) |
| 6962 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6963 | /* |
| 6964 | * Go through the whole case-fold tree, try changes at each node. |
| 6965 | * "tword[]" contains the word collected from nodes in the tree. |
| 6966 | * "fword[]" the word we are trying to match with (initially the bad |
| 6967 | * word). |
| 6968 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6969 | depth = 0; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6970 | sp = &stack[0]; |
| 6971 | sp->ts_state = STATE_START; |
| 6972 | sp->ts_score = 0; |
| 6973 | sp->ts_curi = 1; |
| 6974 | sp->ts_fidx = 0; |
| 6975 | sp->ts_fidxtry = 0; |
| 6976 | sp->ts_twordlen = 0; |
| 6977 | sp->ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6978 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6979 | sp->ts_tcharlen = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6980 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6981 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6982 | /* |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6983 | * When there are postponed prefixes we need to use these first. At |
| 6984 | * the end of the prefix we continue in the case-fold tree. |
| 6985 | */ |
| 6986 | fbyts = lp->lp_slang->sl_fbyts; |
| 6987 | fidxs = lp->lp_slang->sl_fidxs; |
| 6988 | pbyts = lp->lp_slang->sl_pbyts; |
| 6989 | pidxs = lp->lp_slang->sl_pidxs; |
| 6990 | if (pbyts != NULL) |
| 6991 | { |
| 6992 | byts = pbyts; |
| 6993 | idxs = pidxs; |
| 6994 | sp->ts_prefixdepth = PREFIXTREE; |
| 6995 | sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
| 6996 | } |
| 6997 | else |
| 6998 | { |
| 6999 | byts = fbyts; |
| 7000 | idxs = fidxs; |
| 7001 | sp->ts_prefixdepth = NOPREFIX; |
| 7002 | } |
| 7003 | |
| 7004 | /* |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7005 | * Loop to find all suggestions. At each round we either: |
| 7006 | * - For the current state try one operation, advance "ts_curi", |
| 7007 | * increase "depth". |
| 7008 | * - When a state is done go to the next, set "ts_state". |
| 7009 | * - When all states are tried decrease "depth". |
| 7010 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7011 | while (depth >= 0 && !got_int) |
| 7012 | { |
| 7013 | sp = &stack[depth]; |
| 7014 | switch (sp->ts_state) |
| 7015 | { |
| 7016 | case STATE_START: |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7017 | case STATE_NOPREFIX: |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7018 | /* |
| 7019 | * Start of node: Deal with NUL bytes, which means |
| 7020 | * tword[] may end here. |
| 7021 | */ |
| 7022 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 7023 | len = byts[arridx]; /* bytes in this node */ |
| 7024 | arridx += sp->ts_curi; /* index of current byte */ |
| 7025 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7026 | if (sp->ts_prefixdepth == PREFIXTREE) |
| 7027 | { |
| 7028 | /* Skip over the NUL bytes, we use them later. */ |
| 7029 | for (n = 0; n < len && byts[arridx + n] == 0; ++n) |
| 7030 | ; |
| 7031 | sp->ts_curi += n; |
| 7032 | |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7033 | /* Always past NUL bytes now. */ |
| 7034 | n = (int)sp->ts_state; |
| 7035 | sp->ts_state = STATE_ENDNUL; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7036 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7037 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7038 | /* At end of a prefix or at start of prefixtree: check for |
| 7039 | * following word. */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7040 | if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX) |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7041 | { |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7042 | /* Set su->su_badflags to the caps type at this |
| 7043 | * position. Use the caps type until here for the |
| 7044 | * prefix itself. */ |
| 7045 | #ifdef FEAT_MBYTE |
| 7046 | if (has_mbyte) |
| 7047 | n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
| 7048 | else |
| 7049 | #endif |
| 7050 | n = sp->ts_fidx; |
| 7051 | flags = captype(su->su_badptr, su->su_badptr + n); |
| 7052 | su->su_badflags = captype(su->su_badptr + n, |
| 7053 | su->su_badptr + su->su_badlen); |
| 7054 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7055 | ++depth; |
| 7056 | stack[depth] = stack[depth - 1]; |
| 7057 | sp = &stack[depth]; |
| 7058 | sp->ts_prefixdepth = depth - 1; |
| 7059 | byts = fbyts; |
| 7060 | idxs = fidxs; |
| 7061 | sp->ts_state = STATE_START; |
| 7062 | sp->ts_curi = 1; /* start just after length byte */ |
| 7063 | sp->ts_arridx = 0; |
| 7064 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7065 | /* Move the prefix to preword[] with the right case |
| 7066 | * and make find_keepcap_word() works. */ |
| 7067 | splitoff = sp->ts_twordlen; |
| 7068 | tword[splitoff] = NUL; |
| 7069 | make_case_word(tword, preword, flags); |
| 7070 | prewordlen = STRLEN(preword); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7071 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7072 | break; |
| 7073 | } |
| 7074 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7075 | if (sp->ts_curi > len || byts[arridx] != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7076 | { |
| 7077 | /* Past bytes in node and/or past NUL bytes. */ |
| 7078 | sp->ts_state = STATE_ENDNUL; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7079 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7080 | break; |
| 7081 | } |
| 7082 | |
| 7083 | /* |
| 7084 | * End of word in tree. |
| 7085 | */ |
| 7086 | ++sp->ts_curi; /* eat one NUL byte */ |
| 7087 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7088 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7089 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7090 | if (sp->ts_prefixdepth < MAXWLEN) |
| 7091 | { |
| 7092 | /* There was a prefix before the word. Check that the |
| 7093 | * prefix can be used with this word. */ |
| 7094 | /* Count the length of the NULs in the prefix. If there |
| 7095 | * are none this must be the first try without a prefix. |
| 7096 | */ |
| 7097 | n = stack[sp->ts_prefixdepth].ts_arridx; |
| 7098 | len = pbyts[n++]; |
| 7099 | for (c = 0; c < len && pbyts[n + c] == 0; ++c) |
| 7100 | ; |
| 7101 | if (c > 0) |
| 7102 | { |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7103 | /* The prefix ID is stored three bytes above the |
| 7104 | * flags. */ |
| 7105 | c = valid_word_prefix(c, n, flags, |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7106 | tword + splitoff, lp->lp_slang, FALSE); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7107 | if (c == 0) |
| 7108 | break; |
| 7109 | |
| 7110 | /* Use the WF_RARE flag for a rare prefix. */ |
| 7111 | if (c & WF_RAREPFX) |
| 7112 | flags |= WF_RARE; |
| 7113 | } |
| 7114 | } |
| 7115 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7116 | /* |
| 7117 | * Form the word with proper case in preword. |
| 7118 | * If there is a word from a previous split, append. |
| 7119 | */ |
| 7120 | tword[sp->ts_twordlen] = NUL; |
| 7121 | if (flags & WF_KEEPCAP) |
| 7122 | /* Must find the word in the keep-case tree. */ |
| 7123 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 7124 | preword + prewordlen); |
| 7125 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7126 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7127 | /* Include badflags: if the badword is onecap or allcap |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7128 | * use that for the goodword too. But if the badword is |
| 7129 | * allcap and it's only one char long use onecap. */ |
| 7130 | c = su->su_badflags; |
| 7131 | if ((c & WF_ALLCAP) |
| 7132 | #ifdef FEAT_MBYTE |
| 7133 | && su->su_badlen == mb_ptr2len_check(su->su_badptr) |
| 7134 | #else |
| 7135 | && su->su_badlen == 1 |
| 7136 | #endif |
| 7137 | ) |
| 7138 | c = WF_ONECAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7139 | make_case_word(tword + splitoff, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7140 | preword + prewordlen, flags | c); |
| 7141 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7142 | |
| 7143 | /* Don't use a banned word. It may appear again as a good |
| 7144 | * word, thus remember it. */ |
| 7145 | if (flags & WF_BANNED) |
| 7146 | { |
| 7147 | add_banned(su, preword + prewordlen); |
| 7148 | break; |
| 7149 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 7150 | if (was_banned(su, preword + prewordlen) |
| 7151 | || was_banned(su, preword)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7152 | break; |
| 7153 | |
| 7154 | newscore = 0; |
| 7155 | if ((flags & WF_REGION) |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7156 | && (((unsigned)flags >> 16) & lp->lp_region) == 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7157 | newscore += SCORE_REGION; |
| 7158 | if (flags & WF_RARE) |
| 7159 | newscore += SCORE_RARE; |
| 7160 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7161 | if (!spell_valid_case(su->su_badflags, |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7162 | captype(preword + prewordlen, NULL))) |
| 7163 | newscore += SCORE_ICASE; |
| 7164 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7165 | if ((fword[sp->ts_fidx] == NUL |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7166 | || !spell_iswordp(fword + sp->ts_fidx, curbuf)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7167 | && sp->ts_fidx >= sp->ts_fidxtry) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7168 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7169 | /* The badword also ends: add suggestions. Give a penalty |
| 7170 | * when changing non-word char to word char, e.g., "thes," |
| 7171 | * -> "these". */ |
| 7172 | p = fword + sp->ts_fidx; |
| 7173 | #ifdef FEAT_MBYTE |
| 7174 | if (has_mbyte) |
| 7175 | mb_ptr_back(fword, p); |
| 7176 | else |
| 7177 | #endif |
| 7178 | --p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7179 | if (!spell_iswordp(p, curbuf)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7180 | { |
| 7181 | p = preword + STRLEN(preword); |
| 7182 | #ifdef FEAT_MBYTE |
| 7183 | if (has_mbyte) |
| 7184 | mb_ptr_back(preword, p); |
| 7185 | else |
| 7186 | #endif |
| 7187 | --p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7188 | if (spell_iswordp(p, curbuf)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7189 | newscore += SCORE_NONWORD; |
| 7190 | } |
| 7191 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7192 | add_suggestion(su, &su->su_ga, preword, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7193 | sp->ts_fidx - repextra, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7194 | sp->ts_score + newscore, 0, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7195 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7196 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 7197 | #ifdef FEAT_MBYTE |
| 7198 | /* Don't split halfway a character. */ |
| 7199 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 7200 | #endif |
| 7201 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7202 | { |
| 7203 | /* The word in the tree ends but the badword |
| 7204 | * continues: try inserting a space and check that a valid |
| 7205 | * words starts at fword[sp->ts_fidx]. */ |
| 7206 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 7207 | { |
| 7208 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 7209 | sp->ts_save_prewordlen = prewordlen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7210 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7211 | sp->ts_save_splitoff = splitoff; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7212 | sp->ts_state = STATE_SPLITUNDO; |
| 7213 | |
| 7214 | ++depth; |
| 7215 | sp = &stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7216 | |
| 7217 | /* Append a space to preword. */ |
| 7218 | STRCAT(preword, " "); |
| 7219 | prewordlen = STRLEN(preword); |
| 7220 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7221 | |
| 7222 | /* If the badword has a non-word character at this |
| 7223 | * position skip it. That means replacing the |
| 7224 | * non-word character with a space. */ |
| 7225 | if (!spell_iswordp_nmw(fword + sp->ts_fidx)) |
| 7226 | { |
| 7227 | sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; |
| 7228 | #ifdef FEAT_MBYTE |
| 7229 | if (has_mbyte) |
| 7230 | sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
| 7231 | else |
| 7232 | #endif |
| 7233 | ++sp->ts_fidx; |
| 7234 | } |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7235 | |
| 7236 | /* set su->su_badflags to the caps type at this |
| 7237 | * position */ |
| 7238 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7239 | #ifdef FEAT_MBYTE |
| 7240 | if (has_mbyte) |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7241 | n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7242 | else |
| 7243 | #endif |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7244 | n = sp->ts_fidx; |
| 7245 | su->su_badflags = captype(su->su_badptr + n, |
| 7246 | su->su_badptr + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7247 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7248 | /* Restart at top of the tree. */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7249 | sp->ts_arridx = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7250 | } |
| 7251 | } |
| 7252 | break; |
| 7253 | |
| 7254 | case STATE_SPLITUNDO: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7255 | /* Undo the changes done for word split. */ |
| 7256 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7257 | splitoff = sp->ts_save_splitoff; |
| 7258 | prewordlen = sp->ts_save_prewordlen; |
| 7259 | |
| 7260 | /* Continue looking for NUL bytes. */ |
| 7261 | sp->ts_state = STATE_START; |
| 7262 | break; |
| 7263 | |
| 7264 | case STATE_ENDNUL: |
| 7265 | /* Past the NUL bytes in the node. */ |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7266 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7267 | if (fword[sp->ts_fidx] == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7268 | { |
| 7269 | /* The badword ends, can't use the bytes in this node. */ |
| 7270 | sp->ts_state = STATE_DEL; |
| 7271 | break; |
| 7272 | } |
| 7273 | sp->ts_state = STATE_PLAIN; |
| 7274 | /*FALLTHROUGH*/ |
| 7275 | |
| 7276 | case STATE_PLAIN: |
| 7277 | /* |
| 7278 | * Go over all possible bytes at this node, add each to |
| 7279 | * tword[] and use child node. "ts_curi" is the index. |
| 7280 | */ |
| 7281 | arridx = sp->ts_arridx; |
| 7282 | if (sp->ts_curi > byts[arridx]) |
| 7283 | { |
| 7284 | /* Done all bytes at this node, do next state. When still |
| 7285 | * at already changed bytes skip the other tricks. */ |
| 7286 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 7287 | sp->ts_state = STATE_DEL; |
| 7288 | else |
| 7289 | sp->ts_state = STATE_FINAL; |
| 7290 | } |
| 7291 | else |
| 7292 | { |
| 7293 | arridx += sp->ts_curi++; |
| 7294 | c = byts[arridx]; |
| 7295 | |
| 7296 | /* Normal byte, go one level deeper. If it's not equal to |
| 7297 | * the byte in the bad word adjust the score. But don't |
| 7298 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7299 | if (c == fword[sp->ts_fidx] |
| 7300 | #ifdef FEAT_MBYTE |
| 7301 | || (sp->ts_tcharlen > 0 |
| 7302 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7303 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7304 | ) |
| 7305 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7306 | else |
| 7307 | newscore = SCORE_SUBST; |
| 7308 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 7309 | && try_deeper(su, stack, depth, newscore)) |
| 7310 | { |
| 7311 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7312 | sp = &stack[depth]; |
| 7313 | ++sp->ts_fidx; |
| 7314 | tword[sp->ts_twordlen++] = c; |
| 7315 | sp->ts_arridx = idxs[arridx]; |
| 7316 | #ifdef FEAT_MBYTE |
| 7317 | if (newscore == SCORE_SUBST) |
| 7318 | sp->ts_isdiff = DIFF_YES; |
| 7319 | if (has_mbyte) |
| 7320 | { |
| 7321 | /* Multi-byte characters are a bit complicated to |
| 7322 | * handle: They differ when any of the bytes |
| 7323 | * differ and then their length may also differ. */ |
| 7324 | if (sp->ts_tcharlen == 0) |
| 7325 | { |
| 7326 | /* First byte. */ |
| 7327 | sp->ts_tcharidx = 0; |
| 7328 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 7329 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 7330 | sp->ts_isdiff = (newscore != 0) |
| 7331 | ? DIFF_YES : DIFF_NONE; |
| 7332 | } |
| 7333 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 7334 | /* When inserting trail bytes don't advance in |
| 7335 | * the bad word. */ |
| 7336 | --sp->ts_fidx; |
| 7337 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 7338 | { |
| 7339 | /* Last byte of character. */ |
| 7340 | if (sp->ts_isdiff == DIFF_YES) |
| 7341 | { |
| 7342 | /* Correct ts_fidx for the byte length of |
| 7343 | * the character (we didn't check that |
| 7344 | * before). */ |
| 7345 | sp->ts_fidx = sp->ts_fcharstart |
| 7346 | + MB_BYTE2LEN( |
| 7347 | fword[sp->ts_fcharstart]); |
| 7348 | |
| 7349 | /* For a similar character adjust score |
| 7350 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 7351 | if (lp->lp_slang->sl_has_map |
| 7352 | && similar_chars(lp->lp_slang, |
| 7353 | mb_ptr2char(tword |
| 7354 | + sp->ts_twordlen |
| 7355 | - sp->ts_tcharlen), |
| 7356 | mb_ptr2char(fword |
| 7357 | + sp->ts_fcharstart))) |
| 7358 | sp->ts_score -= |
| 7359 | SCORE_SUBST - SCORE_SIMILAR; |
| 7360 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7361 | else if (sp->ts_isdiff == DIFF_INSERT |
| 7362 | && sp->ts_twordlen > sp->ts_tcharlen) |
| 7363 | { |
| 7364 | /* If the previous character was the same, |
| 7365 | * thus doubling a character, give a bonus |
| 7366 | * to the score. */ |
| 7367 | p = tword + sp->ts_twordlen |
| 7368 | - sp->ts_tcharlen; |
| 7369 | c = mb_ptr2char(p); |
| 7370 | mb_ptr_back(tword, p); |
| 7371 | if (c == mb_ptr2char(p)) |
| 7372 | sp->ts_score -= SCORE_INS |
| 7373 | - SCORE_INSDUP; |
| 7374 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7375 | |
| 7376 | /* Starting a new char, reset the length. */ |
| 7377 | sp->ts_tcharlen = 0; |
| 7378 | } |
| 7379 | } |
| 7380 | else |
| 7381 | #endif |
| 7382 | { |
| 7383 | /* If we found a similar char adjust the score. |
| 7384 | * We do this after calling try_deeper() because |
| 7385 | * it's slow. */ |
| 7386 | if (newscore != 0 |
| 7387 | && lp->lp_slang->sl_has_map |
| 7388 | && similar_chars(lp->lp_slang, |
| 7389 | c, fword[sp->ts_fidx - 1])) |
| 7390 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 7391 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7392 | } |
| 7393 | } |
| 7394 | break; |
| 7395 | |
| 7396 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7397 | #ifdef FEAT_MBYTE |
| 7398 | /* When past the first byte of a multi-byte char don't try |
| 7399 | * delete/insert/swap a character. */ |
| 7400 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 7401 | { |
| 7402 | sp->ts_state = STATE_FINAL; |
| 7403 | break; |
| 7404 | } |
| 7405 | #endif |
| 7406 | /* |
| 7407 | * Try skipping one character in the bad word (delete it). |
| 7408 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7409 | sp->ts_state = STATE_INS; |
| 7410 | sp->ts_curi = 1; |
| 7411 | if (fword[sp->ts_fidx] != NUL |
| 7412 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 7413 | { |
| 7414 | ++depth; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7415 | |
| 7416 | /* Advance over the character in fword[]. Give a bonus to |
| 7417 | * the score if the same character is following "nn" -> |
| 7418 | * "n". */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7419 | #ifdef FEAT_MBYTE |
| 7420 | if (has_mbyte) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7421 | { |
| 7422 | c = mb_ptr2char(fword + sp->ts_fidx); |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7423 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7424 | if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) |
| 7425 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 7426 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7427 | else |
| 7428 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7429 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7430 | ++stack[depth].ts_fidx; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7431 | if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) |
| 7432 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 7433 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7434 | break; |
| 7435 | } |
| 7436 | /*FALLTHROUGH*/ |
| 7437 | |
| 7438 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7439 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7440 | * node. */ |
| 7441 | n = sp->ts_arridx; |
| 7442 | if (sp->ts_curi > byts[n]) |
| 7443 | { |
| 7444 | /* Done all bytes at this node, do next state. */ |
| 7445 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7446 | } |
| 7447 | else |
| 7448 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7449 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7450 | n += sp->ts_curi++; |
| 7451 | c = byts[n]; |
| 7452 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 7453 | { |
| 7454 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7455 | sp = &stack[depth]; |
| 7456 | tword[sp->ts_twordlen++] = c; |
| 7457 | sp->ts_arridx = idxs[n]; |
| 7458 | #ifdef FEAT_MBYTE |
| 7459 | if (has_mbyte) |
| 7460 | { |
| 7461 | fl = MB_BYTE2LEN(c); |
| 7462 | if (fl > 1) |
| 7463 | { |
| 7464 | /* There are following bytes for the same |
| 7465 | * character. We must find all bytes before |
| 7466 | * trying delete/insert/swap/etc. */ |
| 7467 | sp->ts_tcharlen = fl; |
| 7468 | sp->ts_tcharidx = 1; |
| 7469 | sp->ts_isdiff = DIFF_INSERT; |
| 7470 | } |
| 7471 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7472 | else |
| 7473 | fl = 1; |
| 7474 | if (fl == 1) |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7475 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7476 | { |
| 7477 | /* If the previous character was the same, thus |
| 7478 | * doubling a character, give a bonus to the |
| 7479 | * score. */ |
| 7480 | if (sp->ts_twordlen >= 2 |
| 7481 | && tword[sp->ts_twordlen - 2] == c) |
| 7482 | sp->ts_score -= SCORE_INS - SCORE_INSDUP; |
| 7483 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7484 | } |
| 7485 | } |
| 7486 | break; |
| 7487 | |
| 7488 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7489 | /* |
| 7490 | * Swap two bytes in the bad word: "12" -> "21". |
| 7491 | * We change "fword" here, it's changed back afterwards. |
| 7492 | */ |
| 7493 | p = fword + sp->ts_fidx; |
| 7494 | c = *p; |
| 7495 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7496 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7497 | /* End of word, can't swap or replace. */ |
| 7498 | sp->ts_state = STATE_FINAL; |
| 7499 | break; |
| 7500 | } |
| 7501 | #ifdef FEAT_MBYTE |
| 7502 | if (has_mbyte) |
| 7503 | { |
| 7504 | n = mb_ptr2len_check(p); |
| 7505 | c = mb_ptr2char(p); |
| 7506 | c2 = mb_ptr2char(p + n); |
| 7507 | } |
| 7508 | else |
| 7509 | #endif |
| 7510 | c2 = p[1]; |
| 7511 | if (c == c2) |
| 7512 | { |
| 7513 | /* Characters are identical, swap won't do anything. */ |
| 7514 | sp->ts_state = STATE_SWAP3; |
| 7515 | break; |
| 7516 | } |
| 7517 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 7518 | { |
| 7519 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7520 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7521 | #ifdef FEAT_MBYTE |
| 7522 | if (has_mbyte) |
| 7523 | { |
| 7524 | fl = mb_char2len(c2); |
| 7525 | mch_memmove(p, p + n, fl); |
| 7526 | mb_char2bytes(c, p + fl); |
| 7527 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 7528 | } |
| 7529 | else |
| 7530 | #endif |
| 7531 | { |
| 7532 | p[0] = c2; |
| 7533 | p[1] = c; |
| 7534 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 7535 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7536 | } |
| 7537 | else |
| 7538 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 7539 | sp->ts_state = STATE_REP_INI; |
| 7540 | break; |
| 7541 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7542 | case STATE_UNSWAP: |
| 7543 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 7544 | p = fword + sp->ts_fidx; |
| 7545 | #ifdef FEAT_MBYTE |
| 7546 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7547 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7548 | n = MB_BYTE2LEN(*p); |
| 7549 | c = mb_ptr2char(p + n); |
| 7550 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 7551 | mb_char2bytes(c, p); |
| 7552 | } |
| 7553 | else |
| 7554 | #endif |
| 7555 | { |
| 7556 | c = *p; |
| 7557 | *p = p[1]; |
| 7558 | p[1] = c; |
| 7559 | } |
| 7560 | /*FALLTHROUGH*/ |
| 7561 | |
| 7562 | case STATE_SWAP3: |
| 7563 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 7564 | * "fword" here, it's changed back afterwards. */ |
| 7565 | p = fword + sp->ts_fidx; |
| 7566 | #ifdef FEAT_MBYTE |
| 7567 | if (has_mbyte) |
| 7568 | { |
| 7569 | n = mb_ptr2len_check(p); |
| 7570 | c = mb_ptr2char(p); |
| 7571 | fl = mb_ptr2len_check(p + n); |
| 7572 | c2 = mb_ptr2char(p + n); |
| 7573 | c3 = mb_ptr2char(p + n + fl); |
| 7574 | } |
| 7575 | else |
| 7576 | #endif |
| 7577 | { |
| 7578 | c = *p; |
| 7579 | c2 = p[1]; |
| 7580 | c3 = p[2]; |
| 7581 | } |
| 7582 | |
| 7583 | /* When characters are identical: "121" then SWAP3 result is |
| 7584 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 7585 | * result is same as SWAP on next char: "112". Thus skip all |
| 7586 | * swapping. Also skip when c3 is NUL. */ |
| 7587 | if (c == c3 || c3 == NUL) |
| 7588 | { |
| 7589 | sp->ts_state = STATE_REP_INI; |
| 7590 | break; |
| 7591 | } |
| 7592 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7593 | { |
| 7594 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7595 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7596 | #ifdef FEAT_MBYTE |
| 7597 | if (has_mbyte) |
| 7598 | { |
| 7599 | tl = mb_char2len(c3); |
| 7600 | mch_memmove(p, p + n + fl, tl); |
| 7601 | mb_char2bytes(c2, p + tl); |
| 7602 | mb_char2bytes(c, p + fl + tl); |
| 7603 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 7604 | } |
| 7605 | else |
| 7606 | #endif |
| 7607 | { |
| 7608 | p[0] = p[2]; |
| 7609 | p[2] = c; |
| 7610 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7611 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7612 | } |
| 7613 | else |
| 7614 | sp->ts_state = STATE_REP_INI; |
| 7615 | break; |
| 7616 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7617 | case STATE_UNSWAP3: |
| 7618 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 7619 | p = fword + sp->ts_fidx; |
| 7620 | #ifdef FEAT_MBYTE |
| 7621 | if (has_mbyte) |
| 7622 | { |
| 7623 | n = MB_BYTE2LEN(*p); |
| 7624 | c2 = mb_ptr2char(p + n); |
| 7625 | fl = MB_BYTE2LEN(p[n]); |
| 7626 | c = mb_ptr2char(p + n + fl); |
| 7627 | tl = MB_BYTE2LEN(p[n + fl]); |
| 7628 | mch_memmove(p + fl + tl, p, n); |
| 7629 | mb_char2bytes(c, p); |
| 7630 | mb_char2bytes(c2, p + tl); |
| 7631 | } |
| 7632 | else |
| 7633 | #endif |
| 7634 | { |
| 7635 | c = *p; |
| 7636 | *p = p[2]; |
| 7637 | p[2] = c; |
| 7638 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7639 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7640 | /* Rotate three characters left: "123" -> "231". We change |
| 7641 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7642 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7643 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7644 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7645 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7646 | p = fword + sp->ts_fidx; |
| 7647 | #ifdef FEAT_MBYTE |
| 7648 | if (has_mbyte) |
| 7649 | { |
| 7650 | n = mb_ptr2len_check(p); |
| 7651 | c = mb_ptr2char(p); |
| 7652 | fl = mb_ptr2len_check(p + n); |
| 7653 | fl += mb_ptr2len_check(p + n + fl); |
| 7654 | mch_memmove(p, p + n, fl); |
| 7655 | mb_char2bytes(c, p + fl); |
| 7656 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 7657 | } |
| 7658 | else |
| 7659 | #endif |
| 7660 | { |
| 7661 | c = *p; |
| 7662 | *p = p[1]; |
| 7663 | p[1] = p[2]; |
| 7664 | p[2] = c; |
| 7665 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7666 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7667 | } |
| 7668 | else |
| 7669 | sp->ts_state = STATE_REP_INI; |
| 7670 | break; |
| 7671 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7672 | case STATE_UNROT3L: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7673 | /* Undo ROT3L: "231" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7674 | p = fword + sp->ts_fidx; |
| 7675 | #ifdef FEAT_MBYTE |
| 7676 | if (has_mbyte) |
| 7677 | { |
| 7678 | n = MB_BYTE2LEN(*p); |
| 7679 | n += MB_BYTE2LEN(p[n]); |
| 7680 | c = mb_ptr2char(p + n); |
| 7681 | tl = MB_BYTE2LEN(p[n]); |
| 7682 | mch_memmove(p + tl, p, n); |
| 7683 | mb_char2bytes(c, p); |
| 7684 | } |
| 7685 | else |
| 7686 | #endif |
| 7687 | { |
| 7688 | c = p[2]; |
| 7689 | p[2] = p[1]; |
| 7690 | p[1] = *p; |
| 7691 | *p = c; |
| 7692 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7693 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7694 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7695 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7696 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7697 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7698 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7699 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7700 | p = fword + sp->ts_fidx; |
| 7701 | #ifdef FEAT_MBYTE |
| 7702 | if (has_mbyte) |
| 7703 | { |
| 7704 | n = mb_ptr2len_check(p); |
| 7705 | n += mb_ptr2len_check(p + n); |
| 7706 | c = mb_ptr2char(p + n); |
| 7707 | tl = mb_ptr2len_check(p + n); |
| 7708 | mch_memmove(p + tl, p, n); |
| 7709 | mb_char2bytes(c, p); |
| 7710 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 7711 | } |
| 7712 | else |
| 7713 | #endif |
| 7714 | { |
| 7715 | c = p[2]; |
| 7716 | p[2] = p[1]; |
| 7717 | p[1] = *p; |
| 7718 | *p = c; |
| 7719 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7720 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7721 | } |
| 7722 | else |
| 7723 | sp->ts_state = STATE_REP_INI; |
| 7724 | break; |
| 7725 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7726 | case STATE_UNROT3R: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7727 | /* Undo ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7728 | p = fword + sp->ts_fidx; |
| 7729 | #ifdef FEAT_MBYTE |
| 7730 | if (has_mbyte) |
| 7731 | { |
| 7732 | c = mb_ptr2char(p); |
| 7733 | tl = MB_BYTE2LEN(*p); |
| 7734 | n = MB_BYTE2LEN(p[tl]); |
| 7735 | n += MB_BYTE2LEN(p[tl + n]); |
| 7736 | mch_memmove(p, p + tl, n); |
| 7737 | mb_char2bytes(c, p + n); |
| 7738 | } |
| 7739 | else |
| 7740 | #endif |
| 7741 | { |
| 7742 | c = *p; |
| 7743 | *p = p[1]; |
| 7744 | p[1] = p[2]; |
| 7745 | p[2] = c; |
| 7746 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7747 | /*FALLTHROUGH*/ |
| 7748 | |
| 7749 | case STATE_REP_INI: |
| 7750 | /* Check if matching with REP items from the .aff file would |
| 7751 | * work. Quickly skip if there are no REP items or the score |
| 7752 | * is going to be too high anyway. */ |
| 7753 | gap = &lp->lp_slang->sl_rep; |
| 7754 | if (gap->ga_len == 0 |
| 7755 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 7756 | { |
| 7757 | sp->ts_state = STATE_FINAL; |
| 7758 | break; |
| 7759 | } |
| 7760 | |
| 7761 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7762 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7763 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 7764 | if (sp->ts_curi < 0) |
| 7765 | { |
| 7766 | sp->ts_state = STATE_FINAL; |
| 7767 | break; |
| 7768 | } |
| 7769 | |
| 7770 | sp->ts_state = STATE_REP; |
| 7771 | /*FALLTHROUGH*/ |
| 7772 | |
| 7773 | case STATE_REP: |
| 7774 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7775 | * match replace the characters and check if the resulting |
| 7776 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7777 | p = fword + sp->ts_fidx; |
| 7778 | |
| 7779 | gap = &lp->lp_slang->sl_rep; |
| 7780 | while (sp->ts_curi < gap->ga_len) |
| 7781 | { |
| 7782 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 7783 | if (*ftp->ft_from != *p) |
| 7784 | { |
| 7785 | /* past possible matching entries */ |
| 7786 | sp->ts_curi = gap->ga_len; |
| 7787 | break; |
| 7788 | } |
| 7789 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 7790 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 7791 | { |
| 7792 | /* Need to undo this afterwards. */ |
| 7793 | sp->ts_state = STATE_REP_UNDO; |
| 7794 | |
| 7795 | /* Change the "from" to the "to" string. */ |
| 7796 | ++depth; |
| 7797 | fl = STRLEN(ftp->ft_from); |
| 7798 | tl = STRLEN(ftp->ft_to); |
| 7799 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7800 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7801 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7802 | repextra += tl - fl; |
| 7803 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7804 | mch_memmove(p, ftp->ft_to, tl); |
| 7805 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7806 | #ifdef FEAT_MBYTE |
| 7807 | stack[depth].ts_tcharlen = 0; |
| 7808 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7809 | break; |
| 7810 | } |
| 7811 | } |
| 7812 | |
| 7813 | if (sp->ts_curi >= gap->ga_len) |
| 7814 | /* No (more) matches. */ |
| 7815 | sp->ts_state = STATE_FINAL; |
| 7816 | |
| 7817 | break; |
| 7818 | |
| 7819 | case STATE_REP_UNDO: |
| 7820 | /* Undo a REP replacement and continue with the next one. */ |
| 7821 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 7822 | + sp->ts_curi - 1; |
| 7823 | fl = STRLEN(ftp->ft_from); |
| 7824 | tl = STRLEN(ftp->ft_to); |
| 7825 | p = fword + sp->ts_fidx; |
| 7826 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7827 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7828 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7829 | repextra -= tl - fl; |
| 7830 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7831 | mch_memmove(p, ftp->ft_from, fl); |
| 7832 | sp->ts_state = STATE_REP; |
| 7833 | break; |
| 7834 | |
| 7835 | default: |
| 7836 | /* Did all possible states at this level, go up one level. */ |
| 7837 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7838 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7839 | if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE) |
| 7840 | { |
| 7841 | /* Continue in or go back to the prefix tree. */ |
| 7842 | byts = pbyts; |
| 7843 | idxs = pidxs; |
| 7844 | splitoff = 0; |
| 7845 | } |
| 7846 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7847 | /* Don't check for CTRL-C too often, it takes time. */ |
| 7848 | line_breakcheck(); |
| 7849 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7850 | } |
| 7851 | } |
| 7852 | } |
| 7853 | |
| 7854 | /* |
| 7855 | * Try going one level deeper in the tree. |
| 7856 | */ |
| 7857 | static int |
| 7858 | try_deeper(su, stack, depth, score_add) |
| 7859 | suginfo_T *su; |
| 7860 | trystate_T *stack; |
| 7861 | int depth; |
| 7862 | int score_add; |
| 7863 | { |
| 7864 | int newscore; |
| 7865 | |
| 7866 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 7867 | newscore = stack[depth].ts_score + score_add; |
| 7868 | if (newscore >= su->su_maxscore) |
| 7869 | return FALSE; |
| 7870 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7871 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7872 | stack[depth + 1].ts_state = STATE_START; |
| 7873 | stack[depth + 1].ts_score = newscore; |
| 7874 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7875 | return TRUE; |
| 7876 | } |
| 7877 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 7878 | #ifdef FEAT_MBYTE |
| 7879 | /* |
| 7880 | * Case-folding may change the number of bytes: Count nr of chars in |
| 7881 | * fword[flen] and return the byte length of that many chars in "word". |
| 7882 | */ |
| 7883 | static int |
| 7884 | nofold_len(fword, flen, word) |
| 7885 | char_u *fword; |
| 7886 | int flen; |
| 7887 | char_u *word; |
| 7888 | { |
| 7889 | char_u *p; |
| 7890 | int i = 0; |
| 7891 | |
| 7892 | for (p = fword; p < fword + flen; mb_ptr_adv(p)) |
| 7893 | ++i; |
| 7894 | for (p = word; i > 0; mb_ptr_adv(p)) |
| 7895 | --i; |
| 7896 | return (int)(p - word); |
| 7897 | } |
| 7898 | #endif |
| 7899 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7900 | /* |
| 7901 | * "fword" is a good word with case folded. Find the matching keep-case |
| 7902 | * words and put it in "kword". |
| 7903 | * Theoretically there could be several keep-case words that result in the |
| 7904 | * same case-folded word, but we only find one... |
| 7905 | */ |
| 7906 | static void |
| 7907 | find_keepcap_word(slang, fword, kword) |
| 7908 | slang_T *slang; |
| 7909 | char_u *fword; |
| 7910 | char_u *kword; |
| 7911 | { |
| 7912 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 7913 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7914 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7915 | |
| 7916 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7917 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7918 | int round[MAXWLEN]; |
| 7919 | int fwordidx[MAXWLEN]; |
| 7920 | int uwordidx[MAXWLEN]; |
| 7921 | int kwordlen[MAXWLEN]; |
| 7922 | |
| 7923 | int flen, ulen; |
| 7924 | int l; |
| 7925 | int len; |
| 7926 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7927 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7928 | char_u *p; |
| 7929 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7930 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7931 | |
| 7932 | if (byts == NULL) |
| 7933 | { |
| 7934 | /* array is empty: "cannot happen" */ |
| 7935 | *kword = NUL; |
| 7936 | return; |
| 7937 | } |
| 7938 | |
| 7939 | /* Make an all-cap version of "fword". */ |
| 7940 | allcap_copy(fword, uword); |
| 7941 | |
| 7942 | /* |
| 7943 | * Each character needs to be tried both case-folded and upper-case. |
| 7944 | * All this gets very complicated if we keep in mind that changing case |
| 7945 | * may change the byte length of a multi-byte character... |
| 7946 | */ |
| 7947 | depth = 0; |
| 7948 | arridx[0] = 0; |
| 7949 | round[0] = 0; |
| 7950 | fwordidx[0] = 0; |
| 7951 | uwordidx[0] = 0; |
| 7952 | kwordlen[0] = 0; |
| 7953 | while (depth >= 0) |
| 7954 | { |
| 7955 | if (fword[fwordidx[depth]] == NUL) |
| 7956 | { |
| 7957 | /* We are at the end of "fword". If the tree allows a word to end |
| 7958 | * here we have found a match. */ |
| 7959 | if (byts[arridx[depth] + 1] == 0) |
| 7960 | { |
| 7961 | kword[kwordlen[depth]] = NUL; |
| 7962 | return; |
| 7963 | } |
| 7964 | |
| 7965 | /* kword is getting too long, continue one level up */ |
| 7966 | --depth; |
| 7967 | } |
| 7968 | else if (++round[depth] > 2) |
| 7969 | { |
| 7970 | /* tried both fold-case and upper-case character, continue one |
| 7971 | * level up */ |
| 7972 | --depth; |
| 7973 | } |
| 7974 | else |
| 7975 | { |
| 7976 | /* |
| 7977 | * round[depth] == 1: Try using the folded-case character. |
| 7978 | * round[depth] == 2: Try using the upper-case character. |
| 7979 | */ |
| 7980 | #ifdef FEAT_MBYTE |
| 7981 | if (has_mbyte) |
| 7982 | { |
| 7983 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 7984 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 7985 | } |
| 7986 | else |
| 7987 | #endif |
| 7988 | ulen = flen = 1; |
| 7989 | if (round[depth] == 1) |
| 7990 | { |
| 7991 | p = fword + fwordidx[depth]; |
| 7992 | l = flen; |
| 7993 | } |
| 7994 | else |
| 7995 | { |
| 7996 | p = uword + uwordidx[depth]; |
| 7997 | l = ulen; |
| 7998 | } |
| 7999 | |
| 8000 | for (tryidx = arridx[depth]; l > 0; --l) |
| 8001 | { |
| 8002 | /* Perform a binary search in the list of accepted bytes. */ |
| 8003 | len = byts[tryidx++]; |
| 8004 | c = *p++; |
| 8005 | lo = tryidx; |
| 8006 | hi = tryidx + len - 1; |
| 8007 | while (lo < hi) |
| 8008 | { |
| 8009 | m = (lo + hi) / 2; |
| 8010 | if (byts[m] > c) |
| 8011 | hi = m - 1; |
| 8012 | else if (byts[m] < c) |
| 8013 | lo = m + 1; |
| 8014 | else |
| 8015 | { |
| 8016 | lo = hi = m; |
| 8017 | break; |
| 8018 | } |
| 8019 | } |
| 8020 | |
| 8021 | /* Stop if there is no matching byte. */ |
| 8022 | if (hi < lo || byts[lo] != c) |
| 8023 | break; |
| 8024 | |
| 8025 | /* Continue at the child (if there is one). */ |
| 8026 | tryidx = idxs[lo]; |
| 8027 | } |
| 8028 | |
| 8029 | if (l == 0) |
| 8030 | { |
| 8031 | /* |
| 8032 | * Found the matching char. Copy it to "kword" and go a |
| 8033 | * level deeper. |
| 8034 | */ |
| 8035 | if (round[depth] == 1) |
| 8036 | { |
| 8037 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 8038 | flen); |
| 8039 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 8040 | } |
| 8041 | else |
| 8042 | { |
| 8043 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 8044 | ulen); |
| 8045 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 8046 | } |
| 8047 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 8048 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 8049 | |
| 8050 | ++depth; |
| 8051 | arridx[depth] = tryidx; |
| 8052 | round[depth] = 0; |
| 8053 | } |
| 8054 | } |
| 8055 | } |
| 8056 | |
| 8057 | /* Didn't find it: "cannot happen". */ |
| 8058 | *kword = NUL; |
| 8059 | } |
| 8060 | |
| 8061 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8062 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 8063 | * su->su_sga. |
| 8064 | */ |
| 8065 | static void |
| 8066 | score_comp_sal(su) |
| 8067 | suginfo_T *su; |
| 8068 | { |
| 8069 | langp_T *lp; |
| 8070 | char_u badsound[MAXWLEN]; |
| 8071 | int i; |
| 8072 | suggest_T *stp; |
| 8073 | suggest_T *sstp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8074 | int score; |
| 8075 | |
| 8076 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 8077 | return; |
| 8078 | |
| 8079 | /* Use the sound-folding of the first language that supports it. */ |
| 8080 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8081 | lp->lp_slang != NULL; ++lp) |
| 8082 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8083 | { |
| 8084 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8085 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8086 | |
| 8087 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 8088 | { |
| 8089 | stp = &SUG(su->su_ga, i); |
| 8090 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8091 | /* Case-fold the suggested word, sound-fold it and compute the |
| 8092 | * sound-a-like score. */ |
| 8093 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8094 | if (score < SCORE_MAXMAX) |
| 8095 | { |
| 8096 | /* Add the suggestion. */ |
| 8097 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 8098 | sstp->st_word = vim_strsave(stp->st_word); |
| 8099 | if (sstp->st_word != NULL) |
| 8100 | { |
| 8101 | sstp->st_score = score; |
| 8102 | sstp->st_altscore = 0; |
| 8103 | sstp->st_orglen = stp->st_orglen; |
| 8104 | ++su->su_sga.ga_len; |
| 8105 | } |
| 8106 | } |
| 8107 | } |
| 8108 | break; |
| 8109 | } |
| 8110 | } |
| 8111 | |
| 8112 | /* |
| 8113 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 8114 | * They are intwined. |
| 8115 | */ |
| 8116 | static void |
| 8117 | score_combine(su) |
| 8118 | suginfo_T *su; |
| 8119 | { |
| 8120 | int i; |
| 8121 | int j; |
| 8122 | garray_T ga; |
| 8123 | garray_T *gap; |
| 8124 | langp_T *lp; |
| 8125 | suggest_T *stp; |
| 8126 | char_u *p; |
| 8127 | char_u badsound[MAXWLEN]; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8128 | int round; |
| 8129 | |
| 8130 | /* Add the alternate score to su_ga. */ |
| 8131 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8132 | lp->lp_slang != NULL; ++lp) |
| 8133 | { |
| 8134 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8135 | { |
| 8136 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8137 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8138 | |
| 8139 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 8140 | { |
| 8141 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8142 | stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang, |
| 8143 | badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8144 | if (stp->st_altscore == SCORE_MAXMAX) |
| 8145 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 8146 | else |
| 8147 | stp->st_score = (stp->st_score * 3 |
| 8148 | + stp->st_altscore) / 4; |
| 8149 | stp->st_salscore = FALSE; |
| 8150 | } |
| 8151 | break; |
| 8152 | } |
| 8153 | } |
| 8154 | |
| 8155 | /* Add the alternate score to su_sga. */ |
| 8156 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 8157 | { |
| 8158 | stp = &SUG(su->su_sga, i); |
| 8159 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 8160 | if (stp->st_score == SCORE_MAXMAX) |
| 8161 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 8162 | else |
| 8163 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 8164 | stp->st_salscore = TRUE; |
| 8165 | } |
| 8166 | |
| 8167 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 8168 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 8169 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 8170 | |
| 8171 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 8172 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 8173 | return; |
| 8174 | |
| 8175 | stp = &SUG(ga, 0); |
| 8176 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 8177 | { |
| 8178 | /* round 1: get a suggestion from su_ga |
| 8179 | * round 2: get a suggestion from su_sga */ |
| 8180 | for (round = 1; round <= 2; ++round) |
| 8181 | { |
| 8182 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 8183 | if (i < gap->ga_len) |
| 8184 | { |
| 8185 | /* Don't add a word if it's already there. */ |
| 8186 | p = SUG(*gap, i).st_word; |
| 8187 | for (j = 0; j < ga.ga_len; ++j) |
| 8188 | if (STRCMP(stp[j].st_word, p) == 0) |
| 8189 | break; |
| 8190 | if (j == ga.ga_len) |
| 8191 | stp[ga.ga_len++] = SUG(*gap, i); |
| 8192 | else |
| 8193 | vim_free(p); |
| 8194 | } |
| 8195 | } |
| 8196 | } |
| 8197 | |
| 8198 | ga_clear(&su->su_ga); |
| 8199 | ga_clear(&su->su_sga); |
| 8200 | |
| 8201 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 8202 | if (ga.ga_len > su->su_maxcount) |
| 8203 | { |
| 8204 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 8205 | vim_free(stp[i].st_word); |
| 8206 | ga.ga_len = su->su_maxcount; |
| 8207 | } |
| 8208 | |
| 8209 | su->su_ga = ga; |
| 8210 | } |
| 8211 | |
| 8212 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8213 | * For the goodword in "stp" compute the soundalike score compared to the |
| 8214 | * badword. |
| 8215 | */ |
| 8216 | static int |
| 8217 | stp_sal_score(stp, su, slang, badsound) |
| 8218 | suggest_T *stp; |
| 8219 | suginfo_T *su; |
| 8220 | slang_T *slang; |
| 8221 | char_u *badsound; /* sound-folded badword */ |
| 8222 | { |
| 8223 | char_u *p; |
| 8224 | char_u badsound2[MAXWLEN]; |
| 8225 | char_u fword[MAXWLEN]; |
| 8226 | char_u goodsound[MAXWLEN]; |
| 8227 | |
| 8228 | if (stp->st_orglen <= su->su_badlen) |
| 8229 | p = badsound; |
| 8230 | else |
| 8231 | { |
| 8232 | /* soundfold the bad word with more characters following */ |
| 8233 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 8234 | |
| 8235 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 8236 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 8237 | * removing the space. Don't do it when the good word also contains a |
| 8238 | * space. */ |
| 8239 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 8240 | && *skiptowhite(stp->st_word) == NUL) |
| 8241 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| 8242 | mch_memmove(p, p + 1, STRLEN(p)); |
| 8243 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8244 | spell_soundfold(slang, fword, TRUE, badsound2); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8245 | p = badsound2; |
| 8246 | } |
| 8247 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8248 | /* Sound-fold the word and compute the score for the difference. */ |
| 8249 | spell_soundfold(slang, stp->st_word, FALSE, goodsound); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8250 | |
| 8251 | return soundalike_score(goodsound, p); |
| 8252 | } |
| 8253 | |
| 8254 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8255 | * Find suggestions by comparing the word in a sound-a-like form. |
| 8256 | */ |
| 8257 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8258 | suggest_try_soundalike(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8259 | suginfo_T *su; |
| 8260 | { |
| 8261 | char_u salword[MAXWLEN]; |
| 8262 | char_u tword[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8263 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8264 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8265 | int curi[MAXWLEN]; |
| 8266 | langp_T *lp; |
| 8267 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8268 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8269 | int depth; |
| 8270 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8271 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8272 | int round; |
| 8273 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8274 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8275 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8276 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8277 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8278 | lp->lp_slang != NULL; ++lp) |
| 8279 | { |
| 8280 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8281 | { |
| 8282 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8283 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8284 | |
| 8285 | /* |
| 8286 | * Go through the whole tree, soundfold each word and compare. |
| 8287 | * round 1: use the case-folded tree. |
| 8288 | * round 2: use the keep-case tree. |
| 8289 | */ |
| 8290 | for (round = 1; round <= 2; ++round) |
| 8291 | { |
| 8292 | if (round == 1) |
| 8293 | { |
| 8294 | byts = lp->lp_slang->sl_fbyts; |
| 8295 | idxs = lp->lp_slang->sl_fidxs; |
| 8296 | } |
| 8297 | else |
| 8298 | { |
| 8299 | byts = lp->lp_slang->sl_kbyts; |
| 8300 | idxs = lp->lp_slang->sl_kidxs; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 8301 | if (byts == NULL) /* no keep-case words */ |
| 8302 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8303 | } |
| 8304 | |
| 8305 | depth = 0; |
| 8306 | arridx[0] = 0; |
| 8307 | curi[0] = 1; |
| 8308 | while (depth >= 0 && !got_int) |
| 8309 | { |
| 8310 | if (curi[depth] > byts[arridx[depth]]) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8311 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8312 | /* Done all bytes at this node, go up one level. */ |
| 8313 | --depth; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8314 | line_breakcheck(); |
| 8315 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8316 | else |
| 8317 | { |
| 8318 | /* Do one more byte at this node. */ |
| 8319 | n = arridx[depth] + curi[depth]; |
| 8320 | ++curi[depth]; |
| 8321 | c = byts[n]; |
| 8322 | if (c == 0) |
| 8323 | { |
| 8324 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8325 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8326 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 8327 | { |
| 8328 | tword[depth] = NUL; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8329 | /* Sound-fold. Only in keep-case tree need to |
| 8330 | * case-fold the word. */ |
| 8331 | spell_soundfold(lp->lp_slang, tword, |
| 8332 | round == 1, tsalword); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8333 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8334 | /* Compute the edit distance between the |
| 8335 | * sound-a-like words. */ |
| 8336 | sound_score = soundalike_score(salword, |
| 8337 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8338 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8339 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8340 | char_u cword[MAXWLEN]; |
| 8341 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8342 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8343 | |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 8344 | flags |= su->su_badflags; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8345 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8346 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8347 | /* Need to fix case according to |
| 8348 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8349 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8350 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8351 | } |
| 8352 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8353 | p = tword; |
| 8354 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8355 | if (sps_flags & SPS_DOUBLE) |
| 8356 | add_suggestion(su, &su->su_sga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8357 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8358 | sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8359 | else |
| 8360 | { |
| 8361 | /* Compute the score. */ |
| 8362 | score = spell_edit_score( |
| 8363 | su->su_badword, p); |
| 8364 | if (sps_flags & SPS_BEST) |
| 8365 | /* give a bonus for the good word |
| 8366 | * sounding the same as the bad |
| 8367 | * word */ |
| 8368 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8369 | su->su_badlen, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8370 | RESCORE(score, sound_score), |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8371 | sound_score, TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8372 | else |
| 8373 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8374 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8375 | score + sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8376 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8377 | } |
| 8378 | } |
| 8379 | |
| 8380 | /* Skip over other NUL bytes. */ |
| 8381 | while (byts[n + 1] == 0) |
| 8382 | { |
| 8383 | ++n; |
| 8384 | ++curi[depth]; |
| 8385 | } |
| 8386 | } |
| 8387 | else |
| 8388 | { |
| 8389 | /* Normal char, go one level deeper. */ |
| 8390 | tword[depth++] = c; |
| 8391 | arridx[depth] = idxs[n]; |
| 8392 | curi[depth] = 1; |
| 8393 | } |
| 8394 | } |
| 8395 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8396 | } |
| 8397 | } |
| 8398 | } |
| 8399 | } |
| 8400 | |
| 8401 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8402 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8403 | */ |
| 8404 | static void |
| 8405 | make_case_word(fword, cword, flags) |
| 8406 | char_u *fword; |
| 8407 | char_u *cword; |
| 8408 | int flags; |
| 8409 | { |
| 8410 | if (flags & WF_ALLCAP) |
| 8411 | /* Make it all upper-case */ |
| 8412 | allcap_copy(fword, cword); |
| 8413 | else if (flags & WF_ONECAP) |
| 8414 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8415 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8416 | else |
| 8417 | /* Use goodword as-is. */ |
| 8418 | STRCPY(cword, fword); |
| 8419 | } |
| 8420 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8421 | /* |
| 8422 | * Use map string "map" for languages "lp". |
| 8423 | */ |
| 8424 | static void |
| 8425 | set_map_str(lp, map) |
| 8426 | slang_T *lp; |
| 8427 | char_u *map; |
| 8428 | { |
| 8429 | char_u *p; |
| 8430 | int headc = 0; |
| 8431 | int c; |
| 8432 | int i; |
| 8433 | |
| 8434 | if (*map == NUL) |
| 8435 | { |
| 8436 | lp->sl_has_map = FALSE; |
| 8437 | return; |
| 8438 | } |
| 8439 | lp->sl_has_map = TRUE; |
| 8440 | |
| 8441 | /* Init the array and hash table empty. */ |
| 8442 | for (i = 0; i < 256; ++i) |
| 8443 | lp->sl_map_array[i] = 0; |
| 8444 | #ifdef FEAT_MBYTE |
| 8445 | hash_init(&lp->sl_map_hash); |
| 8446 | #endif |
| 8447 | |
| 8448 | /* |
| 8449 | * The similar characters are stored separated with slashes: |
| 8450 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 8451 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 8452 | */ |
| 8453 | for (p = map; *p != NUL; ) |
| 8454 | { |
| 8455 | #ifdef FEAT_MBYTE |
| 8456 | c = mb_ptr2char_adv(&p); |
| 8457 | #else |
| 8458 | c = *p++; |
| 8459 | #endif |
| 8460 | if (c == '/') |
| 8461 | headc = 0; |
| 8462 | else |
| 8463 | { |
| 8464 | if (headc == 0) |
| 8465 | headc = c; |
| 8466 | |
| 8467 | #ifdef FEAT_MBYTE |
| 8468 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 8469 | * the hash table. Each entry is the char, a NUL the headchar and |
| 8470 | * a NUL. */ |
| 8471 | if (c >= 256) |
| 8472 | { |
| 8473 | int cl = mb_char2len(c); |
| 8474 | int headcl = mb_char2len(headc); |
| 8475 | char_u *b; |
| 8476 | hash_T hash; |
| 8477 | hashitem_T *hi; |
| 8478 | |
| 8479 | b = alloc((unsigned)(cl + headcl + 2)); |
| 8480 | if (b == NULL) |
| 8481 | return; |
| 8482 | mb_char2bytes(c, b); |
| 8483 | b[cl] = NUL; |
| 8484 | mb_char2bytes(headc, b + cl + 1); |
| 8485 | b[cl + 1 + headcl] = NUL; |
| 8486 | hash = hash_hash(b); |
| 8487 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 8488 | if (HASHITEM_EMPTY(hi)) |
| 8489 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 8490 | else |
| 8491 | { |
| 8492 | /* This should have been checked when generating the .spl |
| 8493 | * file. */ |
| 8494 | EMSG(_("E999: duplicate char in MAP entry")); |
| 8495 | vim_free(b); |
| 8496 | } |
| 8497 | } |
| 8498 | else |
| 8499 | #endif |
| 8500 | lp->sl_map_array[c] = headc; |
| 8501 | } |
| 8502 | } |
| 8503 | } |
| 8504 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8505 | /* |
| 8506 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 8507 | * lines in the .aff file. |
| 8508 | */ |
| 8509 | static int |
| 8510 | similar_chars(slang, c1, c2) |
| 8511 | slang_T *slang; |
| 8512 | int c1; |
| 8513 | int c2; |
| 8514 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8515 | int m1, m2; |
| 8516 | #ifdef FEAT_MBYTE |
| 8517 | char_u buf[MB_MAXBYTES]; |
| 8518 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8519 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8520 | if (c1 >= 256) |
| 8521 | { |
| 8522 | buf[mb_char2bytes(c1, buf)] = 0; |
| 8523 | hi = hash_find(&slang->sl_map_hash, buf); |
| 8524 | if (HASHITEM_EMPTY(hi)) |
| 8525 | m1 = 0; |
| 8526 | else |
| 8527 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 8528 | } |
| 8529 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8530 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8531 | m1 = slang->sl_map_array[c1]; |
| 8532 | if (m1 == 0) |
| 8533 | return FALSE; |
| 8534 | |
| 8535 | |
| 8536 | #ifdef FEAT_MBYTE |
| 8537 | if (c2 >= 256) |
| 8538 | { |
| 8539 | buf[mb_char2bytes(c2, buf)] = 0; |
| 8540 | hi = hash_find(&slang->sl_map_hash, buf); |
| 8541 | if (HASHITEM_EMPTY(hi)) |
| 8542 | m2 = 0; |
| 8543 | else |
| 8544 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 8545 | } |
| 8546 | else |
| 8547 | #endif |
| 8548 | m2 = slang->sl_map_array[c2]; |
| 8549 | |
| 8550 | return m1 == m2; |
| 8551 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8552 | |
| 8553 | /* |
| 8554 | * Add a suggestion to the list of suggestions. |
| 8555 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 8556 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 8557 | * with spell_edit_score(). |
| 8558 | */ |
| 8559 | static void |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8560 | add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8561 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8562 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8563 | char_u *goodword; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8564 | int badlen; /* length of bad word used */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8565 | int score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8566 | int altscore; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8567 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8568 | { |
| 8569 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8570 | int i; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8571 | char_u *p = NULL; |
| 8572 | int c = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8573 | |
| 8574 | /* Check that the word wasn't banned. */ |
| 8575 | if (was_banned(su, goodword)) |
| 8576 | return; |
| 8577 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8578 | /* If past "su_badlen" and the rest is identical stop at "su_badlen". |
| 8579 | * Remove the common part from "goodword". */ |
| 8580 | i = badlen - su->su_badlen; |
| 8581 | if (i > 0) |
| 8582 | { |
| 8583 | /* This assumes there was no case folding or it didn't change the |
| 8584 | * length... */ |
| 8585 | p = goodword + STRLEN(goodword) - i; |
| 8586 | if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0) |
| 8587 | { |
| 8588 | badlen = su->su_badlen; |
| 8589 | c = *p; |
| 8590 | *p = NUL; |
| 8591 | } |
| 8592 | else |
| 8593 | p = NULL; |
| 8594 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8595 | else if (i < 0) |
| 8596 | { |
| 8597 | /* When replacing part of the word check that we actually change |
| 8598 | * something. For "the the" a suggestion can be replacing the first |
| 8599 | * "the" with itself, since "the" wasn't banned. */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8600 | if (badlen == (int)STRLEN(goodword) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8601 | && STRNCMP(su->su_badword, goodword, badlen) == 0) |
| 8602 | return; |
| 8603 | } |
| 8604 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8605 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8606 | if (score <= su->su_maxscore) |
| 8607 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8608 | /* Check if the word is already there. Also check the length that is |
| 8609 | * being replaced "thes," -> "these" is a different suggestion from |
| 8610 | * "thes" -> "these". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8611 | stp = &SUG(*gap, 0); |
| 8612 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8613 | if (STRCMP(stp[i].st_word, goodword) == 0 |
| 8614 | && stp[i].st_orglen == badlen) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8615 | { |
| 8616 | /* Found it. Remember the lowest score. */ |
| 8617 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8618 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8619 | stp[i].st_score = score; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8620 | stp[i].st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8621 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8622 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8623 | break; |
| 8624 | } |
| 8625 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8626 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8627 | { |
| 8628 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8629 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8630 | stp->st_word = vim_strsave(goodword); |
| 8631 | if (stp->st_word != NULL) |
| 8632 | { |
| 8633 | stp->st_score = score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8634 | stp->st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8635 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8636 | stp->st_orglen = badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8637 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8638 | |
| 8639 | /* If we have too many suggestions now, sort the list and keep |
| 8640 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8641 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 8642 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 8643 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8644 | } |
| 8645 | } |
| 8646 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8647 | |
| 8648 | if (p != NULL) |
| 8649 | *p = c; /* restore "goodword" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8650 | } |
| 8651 | |
| 8652 | /* |
| 8653 | * Add a word to be banned. |
| 8654 | */ |
| 8655 | static void |
| 8656 | add_banned(su, word) |
| 8657 | suginfo_T *su; |
| 8658 | char_u *word; |
| 8659 | { |
| 8660 | char_u *s = vim_strsave(word); |
| 8661 | hash_T hash; |
| 8662 | hashitem_T *hi; |
| 8663 | |
| 8664 | if (s != NULL) |
| 8665 | { |
| 8666 | hash = hash_hash(s); |
| 8667 | hi = hash_lookup(&su->su_banned, s, hash); |
| 8668 | if (HASHITEM_EMPTY(hi)) |
| 8669 | hash_add_item(&su->su_banned, hi, s, hash); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8670 | else |
| 8671 | vim_free(s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8672 | } |
| 8673 | } |
| 8674 | |
| 8675 | /* |
| 8676 | * Return TRUE if a word appears in the list of banned words. |
| 8677 | */ |
| 8678 | static int |
| 8679 | was_banned(su, word) |
| 8680 | suginfo_T *su; |
| 8681 | char_u *word; |
| 8682 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8683 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 8684 | |
| 8685 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8686 | } |
| 8687 | |
| 8688 | /* |
| 8689 | * Free the banned words in "su". |
| 8690 | */ |
| 8691 | static void |
| 8692 | free_banned(su) |
| 8693 | suginfo_T *su; |
| 8694 | { |
| 8695 | int todo; |
| 8696 | hashitem_T *hi; |
| 8697 | |
| 8698 | todo = su->su_banned.ht_used; |
| 8699 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 8700 | { |
| 8701 | if (!HASHITEM_EMPTY(hi)) |
| 8702 | { |
| 8703 | vim_free(hi->hi_key); |
| 8704 | --todo; |
| 8705 | } |
| 8706 | } |
| 8707 | hash_clear(&su->su_banned); |
| 8708 | } |
| 8709 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8710 | /* |
| 8711 | * Recompute the score if sound-folding is possible. This is slow, |
| 8712 | * thus only done for the final results. |
| 8713 | */ |
| 8714 | static void |
| 8715 | rescore_suggestions(su) |
| 8716 | suginfo_T *su; |
| 8717 | { |
| 8718 | langp_T *lp; |
| 8719 | suggest_T *stp; |
| 8720 | char_u sal_badword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8721 | int i; |
| 8722 | |
| 8723 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8724 | lp->lp_slang != NULL; ++lp) |
| 8725 | { |
| 8726 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8727 | { |
| 8728 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8729 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8730 | |
| 8731 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 8732 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8733 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8734 | if (!stp->st_had_bonus) |
| 8735 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8736 | stp->st_altscore = stp_sal_score(stp, su, |
| 8737 | lp->lp_slang, sal_badword); |
| 8738 | if (stp->st_altscore == SCORE_MAXMAX) |
| 8739 | stp->st_altscore = SCORE_BIG; |
| 8740 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8741 | } |
| 8742 | } |
| 8743 | break; |
| 8744 | } |
| 8745 | } |
| 8746 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8747 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8748 | static int |
| 8749 | #ifdef __BORLANDC__ |
| 8750 | _RTLENTRYF |
| 8751 | #endif |
| 8752 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 8753 | |
| 8754 | /* |
| 8755 | * Function given to qsort() to sort the suggestions on st_score. |
| 8756 | */ |
| 8757 | static int |
| 8758 | #ifdef __BORLANDC__ |
| 8759 | _RTLENTRYF |
| 8760 | #endif |
| 8761 | sug_compare(s1, s2) |
| 8762 | const void *s1; |
| 8763 | const void *s2; |
| 8764 | { |
| 8765 | suggest_T *p1 = (suggest_T *)s1; |
| 8766 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8767 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8768 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8769 | if (n == 0) |
| 8770 | return p1->st_altscore - p2->st_altscore; |
| 8771 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
| 8774 | /* |
| 8775 | * Cleanup the suggestions: |
| 8776 | * - Sort on score. |
| 8777 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8778 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8779 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8780 | static int |
| 8781 | cleanup_suggestions(gap, maxscore, keep) |
| 8782 | garray_T *gap; |
| 8783 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8784 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8785 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8786 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8787 | int i; |
| 8788 | |
| 8789 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8790 | 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] | 8791 | |
| 8792 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8793 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8794 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8795 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8796 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8797 | gap->ga_len = keep; |
| 8798 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8799 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8800 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8801 | } |
| 8802 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8803 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 8804 | /* |
| 8805 | * Soundfold a string, for soundfold(). |
| 8806 | * Result is in allocated memory, NULL for an error. |
| 8807 | */ |
| 8808 | char_u * |
| 8809 | eval_soundfold(word) |
| 8810 | char_u *word; |
| 8811 | { |
| 8812 | langp_T *lp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8813 | char_u sound[MAXWLEN]; |
| 8814 | |
| 8815 | if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) |
| 8816 | /* Use the sound-folding of the first language that supports it. */ |
| 8817 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8818 | lp->lp_slang != NULL; ++lp) |
| 8819 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8820 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8821 | /* soundfold the word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8822 | spell_soundfold(lp->lp_slang, word, FALSE, sound); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8823 | return vim_strsave(sound); |
| 8824 | } |
| 8825 | |
| 8826 | /* No language with sound folding, return word as-is. */ |
| 8827 | return vim_strsave(word); |
| 8828 | } |
| 8829 | #endif |
| 8830 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8831 | /* |
| 8832 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 8833 | */ |
| 8834 | static void |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8835 | spell_soundfold(slang, inword, folded, res) |
| 8836 | slang_T *slang; |
| 8837 | char_u *inword; |
| 8838 | int folded; /* "inword" is already case-folded */ |
| 8839 | char_u *res; |
| 8840 | { |
| 8841 | char_u fword[MAXWLEN]; |
| 8842 | char_u *word; |
| 8843 | |
| 8844 | if (slang->sl_sofo) |
| 8845 | /* SOFOFROM and SOFOTO used */ |
| 8846 | spell_soundfold_sofo(slang, inword, res); |
| 8847 | else |
| 8848 | { |
| 8849 | /* SAL items used. Requires the word to be case-folded. */ |
| 8850 | if (folded) |
| 8851 | word = inword; |
| 8852 | else |
| 8853 | { |
| 8854 | (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN); |
| 8855 | word = fword; |
| 8856 | } |
| 8857 | |
| 8858 | #ifdef FEAT_MBYTE |
| 8859 | if (has_mbyte) |
| 8860 | spell_soundfold_wsal(slang, word, res); |
| 8861 | else |
| 8862 | #endif |
| 8863 | spell_soundfold_sal(slang, word, res); |
| 8864 | } |
| 8865 | } |
| 8866 | |
| 8867 | /* |
| 8868 | * Perform sound folding of "inword" into "res" according to SOFOFROM and |
| 8869 | * SOFOTO lines. |
| 8870 | */ |
| 8871 | static void |
| 8872 | spell_soundfold_sofo(slang, inword, res) |
| 8873 | slang_T *slang; |
| 8874 | char_u *inword; |
| 8875 | char_u *res; |
| 8876 | { |
| 8877 | char_u *s; |
| 8878 | int ri = 0; |
| 8879 | int c; |
| 8880 | |
| 8881 | #ifdef FEAT_MBYTE |
| 8882 | if (has_mbyte) |
| 8883 | { |
| 8884 | int prevc = 0; |
| 8885 | int *ip; |
| 8886 | |
| 8887 | /* The sl_sal_first[] table contains the translation for chars up to |
| 8888 | * 255, sl_sal the rest. */ |
| 8889 | for (s = inword; *s != NUL; ) |
| 8890 | { |
| 8891 | c = mb_ptr2char_adv(&s); |
| 8892 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 8893 | c = ' '; |
| 8894 | else if (c < 256) |
| 8895 | c = slang->sl_sal_first[c]; |
| 8896 | else |
| 8897 | { |
| 8898 | ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; |
| 8899 | if (ip == NULL) /* empty list, can't match */ |
| 8900 | c = NUL; |
| 8901 | else |
| 8902 | for (;;) /* find "c" in the list */ |
| 8903 | { |
| 8904 | if (*ip == 0) /* not found */ |
| 8905 | { |
| 8906 | c = NUL; |
| 8907 | break; |
| 8908 | } |
| 8909 | if (*ip == c) /* match! */ |
| 8910 | { |
| 8911 | c = ip[1]; |
| 8912 | break; |
| 8913 | } |
| 8914 | ip += 2; |
| 8915 | } |
| 8916 | } |
| 8917 | |
| 8918 | if (c != NUL && c != prevc) |
| 8919 | { |
| 8920 | ri += mb_char2bytes(c, res + ri); |
| 8921 | if (ri + MB_MAXBYTES > MAXWLEN) |
| 8922 | break; |
| 8923 | prevc = c; |
| 8924 | } |
| 8925 | } |
| 8926 | } |
| 8927 | else |
| 8928 | #endif |
| 8929 | { |
| 8930 | /* The sl_sal_first[] table contains the translation. */ |
| 8931 | for (s = inword; (c = *s) != NUL; ++s) |
| 8932 | { |
| 8933 | if (vim_iswhite(c)) |
| 8934 | c = ' '; |
| 8935 | else |
| 8936 | c = slang->sl_sal_first[c]; |
| 8937 | if (c != NUL && (ri == 0 || res[ri - 1] != c)) |
| 8938 | res[ri++] = c; |
| 8939 | } |
| 8940 | } |
| 8941 | |
| 8942 | res[ri] = NUL; |
| 8943 | } |
| 8944 | |
| 8945 | static void |
| 8946 | spell_soundfold_sal(slang, inword, res) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8947 | slang_T *slang; |
| 8948 | char_u *inword; |
| 8949 | char_u *res; |
| 8950 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8951 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8952 | char_u word[MAXWLEN]; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8953 | char_u *s = inword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8954 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8955 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8956 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8957 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8958 | int n, k = 0; |
| 8959 | int z0; |
| 8960 | int k0; |
| 8961 | int n0; |
| 8962 | int c; |
| 8963 | int pri; |
| 8964 | int p0 = -333; |
| 8965 | int c0; |
| 8966 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8967 | /* Remove accents, if wanted. We actually remove all non-word characters. |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8968 | * 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] | 8969 | if (slang->sl_rem_accents) |
| 8970 | { |
| 8971 | t = word; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8972 | while (*s != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8973 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8974 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8975 | { |
| 8976 | *t++ = ' '; |
| 8977 | s = skipwhite(s); |
| 8978 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8979 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8980 | { |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8981 | if (spell_iswordp_nmw(s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8982 | *t++ = *s; |
| 8983 | ++s; |
| 8984 | } |
| 8985 | } |
| 8986 | *t = NUL; |
| 8987 | } |
| 8988 | else |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8989 | STRCPY(word, s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8990 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8991 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8992 | |
| 8993 | /* |
| 8994 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8995 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8996 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8997 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8998 | while ((c = word[i]) != NUL) |
| 8999 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9000 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9001 | n = slang->sl_sal_first[c]; |
| 9002 | z0 = 0; |
| 9003 | |
| 9004 | if (n >= 0) |
| 9005 | { |
| 9006 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9007 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9008 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9009 | /* Quickly skip entries that don't match the word. Most |
| 9010 | * entries are less then three chars, optimize for that. */ |
| 9011 | k = smp[n].sm_leadlen; |
| 9012 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9013 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9014 | if (word[i + 1] != s[1]) |
| 9015 | continue; |
| 9016 | if (k > 2) |
| 9017 | { |
| 9018 | for (j = 2; j < k; ++j) |
| 9019 | if (word[i + j] != s[j]) |
| 9020 | break; |
| 9021 | if (j < k) |
| 9022 | continue; |
| 9023 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9024 | } |
| 9025 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9026 | if ((pf = smp[n].sm_oneof) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9027 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9028 | /* Check for match with one of the chars in "sm_oneof". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9029 | while (*pf != NUL && *pf != word[i + k]) |
| 9030 | ++pf; |
| 9031 | if (*pf == NUL) |
| 9032 | continue; |
| 9033 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9034 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9035 | s = smp[n].sm_rules; |
| 9036 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9037 | |
| 9038 | p0 = *s; |
| 9039 | k0 = k; |
| 9040 | while (*s == '-' && k > 1) |
| 9041 | { |
| 9042 | k--; |
| 9043 | s++; |
| 9044 | } |
| 9045 | if (*s == '<') |
| 9046 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9047 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9048 | { |
| 9049 | /* determine priority */ |
| 9050 | pri = *s - '0'; |
| 9051 | s++; |
| 9052 | } |
| 9053 | if (*s == '^' && *(s + 1) == '^') |
| 9054 | s++; |
| 9055 | |
| 9056 | if (*s == NUL |
| 9057 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9058 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9059 | || spell_iswordp(word + i - 1, curbuf))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9060 | && (*(s + 1) != '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9061 | || (!spell_iswordp(word + i + k0, curbuf)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9062 | || (*s == '$' && i > 0 |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9063 | && spell_iswordp(word + i - 1, curbuf) |
| 9064 | && (!spell_iswordp(word + i + k0, curbuf)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9065 | { |
| 9066 | /* search for followup rules, if: */ |
| 9067 | /* followup and k > 1 and NO '-' in searchstring */ |
| 9068 | c0 = word[i + k - 1]; |
| 9069 | n0 = slang->sl_sal_first[c0]; |
| 9070 | |
| 9071 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9072 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9073 | { |
| 9074 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9075 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9076 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9077 | /* Quickly skip entries that don't match the word. |
| 9078 | * */ |
| 9079 | k0 = smp[n0].sm_leadlen; |
| 9080 | if (k0 > 1) |
| 9081 | { |
| 9082 | if (word[i + k] != s[1]) |
| 9083 | continue; |
| 9084 | if (k0 > 2) |
| 9085 | { |
| 9086 | pf = word + i + k + 1; |
| 9087 | for (j = 2; j < k0; ++j) |
| 9088 | if (*pf++ != s[j]) |
| 9089 | break; |
| 9090 | if (j < k0) |
| 9091 | continue; |
| 9092 | } |
| 9093 | } |
| 9094 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9095 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9096 | if ((pf = smp[n0].sm_oneof) != NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9097 | { |
| 9098 | /* Check for match with one of the chars in |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9099 | * "sm_oneof". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9100 | while (*pf != NUL && *pf != word[i + k0]) |
| 9101 | ++pf; |
| 9102 | if (*pf == NUL) |
| 9103 | continue; |
| 9104 | ++k0; |
| 9105 | } |
| 9106 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9107 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9108 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9109 | while (*s == '-') |
| 9110 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9111 | /* "k0" gets NOT reduced because |
| 9112 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9113 | s++; |
| 9114 | } |
| 9115 | if (*s == '<') |
| 9116 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9117 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9118 | { |
| 9119 | p0 = *s - '0'; |
| 9120 | s++; |
| 9121 | } |
| 9122 | |
| 9123 | if (*s == NUL |
| 9124 | /* *s == '^' cuts */ |
| 9125 | || (*s == '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9126 | && !spell_iswordp(word + i + k0, |
| 9127 | curbuf))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9128 | { |
| 9129 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9130 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9131 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9132 | |
| 9133 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9134 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9135 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9136 | /* rule fits; stop search */ |
| 9137 | break; |
| 9138 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9139 | } |
| 9140 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9141 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9142 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9146 | s = smp[n].sm_to; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9147 | if (s == NULL) |
| 9148 | s = (char_u *)""; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9149 | pf = smp[n].sm_rules; |
| 9150 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9151 | if (p0 == 1 && z == 0) |
| 9152 | { |
| 9153 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9154 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 9155 | || res[reslen - 1] == *s)) |
| 9156 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9157 | z0 = 1; |
| 9158 | z = 1; |
| 9159 | k0 = 0; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9160 | while (*s != NUL && word[i + k0] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9161 | { |
| 9162 | word[i + k0] = *s; |
| 9163 | k0++; |
| 9164 | s++; |
| 9165 | } |
| 9166 | if (k > k0) |
| 9167 | mch_memmove(word + i + k0, word + i + k, |
| 9168 | STRLEN(word + i + k) + 1); |
| 9169 | |
| 9170 | /* new "actual letter" */ |
| 9171 | c = word[i]; |
| 9172 | } |
| 9173 | else |
| 9174 | { |
| 9175 | /* no '<' rule used */ |
| 9176 | i += k - 1; |
| 9177 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9178 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9179 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9180 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9181 | res[reslen++] = *s; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9182 | s++; |
| 9183 | } |
| 9184 | /* new "actual letter" */ |
| 9185 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9186 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9187 | { |
| 9188 | if (c != NUL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9189 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9190 | mch_memmove(word, word + i + 1, |
| 9191 | STRLEN(word + i + 1) + 1); |
| 9192 | i = 0; |
| 9193 | z0 = 1; |
| 9194 | } |
| 9195 | } |
| 9196 | break; |
| 9197 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9198 | } |
| 9199 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9200 | else if (vim_iswhite(c)) |
| 9201 | { |
| 9202 | c = ' '; |
| 9203 | k = 1; |
| 9204 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9205 | |
| 9206 | if (z0 == 0) |
| 9207 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9208 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 9209 | && (!slang->sl_collapse || reslen == 0 |
| 9210 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9211 | /* condense only double letters */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9212 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9213 | |
| 9214 | i++; |
| 9215 | z = 0; |
| 9216 | k = 0; |
| 9217 | } |
| 9218 | } |
| 9219 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9220 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9221 | } |
| 9222 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9223 | #ifdef FEAT_MBYTE |
| 9224 | /* |
| 9225 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 9226 | * Multi-byte version of spell_soundfold(). |
| 9227 | */ |
| 9228 | static void |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9229 | spell_soundfold_wsal(slang, inword, res) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9230 | slang_T *slang; |
| 9231 | char_u *inword; |
| 9232 | char_u *res; |
| 9233 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9234 | salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9235 | int word[MAXWLEN]; |
| 9236 | int wres[MAXWLEN]; |
| 9237 | int l; |
| 9238 | char_u *s; |
| 9239 | int *ws; |
| 9240 | char_u *t; |
| 9241 | int *pf; |
| 9242 | int i, j, z; |
| 9243 | int reslen; |
| 9244 | int n, k = 0; |
| 9245 | int z0; |
| 9246 | int k0; |
| 9247 | int n0; |
| 9248 | int c; |
| 9249 | int pri; |
| 9250 | int p0 = -333; |
| 9251 | int c0; |
| 9252 | int did_white = FALSE; |
| 9253 | |
| 9254 | /* |
| 9255 | * Convert the multi-byte string to a wide-character string. |
| 9256 | * Remove accents, if wanted. We actually remove all non-word characters. |
| 9257 | * But keep white space. |
| 9258 | */ |
| 9259 | n = 0; |
| 9260 | for (s = inword; *s != NUL; ) |
| 9261 | { |
| 9262 | t = s; |
| 9263 | c = mb_ptr2char_adv(&s); |
| 9264 | if (slang->sl_rem_accents) |
| 9265 | { |
| 9266 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 9267 | { |
| 9268 | if (did_white) |
| 9269 | continue; |
| 9270 | c = ' '; |
| 9271 | did_white = TRUE; |
| 9272 | } |
| 9273 | else |
| 9274 | { |
| 9275 | did_white = FALSE; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9276 | if (!spell_iswordp_nmw(t)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9277 | continue; |
| 9278 | } |
| 9279 | } |
| 9280 | word[n++] = c; |
| 9281 | } |
| 9282 | word[n] = NUL; |
| 9283 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9284 | /* |
| 9285 | * This comes from Aspell phonet.cpp. |
| 9286 | * Converted from C++ to C. Added support for multi-byte chars. |
| 9287 | * Changed to keep spaces. |
| 9288 | */ |
| 9289 | i = reslen = z = 0; |
| 9290 | while ((c = word[i]) != NUL) |
| 9291 | { |
| 9292 | /* Start with the first rule that has the character in the word. */ |
| 9293 | n = slang->sl_sal_first[c & 0xff]; |
| 9294 | z0 = 0; |
| 9295 | |
| 9296 | if (n >= 0) |
| 9297 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9298 | /* check all rules for the same index byte */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9299 | for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n) |
| 9300 | { |
| 9301 | /* Quickly skip entries that don't match the word. Most |
| 9302 | * entries are less then three chars, optimize for that. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9303 | if (c != ws[0]) |
| 9304 | continue; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9305 | k = smp[n].sm_leadlen; |
| 9306 | if (k > 1) |
| 9307 | { |
| 9308 | if (word[i + 1] != ws[1]) |
| 9309 | continue; |
| 9310 | if (k > 2) |
| 9311 | { |
| 9312 | for (j = 2; j < k; ++j) |
| 9313 | if (word[i + j] != ws[j]) |
| 9314 | break; |
| 9315 | if (j < k) |
| 9316 | continue; |
| 9317 | } |
| 9318 | } |
| 9319 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9320 | if ((pf = smp[n].sm_oneof_w) != NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9321 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9322 | /* Check for match with one of the chars in "sm_oneof". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9323 | while (*pf != NUL && *pf != word[i + k]) |
| 9324 | ++pf; |
| 9325 | if (*pf == NUL) |
| 9326 | continue; |
| 9327 | ++k; |
| 9328 | } |
| 9329 | s = smp[n].sm_rules; |
| 9330 | pri = 5; /* default priority */ |
| 9331 | |
| 9332 | p0 = *s; |
| 9333 | k0 = k; |
| 9334 | while (*s == '-' && k > 1) |
| 9335 | { |
| 9336 | k--; |
| 9337 | s++; |
| 9338 | } |
| 9339 | if (*s == '<') |
| 9340 | s++; |
| 9341 | if (VIM_ISDIGIT(*s)) |
| 9342 | { |
| 9343 | /* determine priority */ |
| 9344 | pri = *s - '0'; |
| 9345 | s++; |
| 9346 | } |
| 9347 | if (*s == '^' && *(s + 1) == '^') |
| 9348 | s++; |
| 9349 | |
| 9350 | if (*s == NUL |
| 9351 | || (*s == '^' |
| 9352 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9353 | || spell_iswordp_w(word + i - 1, curbuf))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9354 | && (*(s + 1) != '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9355 | || (!spell_iswordp_w(word + i + k0, curbuf)))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9356 | || (*s == '$' && i > 0 |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9357 | && spell_iswordp_w(word + i - 1, curbuf) |
| 9358 | && (!spell_iswordp_w(word + i + k0, curbuf)))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9359 | { |
| 9360 | /* search for followup rules, if: */ |
| 9361 | /* followup and k > 1 and NO '-' in searchstring */ |
| 9362 | c0 = word[i + k - 1]; |
| 9363 | n0 = slang->sl_sal_first[c0 & 0xff]; |
| 9364 | |
| 9365 | if (slang->sl_followup && k > 1 && n0 >= 0 |
| 9366 | && p0 != '-' && word[i + k] != NUL) |
| 9367 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9368 | /* Test follow-up rule for "word[i + k]"; loop over |
| 9369 | * all entries with the same index byte. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9370 | for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
| 9371 | == (c0 & 0xff); ++n0) |
| 9372 | { |
| 9373 | /* Quickly skip entries that don't match the word. |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9374 | */ |
| 9375 | if (c0 != ws[0]) |
| 9376 | continue; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9377 | k0 = smp[n0].sm_leadlen; |
| 9378 | if (k0 > 1) |
| 9379 | { |
| 9380 | if (word[i + k] != ws[1]) |
| 9381 | continue; |
| 9382 | if (k0 > 2) |
| 9383 | { |
| 9384 | pf = word + i + k + 1; |
| 9385 | for (j = 2; j < k0; ++j) |
| 9386 | if (*pf++ != ws[j]) |
| 9387 | break; |
| 9388 | if (j < k0) |
| 9389 | continue; |
| 9390 | } |
| 9391 | } |
| 9392 | k0 += k - 1; |
| 9393 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9394 | if ((pf = smp[n0].sm_oneof_w) != NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9395 | { |
| 9396 | /* Check for match with one of the chars in |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9397 | * "sm_oneof". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9398 | while (*pf != NUL && *pf != word[i + k0]) |
| 9399 | ++pf; |
| 9400 | if (*pf == NUL) |
| 9401 | continue; |
| 9402 | ++k0; |
| 9403 | } |
| 9404 | |
| 9405 | p0 = 5; |
| 9406 | s = smp[n0].sm_rules; |
| 9407 | while (*s == '-') |
| 9408 | { |
| 9409 | /* "k0" gets NOT reduced because |
| 9410 | * "if (k0 == k)" */ |
| 9411 | s++; |
| 9412 | } |
| 9413 | if (*s == '<') |
| 9414 | s++; |
| 9415 | if (VIM_ISDIGIT(*s)) |
| 9416 | { |
| 9417 | p0 = *s - '0'; |
| 9418 | s++; |
| 9419 | } |
| 9420 | |
| 9421 | if (*s == NUL |
| 9422 | /* *s == '^' cuts */ |
| 9423 | || (*s == '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9424 | && !spell_iswordp_w(word + i + k0, |
| 9425 | curbuf))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9426 | { |
| 9427 | if (k0 == k) |
| 9428 | /* this is just a piece of the string */ |
| 9429 | continue; |
| 9430 | |
| 9431 | if (p0 < pri) |
| 9432 | /* priority too low */ |
| 9433 | continue; |
| 9434 | /* rule fits; stop search */ |
| 9435 | break; |
| 9436 | } |
| 9437 | } |
| 9438 | |
| 9439 | if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) |
| 9440 | == (c0 & 0xff)) |
| 9441 | continue; |
| 9442 | } |
| 9443 | |
| 9444 | /* replace string */ |
| 9445 | ws = smp[n].sm_to_w; |
| 9446 | s = smp[n].sm_rules; |
| 9447 | p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; |
| 9448 | if (p0 == 1 && z == 0) |
| 9449 | { |
| 9450 | /* rule with '<' is used */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9451 | if (reslen > 0 && ws != NULL && *ws != NUL |
| 9452 | && (wres[reslen - 1] == c |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9453 | || wres[reslen - 1] == *ws)) |
| 9454 | reslen--; |
| 9455 | z0 = 1; |
| 9456 | z = 1; |
| 9457 | k0 = 0; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9458 | if (ws != NULL) |
| 9459 | while (*ws != NUL && word[i + k0] != NUL) |
| 9460 | { |
| 9461 | word[i + k0] = *ws; |
| 9462 | k0++; |
| 9463 | ws++; |
| 9464 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9465 | if (k > k0) |
| 9466 | mch_memmove(word + i + k0, word + i + k, |
| 9467 | sizeof(int) * (STRLEN(word + i + k) + 1)); |
| 9468 | |
| 9469 | /* new "actual letter" */ |
| 9470 | c = word[i]; |
| 9471 | } |
| 9472 | else |
| 9473 | { |
| 9474 | /* no '<' rule used */ |
| 9475 | i += k - 1; |
| 9476 | z = 0; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9477 | if (ws != NULL) |
| 9478 | while (*ws != NUL && ws[1] != NUL |
| 9479 | && reslen < MAXWLEN) |
| 9480 | { |
| 9481 | if (reslen == 0 || wres[reslen - 1] != *ws) |
| 9482 | wres[reslen++] = *ws; |
| 9483 | ws++; |
| 9484 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9485 | /* new "actual letter" */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9486 | if (ws == NULL) |
| 9487 | c = NUL; |
| 9488 | else |
| 9489 | c = *ws; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9490 | if (strstr((char *)s, "^^") != NULL) |
| 9491 | { |
| 9492 | if (c != NUL) |
| 9493 | wres[reslen++] = c; |
| 9494 | mch_memmove(word, word + i + 1, |
| 9495 | sizeof(int) * (STRLEN(word + i + 1) + 1)); |
| 9496 | i = 0; |
| 9497 | z0 = 1; |
| 9498 | } |
| 9499 | } |
| 9500 | break; |
| 9501 | } |
| 9502 | } |
| 9503 | } |
| 9504 | else if (vim_iswhite(c)) |
| 9505 | { |
| 9506 | c = ' '; |
| 9507 | k = 1; |
| 9508 | } |
| 9509 | |
| 9510 | if (z0 == 0) |
| 9511 | { |
| 9512 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 9513 | && (!slang->sl_collapse || reslen == 0 |
| 9514 | || wres[reslen - 1] != c)) |
| 9515 | /* condense only double letters */ |
| 9516 | wres[reslen++] = c; |
| 9517 | |
| 9518 | i++; |
| 9519 | z = 0; |
| 9520 | k = 0; |
| 9521 | } |
| 9522 | } |
| 9523 | |
| 9524 | /* Convert wide characters in "wres" to a multi-byte string in "res". */ |
| 9525 | l = 0; |
| 9526 | for (n = 0; n < reslen; ++n) |
| 9527 | { |
| 9528 | l += mb_char2bytes(wres[n], res + l); |
| 9529 | if (l + MB_MAXBYTES > MAXWLEN) |
| 9530 | break; |
| 9531 | } |
| 9532 | res[l] = NUL; |
| 9533 | } |
| 9534 | #endif |
| 9535 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9536 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9537 | * Compute a score for two sound-a-like words. |
| 9538 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 9539 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 9540 | * avoiding checks that will not be possible. |
| 9541 | */ |
| 9542 | static int |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9543 | soundalike_score(goodstart, badstart) |
| 9544 | char_u *goodstart; /* sound-folded good word */ |
| 9545 | char_u *badstart; /* sound-folded bad word */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9546 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9547 | char_u *goodsound = goodstart; |
| 9548 | char_u *badsound = badstart; |
| 9549 | int goodlen; |
| 9550 | int badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9551 | int n; |
| 9552 | char_u *pl, *ps; |
| 9553 | char_u *pl2, *ps2; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9554 | int score = 0; |
| 9555 | |
| 9556 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 9557 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 9558 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 9559 | { |
| 9560 | score = SCORE_DEL / 2; |
| 9561 | if (*badsound == '*') |
| 9562 | ++badsound; |
| 9563 | else |
| 9564 | ++goodsound; |
| 9565 | } |
| 9566 | |
| 9567 | goodlen = STRLEN(goodsound); |
| 9568 | badlen = STRLEN(badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9569 | |
| 9570 | /* Return quickly if the lenghts are too different to be fixed by two |
| 9571 | * changes. */ |
| 9572 | n = goodlen - badlen; |
| 9573 | if (n < -2 || n > 2) |
| 9574 | return SCORE_MAXMAX; |
| 9575 | |
| 9576 | if (n > 0) |
| 9577 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9578 | pl = goodsound; /* goodsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9579 | ps = badsound; |
| 9580 | } |
| 9581 | else |
| 9582 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9583 | pl = badsound; /* badsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9584 | ps = goodsound; |
| 9585 | } |
| 9586 | |
| 9587 | /* Skip over the identical part. */ |
| 9588 | while (*pl == *ps && *pl != NUL) |
| 9589 | { |
| 9590 | ++pl; |
| 9591 | ++ps; |
| 9592 | } |
| 9593 | |
| 9594 | switch (n) |
| 9595 | { |
| 9596 | case -2: |
| 9597 | case 2: |
| 9598 | /* |
| 9599 | * Must delete two characters from "pl". |
| 9600 | */ |
| 9601 | ++pl; /* first delete */ |
| 9602 | while (*pl == *ps) |
| 9603 | { |
| 9604 | ++pl; |
| 9605 | ++ps; |
| 9606 | } |
| 9607 | /* strings must be equal after second delete */ |
| 9608 | if (STRCMP(pl + 1, ps) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9609 | return score + SCORE_DEL * 2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9610 | |
| 9611 | /* Failed to compare. */ |
| 9612 | break; |
| 9613 | |
| 9614 | case -1: |
| 9615 | case 1: |
| 9616 | /* |
| 9617 | * Minimal one delete from "pl" required. |
| 9618 | */ |
| 9619 | |
| 9620 | /* 1: delete */ |
| 9621 | pl2 = pl + 1; |
| 9622 | ps2 = ps; |
| 9623 | while (*pl2 == *ps2) |
| 9624 | { |
| 9625 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9626 | return score + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9627 | ++pl2; |
| 9628 | ++ps2; |
| 9629 | } |
| 9630 | |
| 9631 | /* 2: delete then swap, then rest must be equal */ |
| 9632 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9633 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9634 | return score + SCORE_DEL + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9635 | |
| 9636 | /* 3: delete then substitute, then the rest must be equal */ |
| 9637 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9638 | return score + SCORE_DEL + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9639 | |
| 9640 | /* 4: first swap then delete */ |
| 9641 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 9642 | { |
| 9643 | pl2 = pl + 2; /* swap, skip two chars */ |
| 9644 | ps2 = ps + 2; |
| 9645 | while (*pl2 == *ps2) |
| 9646 | { |
| 9647 | ++pl2; |
| 9648 | ++ps2; |
| 9649 | } |
| 9650 | /* delete a char and then strings must be equal */ |
| 9651 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9652 | return score + SCORE_SWAP + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9653 | } |
| 9654 | |
| 9655 | /* 5: first substitute then delete */ |
| 9656 | pl2 = pl + 1; /* substitute, skip one char */ |
| 9657 | ps2 = ps + 1; |
| 9658 | while (*pl2 == *ps2) |
| 9659 | { |
| 9660 | ++pl2; |
| 9661 | ++ps2; |
| 9662 | } |
| 9663 | /* delete a char and then strings must be equal */ |
| 9664 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9665 | return score + SCORE_SUBST + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9666 | |
| 9667 | /* Failed to compare. */ |
| 9668 | break; |
| 9669 | |
| 9670 | case 0: |
| 9671 | /* |
| 9672 | * Lenghts are equal, thus changes must result in same length: An |
| 9673 | * insert is only possible in combination with a delete. |
| 9674 | * 1: check if for identical strings |
| 9675 | */ |
| 9676 | if (*pl == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9677 | return score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9678 | |
| 9679 | /* 2: swap */ |
| 9680 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 9681 | { |
| 9682 | pl2 = pl + 2; /* swap, skip two chars */ |
| 9683 | ps2 = ps + 2; |
| 9684 | while (*pl2 == *ps2) |
| 9685 | { |
| 9686 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9687 | return score + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9688 | ++pl2; |
| 9689 | ++ps2; |
| 9690 | } |
| 9691 | /* 3: swap and swap again */ |
| 9692 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9693 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9694 | return score + SCORE_SWAP + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9695 | |
| 9696 | /* 4: swap and substitute */ |
| 9697 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9698 | return score + SCORE_SWAP + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9699 | } |
| 9700 | |
| 9701 | /* 5: substitute */ |
| 9702 | pl2 = pl + 1; |
| 9703 | ps2 = ps + 1; |
| 9704 | while (*pl2 == *ps2) |
| 9705 | { |
| 9706 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9707 | return score + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9708 | ++pl2; |
| 9709 | ++ps2; |
| 9710 | } |
| 9711 | |
| 9712 | /* 6: substitute and swap */ |
| 9713 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9714 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9715 | return score + SCORE_SUBST + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9716 | |
| 9717 | /* 7: substitute and substitute */ |
| 9718 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9719 | return score + SCORE_SUBST + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9720 | |
| 9721 | /* 8: insert then delete */ |
| 9722 | pl2 = pl; |
| 9723 | ps2 = ps + 1; |
| 9724 | while (*pl2 == *ps2) |
| 9725 | { |
| 9726 | ++pl2; |
| 9727 | ++ps2; |
| 9728 | } |
| 9729 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9730 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9731 | |
| 9732 | /* 9: delete then insert */ |
| 9733 | pl2 = pl + 1; |
| 9734 | ps2 = ps; |
| 9735 | while (*pl2 == *ps2) |
| 9736 | { |
| 9737 | ++pl2; |
| 9738 | ++ps2; |
| 9739 | } |
| 9740 | if (STRCMP(pl2, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9741 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9742 | |
| 9743 | /* Failed to compare. */ |
| 9744 | break; |
| 9745 | } |
| 9746 | |
| 9747 | return SCORE_MAXMAX; |
| 9748 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9749 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9750 | /* |
| 9751 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9752 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9753 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9754 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9755 | * It has been converted from C++ to C and modified to support multi-byte |
| 9756 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9757 | */ |
| 9758 | static int |
| 9759 | spell_edit_score(badword, goodword) |
| 9760 | char_u *badword; |
| 9761 | char_u *goodword; |
| 9762 | { |
| 9763 | int *cnt; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9764 | int badlen, goodlen; /* lenghts including NUL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9765 | int j, i; |
| 9766 | int t; |
| 9767 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9768 | int pbc, pgc; |
| 9769 | #ifdef FEAT_MBYTE |
| 9770 | char_u *p; |
| 9771 | int wbadword[MAXWLEN]; |
| 9772 | int wgoodword[MAXWLEN]; |
| 9773 | |
| 9774 | if (has_mbyte) |
| 9775 | { |
| 9776 | /* Get the characters from the multi-byte strings and put them in an |
| 9777 | * int array for easy access. */ |
| 9778 | for (p = badword, badlen = 0; *p != NUL; ) |
| 9779 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
Bram Moolenaar | 97409f1 | 2005-07-08 22:17:29 +0000 | [diff] [blame] | 9780 | wbadword[badlen++] = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9781 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 9782 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
Bram Moolenaar | 97409f1 | 2005-07-08 22:17:29 +0000 | [diff] [blame] | 9783 | wgoodword[goodlen++] = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9784 | } |
| 9785 | else |
| 9786 | #endif |
| 9787 | { |
| 9788 | badlen = STRLEN(badword) + 1; |
| 9789 | goodlen = STRLEN(goodword) + 1; |
| 9790 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9791 | |
| 9792 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 9793 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9794 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 9795 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9796 | if (cnt == NULL) |
| 9797 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9798 | |
| 9799 | CNT(0, 0) = 0; |
| 9800 | for (j = 1; j <= goodlen; ++j) |
| 9801 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 9802 | |
| 9803 | for (i = 1; i <= badlen; ++i) |
| 9804 | { |
| 9805 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 9806 | for (j = 1; j <= goodlen; ++j) |
| 9807 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9808 | #ifdef FEAT_MBYTE |
| 9809 | if (has_mbyte) |
| 9810 | { |
| 9811 | bc = wbadword[i - 1]; |
| 9812 | gc = wgoodword[j - 1]; |
| 9813 | } |
| 9814 | else |
| 9815 | #endif |
| 9816 | { |
| 9817 | bc = badword[i - 1]; |
| 9818 | gc = goodword[j - 1]; |
| 9819 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9820 | if (bc == gc) |
| 9821 | CNT(i, j) = CNT(i - 1, j - 1); |
| 9822 | else |
| 9823 | { |
| 9824 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9825 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9826 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 9827 | else |
| 9828 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 9829 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9830 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9831 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9832 | #ifdef FEAT_MBYTE |
| 9833 | if (has_mbyte) |
| 9834 | { |
| 9835 | pbc = wbadword[i - 2]; |
| 9836 | pgc = wgoodword[j - 2]; |
| 9837 | } |
| 9838 | else |
| 9839 | #endif |
| 9840 | { |
| 9841 | pbc = badword[i - 2]; |
| 9842 | pgc = goodword[j - 2]; |
| 9843 | } |
| 9844 | if (bc == pgc && pbc == gc) |
| 9845 | { |
| 9846 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 9847 | if (t < CNT(i, j)) |
| 9848 | CNT(i, j) = t; |
| 9849 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9850 | } |
| 9851 | t = SCORE_DEL + CNT(i - 1, j); |
| 9852 | if (t < CNT(i, j)) |
| 9853 | CNT(i, j) = t; |
| 9854 | t = SCORE_INS + CNT(i, j - 1); |
| 9855 | if (t < CNT(i, j)) |
| 9856 | CNT(i, j) = t; |
| 9857 | } |
| 9858 | } |
| 9859 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9860 | |
| 9861 | i = CNT(badlen - 1, goodlen - 1); |
| 9862 | vim_free(cnt); |
| 9863 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9864 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9865 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9866 | /* |
| 9867 | * ":spelldump" |
| 9868 | */ |
| 9869 | /*ARGSUSED*/ |
| 9870 | void |
| 9871 | ex_spelldump(eap) |
| 9872 | exarg_T *eap; |
| 9873 | { |
| 9874 | buf_T *buf = curbuf; |
| 9875 | langp_T *lp; |
| 9876 | slang_T *slang; |
| 9877 | idx_T arridx[MAXWLEN]; |
| 9878 | int curi[MAXWLEN]; |
| 9879 | char_u word[MAXWLEN]; |
| 9880 | int c; |
| 9881 | char_u *byts; |
| 9882 | idx_T *idxs; |
| 9883 | linenr_T lnum = 0; |
| 9884 | int round; |
| 9885 | int depth; |
| 9886 | int n; |
| 9887 | int flags; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9888 | char_u *region_names = NULL; /* region names being used */ |
| 9889 | int do_region = TRUE; /* dump region names and numbers */ |
| 9890 | char_u *p; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9891 | |
| 9892 | if (no_spell_checking()) |
| 9893 | return; |
| 9894 | |
| 9895 | /* Create a new empty buffer by splitting the window. */ |
| 9896 | do_cmdline_cmd((char_u *)"new"); |
| 9897 | if (!bufempty() || !buf_valid(buf)) |
| 9898 | return; |
| 9899 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9900 | /* Find out if we can support regions: All languages must support the same |
| 9901 | * regions or none at all. */ |
| 9902 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 9903 | { |
| 9904 | p = lp->lp_slang->sl_regions; |
| 9905 | if (p[0] != 0) |
| 9906 | { |
| 9907 | if (region_names == NULL) /* first language with regions */ |
| 9908 | region_names = p; |
| 9909 | else if (STRCMP(region_names, p) != 0) |
| 9910 | { |
| 9911 | do_region = FALSE; /* region names are different */ |
| 9912 | break; |
| 9913 | } |
| 9914 | } |
| 9915 | } |
| 9916 | |
| 9917 | if (do_region && region_names != NULL) |
| 9918 | { |
| 9919 | vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); |
| 9920 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 9921 | } |
| 9922 | else |
| 9923 | do_region = FALSE; |
| 9924 | |
| 9925 | /* |
| 9926 | * Loop over all files loaded for the entries in 'spelllang'. |
| 9927 | */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9928 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 9929 | { |
| 9930 | slang = lp->lp_slang; |
| 9931 | |
| 9932 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 9933 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 9934 | |
| 9935 | /* round 1: case-folded tree |
| 9936 | * round 2: keep-case tree */ |
| 9937 | for (round = 1; round <= 2; ++round) |
| 9938 | { |
| 9939 | if (round == 1) |
| 9940 | { |
| 9941 | byts = slang->sl_fbyts; |
| 9942 | idxs = slang->sl_fidxs; |
| 9943 | } |
| 9944 | else |
| 9945 | { |
| 9946 | byts = slang->sl_kbyts; |
| 9947 | idxs = slang->sl_kidxs; |
| 9948 | } |
| 9949 | if (byts == NULL) |
| 9950 | continue; /* array is empty */ |
| 9951 | |
| 9952 | depth = 0; |
| 9953 | arridx[0] = 0; |
| 9954 | curi[0] = 1; |
| 9955 | while (depth >= 0 && !got_int) |
| 9956 | { |
| 9957 | if (curi[depth] > byts[arridx[depth]]) |
| 9958 | { |
| 9959 | /* Done all bytes at this node, go up one level. */ |
| 9960 | --depth; |
| 9961 | line_breakcheck(); |
| 9962 | } |
| 9963 | else |
| 9964 | { |
| 9965 | /* Do one more byte at this node. */ |
| 9966 | n = arridx[depth] + curi[depth]; |
| 9967 | ++curi[depth]; |
| 9968 | c = byts[n]; |
| 9969 | if (c == 0) |
| 9970 | { |
| 9971 | /* End of word, deal with the word. |
| 9972 | * Don't use keep-case words in the fold-case tree, |
| 9973 | * they will appear in the keep-case tree. |
| 9974 | * Only use the word when the region matches. */ |
| 9975 | flags = (int)idxs[n]; |
| 9976 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9977 | && (do_region |
| 9978 | || (flags & WF_REGION) == 0 |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 9979 | || (((unsigned)flags >> 16) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9980 | & lp->lp_region) != 0)) |
| 9981 | { |
| 9982 | word[depth] = NUL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9983 | if (!do_region) |
| 9984 | flags &= ~WF_REGION; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9985 | |
| 9986 | /* Dump the basic word if there is no prefix or |
| 9987 | * when it's the first one. */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 9988 | c = (unsigned)flags >> 24; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9989 | if (c == 0 || curi[depth] == 2) |
| 9990 | dump_word(word, round, flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9991 | |
| 9992 | /* Apply the prefix, if there is one. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9993 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9994 | lnum = apply_prefixes(slang, word, round, |
| 9995 | flags, lnum); |
| 9996 | } |
| 9997 | } |
| 9998 | else |
| 9999 | { |
| 10000 | /* Normal char, go one level deeper. */ |
| 10001 | word[depth++] = c; |
| 10002 | arridx[depth] = idxs[n]; |
| 10003 | curi[depth] = 1; |
| 10004 | } |
| 10005 | } |
| 10006 | } |
| 10007 | } |
| 10008 | } |
| 10009 | |
| 10010 | /* Delete the empty line that we started with. */ |
| 10011 | if (curbuf->b_ml.ml_line_count > 1) |
| 10012 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 10013 | |
| 10014 | redraw_later(NOT_VALID); |
| 10015 | } |
| 10016 | |
| 10017 | /* |
| 10018 | * Dump one word: apply case modifications and append a line to the buffer. |
| 10019 | */ |
| 10020 | static void |
| 10021 | dump_word(word, round, flags, lnum) |
| 10022 | char_u *word; |
| 10023 | int round; |
| 10024 | int flags; |
| 10025 | linenr_T lnum; |
| 10026 | { |
| 10027 | int keepcap = FALSE; |
| 10028 | char_u *p; |
| 10029 | char_u cword[MAXWLEN]; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 10030 | char_u badword[MAXWLEN + 10]; |
| 10031 | int i; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10032 | |
| 10033 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
| 10034 | { |
| 10035 | /* Need to fix case according to "flags". */ |
| 10036 | make_case_word(word, cword, flags); |
| 10037 | p = cword; |
| 10038 | } |
| 10039 | else |
| 10040 | { |
| 10041 | p = word; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 10042 | if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0 |
| 10043 | || (flags & WF_FIXCAP) != 0)) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10044 | keepcap = TRUE; |
| 10045 | } |
| 10046 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 10047 | /* Add flags and regions after a slash. */ |
| 10048 | if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10049 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 10050 | STRCPY(badword, p); |
| 10051 | STRCAT(badword, "/"); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10052 | if (keepcap) |
| 10053 | STRCAT(badword, "="); |
| 10054 | if (flags & WF_BANNED) |
| 10055 | STRCAT(badword, "!"); |
| 10056 | else if (flags & WF_RARE) |
| 10057 | STRCAT(badword, "?"); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 10058 | if (flags & WF_REGION) |
| 10059 | for (i = 0; i < 7; ++i) |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10060 | if (flags & (0x10000 << i)) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 10061 | sprintf((char *)badword + STRLEN(badword), "%d", i + 1); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10062 | p = badword; |
| 10063 | } |
| 10064 | |
| 10065 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 10066 | } |
| 10067 | |
| 10068 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10069 | * For ":spelldump": Find matching prefixes for "word". Prepend each to |
| 10070 | * "word" and append a line to the buffer. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10071 | * Return the updated line number. |
| 10072 | */ |
| 10073 | static linenr_T |
| 10074 | apply_prefixes(slang, word, round, flags, startlnum) |
| 10075 | slang_T *slang; |
| 10076 | char_u *word; /* case-folded word */ |
| 10077 | int round; |
| 10078 | int flags; /* flags with prefix ID */ |
| 10079 | linenr_T startlnum; |
| 10080 | { |
| 10081 | idx_T arridx[MAXWLEN]; |
| 10082 | int curi[MAXWLEN]; |
| 10083 | char_u prefix[MAXWLEN]; |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 10084 | char_u word_up[MAXWLEN]; |
| 10085 | int has_word_up = FALSE; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10086 | int c; |
| 10087 | char_u *byts; |
| 10088 | idx_T *idxs; |
| 10089 | linenr_T lnum = startlnum; |
| 10090 | int depth; |
| 10091 | int n; |
| 10092 | int len; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10093 | int i; |
| 10094 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 10095 | /* if the word starts with a lower-case letter make the word with an |
| 10096 | * upper-case letter in word_up[]. */ |
| 10097 | c = PTR2CHAR(word); |
| 10098 | if (SPELL_TOUPPER(c) != c) |
| 10099 | { |
| 10100 | onecap_copy(word, word_up, TRUE); |
| 10101 | has_word_up = TRUE; |
| 10102 | } |
| 10103 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10104 | byts = slang->sl_pbyts; |
| 10105 | idxs = slang->sl_pidxs; |
| 10106 | if (byts != NULL) /* array not is empty */ |
| 10107 | { |
| 10108 | /* |
| 10109 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10110 | * When at the end of a prefix check that it supports "flags". |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10111 | */ |
| 10112 | depth = 0; |
| 10113 | arridx[0] = 0; |
| 10114 | curi[0] = 1; |
| 10115 | while (depth >= 0 && !got_int) |
| 10116 | { |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10117 | n = arridx[depth]; |
| 10118 | len = byts[n]; |
| 10119 | if (curi[depth] > len) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10120 | { |
| 10121 | /* Done all bytes at this node, go up one level. */ |
| 10122 | --depth; |
| 10123 | line_breakcheck(); |
| 10124 | } |
| 10125 | else |
| 10126 | { |
| 10127 | /* Do one more byte at this node. */ |
Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10128 | n += curi[depth]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10129 | ++curi[depth]; |
| 10130 | c = byts[n]; |
| 10131 | if (c == 0) |
| 10132 | { |
| 10133 | /* End of prefix, find out how many IDs there are. */ |
| 10134 | for (i = 1; i < len; ++i) |
| 10135 | if (byts[n + i] != 0) |
| 10136 | break; |
| 10137 | curi[depth] += i - 1; |
| 10138 | |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 10139 | c = valid_word_prefix(i, n, flags, word, slang, FALSE); |
| 10140 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10141 | { |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10142 | vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 10143 | dump_word(prefix, round, |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 10144 | (c & WF_RAREPFX) ? (flags | WF_RARE) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 10145 | : flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10146 | } |
Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 10147 | |
| 10148 | /* Check for prefix that matches the word when the |
| 10149 | * first letter is upper-case, but only if the prefix has |
| 10150 | * a condition. */ |
| 10151 | if (has_word_up) |
| 10152 | { |
| 10153 | c = valid_word_prefix(i, n, flags, word_up, slang, |
| 10154 | TRUE); |
| 10155 | if (c != 0) |
| 10156 | { |
| 10157 | vim_strncpy(prefix + depth, word_up, |
| 10158 | MAXWLEN - depth - 1); |
| 10159 | dump_word(prefix, round, |
| 10160 | (c & WF_RAREPFX) ? (flags | WF_RARE) |
| 10161 | : flags, lnum++); |
| 10162 | } |
| 10163 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10164 | } |
| 10165 | else |
| 10166 | { |
| 10167 | /* Normal char, go one level deeper. */ |
| 10168 | prefix[depth++] = c; |
| 10169 | arridx[depth] = idxs[n]; |
| 10170 | curi[depth] = 1; |
| 10171 | } |
| 10172 | } |
| 10173 | } |
| 10174 | } |
| 10175 | |
| 10176 | return lnum; |
| 10177 | } |
| 10178 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 10179 | #endif /* FEAT_SYN_HL */ |