Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * spell.c: code for spell checking |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 12 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 13 | * The spell checking mechanism uses a tree (aka trie). Each node in the tree |
| 14 | * has a list of bytes that can appear (siblings). For each byte there is a |
| 15 | * pointer to the node with the byte that follows in the word (child). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 16 | * |
| 17 | * A NUL byte is used where the word may end. The bytes are sorted, so that |
| 18 | * binary searching can be used and the NUL bytes are at the start. The |
| 19 | * number of possible bytes is stored before the list of bytes. |
| 20 | * |
| 21 | * The tree uses two arrays: "byts" stores the characters, "idxs" stores |
| 22 | * either the next index or flags. The tree starts at index 0. For example, |
| 23 | * to lookup "vi" this sequence is followed: |
| 24 | * i = 0 |
| 25 | * len = byts[i] |
| 26 | * n = where "v" appears in byts[i + 1] to byts[i + len] |
| 27 | * i = idxs[n] |
| 28 | * len = byts[i] |
| 29 | * n = where "i" appears in byts[i + 1] to byts[i + len] |
| 30 | * i = idxs[n] |
| 31 | * len = byts[i] |
| 32 | * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi". |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 33 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 34 | * There are two word trees: one with case-folded words and one with words in |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 35 | * original case. The second one is only used for keep-case words and is |
| 36 | * usually small. |
| 37 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 38 | * There is one additional tree for when prefixes are not applied when |
| 39 | * generating the .spl file. This tree stores all the possible prefixes, as |
| 40 | * if they were words. At each word (prefix) end the prefix nr is stored, the |
| 41 | * following word must support this prefix nr. And the condition nr is |
| 42 | * stored, used to lookup the condition that the word must match with. |
| 43 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 44 | * Thanks to Olaf Seibert for providing an example implementation of this tree |
| 45 | * and the compression mechanism. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 46 | * |
| 47 | * Matching involves checking the caps type: Onecap ALLCAP KeepCap. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 48 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 49 | * Why doesn't Vim use aspell/ispell/myspell/etc.? |
| 50 | * See ":help develop-spell". |
| 51 | */ |
| 52 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 53 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 54 | * Use this to adjust the score after finding suggestions, based on the |
| 55 | * suggested word sounding like the bad word. This is much faster than doing |
| 56 | * it for every possible suggestion. |
| 57 | * Disadvantage: When "the" is typed as "hte" it sounds different and goes |
| 58 | * down in the list. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 59 | * Used when 'spellsuggest' is set to "best". |
| 60 | */ |
| 61 | #define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4) |
| 62 | |
| 63 | /* |
| 64 | * The double scoring mechanism is based on the principle that there are two |
| 65 | * kinds of spelling mistakes: |
| 66 | * 1. You know how to spell the word, but mistype something. This results in |
| 67 | * a small editing distance (character swapped/omitted/inserted) and |
| 68 | * possibly a word that sounds completely different. |
| 69 | * 2. You don't know how to spell the word and type something that sounds |
| 70 | * right. The edit distance can be big but the word is similar after |
| 71 | * sound-folding. |
| 72 | * Since scores for these two mistakes will be very different we use a list |
| 73 | * for each. |
| 74 | * The sound-folding is slow, only do double scoring when 'spellsuggest' is |
| 75 | * "double". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
| 78 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 79 | * Vim spell file format: <HEADER> |
| 80 | * <SUGGEST> |
| 81 | * <LWORDTREE> |
| 82 | * <KWORDTREE> |
| 83 | * <PREFIXTREE> |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 84 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 85 | * <HEADER>: <fileID> |
| 86 | * <regioncnt> <regionname> ... |
| 87 | * <charflagslen> <charflags> |
| 88 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 89 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 90 | * <prefcondcnt> <prefcond> ... |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 91 | * |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 92 | * <fileID> 10 bytes "VIMspell08" |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 93 | * <regioncnt> 1 byte number of regions following (8 supported) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 94 | * <regionname> 2 bytes Region name: ca, au, etc. Lower case. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 95 | * First <regionname> is region 1. |
| 96 | * |
| 97 | * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128). |
| 98 | * <charflags> N bytes List of flags (first one is for character 128): |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 99 | * 0x01 word character CF_WORD |
| 100 | * 0x02 upper-case character CF_UPPER |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 101 | * <fcharslen> 2 bytes Number of bytes in <fchars>. |
| 102 | * <fchars> N bytes Folded characters, first one is for character 128. |
| 103 | * |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 104 | * <midwordlen> 2 bytes Number of bytes in <midword>. |
| 105 | * <midword> N bytes Characters that are word characters only when used |
| 106 | * in the middle of a word. |
| 107 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 108 | * <prefcondcnt> 2 bytes Number of <prefcond> items following. |
| 109 | * |
| 110 | * <prefcond> : <condlen> <condstr> |
| 111 | * |
| 112 | * <condlen> 1 byte Length of <condstr>. |
| 113 | * |
| 114 | * <condstr> N bytes Condition for the prefix. |
| 115 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 116 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 117 | * <SUGGEST> : <repcount> <rep> ... |
| 118 | * <salflags> <salcount> <sal> ... |
| 119 | * <maplen> <mapstr> |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 120 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 121 | * <repcount> 2 bytes number of <rep> items, MSB first. |
| 122 | * |
| 123 | * <rep> : <repfromlen> <repfrom> <reptolen> <repto> |
| 124 | * |
| 125 | * <repfromlen> 1 byte length of <repfrom> |
| 126 | * |
| 127 | * <repfrom> N bytes "from" part of replacement |
| 128 | * |
| 129 | * <reptolen> 1 byte length of <repto> |
| 130 | * |
| 131 | * <repto> N bytes "to" part of replacement |
| 132 | * |
| 133 | * <salflags> 1 byte flags for soundsalike conversion: |
| 134 | * SAL_F0LLOWUP |
| 135 | * SAL_COLLAPSE |
| 136 | * SAL_REM_ACCENTS |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 137 | * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL |
| 138 | * |
| 139 | * <salcount> 2 bytes number of <sal> items following |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 140 | * |
| 141 | * <sal> : <salfromlen> <salfrom> <saltolen> <salto> |
| 142 | * |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 143 | * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 144 | * |
| 145 | * <salfrom> N bytes "from" part of soundsalike |
| 146 | * |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 147 | * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 148 | * |
| 149 | * <salto> N bytes "to" part of soundsalike |
| 150 | * |
| 151 | * <maplen> 2 bytes length of <mapstr>, MSB first |
| 152 | * |
| 153 | * <mapstr> N bytes String with sequences of similar characters, |
| 154 | * separated by slashes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 155 | * |
| 156 | * |
| 157 | * <LWORDTREE>: <wordtree> |
| 158 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 159 | * <KWORDTREE>: <wordtree> |
| 160 | * |
| 161 | * <PREFIXTREE>: <wordtree> |
| 162 | * |
| 163 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 164 | * <wordtree>: <nodecount> <nodedata> ... |
| 165 | * |
| 166 | * <nodecount> 4 bytes Number of nodes following. MSB first. |
| 167 | * |
| 168 | * <nodedata>: <siblingcount> <sibling> ... |
| 169 | * |
| 170 | * <siblingcount> 1 byte Number of siblings in this node. The siblings |
| 171 | * follow in sorted order. |
| 172 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 173 | * <sibling>: <byte> [ <nodeidx> <xbyte> |
| 174 | * | <flags> [<region>] [<prefixID>] |
| 175 | * | <prefixID> <prefcondnr> ] |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 176 | * |
| 177 | * <byte> 1 byte Byte value of the sibling. Special cases: |
| 178 | * BY_NOFLAGS: End of word without flags and for all |
| 179 | * regions. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 180 | * For PREFIXTREE <prefixID> and |
| 181 | * <prefcondnr> follow. |
| 182 | * BY_FLAGS: End of word, <flags> follow. |
| 183 | * For PREFIXTREE <prefixID> and |
| 184 | * <prefcondnr> follow for rare prefix. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 185 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
| 186 | * and <xbyte> follow. |
| 187 | * |
| 188 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 189 | * |
| 190 | * <xbyte> 1 byte byte value of the sibling. |
| 191 | * |
| 192 | * <flags> 1 byte bitmask of: |
| 193 | * WF_ALLCAP word must have only capitals |
| 194 | * WF_ONECAP first char of word must be capital |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 195 | * WF_KEEPCAP keep-case word |
| 196 | * WF_FIXCAP keep-case word, all caps not allowed |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 197 | * WF_RARE rare word |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 198 | * WF_BANNED bad word |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 199 | * WF_REGION <region> follows |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 200 | * WF_PFX <prefixID> follows |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 201 | * |
| 202 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 203 | * omitted it's valid in all regions. |
| 204 | * Lowest bit is for region 1. |
| 205 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 206 | * <prefixID> 1 byte ID of prefix that can be used with this word. For |
| 207 | * PREFIXTREE used for the required prefix ID. |
| 208 | * |
| 209 | * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list |
| 210 | * from HEADER. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 211 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 212 | * All text characters are in 'encoding', but stored as single bytes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 213 | */ |
| 214 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 215 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 216 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 217 | #endif |
| 218 | |
| 219 | #include "vim.h" |
| 220 | |
| 221 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 222 | |
| 223 | #ifdef HAVE_FCNTL_H |
| 224 | # include <fcntl.h> |
| 225 | #endif |
| 226 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 227 | #define MAXWLEN 250 /* Assume max. word len is this many bytes. |
| 228 | Some places assume a word length fits in a |
| 229 | byte, thus it can't be above 255. */ |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 230 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 231 | /* Type used for indexes in the word tree need to be at least 3 bytes. If int |
| 232 | * is 8 bytes we could use something smaller, but what? */ |
| 233 | #if SIZEOF_INT > 2 |
| 234 | typedef int idx_T; |
| 235 | #else |
| 236 | typedef long idx_T; |
| 237 | #endif |
| 238 | |
| 239 | /* Flags used for a word. Only the lowest byte can be used, the region byte |
| 240 | * comes above it. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 241 | #define WF_REGION 0x01 /* region byte follows */ |
| 242 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 243 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 244 | #define WF_RARE 0x08 /* rare word */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 245 | #define WF_BANNED 0x10 /* bad word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 246 | #define WF_PFX 0x20 /* prefix ID list follows */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 247 | #define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 248 | #define WF_KEEPCAP 0x80 /* keep-case word */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 249 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 250 | #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 251 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 252 | #define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed |
| 253 | prefix; must be above prefixID (one byte) |
| 254 | and prefcondnr (two bytes) */ |
| 255 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 256 | #define BY_NOFLAGS 0 /* end of word without flags or region */ |
| 257 | #define BY_FLAGS 1 /* end of word, flag byte follows */ |
| 258 | #define BY_INDEX 2 /* child is shared, index follows */ |
| 259 | #define BY_SPECIAL BY_INDEX /* hightest special byte value */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 260 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 261 | /* 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] | 262 | * and si_sal. Not for sl_sal! |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 263 | * One replacement: from "ft_from" to "ft_to". */ |
| 264 | typedef struct fromto_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 265 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 266 | char_u *ft_from; |
| 267 | char_u *ft_to; |
| 268 | } fromto_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 269 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 270 | /* Info from "SAL" entries in ".aff" file used in sl_sal. |
| 271 | * The info is split for quick processing by spell_soundfold(). |
| 272 | * Note that "sm_oneof" and "sm_rules" point into sm_lead. */ |
| 273 | typedef struct salitem_S |
| 274 | { |
| 275 | char_u *sm_lead; /* leading letters */ |
| 276 | int sm_leadlen; /* length of "sm_lead" */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 277 | char_u *sm_oneof; /* letters from () or NULL */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 278 | char_u *sm_rules; /* rules like ^, $, priority */ |
| 279 | char_u *sm_to; /* replacement. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 280 | #ifdef FEAT_MBYTE |
| 281 | int *sm_lead_w; /* wide character copy of "sm_lead" */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 282 | int *sm_oneof_w; /* wide character copy of "sm_oneof" */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 283 | int *sm_to_w; /* wide character copy of "sm_to" */ |
| 284 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 285 | } salitem_T; |
| 286 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 287 | #ifdef FEAT_MBYTE |
| 288 | typedef int salfirst_T; |
| 289 | #else |
| 290 | typedef short salfirst_T; |
| 291 | #endif |
| 292 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 293 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 294 | * Structure used to store words and other info for one language, loaded from |
| 295 | * a .spl file. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 296 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 297 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 298 | * |
| 299 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 300 | * the number of possible bytes, sorted on byte value: |
| 301 | * <len> <byte1> <byte2> ... |
| 302 | * The "idxs" array stores the index of the child node corresponding to the |
| 303 | * byte in "byts". |
| 304 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
| 305 | * the flags and region for the word. There may be several zeros in sequence |
| 306 | * for alternative flag/region combinations. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 307 | */ |
| 308 | typedef struct slang_S slang_T; |
| 309 | struct slang_S |
| 310 | { |
| 311 | slang_T *sl_next; /* next language */ |
| 312 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 313 | char_u *sl_fname; /* name of .spl file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 314 | int sl_add; /* TRUE if it's a .add file. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 315 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 316 | char_u *sl_fbyts; /* case-folded word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 317 | idx_T *sl_fidxs; /* case-folded word indexes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 318 | char_u *sl_kbyts; /* keep-case word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 319 | idx_T *sl_kidxs; /* keep-case word indexes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 320 | char_u *sl_pbyts; /* prefix tree word bytes */ |
| 321 | idx_T *sl_pidxs; /* prefix tree word indexes */ |
| 322 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 323 | 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] | 324 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 325 | char_u *sl_midword; /* MIDWORD string or NULL */ |
| 326 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 327 | int sl_prefixcnt; /* number of items in "sl_prefprog" */ |
| 328 | regprog_T **sl_prefprog; /* table with regprogs for prefixes */ |
| 329 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 330 | garray_T sl_rep; /* list of fromto_T entries from REP lines */ |
| 331 | short sl_rep_first[256]; /* indexes where byte first appears, -1 if |
| 332 | there is none */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 333 | garray_T sl_sal; /* list of salitem_T entries from SAL lines */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 334 | 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] | 335 | there is none */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 336 | int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items: |
| 337 | * "sl_sal_first" maps chars, when has_mbyte |
| 338 | * "sl_sal" is a list of wide char lists. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 339 | int sl_followup; /* SAL followup */ |
| 340 | int sl_collapse; /* SAL collapse_result */ |
| 341 | int sl_rem_accents; /* SAL remove_accents */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 342 | int sl_has_map; /* TRUE if there is a MAP line */ |
| 343 | #ifdef FEAT_MBYTE |
| 344 | hashtab_T sl_map_hash; /* MAP for multi-byte chars */ |
| 345 | int sl_map_array[256]; /* MAP for first 256 chars */ |
| 346 | #else |
| 347 | char_u sl_map_array[256]; /* MAP for first 256 chars */ |
| 348 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 351 | /* First language that is loaded, start of the linked list of loaded |
| 352 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 353 | static slang_T *first_lang = NULL; |
| 354 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 355 | /* Flags used in .spl file for soundsalike flags. */ |
| 356 | #define SAL_F0LLOWUP 1 |
| 357 | #define SAL_COLLAPSE 2 |
| 358 | #define SAL_REM_ACCENTS 4 |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 359 | #define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 360 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 361 | /* |
| 362 | * Structure used in "b_langp", filled from 'spelllang'. |
| 363 | */ |
| 364 | typedef struct langp_S |
| 365 | { |
| 366 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 367 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 368 | } langp_T; |
| 369 | |
| 370 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 371 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 372 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 373 | |
| 374 | /* Result values. Lower number is accepted over higher one. */ |
| 375 | #define SP_BANNED -1 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 376 | #define SP_OK 0 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 377 | #define SP_RARE 1 |
| 378 | #define SP_LOCAL 2 |
| 379 | #define SP_BAD 3 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 380 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 381 | #define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 382 | #define VIMSPELLMAGICL 10 |
| 383 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 384 | /* file used for "zG" and "zW" */ |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 385 | static char_u *int_wordlist = NULL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 386 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 387 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 388 | * Information used when looking for suggestions. |
| 389 | */ |
| 390 | typedef struct suginfo_S |
| 391 | { |
| 392 | garray_T su_ga; /* suggestions, contains "suggest_T" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 393 | int su_maxcount; /* max. number of suggestions displayed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 394 | int su_maxscore; /* maximum score for adding to su_ga */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 395 | garray_T su_sga; /* like su_ga, sound-folded scoring */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 396 | char_u *su_badptr; /* start of bad word in line */ |
| 397 | int su_badlen; /* length of detected bad word in line */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 398 | int su_badflags; /* caps flags for bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 399 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 400 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| 401 | hashtab_T su_banned; /* table with banned words */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 402 | } suginfo_T; |
| 403 | |
| 404 | /* One word suggestion. Used in "si_ga". */ |
| 405 | typedef struct suggest_S |
| 406 | { |
| 407 | char_u *st_word; /* suggested word, allocated string */ |
| 408 | int st_orglen; /* length of replaced text */ |
| 409 | int st_score; /* lower is better */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 410 | int st_altscore; /* used when st_score compares equal */ |
| 411 | int st_salscore; /* st_score is for soundalike */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 412 | int st_had_bonus; /* bonus already included in score */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 413 | } suggest_T; |
| 414 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 415 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 416 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 417 | /* Number of suggestions kept when cleaning up. When rescore_suggestions() is |
| 418 | * called the score may change, thus we need to keep more than what is |
| 419 | * displayed. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 420 | #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] | 421 | |
| 422 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 423 | * of suggestions that are not going to be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 424 | #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 425 | |
| 426 | /* score for various changes */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 427 | #define SCORE_SPLIT 149 /* split bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 428 | #define SCORE_ICASE 52 /* slightly different case */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 429 | #define SCORE_REGION 70 /* word is for different region */ |
| 430 | #define SCORE_RARE 180 /* rare word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 431 | #define SCORE_SWAP 90 /* swap two characters */ |
| 432 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| 433 | #define SCORE_REP 87 /* REP replacement */ |
| 434 | #define SCORE_SUBST 93 /* substitute a character */ |
| 435 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 436 | #define SCORE_DEL 94 /* delete a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 437 | #define SCORE_DELDUP 64 /* delete a duplicated character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 438 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 439 | #define SCORE_INSDUP 66 /* insert a duplicate character */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 440 | #define SCORE_NONWORD 103 /* change non-word to word char */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 441 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 442 | #define SCORE_FILE 30 /* suggestion from a file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 443 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 444 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 445 | |
| 446 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 447 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 448 | |
| 449 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 450 | * Structure to store info for word matching. |
| 451 | */ |
| 452 | typedef struct matchinf_S |
| 453 | { |
| 454 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 455 | |
| 456 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 457 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 458 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 459 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 460 | char_u *mi_cend; /* char after what was used for |
| 461 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 462 | |
| 463 | /* case-folded text */ |
| 464 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 465 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 466 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 467 | /* for when checking word after a prefix */ |
| 468 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 469 | prefixID/condition */ |
| 470 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 471 | int mi_prefixlen; /* byte length of prefix */ |
| 472 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 473 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 474 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 475 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 476 | buf_T *mi_buf; /* buffer being checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 477 | } matchinf_T; |
| 478 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 479 | /* |
| 480 | * The tables used for recognizing word characters according to spelling. |
| 481 | * These are only used for the first 256 characters of 'encoding'. |
| 482 | */ |
| 483 | typedef struct spelltab_S |
| 484 | { |
| 485 | char_u st_isw[256]; /* flags: is word char */ |
| 486 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 487 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 488 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 489 | } spelltab_T; |
| 490 | |
| 491 | static spelltab_T spelltab; |
| 492 | static int did_set_spelltab; |
| 493 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 494 | #define CF_WORD 0x01 |
| 495 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 496 | |
| 497 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 498 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 499 | static int spell_iswordp __ARGS((char_u *p, buf_T *buf)); |
| 500 | static int spell_iswordp_nmw __ARGS((char_u *p)); |
| 501 | #ifdef FEAT_MBYTE |
| 502 | static int spell_iswordp_w __ARGS((int *p, buf_T *buf)); |
| 503 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 504 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 505 | |
| 506 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 507 | * Return TRUE if "p" points to a word character. Like spell_iswordp() but |
| 508 | * without the special handling of a single quote. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 509 | * Checking for a word character is done very often, avoid the function call |
| 510 | * overhead. |
| 511 | */ |
| 512 | #ifdef FEAT_MBYTE |
| 513 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 514 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 515 | #else |
| 516 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 517 | #endif |
| 518 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 519 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 520 | * 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] | 521 | */ |
| 522 | typedef enum |
| 523 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 524 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 525 | * ends); if badword ends there is a match, otherwise |
| 526 | * try splitting word. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 527 | STATE_NOPREFIX, /* try without prefix */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 528 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 529 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 530 | STATE_PLAIN, /* Use each byte of the node. */ |
| 531 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 532 | STATE_INS, /* Insert a byte in the bad word. */ |
| 533 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 534 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 535 | STATE_SWAP3, /* Swap two characters over three. */ |
| 536 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 537 | STATE_UNROT3L, /* Undo rotate three characters left */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 538 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 539 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 540 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 541 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 542 | STATE_FINAL /* End of this node. */ |
| 543 | } state_T; |
| 544 | |
| 545 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 546 | * Struct to keep the state at each level in suggest_try_change(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 547 | */ |
| 548 | typedef struct trystate_S |
| 549 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 550 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 551 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 552 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 553 | short ts_curi; /* index in list of child nodes */ |
| 554 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 555 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 556 | char_u ts_twordlen; /* valid length of tword[] */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 557 | char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE |
| 558 | * or NOPREFIX */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 559 | #ifdef FEAT_MBYTE |
| 560 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 561 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 562 | char_u ts_isdiff; /* DIFF_ values */ |
| 563 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 564 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 565 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 566 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 567 | char_u ts_save_badflags; /* su_badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 568 | } trystate_T; |
| 569 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 570 | /* values for ts_isdiff */ |
| 571 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 572 | #define DIFF_YES 1 /* different byte found */ |
| 573 | #define DIFF_INSERT 2 /* inserting character */ |
| 574 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 575 | /* special values ts_prefixdepth */ |
| 576 | #define PREFIXTREE 0xfe /* walking through the prefix tree */ |
| 577 | #define NOPREFIX 0xff /* not using prefixes */ |
| 578 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 579 | /* mode values for find_word */ |
| 580 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 581 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 582 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 583 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 584 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 585 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 586 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 587 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 588 | static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int prefid, char_u *word, slang_T *slang)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 589 | static void find_prefix __ARGS((matchinf_T *mip)); |
| 590 | static int fold_more __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 591 | static int spell_valid_case __ARGS((int wordflags, int treeflags)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 592 | static int no_spell_checking __ARGS((void)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 593 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 594 | static char_u *spell_enc __ARGS((void)); |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 595 | static void int_wordlist_spl __ARGS((char_u *fname)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 596 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 597 | 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^] | 598 | 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] | 599 | static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to)); |
| 600 | static void set_sal_first __ARGS((slang_T *lp)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 601 | #ifdef FEAT_MBYTE |
| 602 | static int *mb_str2wide __ARGS((char_u *s)); |
| 603 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 604 | 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] | 605 | static void clear_midword __ARGS((buf_T *buf)); |
| 606 | static void use_midword __ARGS((slang_T *lp, buf_T *buf)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 607 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 608 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 609 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 610 | 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] | 611 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 612 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 613 | 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] | 614 | 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] | 615 | #ifdef FEAT_EVAL |
| 616 | static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr)); |
| 617 | #endif |
| 618 | static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname)); |
| 619 | static void spell_suggest_intern __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 620 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 621 | 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] | 622 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 623 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 624 | static void suggest_try_change __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 625 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
| 626 | 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] | 627 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 628 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 629 | 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] | 630 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 631 | 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] | 632 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 633 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 634 | 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] | 635 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 636 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 637 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 638 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 639 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 640 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res)); |
| 641 | static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| 642 | 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] | 643 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 644 | 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] | 645 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 646 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 647 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 648 | static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum)); |
| 649 | 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] | 650 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 651 | /* |
| 652 | * Use our own character-case definitions, because the current locale may |
| 653 | * differ from what the .spl file uses. |
| 654 | * These must not be called with negative number! |
| 655 | */ |
| 656 | #ifndef FEAT_MBYTE |
| 657 | /* Non-multi-byte implementation. */ |
| 658 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 659 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 660 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 661 | #else |
| 662 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 663 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 664 | * the "w" library function for characters above 255 if available. */ |
| 665 | # ifdef HAVE_TOWLOWER |
| 666 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 667 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 668 | # else |
| 669 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 670 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 671 | # endif |
| 672 | |
| 673 | # ifdef HAVE_TOWUPPER |
| 674 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 675 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 676 | # else |
| 677 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 678 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 679 | # endif |
| 680 | |
| 681 | # ifdef HAVE_ISWUPPER |
| 682 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 683 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 684 | # else |
| 685 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 686 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 687 | # endif |
| 688 | #endif |
| 689 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 690 | |
| 691 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 692 | static char *e_spell_trunc = N_("E758: Truncated spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 696 | * "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] | 697 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 698 | * or when it's OK it remains unchanged. |
| 699 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 700 | * |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 701 | * "capcol" is used to check for a Capitalised word after the end of a |
| 702 | * sentence. If it's zero then perform the check. Return the column where to |
| 703 | * check next, or -1 when no sentence end was found. If it's NULL then don't |
| 704 | * worry. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 705 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 706 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 707 | * caller can skip over the word. |
| 708 | */ |
| 709 | int |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 710 | spell_check(wp, ptr, attrp, capcol) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 711 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 712 | char_u *ptr; |
| 713 | int *attrp; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 714 | int *capcol; /* column to check for Capital */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 715 | { |
| 716 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 717 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 718 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 719 | int c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 720 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 721 | /* A word never starts at a space or a control character. Return quickly |
| 722 | * then, skipping over the character. */ |
| 723 | if (*ptr <= ' ') |
| 724 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 725 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 726 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 727 | * 0X99FF. But when a word character follows do check spelling to find |
| 728 | * "3GPP". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 729 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 730 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 731 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 732 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 733 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 734 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 735 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 736 | nrlen = mi.mi_end - ptr; |
| 737 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 738 | if (!spell_iswordp(mi.mi_end, wp->w_buffer)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 739 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 740 | |
| 741 | /* Try including the digits in the word. */ |
| 742 | mi.mi_fend = ptr + nrlen; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 743 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 744 | else |
| 745 | mi.mi_fend = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 746 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 747 | /* 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] | 748 | mi.mi_word = ptr; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 749 | if (spell_iswordp(mi.mi_fend, wp->w_buffer)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 750 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 751 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 752 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 753 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 754 | } 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] | 755 | |
| 756 | if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL) |
| 757 | { |
| 758 | /* Check word starting with capital letter. */ |
| 759 | #ifdef FEAT_MBYTE |
| 760 | c = mb_ptr2char(ptr); |
| 761 | #else |
| 762 | c = *ptr; |
| 763 | #endif |
| 764 | if (!SPELL_ISUPPER(c)) |
| 765 | { |
| 766 | *attrp = highlight_attr[HLF_SPC]; |
| 767 | return (int)(mi.mi_fend - ptr); |
| 768 | } |
| 769 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 770 | } |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 771 | if (capcol != NULL) |
| 772 | *capcol = -1; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 773 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 774 | /* We always use the characters up to the next non-word character, |
| 775 | * also for bad words. */ |
| 776 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 777 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 778 | /* Check caps type later. */ |
| 779 | mi.mi_capflags = 0; |
| 780 | mi.mi_cend = NULL; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 781 | mi.mi_buf = wp->w_buffer; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 782 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 783 | /* Include one non-word character so that we can check for the |
| 784 | * word end. */ |
| 785 | if (*mi.mi_fend != NUL) |
| 786 | mb_ptr_adv(mi.mi_fend); |
| 787 | |
| 788 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 789 | MAXWLEN + 1); |
| 790 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 791 | |
| 792 | /* The word is bad unless we recognize it. */ |
| 793 | mi.mi_result = SP_BAD; |
| 794 | |
| 795 | /* |
| 796 | * Loop over the languages specified in 'spelllang'. |
| 797 | * We check them all, because a matching word may be longer than an |
| 798 | * already found matching word. |
| 799 | */ |
| 800 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 801 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 802 | { |
| 803 | /* Check for a matching word in case-folded words. */ |
| 804 | find_word(&mi, FIND_FOLDWORD); |
| 805 | |
| 806 | /* Check for a matching word in keep-case words. */ |
| 807 | find_word(&mi, FIND_KEEPWORD); |
| 808 | |
| 809 | /* Check for matching prefixes. */ |
| 810 | find_prefix(&mi); |
| 811 | } |
| 812 | |
| 813 | if (mi.mi_result != SP_OK) |
| 814 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 815 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 816 | * rare and local words, e.g., "3GPP". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 817 | if (nrlen > 0) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 818 | { |
| 819 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 820 | return nrlen; |
| 821 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 822 | |
| 823 | /* When we are at a non-word character there is no error, just |
| 824 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 825 | else if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 826 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 827 | if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL) |
| 828 | { |
| 829 | regmatch_T regmatch; |
| 830 | |
| 831 | /* Check for end of sentence. */ |
| 832 | regmatch.regprog = wp->w_buffer->b_cap_prog; |
| 833 | regmatch.rm_ic = FALSE; |
| 834 | if (vim_regexec(®match, ptr, 0)) |
| 835 | *capcol = (int)(regmatch.endp[0] - ptr); |
| 836 | } |
| 837 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 838 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 839 | if (has_mbyte) |
| 840 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 841 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 842 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 843 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 844 | |
| 845 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 846 | *attrp = highlight_attr[HLF_SPB]; |
| 847 | else if (mi.mi_result == SP_RARE) |
| 848 | *attrp = highlight_attr[HLF_SPR]; |
| 849 | else |
| 850 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 853 | return (int)(mi.mi_end - ptr); |
| 854 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 855 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 856 | /* |
| 857 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 858 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 859 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 860 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 861 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 862 | * |
| 863 | * For a match mip->mi_result is updated. |
| 864 | */ |
| 865 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 866 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 867 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 868 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 869 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 870 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 871 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 872 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 873 | int endidxcnt = 0; |
| 874 | int len; |
| 875 | int wlen = 0; |
| 876 | int flen; |
| 877 | int c; |
| 878 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 879 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 880 | #ifdef FEAT_MBYTE |
| 881 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 882 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 883 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 884 | int res = SP_BAD; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 885 | slang_T *slang = mip->mi_lp->lp_slang; |
| 886 | unsigned flags; |
| 887 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 888 | idx_T *idxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 889 | int prefid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 890 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 891 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 892 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 893 | /* Check for word with matching case in keep-case tree. */ |
| 894 | ptr = mip->mi_word; |
| 895 | flen = 9999; /* no case folding, always enough bytes */ |
| 896 | byts = slang->sl_kbyts; |
| 897 | idxs = slang->sl_kidxs; |
| 898 | } |
| 899 | else |
| 900 | { |
| 901 | /* Check for case-folded in case-folded tree. */ |
| 902 | ptr = mip->mi_fword; |
| 903 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 904 | byts = slang->sl_fbyts; |
| 905 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 906 | |
| 907 | if (mode == FIND_PREFIX) |
| 908 | { |
| 909 | /* Skip over the prefix. */ |
| 910 | wlen = mip->mi_prefixlen; |
| 911 | flen -= mip->mi_prefixlen; |
| 912 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 915 | if (byts == NULL) |
| 916 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 917 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 918 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 919 | * Repeat advancing in the tree until: |
| 920 | * - there is a byte that doesn't match, |
| 921 | * - we reach the end of the tree, |
| 922 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 923 | */ |
| 924 | for (;;) |
| 925 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 926 | if (flen <= 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 927 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 928 | |
| 929 | len = byts[arridx++]; |
| 930 | |
| 931 | /* If the first possible byte is a zero the word could end here. |
| 932 | * Remember this index, we first check for the longest word. */ |
| 933 | if (byts[arridx] == 0) |
| 934 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 935 | if (endidxcnt == MAXWLEN) |
| 936 | { |
| 937 | /* Must be a corrupted spell file. */ |
| 938 | EMSG(_(e_format)); |
| 939 | return; |
| 940 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 941 | endlen[endidxcnt] = wlen; |
| 942 | endidx[endidxcnt++] = arridx++; |
| 943 | --len; |
| 944 | |
| 945 | /* Skip over the zeros, there can be several flag/region |
| 946 | * combinations. */ |
| 947 | while (len > 0 && byts[arridx] == 0) |
| 948 | { |
| 949 | ++arridx; |
| 950 | --len; |
| 951 | } |
| 952 | if (len == 0) |
| 953 | break; /* no children, word must end here */ |
| 954 | } |
| 955 | |
| 956 | /* Stop looking at end of the line. */ |
| 957 | if (ptr[wlen] == NUL) |
| 958 | break; |
| 959 | |
| 960 | /* Perform a binary search in the list of accepted bytes. */ |
| 961 | c = ptr[wlen]; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 962 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 963 | c = ' '; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 964 | lo = arridx; |
| 965 | hi = arridx + len - 1; |
| 966 | while (lo < hi) |
| 967 | { |
| 968 | m = (lo + hi) / 2; |
| 969 | if (byts[m] > c) |
| 970 | hi = m - 1; |
| 971 | else if (byts[m] < c) |
| 972 | lo = m + 1; |
| 973 | else |
| 974 | { |
| 975 | lo = hi = m; |
| 976 | break; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /* Stop if there is no matching byte. */ |
| 981 | if (hi < lo || byts[lo] != c) |
| 982 | break; |
| 983 | |
| 984 | /* Continue at the child (if there is one). */ |
| 985 | arridx = idxs[lo]; |
| 986 | ++wlen; |
| 987 | --flen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 988 | |
| 989 | /* One space in the good word may stand for several spaces in the |
| 990 | * checked word. */ |
| 991 | if (c == ' ') |
| 992 | { |
| 993 | for (;;) |
| 994 | { |
| 995 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 996 | flen = fold_more(mip); |
| 997 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 998 | break; |
| 999 | ++wlen; |
| 1000 | --flen; |
| 1001 | } |
| 1002 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * Verify that one of the possible endings is valid. Try the longest |
| 1007 | * first. |
| 1008 | */ |
| 1009 | while (endidxcnt > 0) |
| 1010 | { |
| 1011 | --endidxcnt; |
| 1012 | arridx = endidx[endidxcnt]; |
| 1013 | wlen = endlen[endidxcnt]; |
| 1014 | |
| 1015 | #ifdef FEAT_MBYTE |
| 1016 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 1017 | continue; /* not at first byte of character */ |
| 1018 | #endif |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1019 | if (spell_iswordp(ptr + wlen, mip->mi_buf)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1020 | continue; /* next char is a word character */ |
| 1021 | |
| 1022 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1023 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1024 | { |
| 1025 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1026 | * when folding case. This can be slow, take a shortcut when the |
| 1027 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1028 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1029 | if (STRNCMP(ptr, p, wlen) != 0) |
| 1030 | { |
| 1031 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 1032 | mb_ptr_adv(p); |
| 1033 | wlen = p - mip->mi_word; |
| 1034 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1035 | } |
| 1036 | #endif |
| 1037 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1038 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 1039 | * prefix ID. |
| 1040 | * Repeat this if there are more flags/region alternatives until there |
| 1041 | * is a match. */ |
| 1042 | res = SP_BAD; |
| 1043 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 1044 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1045 | { |
| 1046 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1047 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1048 | /* For the fold-case tree check that the case of the checked word |
| 1049 | * matches with what the word in the tree requires. |
| 1050 | * For keep-case tree the case is always right. For prefixes we |
| 1051 | * don't bother to check. */ |
| 1052 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1053 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1054 | if (mip->mi_cend != mip->mi_word + wlen) |
| 1055 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1056 | /* mi_capflags was set for a different word length, need |
| 1057 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1058 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1059 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1062 | if (mip->mi_capflags == WF_KEEPCAP |
| 1063 | || !spell_valid_case(mip->mi_capflags, flags)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1064 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1067 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 1068 | * 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] | 1069 | * mip->mi_prefarridx that find_prefix() filled. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1070 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1071 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1072 | /* The prefix ID is stored two bytes above the flags. */ |
| 1073 | prefid = (unsigned)flags >> 16; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1074 | c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1075 | prefid, mip->mi_fword + mip->mi_prefixlen, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1076 | slang); |
| 1077 | if (c == 0) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1078 | continue; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* Use the WF_RARE flag for a rare prefix. */ |
| 1081 | if (c & WF_RAREPFX) |
| 1082 | flags |= WF_RARE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | if (flags & WF_BANNED) |
| 1086 | res = SP_BANNED; |
| 1087 | else if (flags & WF_REGION) |
| 1088 | { |
| 1089 | /* Check region. */ |
| 1090 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 1091 | res = SP_OK; |
| 1092 | else |
| 1093 | res = SP_LOCAL; |
| 1094 | } |
| 1095 | else if (flags & WF_RARE) |
| 1096 | res = SP_RARE; |
| 1097 | else |
| 1098 | res = SP_OK; |
| 1099 | |
| 1100 | /* Always use the longest match and the best result. */ |
| 1101 | if (mip->mi_result > res) |
| 1102 | { |
| 1103 | mip->mi_result = res; |
| 1104 | mip->mi_end = mip->mi_word + wlen; |
| 1105 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1106 | 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] | 1107 | mip->mi_end = mip->mi_word + wlen; |
| 1108 | |
| 1109 | if (res == SP_OK) |
| 1110 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1113 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1114 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1115 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1118 | /* |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1119 | * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches |
| 1120 | * with the prefix ID "prefid" for the word "word". |
| 1121 | * 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] | 1122 | */ |
| 1123 | static int |
| 1124 | valid_word_prefix(totprefcnt, arridx, prefid, word, slang) |
| 1125 | int totprefcnt; /* nr of prefix IDs */ |
| 1126 | int arridx; /* idx in sl_pidxs[] */ |
| 1127 | int prefid; |
| 1128 | char_u *word; |
| 1129 | slang_T *slang; |
| 1130 | { |
| 1131 | int prefcnt; |
| 1132 | int pidx; |
| 1133 | regprog_T *rp; |
| 1134 | regmatch_T regmatch; |
| 1135 | |
| 1136 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1137 | { |
| 1138 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1139 | |
| 1140 | /* Check the prefix ID. */ |
| 1141 | if (prefid != (pidx & 0xff)) |
| 1142 | continue; |
| 1143 | |
| 1144 | /* Check the condition, if there is one. The condition index is |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1145 | * stored in the two bytes above the prefix ID byte. */ |
| 1146 | rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1147 | if (rp != NULL) |
| 1148 | { |
| 1149 | regmatch.regprog = rp; |
| 1150 | regmatch.rm_ic = FALSE; |
| 1151 | if (!vim_regexec(®match, word, 0)) |
| 1152 | continue; |
| 1153 | } |
| 1154 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1155 | /* It's a match! Return the WF_RAREPFX flag. */ |
| 1156 | return pidx; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1157 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1158 | return 0; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1162 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1163 | * If it does, then check the following word. |
| 1164 | * |
| 1165 | * For a match mip->mi_result is updated. |
| 1166 | */ |
| 1167 | static void |
| 1168 | find_prefix(mip) |
| 1169 | matchinf_T *mip; |
| 1170 | { |
| 1171 | idx_T arridx = 0; |
| 1172 | int len; |
| 1173 | int wlen = 0; |
| 1174 | int flen; |
| 1175 | int c; |
| 1176 | char_u *ptr; |
| 1177 | idx_T lo, hi, m; |
| 1178 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1179 | char_u *byts; |
| 1180 | idx_T *idxs; |
| 1181 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1182 | byts = slang->sl_pbyts; |
| 1183 | if (byts == NULL) |
| 1184 | return; /* array is empty */ |
| 1185 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1186 | /* We use the case-folded word here, since prefixes are always |
| 1187 | * case-folded. */ |
| 1188 | ptr = mip->mi_fword; |
| 1189 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1190 | idxs = slang->sl_pidxs; |
| 1191 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | * Repeat advancing in the tree until: |
| 1194 | * - there is a byte that doesn't match, |
| 1195 | * - we reach the end of the tree, |
| 1196 | * - or we reach the end of the line. |
| 1197 | */ |
| 1198 | for (;;) |
| 1199 | { |
| 1200 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1201 | flen = fold_more(mip); |
| 1202 | |
| 1203 | len = byts[arridx++]; |
| 1204 | |
| 1205 | /* If the first possible byte is a zero the prefix could end here. |
| 1206 | * Check if the following word matches and supports the prefix. */ |
| 1207 | if (byts[arridx] == 0) |
| 1208 | { |
| 1209 | /* There can be several prefixes with different conditions. We |
| 1210 | * try them all, since we don't know which one will give the |
| 1211 | * longest match. The word is the same each time, pass the list |
| 1212 | * of possible prefixes to find_word(). */ |
| 1213 | mip->mi_prefarridx = arridx; |
| 1214 | mip->mi_prefcnt = len; |
| 1215 | while (len > 0 && byts[arridx] == 0) |
| 1216 | { |
| 1217 | ++arridx; |
| 1218 | --len; |
| 1219 | } |
| 1220 | mip->mi_prefcnt -= len; |
| 1221 | |
| 1222 | /* Find the word that comes after the prefix. */ |
| 1223 | mip->mi_prefixlen = wlen; |
| 1224 | find_word(mip, FIND_PREFIX); |
| 1225 | |
| 1226 | |
| 1227 | if (len == 0) |
| 1228 | break; /* no children, word must end here */ |
| 1229 | } |
| 1230 | |
| 1231 | /* Stop looking at end of the line. */ |
| 1232 | if (ptr[wlen] == NUL) |
| 1233 | break; |
| 1234 | |
| 1235 | /* Perform a binary search in the list of accepted bytes. */ |
| 1236 | c = ptr[wlen]; |
| 1237 | lo = arridx; |
| 1238 | hi = arridx + len - 1; |
| 1239 | while (lo < hi) |
| 1240 | { |
| 1241 | m = (lo + hi) / 2; |
| 1242 | if (byts[m] > c) |
| 1243 | hi = m - 1; |
| 1244 | else if (byts[m] < c) |
| 1245 | lo = m + 1; |
| 1246 | else |
| 1247 | { |
| 1248 | lo = hi = m; |
| 1249 | break; |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | /* Stop if there is no matching byte. */ |
| 1254 | if (hi < lo || byts[lo] != c) |
| 1255 | break; |
| 1256 | |
| 1257 | /* Continue at the child (if there is one). */ |
| 1258 | arridx = idxs[lo]; |
| 1259 | ++wlen; |
| 1260 | --flen; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Need to fold at least one more character. Do until next non-word character |
| 1266 | * for efficiency. |
| 1267 | * Return the length of the folded chars in bytes. |
| 1268 | */ |
| 1269 | static int |
| 1270 | fold_more(mip) |
| 1271 | matchinf_T *mip; |
| 1272 | { |
| 1273 | int flen; |
| 1274 | char_u *p; |
| 1275 | |
| 1276 | p = mip->mi_fend; |
| 1277 | do |
| 1278 | { |
| 1279 | mb_ptr_adv(mip->mi_fend); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1280 | } 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] | 1281 | |
| 1282 | /* Include the non-word character so that we can check for the |
| 1283 | * word end. */ |
| 1284 | if (*mip->mi_fend != NUL) |
| 1285 | mb_ptr_adv(mip->mi_fend); |
| 1286 | |
| 1287 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1288 | mip->mi_fword + mip->mi_fwordlen, |
| 1289 | MAXWLEN - mip->mi_fwordlen); |
| 1290 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1291 | mip->mi_fwordlen += flen; |
| 1292 | return flen; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1296 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1297 | * case. |
| 1298 | */ |
| 1299 | static int |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1300 | spell_valid_case(wordflags, treeflags) |
| 1301 | int wordflags; /* flags for the checked word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1302 | int treeflags; /* flags for the word in the spell tree */ |
| 1303 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1304 | return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1305 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1306 | && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP))); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1309 | /* |
| 1310 | * Return TRUE if spell checking is not enabled. |
| 1311 | */ |
| 1312 | static int |
| 1313 | no_spell_checking() |
| 1314 | { |
| 1315 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 1316 | { |
| 1317 | EMSG(_("E756: Spell checking is not enabled")); |
| 1318 | return TRUE; |
| 1319 | } |
| 1320 | return FALSE; |
| 1321 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1322 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1323 | /* |
| 1324 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1325 | * "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] | 1326 | * Return OK if found, FAIL otherwise. |
| 1327 | */ |
| 1328 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1329 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1330 | int dir; /* FORWARD or BACKWARD */ |
| 1331 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1332 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1333 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1334 | linenr_T lnum; |
| 1335 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1336 | char_u *line; |
| 1337 | char_u *p; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1338 | char_u *endp; |
| 1339 | int attr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1340 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1341 | int has_syntax = syntax_present(curbuf); |
| 1342 | int col; |
| 1343 | int can_spell; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1344 | char_u *buf = NULL; |
| 1345 | int buflen = 0; |
| 1346 | int skip = 0; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1347 | int capcol = -1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1348 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1349 | if (no_spell_checking()) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1350 | return FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1351 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1352 | /* |
| 1353 | * 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] | 1354 | * 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] | 1355 | * |
| 1356 | * When searching backwards, we continue in the line to find the last |
| 1357 | * bad word (in the cursor line: before the cursor). |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1358 | * |
| 1359 | * We concatenate the start of the next line, so that wrapped words work |
| 1360 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 1361 | * though... |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1362 | */ |
| 1363 | lnum = curwin->w_cursor.lnum; |
| 1364 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1365 | |
| 1366 | while (!got_int) |
| 1367 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1368 | line = ml_get(lnum); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1369 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1370 | len = STRLEN(line); |
| 1371 | if (buflen < len + MAXWLEN + 2) |
| 1372 | { |
| 1373 | vim_free(buf); |
| 1374 | buflen = len + MAXWLEN + 2; |
| 1375 | buf = alloc(buflen); |
| 1376 | if (buf == NULL) |
| 1377 | break; |
| 1378 | } |
| 1379 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1380 | /* In first line check first word for Capital. */ |
| 1381 | if (lnum == 1) |
| 1382 | capcol = 0; |
| 1383 | |
| 1384 | /* For checking first word with a capital skip white space. */ |
| 1385 | if (capcol == 0) |
| 1386 | capcol = skipwhite(line) - line; |
| 1387 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1388 | /* Copy the line into "buf" and append the start of the next line if |
| 1389 | * possible. */ |
| 1390 | STRCPY(buf, line); |
| 1391 | if (lnum < curbuf->b_ml.ml_line_count) |
| 1392 | spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN); |
| 1393 | |
| 1394 | p = buf + skip; |
| 1395 | endp = buf + len; |
| 1396 | while (p < endp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1397 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1398 | /* When searching backward don't search after the cursor. */ |
| 1399 | if (dir == BACKWARD |
| 1400 | && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1401 | && (colnr_T)(p - buf) >= curwin->w_cursor.col) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1402 | break; |
| 1403 | |
| 1404 | /* start of word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1405 | attr = 0; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1406 | len = spell_check(curwin, p, &attr, &capcol); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1407 | |
| 1408 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1409 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1410 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1411 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1412 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1413 | /* When searching forward only accept a bad word after |
| 1414 | * the cursor. */ |
| 1415 | if (dir == BACKWARD |
| 1416 | || lnum > curwin->w_cursor.lnum |
| 1417 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1418 | && (colnr_T)(curline ? p - buf + len |
| 1419 | : p - buf) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1420 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1421 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1422 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1423 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1424 | col = p - buf; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1425 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1426 | FALSE, &can_spell); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1427 | } |
| 1428 | else |
| 1429 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1430 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1431 | if (can_spell) |
| 1432 | { |
| 1433 | found_pos.lnum = lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1434 | found_pos.col = p - buf; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1435 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1436 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1437 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1438 | if (dir == FORWARD) |
| 1439 | { |
| 1440 | /* No need to search further. */ |
| 1441 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1442 | vim_free(buf); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1443 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1444 | } |
| 1445 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1446 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1447 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1450 | /* advance to character after the word */ |
| 1451 | p += len; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1452 | capcol -= len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1455 | if (curline) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1456 | break; /* only check cursor line */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1457 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1458 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1459 | if (dir == BACKWARD) |
| 1460 | { |
| 1461 | if (found_pos.lnum != 0) |
| 1462 | { |
| 1463 | /* Use the last match in the line. */ |
| 1464 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1465 | vim_free(buf); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1466 | return OK; |
| 1467 | } |
| 1468 | if (lnum == 1) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1469 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1470 | --lnum; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1471 | capcol = -1; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1472 | } |
| 1473 | else |
| 1474 | { |
| 1475 | if (lnum == curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1476 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1477 | ++lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1478 | |
| 1479 | /* Skip the characters at the start of the next line that were |
| 1480 | * included in a match crossing line boundaries. */ |
| 1481 | if (attr == 0) |
| 1482 | skip = p - endp; |
| 1483 | else |
| 1484 | skip = 0; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Capscol skips over the inserted space. */ |
| 1487 | --capcol; |
| 1488 | |
| 1489 | /* But after empty line check first word in next line */ |
| 1490 | if (*skipwhite(line) == NUL) |
| 1491 | capcol = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1492 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1493 | |
| 1494 | line_breakcheck(); |
| 1495 | } |
| 1496 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1497 | vim_free(buf); |
| 1498 | return FAIL; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * For spell checking: concatenate the start of the following line "line" into |
| 1503 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| 1504 | */ |
| 1505 | void |
| 1506 | spell_cat_line(buf, line, maxlen) |
| 1507 | char_u *buf; |
| 1508 | char_u *line; |
| 1509 | int maxlen; |
| 1510 | { |
| 1511 | char_u *p; |
| 1512 | int n; |
| 1513 | |
| 1514 | p = skipwhite(line); |
| 1515 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 1516 | p = skipwhite(p + 1); |
| 1517 | |
| 1518 | if (*p != NUL) |
| 1519 | { |
| 1520 | *buf = ' '; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1521 | vim_strncpy(buf + 1, line, maxlen - 2); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1522 | n = p - line; |
| 1523 | if (n >= maxlen) |
| 1524 | n = maxlen - 1; |
| 1525 | vim_memset(buf + 1, ' ', n); |
| 1526 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1530 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1531 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1532 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1533 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1534 | spell_load_lang(lang) |
| 1535 | char_u *lang; |
| 1536 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1537 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1538 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1539 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1540 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1541 | /* 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] | 1542 | * It's truncated when an error is detected. */ |
| 1543 | STRCPY(langcp, lang); |
| 1544 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1545 | /* |
| 1546 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1547 | */ |
| 1548 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1549 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1550 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1551 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1552 | if (r == FAIL && *langcp != NUL) |
| 1553 | { |
| 1554 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1555 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1556 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1557 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1560 | if (r == FAIL) |
| 1561 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1562 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1563 | else if (*langcp != NUL) |
| 1564 | { |
| 1565 | /* Load all the additions. */ |
| 1566 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1567 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1573 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1574 | */ |
| 1575 | static char_u * |
| 1576 | spell_enc() |
| 1577 | { |
| 1578 | |
| 1579 | #ifdef FEAT_MBYTE |
| 1580 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1581 | return p_enc; |
| 1582 | #endif |
| 1583 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /* |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1587 | * Get the name of the .spl file for the internal wordlist into |
| 1588 | * "fname[MAXPATHL]". |
| 1589 | */ |
| 1590 | static void |
| 1591 | int_wordlist_spl(fname) |
| 1592 | char_u *fname; |
| 1593 | { |
| 1594 | vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl", |
| 1595 | int_wordlist, spell_enc()); |
| 1596 | } |
| 1597 | |
| 1598 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1599 | * Allocate a new slang_T. |
| 1600 | * Caller must fill "sl_next". |
| 1601 | */ |
| 1602 | static slang_T * |
| 1603 | slang_alloc(lang) |
| 1604 | char_u *lang; |
| 1605 | { |
| 1606 | slang_T *lp; |
| 1607 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1608 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1609 | if (lp != NULL) |
| 1610 | { |
| 1611 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1612 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1613 | ga_init2(&lp->sl_sal, sizeof(salitem_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1614 | } |
| 1615 | return lp; |
| 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | * Free the contents of an slang_T and the structure itself. |
| 1620 | */ |
| 1621 | static void |
| 1622 | slang_free(lp) |
| 1623 | slang_T *lp; |
| 1624 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1625 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1626 | vim_free(lp->sl_fname); |
| 1627 | slang_clear(lp); |
| 1628 | vim_free(lp); |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * Clear an slang_T so that the file can be reloaded. |
| 1633 | */ |
| 1634 | static void |
| 1635 | slang_clear(lp) |
| 1636 | slang_T *lp; |
| 1637 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1638 | garray_T *gap; |
| 1639 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1640 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1641 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1642 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1643 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1644 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1645 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1646 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1647 | vim_free(lp->sl_pbyts); |
| 1648 | lp->sl_pbyts = NULL; |
| 1649 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1650 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1651 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1652 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1653 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1654 | vim_free(lp->sl_pidxs); |
| 1655 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1656 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1657 | gap = &lp->sl_rep; |
| 1658 | while (gap->ga_len > 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1659 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1660 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1661 | vim_free(ftp->ft_from); |
| 1662 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1663 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1664 | ga_clear(gap); |
| 1665 | |
| 1666 | gap = &lp->sl_sal; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1667 | if (lp->sl_sofo) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1668 | { |
| 1669 | /* "ga_len" is set to 1 without adding an item for latin1 */ |
| 1670 | if (gap->ga_data != NULL) |
| 1671 | /* SOFOFROM and SOFOTO items: free lists of wide characters. */ |
| 1672 | for (i = 0; i < gap->ga_len; ++i) |
| 1673 | vim_free(((int **)gap->ga_data)[i]); |
| 1674 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1675 | else |
| 1676 | /* SAL items: free salitem_T items */ |
| 1677 | while (gap->ga_len > 0) |
| 1678 | { |
| 1679 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1680 | vim_free(smp->sm_lead); |
| 1681 | /* Don't free sm_oneof and sm_rules, they point into sm_lead. */ |
| 1682 | vim_free(smp->sm_to); |
| 1683 | #ifdef FEAT_MBYTE |
| 1684 | vim_free(smp->sm_lead_w); |
| 1685 | vim_free(smp->sm_oneof_w); |
| 1686 | vim_free(smp->sm_to_w); |
| 1687 | #endif |
| 1688 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1689 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1690 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1691 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1692 | vim_free(lp->sl_prefprog[i]); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1693 | lp->sl_prefixcnt = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1694 | vim_free(lp->sl_prefprog); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1695 | lp->sl_prefprog = NULL; |
| 1696 | |
| 1697 | vim_free(lp->sl_midword); |
| 1698 | lp->sl_midword = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1699 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1700 | #ifdef FEAT_MBYTE |
| 1701 | { |
| 1702 | int todo = lp->sl_map_hash.ht_used; |
| 1703 | hashitem_T *hi; |
| 1704 | |
| 1705 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1706 | if (!HASHITEM_EMPTY(hi)) |
| 1707 | { |
| 1708 | --todo; |
| 1709 | vim_free(hi->hi_key); |
| 1710 | } |
| 1711 | } |
| 1712 | hash_clear(&lp->sl_map_hash); |
| 1713 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1717 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1718 | * Invoked through do_in_runtimepath(). |
| 1719 | */ |
| 1720 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1721 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1722 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1723 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1724 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1725 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * Load one spell file and store the info into a slang_T. |
| 1730 | * |
| 1731 | * This is invoked in two ways: |
| 1732 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1733 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1734 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1735 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1736 | * 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] | 1737 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1738 | static slang_T * |
| 1739 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1740 | char_u *fname; |
| 1741 | char_u *lang; |
| 1742 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1743 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1744 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1745 | FILE *fd; |
| 1746 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1747 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1748 | char_u *bp; |
| 1749 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1750 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1751 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1752 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1753 | int round; |
| 1754 | char_u *save_sourcing_name = sourcing_name; |
| 1755 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1756 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1757 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1758 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1759 | garray_T *gap; |
| 1760 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1761 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1762 | short *first; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1763 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1764 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1765 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1766 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1767 | if (fd == NULL) |
| 1768 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1769 | if (!silent) |
| 1770 | EMSG2(_(e_notopen), fname); |
| 1771 | else if (p_verbose > 2) |
| 1772 | { |
| 1773 | verbose_enter(); |
| 1774 | smsg((char_u *)e_notopen, fname); |
| 1775 | verbose_leave(); |
| 1776 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1777 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1778 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1779 | if (p_verbose > 2) |
| 1780 | { |
| 1781 | verbose_enter(); |
| 1782 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1783 | verbose_leave(); |
| 1784 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1785 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1786 | if (old_lp == NULL) |
| 1787 | { |
| 1788 | lp = slang_alloc(lang); |
| 1789 | if (lp == NULL) |
| 1790 | goto endFAIL; |
| 1791 | |
| 1792 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1793 | lp->sl_fname = vim_strsave(fname); |
| 1794 | if (lp->sl_fname == NULL) |
| 1795 | goto endFAIL; |
| 1796 | |
| 1797 | /* Check for .add.spl. */ |
| 1798 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1799 | } |
| 1800 | else |
| 1801 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1802 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1803 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1804 | sourcing_name = fname; |
| 1805 | sourcing_lnum = 0; |
| 1806 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1807 | /* <HEADER>: <fileID> |
| 1808 | * <regioncnt> <regionname> ... |
| 1809 | * <charflagslen> <charflags> |
| 1810 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1811 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1812 | * <prefcondcnt> <prefcond> ... |
| 1813 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1814 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1815 | buf[i] = getc(fd); /* <fileID> */ |
| 1816 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1817 | { |
| 1818 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1819 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1823 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1824 | { |
| 1825 | truncerr: |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1826 | EMSG(_(e_spell_trunc)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1827 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1828 | } |
| 1829 | if (cnt > 8) |
| 1830 | { |
| 1831 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1832 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1833 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1834 | } |
| 1835 | for (i = 0; i < cnt; ++i) |
| 1836 | { |
| 1837 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1838 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1839 | } |
| 1840 | lp->sl_regions[cnt * 2] = NUL; |
| 1841 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1842 | /* <charflagslen> <charflags> */ |
| 1843 | p = read_cnt_string(fd, 1, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1844 | if (cnt < 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1845 | goto endFAIL; |
| 1846 | |
| 1847 | /* <fcharslen> <fchars> */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1848 | fol = read_cnt_string(fd, 2, &ccnt); |
| 1849 | if (ccnt < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1850 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1851 | vim_free(p); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1852 | goto endFAIL; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1853 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1854 | |
| 1855 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
| 1856 | if (p != NULL && fol != NULL) |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1857 | i = set_spell_charflags(p, cnt, fol); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1858 | |
| 1859 | vim_free(p); |
| 1860 | vim_free(fol); |
| 1861 | |
| 1862 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1863 | if ((p == NULL) != (fol == NULL)) |
| 1864 | goto formerr; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1865 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1866 | /* <midwordlen> <midword> */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1867 | lp->sl_midword = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1868 | if (cnt < 0) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1869 | goto endFAIL; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1870 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1871 | /* <prefcondcnt> <prefcond> ... */ |
| 1872 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1873 | if (cnt > 0) |
| 1874 | { |
| 1875 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1876 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1877 | if (lp->sl_prefprog == NULL) |
| 1878 | goto endFAIL; |
| 1879 | lp->sl_prefixcnt = cnt; |
| 1880 | |
| 1881 | for (i = 0; i < cnt; ++i) |
| 1882 | { |
| 1883 | /* <prefcond> : <condlen> <condstr> */ |
| 1884 | n = getc(fd); /* <condlen> */ |
| 1885 | if (n < 0) |
| 1886 | goto formerr; |
| 1887 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1888 | * compile the regexp program used to check for the condition. */ |
| 1889 | if (n > 0) |
| 1890 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1891 | buf[0] = '^'; /* always match at one position only */ |
| 1892 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1893 | while (n-- > 0) |
| 1894 | *p++ = getc(fd); /* <condstr> */ |
| 1895 | *p = NUL; |
| 1896 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1902 | /* <SUGGEST> : <repcount> <rep> ... |
| 1903 | * <salflags> <salcount> <sal> ... |
| 1904 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1905 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1906 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1907 | if (cnt < 0) |
| 1908 | goto formerr; |
| 1909 | |
| 1910 | gap = &lp->sl_rep; |
| 1911 | if (ga_grow(gap, cnt) == FAIL) |
| 1912 | goto endFAIL; |
| 1913 | |
| 1914 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1915 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1916 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1917 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1918 | ftp->ft_from = read_cnt_string(fd, 1, &i); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1919 | if (i <= 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1920 | goto endFAIL; |
| 1921 | ftp->ft_to = read_cnt_string(fd, 1, &i); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1922 | if (i <= 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1923 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1924 | vim_free(ftp->ft_from); |
| 1925 | goto endFAIL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | /* Fill the first-index table. */ |
| 1930 | first = lp->sl_rep_first; |
| 1931 | for (i = 0; i < 256; ++i) |
| 1932 | first[i] = -1; |
| 1933 | for (i = 0; i < gap->ga_len; ++i) |
| 1934 | { |
| 1935 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1936 | if (first[*ftp->ft_from] == -1) |
| 1937 | first[*ftp->ft_from] = i; |
| 1938 | } |
| 1939 | |
| 1940 | i = getc(fd); /* <salflags> */ |
| 1941 | if (i & SAL_F0LLOWUP) |
| 1942 | lp->sl_followup = TRUE; |
| 1943 | if (i & SAL_COLLAPSE) |
| 1944 | lp->sl_collapse = TRUE; |
| 1945 | if (i & SAL_REM_ACCENTS) |
| 1946 | lp->sl_rem_accents = TRUE; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1947 | if (i & SAL_SOFO) |
| 1948 | lp->sl_sofo = TRUE; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1949 | else |
| 1950 | lp->sl_sofo = FALSE; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1951 | |
| 1952 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1953 | if (cnt < 0) |
| 1954 | goto formerr; |
| 1955 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1956 | if (lp->sl_sofo) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1957 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1958 | /* |
| 1959 | * SOFOFROM and SOFOTO items come in one <salfrom> and <salto> |
| 1960 | */ |
| 1961 | if (cnt != 1) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1962 | goto formerr; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1963 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1964 | /* <salfromlen> <salfrom> */ |
| 1965 | bp = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1966 | if (cnt < 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1967 | goto endFAIL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1968 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1969 | /* <saltolen> <salto> */ |
| 1970 | fol = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 1971 | if (cnt < 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1972 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1973 | vim_free(bp); |
| 1974 | goto endFAIL; |
| 1975 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1976 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1977 | /* 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^] | 1978 | if (bp != NULL && fol != NULL) |
| 1979 | i = set_sofo(lp, bp, fol); |
| 1980 | else if (bp != NULL || fol != NULL) |
| 1981 | i = FAIL; /* only one of two strings is an error */ |
| 1982 | else |
| 1983 | i = OK; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1984 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1985 | vim_free(bp); |
| 1986 | vim_free(fol); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 1987 | if (i == FAIL) |
| 1988 | goto formerr; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1989 | } |
| 1990 | else |
| 1991 | { |
| 1992 | /* |
| 1993 | * SAL items |
| 1994 | */ |
| 1995 | gap = &lp->sl_sal; |
| 1996 | if (ga_grow(gap, cnt) == FAIL) |
| 1997 | goto endFAIL; |
| 1998 | |
| 1999 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 2000 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 2001 | { |
| 2002 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 2003 | ccnt = getc(fd); /* <salfromlen> */ |
| 2004 | if (ccnt < 0) |
| 2005 | goto formerr; |
| 2006 | if ((p = alloc(ccnt + 2)) == NULL) |
| 2007 | goto endFAIL; |
| 2008 | smp->sm_lead = p; |
| 2009 | |
| 2010 | /* Read up to the first special char into sm_lead. */ |
| 2011 | for (i = 0; i < ccnt; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2012 | { |
| 2013 | c = getc(fd); /* <salfrom> */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2014 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | *p++ = c; |
| 2017 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2018 | smp->sm_leadlen = p - smp->sm_lead; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2019 | *p++ = NUL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2020 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2021 | /* Put (abc) chars in sm_oneof, if any. */ |
| 2022 | if (c == '(') |
| 2023 | { |
| 2024 | smp->sm_oneof = p; |
| 2025 | for (++i; i < ccnt; ++i) |
| 2026 | { |
| 2027 | c = getc(fd); /* <salfrom> */ |
| 2028 | if (c == ')') |
| 2029 | break; |
| 2030 | *p++ = c; |
| 2031 | } |
| 2032 | *p++ = NUL; |
| 2033 | if (++i < ccnt) |
| 2034 | c = getc(fd); |
| 2035 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2036 | else |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2037 | smp->sm_oneof = NULL; |
| 2038 | |
| 2039 | /* Any following chars go in sm_rules. */ |
| 2040 | smp->sm_rules = p; |
| 2041 | if (i < ccnt) |
| 2042 | /* store the char we got while checking for end of sm_lead */ |
| 2043 | *p++ = c; |
| 2044 | for (++i; i < ccnt; ++i) |
| 2045 | *p++ = getc(fd); /* <salfrom> */ |
| 2046 | *p++ = NUL; |
| 2047 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2048 | /* <saltolen> <salto> */ |
| 2049 | smp->sm_to = read_cnt_string(fd, 1, &ccnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2050 | if (ccnt < 0) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2051 | { |
| 2052 | vim_free(smp->sm_lead); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2053 | goto formerr; |
| 2054 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2055 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2056 | #ifdef FEAT_MBYTE |
| 2057 | if (has_mbyte) |
| 2058 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2059 | /* convert the multi-byte strings to wide char strings */ |
| 2060 | smp->sm_lead_w = mb_str2wide(smp->sm_lead); |
| 2061 | smp->sm_leadlen = mb_charlen(smp->sm_lead); |
| 2062 | if (smp->sm_oneof == NULL) |
| 2063 | smp->sm_oneof_w = NULL; |
| 2064 | else |
| 2065 | smp->sm_oneof_w = mb_str2wide(smp->sm_oneof); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2066 | if (smp->sm_to == NULL) |
| 2067 | smp->sm_to_w = NULL; |
| 2068 | else |
| 2069 | smp->sm_to_w = mb_str2wide(smp->sm_to); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2070 | if (smp->sm_lead_w == NULL |
| 2071 | || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL) |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2072 | || (smp->sm_to_w == NULL && smp->sm_to != NULL)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2073 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2074 | vim_free(smp->sm_lead); |
| 2075 | vim_free(smp->sm_to); |
| 2076 | vim_free(smp->sm_lead_w); |
| 2077 | vim_free(smp->sm_oneof_w); |
| 2078 | vim_free(smp->sm_to_w); |
| 2079 | goto endFAIL; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2080 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2081 | } |
| 2082 | #endif |
| 2083 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2084 | |
| 2085 | /* Fill the first-index table. */ |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2086 | set_sal_first(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2089 | /* <maplen> <mapstr> */ |
| 2090 | p = read_cnt_string(fd, 2, &cnt); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2091 | if (cnt < 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2092 | goto endFAIL; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2093 | if (p != NULL) |
| 2094 | { |
| 2095 | set_map_str(lp, p); |
| 2096 | vim_free(p); |
| 2097 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2098 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2099 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2100 | * round 2: <KWORDTREE> |
| 2101 | * round 3: <PREFIXTREE> */ |
| 2102 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2103 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2104 | /* The tree size was computed when writing the file, so that we can |
| 2105 | * allocate it as one long block. <nodecount> */ |
| 2106 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2107 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2108 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2109 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2110 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2111 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2112 | bp = lalloc((long_u)len, TRUE); |
| 2113 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2114 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2115 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2116 | lp->sl_fbyts = bp; |
| 2117 | else if (round == 2) |
| 2118 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2119 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2120 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2121 | |
| 2122 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2123 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 2124 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2125 | goto endFAIL; |
| 2126 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2127 | lp->sl_fidxs = ip; |
| 2128 | else if (round == 2) |
| 2129 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2130 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2131 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2132 | |
| 2133 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2134 | 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] | 2135 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2136 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2137 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2138 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2139 | } |
| 2140 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2141 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2142 | /* For a new file link it in the list of spell files. */ |
| 2143 | if (old_lp == NULL) |
| 2144 | { |
| 2145 | lp->sl_next = first_lang; |
| 2146 | first_lang = lp; |
| 2147 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2148 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2149 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2150 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2151 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2152 | if (lang != NULL) |
| 2153 | /* truncating the name signals the error to spell_load_lang() */ |
| 2154 | *lang = NUL; |
| 2155 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2156 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2157 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2158 | lp = NULL; |
| 2159 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2160 | |
| 2161 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2162 | if (fd != NULL) |
| 2163 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2164 | sourcing_name = save_sourcing_name; |
| 2165 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2166 | |
| 2167 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2170 | /* |
| 2171 | * Read a length field from "fd" in "cnt_bytes" bytes. |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2172 | * 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] | 2173 | * Returns NULL when the count is zero. |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2174 | * 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] | 2175 | */ |
| 2176 | static char_u * |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2177 | read_cnt_string(fd, cnt_bytes, cntp) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2178 | FILE *fd; |
| 2179 | int cnt_bytes; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2180 | int *cntp; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2181 | { |
| 2182 | int cnt = 0; |
| 2183 | int i; |
| 2184 | char_u *str; |
| 2185 | |
| 2186 | /* read the length bytes, MSB first */ |
| 2187 | for (i = 0; i < cnt_bytes; ++i) |
| 2188 | cnt = (cnt << 8) + getc(fd); |
| 2189 | if (cnt < 0) |
| 2190 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2191 | EMSG(_(e_spell_trunc)); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2192 | *cntp = -1; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2193 | return NULL; |
| 2194 | } |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2195 | *cntp = cnt; |
| 2196 | if (cnt == 0) |
| 2197 | return NULL; /* nothing to read, return NULL */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2198 | |
| 2199 | /* allocate memory */ |
| 2200 | str = alloc((unsigned)cnt + 1); |
| 2201 | if (str == NULL) |
| 2202 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2203 | *cntp = -1; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2204 | return NULL; |
| 2205 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2206 | |
| 2207 | /* Read the string. Doesn't check for truncated file. */ |
| 2208 | for (i = 0; i < cnt; ++i) |
| 2209 | str[i] = getc(fd); |
| 2210 | str[i] = NUL; |
| 2211 | |
| 2212 | return str; |
| 2213 | } |
| 2214 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2215 | /* |
| 2216 | * Set the SOFOFROM and SOFOTO items in language "lp". |
| 2217 | * Returns FAIL when there is something wrong. |
| 2218 | */ |
| 2219 | static int |
| 2220 | set_sofo(lp, from, to) |
| 2221 | slang_T *lp; |
| 2222 | char_u *from; |
| 2223 | char_u *to; |
| 2224 | { |
| 2225 | int i; |
| 2226 | |
| 2227 | #ifdef FEAT_MBYTE |
| 2228 | garray_T *gap; |
| 2229 | char_u *s; |
| 2230 | char_u *p; |
| 2231 | int c; |
| 2232 | int *inp; |
| 2233 | |
| 2234 | if (has_mbyte) |
| 2235 | { |
| 2236 | /* Use "sl_sal" as an array with 256 pointers to a list of wide |
| 2237 | * characters. The index is the low byte of the character. |
| 2238 | * The list contains from-to pairs with a terminating NUL. |
| 2239 | * sl_sal_first[] is used for latin1 "from" characters. */ |
| 2240 | gap = &lp->sl_sal; |
| 2241 | ga_init2(gap, sizeof(int *), 1); |
| 2242 | if (ga_grow(gap, 256) == FAIL) |
| 2243 | return FAIL; |
| 2244 | vim_memset(gap->ga_data, 0, sizeof(int *) * 256); |
| 2245 | gap->ga_len = 256; |
| 2246 | |
| 2247 | /* First count the number of items for each list. Temporarily use |
| 2248 | * sl_sal_first[] for this. */ |
| 2249 | for (p = from, s = to; *p != NUL && *s != NUL; ) |
| 2250 | { |
| 2251 | c = mb_ptr2char_adv(&p); |
| 2252 | mb_ptr_adv(s); |
| 2253 | if (c >= 256) |
| 2254 | ++lp->sl_sal_first[c & 0xff]; |
| 2255 | } |
| 2256 | if (*p != NUL || *s != NUL) /* lengths differ */ |
| 2257 | return FAIL; |
| 2258 | |
| 2259 | /* Allocate the lists. */ |
| 2260 | for (i = 0; i < 256; ++i) |
| 2261 | if (lp->sl_sal_first[i] > 0) |
| 2262 | { |
| 2263 | p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1)); |
| 2264 | if (p == NULL) |
| 2265 | return FAIL; |
| 2266 | ((int **)gap->ga_data)[i] = (int *)p; |
| 2267 | *(int *)p = 0; |
| 2268 | } |
| 2269 | |
| 2270 | /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal |
| 2271 | * list. */ |
| 2272 | vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256); |
| 2273 | for (p = from, s = to; *p != NUL && *s != NUL; ) |
| 2274 | { |
| 2275 | c = mb_ptr2char_adv(&p); |
| 2276 | i = mb_ptr2char_adv(&s); |
| 2277 | if (c >= 256) |
| 2278 | { |
| 2279 | /* Append the from-to chars at the end of the list with |
| 2280 | * the low byte. */ |
| 2281 | inp = ((int **)gap->ga_data)[c & 0xff]; |
| 2282 | while (*inp != 0) |
| 2283 | ++inp; |
| 2284 | *inp++ = c; /* from char */ |
| 2285 | *inp++ = i; /* to char */ |
| 2286 | *inp++ = NUL; /* NUL at the end */ |
| 2287 | } |
| 2288 | else |
| 2289 | /* mapping byte to char is done in sl_sal_first[] */ |
| 2290 | lp->sl_sal_first[c] = i; |
| 2291 | } |
| 2292 | } |
| 2293 | else |
| 2294 | #endif |
| 2295 | { |
| 2296 | /* mapping bytes to bytes is done in sl_sal_first[] */ |
| 2297 | if (STRLEN(from) != STRLEN(to)) |
| 2298 | return FAIL; |
| 2299 | |
| 2300 | for (i = 0; to[i] != NUL; ++i) |
| 2301 | lp->sl_sal_first[from[i]] = to[i]; |
| 2302 | lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */ |
| 2303 | } |
| 2304 | |
| 2305 | return OK; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | * Fill the first-index table for "lp". |
| 2310 | */ |
| 2311 | static void |
| 2312 | set_sal_first(lp) |
| 2313 | slang_T *lp; |
| 2314 | { |
| 2315 | salfirst_T *sfirst; |
| 2316 | int i; |
| 2317 | salitem_T *smp; |
| 2318 | int c; |
| 2319 | garray_T *gap = &lp->sl_sal; |
| 2320 | |
| 2321 | sfirst = lp->sl_sal_first; |
| 2322 | for (i = 0; i < 256; ++i) |
| 2323 | sfirst[i] = -1; |
| 2324 | smp = (salitem_T *)gap->ga_data; |
| 2325 | for (i = 0; i < gap->ga_len; ++i) |
| 2326 | { |
| 2327 | #ifdef FEAT_MBYTE |
| 2328 | if (has_mbyte) |
| 2329 | /* Use the lowest byte of the first character. For latin1 it's |
| 2330 | * the character, for other encodings it should differ for most |
| 2331 | * characters. */ |
| 2332 | c = *smp[i].sm_lead_w & 0xff; |
| 2333 | else |
| 2334 | #endif |
| 2335 | c = *smp[i].sm_lead; |
| 2336 | if (sfirst[c] == -1) |
| 2337 | { |
| 2338 | sfirst[c] = i; |
| 2339 | #ifdef FEAT_MBYTE |
| 2340 | if (has_mbyte) |
| 2341 | { |
| 2342 | int n; |
| 2343 | |
| 2344 | /* Make sure all entries with this byte are following each |
| 2345 | * other. Move the ones that are in the wrong position. Do |
| 2346 | * keep the same ordering! */ |
| 2347 | while (i + 1 < gap->ga_len |
| 2348 | && (*smp[i + 1].sm_lead_w & 0xff) == c) |
| 2349 | /* Skip over entry with same index byte. */ |
| 2350 | ++i; |
| 2351 | |
| 2352 | for (n = 1; i + n < gap->ga_len; ++n) |
| 2353 | if ((*smp[i + n].sm_lead_w & 0xff) == c) |
| 2354 | { |
| 2355 | salitem_T tsal; |
| 2356 | |
| 2357 | /* Move entry with same index byte after the entries |
| 2358 | * we already found. */ |
| 2359 | ++i; |
| 2360 | --n; |
| 2361 | tsal = smp[i + n]; |
| 2362 | mch_memmove(smp + i + 1, smp + i, |
| 2363 | sizeof(salitem_T) * n); |
| 2364 | smp[i] = tsal; |
| 2365 | } |
| 2366 | } |
| 2367 | #endif |
| 2368 | } |
| 2369 | } |
| 2370 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2371 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2372 | #ifdef FEAT_MBYTE |
| 2373 | /* |
| 2374 | * Turn a multi-byte string into a wide character string. |
| 2375 | * Return it in allocated memory (NULL for out-of-memory) |
| 2376 | */ |
| 2377 | static int * |
| 2378 | mb_str2wide(s) |
| 2379 | char_u *s; |
| 2380 | { |
| 2381 | int *res; |
| 2382 | char_u *p; |
| 2383 | int i = 0; |
| 2384 | |
| 2385 | res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1)); |
| 2386 | if (res != NULL) |
| 2387 | { |
| 2388 | for (p = s; *p != NUL; ) |
| 2389 | res[i++] = mb_ptr2char_adv(&p); |
| 2390 | res[i] = NUL; |
| 2391 | } |
| 2392 | return res; |
| 2393 | } |
| 2394 | #endif |
| 2395 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2396 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2397 | * Read one row of siblings from the spell file and store it in the byte array |
| 2398 | * "byts" and index array "idxs". Recursively read the children. |
| 2399 | * |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2400 | * NOTE: The code here must match put_node(). |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2401 | * |
| 2402 | * Returns the index follosing the siblings. |
| 2403 | * Returns -1 if the file is shorter than expected. |
| 2404 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2405 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2406 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2407 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2408 | FILE *fd; |
| 2409 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2410 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2411 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2412 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2413 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 2414 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2415 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2416 | int len; |
| 2417 | int i; |
| 2418 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2419 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2420 | int c; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2421 | int c2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2422 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2423 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2424 | len = getc(fd); /* <siblingcount> */ |
| 2425 | if (len <= 0) |
| 2426 | return -1; |
| 2427 | |
| 2428 | if (startidx + len >= maxidx) |
| 2429 | return -2; |
| 2430 | byts[idx++] = len; |
| 2431 | |
| 2432 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 2433 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2434 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2435 | c = getc(fd); /* <byte> */ |
| 2436 | if (c < 0) |
| 2437 | return -1; |
| 2438 | if (c <= BY_SPECIAL) |
| 2439 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2440 | if (c == BY_NOFLAGS && !prefixtree) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2441 | { |
| 2442 | /* No flags, all regions. */ |
| 2443 | idxs[idx] = 0; |
| 2444 | c = 0; |
| 2445 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2446 | else if (c == BY_FLAGS || c == BY_NOFLAGS) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2447 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2448 | if (prefixtree) |
| 2449 | { |
| 2450 | /* Read the prefix ID and the condition nr. In idxs[] |
| 2451 | * store the prefix ID in the low byte, the condition |
| 2452 | * index shifted up 8 bits. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2453 | c2 = getc(fd); /* <prefixID> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2454 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 2455 | if (n >= maxprefcondnr) |
| 2456 | return -2; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2457 | c2 += (n << 8); |
| 2458 | if (c == BY_NOFLAGS) |
| 2459 | c = c2; |
| 2460 | else |
| 2461 | c = c2 | WF_RAREPFX; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2462 | } |
| 2463 | else |
| 2464 | { |
| 2465 | /* Read flags and optional region and prefix ID. In |
| 2466 | * idxs[] the flags go in the low byte, region above that |
| 2467 | * and prefix ID above the region. */ |
| 2468 | c = getc(fd); /* <flags> */ |
| 2469 | if (c & WF_REGION) |
| 2470 | c = (getc(fd) << 8) + c; /* <region> */ |
| 2471 | if (c & WF_PFX) |
| 2472 | c = (getc(fd) << 16) + c; /* <prefixID> */ |
| 2473 | } |
| 2474 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2475 | idxs[idx] = c; |
| 2476 | c = 0; |
| 2477 | } |
| 2478 | else /* c == BY_INDEX */ |
| 2479 | { |
| 2480 | /* <nodeidx> */ |
| 2481 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2482 | if (n < 0 || n >= maxidx) |
| 2483 | return -2; |
| 2484 | idxs[idx] = n + SHARED_MASK; |
| 2485 | c = getc(fd); /* <xbyte> */ |
| 2486 | } |
| 2487 | } |
| 2488 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2491 | /* Recursively read the children for non-shared siblings. |
| 2492 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 2493 | * remove SHARED_MASK) */ |
| 2494 | for (i = 1; i <= len; ++i) |
| 2495 | if (byts[startidx + i] != 0) |
| 2496 | { |
| 2497 | if (idxs[startidx + i] & SHARED_MASK) |
| 2498 | idxs[startidx + i] &= ~SHARED_MASK; |
| 2499 | else |
| 2500 | { |
| 2501 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2502 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 2503 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2504 | if (idx < 0) |
| 2505 | break; |
| 2506 | } |
| 2507 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2508 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2509 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | /* |
| 2513 | * Parse 'spelllang' and set buf->b_langp accordingly. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2514 | * Returns NULL if it's OK, an error message otherwise. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2515 | */ |
| 2516 | char_u * |
| 2517 | did_set_spelllang(buf) |
| 2518 | buf_T *buf; |
| 2519 | { |
| 2520 | garray_T ga; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2521 | char_u *splp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2522 | char_u *region; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2523 | int filename; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2524 | int region_mask; |
| 2525 | slang_T *lp; |
| 2526 | int c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2527 | char_u lang[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2528 | char_u spf_name[MAXPATHL]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2529 | int len; |
| 2530 | char_u *p; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2531 | int round; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2532 | char_u *spf; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2533 | char_u *use_region = NULL; |
| 2534 | int dont_use_region = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2535 | |
| 2536 | ga_init2(&ga, sizeof(langp_T), 2); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2537 | clear_midword(buf); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2538 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2539 | /* loop over comma separated language names. */ |
| 2540 | for (splp = buf->b_p_spl; *splp != NUL; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2541 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2542 | /* Get one language name. */ |
| 2543 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 2544 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2545 | region = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2546 | len = STRLEN(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2547 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2548 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 2549 | * If there is a region name let "region" point to it and remove it |
| 2550 | * from the name. */ |
| 2551 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 2552 | { |
| 2553 | filename = TRUE; |
| 2554 | |
| 2555 | /* Check if we loaded this language before. */ |
| 2556 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2557 | if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME) |
| 2558 | break; |
| 2559 | } |
| 2560 | else |
| 2561 | { |
| 2562 | filename = FALSE; |
| 2563 | if (len > 3 && lang[len - 3] == '_') |
| 2564 | { |
| 2565 | region = lang + len - 2; |
| 2566 | len -= 3; |
| 2567 | lang[len] = NUL; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2568 | |
| 2569 | /* If the region differs from what was used before then don't |
| 2570 | * use it for 'spellfile'. */ |
| 2571 | if (use_region != NULL && STRCMP(region, use_region) != 0) |
| 2572 | dont_use_region = TRUE; |
| 2573 | use_region = region; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2574 | } |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2575 | else |
| 2576 | dont_use_region = TRUE; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2577 | |
| 2578 | /* Check if we loaded this language before. */ |
| 2579 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2580 | if (STRICMP(lang, lp->sl_name) == 0) |
| 2581 | break; |
| 2582 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2583 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2584 | /* If not found try loading the language now. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2585 | if (lp == NULL) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2586 | { |
| 2587 | if (filename) |
| 2588 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 2589 | else |
| 2590 | spell_load_lang(lang); |
| 2591 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2592 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2593 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2594 | * Loop over the languages, there can be several files for "lang". |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2595 | */ |
| 2596 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2597 | if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME |
| 2598 | : STRICMP(lang, lp->sl_name) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2599 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2600 | region_mask = REGION_ALL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2601 | if (!filename && region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2602 | { |
| 2603 | /* find region in sl_regions */ |
| 2604 | c = find_region(lp->sl_regions, region); |
| 2605 | if (c == REGION_ALL) |
| 2606 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2607 | if (lp->sl_add) |
| 2608 | { |
| 2609 | if (*lp->sl_regions != NUL) |
| 2610 | /* This addition file is for other regions. */ |
| 2611 | region_mask = 0; |
| 2612 | } |
| 2613 | else |
| 2614 | /* This is probably an error. Give a warning and |
| 2615 | * accept the words anyway. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2616 | smsg((char_u *) |
| 2617 | _("Warning: region %s not supported"), |
| 2618 | region); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2619 | } |
| 2620 | else |
| 2621 | region_mask = 1 << c; |
| 2622 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2623 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2624 | if (region_mask != 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2625 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2626 | if (ga_grow(&ga, 1) == FAIL) |
| 2627 | { |
| 2628 | ga_clear(&ga); |
| 2629 | return e_outofmem; |
| 2630 | } |
| 2631 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2632 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2633 | ++ga.ga_len; |
| 2634 | use_midword(lp, buf); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2635 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2636 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2639 | /* round 0: load int_wordlist, if possible. |
| 2640 | * round 1: load first name in 'spellfile'. |
| 2641 | * round 2: load second name in 'spellfile. |
| 2642 | * etc. */ |
| 2643 | spf = curbuf->b_p_spf; |
| 2644 | for (round = 0; round == 0 || *spf != NUL; ++round) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2645 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2646 | if (round == 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2647 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2648 | /* Internal wordlist, if there is one. */ |
| 2649 | if (int_wordlist == NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2650 | continue; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2651 | int_wordlist_spl(spf_name); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2652 | } |
| 2653 | else |
| 2654 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2655 | /* One entry in 'spellfile'. */ |
| 2656 | copy_option_part(&spf, spf_name, MAXPATHL - 5, ","); |
| 2657 | STRCAT(spf_name, ".spl"); |
| 2658 | |
| 2659 | /* If it was already found above then skip it. */ |
| 2660 | for (c = 0; c < ga.ga_len; ++c) |
| 2661 | if (fullpathcmp(spf_name, |
| 2662 | LANGP_ENTRY(ga, c)->lp_slang->sl_fname, |
| 2663 | FALSE) == FPC_SAME) |
| 2664 | break; |
| 2665 | if (c < ga.ga_len) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2666 | continue; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2669 | /* Check if it was loaded already. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2670 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2671 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2672 | break; |
| 2673 | if (lp == NULL) |
| 2674 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2675 | /* Not loaded, try loading it now. The language name includes the |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2676 | * region name, the region is ignored otherwise. for int_wordlist |
| 2677 | * use an arbitrary name. */ |
| 2678 | if (round == 0) |
| 2679 | STRCPY(lang, "internal wordlist"); |
| 2680 | else |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2681 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2682 | vim_strncpy(lang, gettail(spf_name), MAXWLEN); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2683 | p = vim_strchr(lang, '.'); |
| 2684 | if (p != NULL) |
| 2685 | *p = NUL; /* truncate at ".encoding.add" */ |
| 2686 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2687 | lp = spell_load_file(spf_name, lang, NULL, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2688 | } |
| 2689 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2690 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2691 | region_mask = REGION_ALL; |
| 2692 | if (use_region != NULL && !dont_use_region) |
| 2693 | { |
| 2694 | /* find region in sl_regions */ |
| 2695 | c = find_region(lp->sl_regions, use_region); |
| 2696 | if (c != REGION_ALL) |
| 2697 | region_mask = 1 << c; |
| 2698 | else if (*lp->sl_regions != NUL) |
| 2699 | /* This spell file is for other regions. */ |
| 2700 | region_mask = 0; |
| 2701 | } |
| 2702 | |
| 2703 | if (region_mask != 0) |
| 2704 | { |
| 2705 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2706 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2707 | ++ga.ga_len; |
| 2708 | use_midword(lp, buf); |
| 2709 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2710 | } |
| 2711 | } |
| 2712 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2713 | /* Add a NULL entry to mark the end of the list. */ |
| 2714 | if (ga_grow(&ga, 1) == FAIL) |
| 2715 | { |
| 2716 | ga_clear(&ga); |
| 2717 | return e_outofmem; |
| 2718 | } |
| 2719 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2720 | ++ga.ga_len; |
| 2721 | |
| 2722 | /* Everything is fine, store the new b_langp value. */ |
| 2723 | ga_clear(&buf->b_langp); |
| 2724 | buf->b_langp = ga; |
| 2725 | |
| 2726 | return NULL; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2730 | * Clear the midword characters for buffer "buf". |
| 2731 | */ |
| 2732 | static void |
| 2733 | clear_midword(buf) |
| 2734 | buf_T *buf; |
| 2735 | { |
| 2736 | vim_memset(buf->b_spell_ismw, 0, 256); |
| 2737 | #ifdef FEAT_MBYTE |
| 2738 | vim_free(buf->b_spell_ismw_mb); |
| 2739 | buf->b_spell_ismw_mb = NULL; |
| 2740 | #endif |
| 2741 | } |
| 2742 | |
| 2743 | /* |
| 2744 | * Use the "sl_midword" field of language "lp" for buffer "buf". |
| 2745 | * They add up to any currently used midword characters. |
| 2746 | */ |
| 2747 | static void |
| 2748 | use_midword(lp, buf) |
| 2749 | slang_T *lp; |
| 2750 | buf_T *buf; |
| 2751 | { |
| 2752 | char_u *p; |
| 2753 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 2754 | if (lp->sl_midword == NULL) /* there aren't any */ |
| 2755 | return; |
| 2756 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2757 | for (p = lp->sl_midword; *p != NUL; ) |
| 2758 | #ifdef FEAT_MBYTE |
| 2759 | if (has_mbyte) |
| 2760 | { |
| 2761 | int c, l, n; |
| 2762 | char_u *bp; |
| 2763 | |
| 2764 | c = mb_ptr2char(p); |
| 2765 | l = mb_ptr2len_check(p); |
| 2766 | if (c < 256) |
| 2767 | buf->b_spell_ismw[c] = TRUE; |
| 2768 | else if (buf->b_spell_ismw_mb == NULL) |
| 2769 | /* First multi-byte char in "b_spell_ismw_mb". */ |
| 2770 | buf->b_spell_ismw_mb = vim_strnsave(p, l); |
| 2771 | else |
| 2772 | { |
| 2773 | /* Append multi-byte chars to "b_spell_ismw_mb". */ |
| 2774 | n = STRLEN(buf->b_spell_ismw_mb); |
| 2775 | bp = vim_strnsave(buf->b_spell_ismw_mb, n + l); |
| 2776 | if (bp != NULL) |
| 2777 | { |
| 2778 | vim_free(buf->b_spell_ismw_mb); |
| 2779 | buf->b_spell_ismw_mb = bp; |
| 2780 | vim_strncpy(bp + n, p, l); |
| 2781 | } |
| 2782 | } |
| 2783 | p += l; |
| 2784 | } |
| 2785 | else |
| 2786 | #endif |
| 2787 | buf->b_spell_ismw[*p++] = TRUE; |
| 2788 | } |
| 2789 | |
| 2790 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2791 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2792 | * 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] | 2793 | * 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] | 2794 | */ |
| 2795 | static int |
| 2796 | find_region(rp, region) |
| 2797 | char_u *rp; |
| 2798 | char_u *region; |
| 2799 | { |
| 2800 | int i; |
| 2801 | |
| 2802 | for (i = 0; ; i += 2) |
| 2803 | { |
| 2804 | if (rp[i] == NUL) |
| 2805 | return REGION_ALL; |
| 2806 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2807 | break; |
| 2808 | } |
| 2809 | return i / 2; |
| 2810 | } |
| 2811 | |
| 2812 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2813 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2814 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2815 | * Word WF_ONECAP |
| 2816 | * W WORD WF_ALLCAP |
| 2817 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2818 | */ |
| 2819 | static int |
| 2820 | captype(word, end) |
| 2821 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2822 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2823 | { |
| 2824 | char_u *p; |
| 2825 | int c; |
| 2826 | int firstcap; |
| 2827 | int allcap; |
| 2828 | int past_second = FALSE; /* past second word char */ |
| 2829 | |
| 2830 | /* find first letter */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2831 | for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2832 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2833 | return 0; /* only non-word characters, illegal word */ |
| 2834 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2835 | if (has_mbyte) |
| 2836 | c = mb_ptr2char_adv(&p); |
| 2837 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2838 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2839 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2840 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2841 | |
| 2842 | /* |
| 2843 | * Need to check all letters to find a word with mixed upper/lower. |
| 2844 | * But a word with an upper char only at start is a ONECAP. |
| 2845 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2846 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2847 | if (spell_iswordp_nmw(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2848 | { |
| 2849 | #ifdef FEAT_MBYTE |
| 2850 | c = mb_ptr2char(p); |
| 2851 | #else |
| 2852 | c = *p; |
| 2853 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2854 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2855 | { |
| 2856 | /* UUl -> KEEPCAP */ |
| 2857 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2858 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2859 | allcap = FALSE; |
| 2860 | } |
| 2861 | else if (!allcap) |
| 2862 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2863 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2864 | past_second = TRUE; |
| 2865 | } |
| 2866 | |
| 2867 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2868 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2869 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2870 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2871 | return 0; |
| 2872 | } |
| 2873 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2874 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 2875 | /* |
| 2876 | * Free all languages. |
| 2877 | */ |
| 2878 | void |
| 2879 | spell_free_all() |
| 2880 | { |
| 2881 | slang_T *lp; |
| 2882 | buf_T *buf; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2883 | char_u fname[MAXPATHL]; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2884 | |
| 2885 | /* Go through all buffers and handle 'spelllang'. */ |
| 2886 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2887 | ga_clear(&buf->b_langp); |
| 2888 | |
| 2889 | while (first_lang != NULL) |
| 2890 | { |
| 2891 | lp = first_lang; |
| 2892 | first_lang = lp->sl_next; |
| 2893 | slang_free(lp); |
| 2894 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2895 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2896 | if (int_wordlist != NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2897 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2898 | /* Delete the internal wordlist and its .spl file */ |
| 2899 | mch_remove(int_wordlist); |
| 2900 | int_wordlist_spl(fname); |
| 2901 | mch_remove(fname); |
| 2902 | vim_free(int_wordlist); |
| 2903 | int_wordlist = NULL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2906 | init_spell_chartab(); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2907 | } |
| 2908 | # endif |
| 2909 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2910 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2911 | /* |
| 2912 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2913 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2914 | */ |
| 2915 | void |
| 2916 | spell_reload() |
| 2917 | { |
| 2918 | buf_T *buf; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2919 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2920 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2921 | /* Initialize the table for spell_iswordp(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2922 | init_spell_chartab(); |
| 2923 | |
| 2924 | /* Unload all allocated memory. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2925 | spell_free_all(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2926 | |
| 2927 | /* Go through all buffers and handle 'spelllang'. */ |
| 2928 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2929 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2930 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2931 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2932 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2933 | { |
| 2934 | FOR_ALL_WINDOWS(wp) |
| 2935 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2936 | { |
| 2937 | (void)did_set_spelllang(buf); |
| 2938 | # ifdef FEAT_WINDOWS |
| 2939 | break; |
| 2940 | # endif |
| 2941 | } |
| 2942 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | # endif |
| 2946 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2947 | /* |
| 2948 | * Reload the spell file "fname" if it's loaded. |
| 2949 | */ |
| 2950 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2951 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2952 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2953 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2954 | { |
| 2955 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2956 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2957 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2958 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2959 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 2960 | { |
| 2961 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2962 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2963 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2964 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2965 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2966 | |
| 2967 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 2968 | * 'spelllang' to get it loaded. */ |
| 2969 | if (added_word && !didit) |
| 2970 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2974 | /* |
| 2975 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2976 | */ |
| 2977 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2978 | #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] | 2979 | and .dic file. */ |
| 2980 | /* |
| 2981 | * Main structure to store the contents of a ".aff" file. |
| 2982 | */ |
| 2983 | typedef struct afffile_S |
| 2984 | { |
| 2985 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2986 | int af_rar; /* RAR ID for rare word */ |
| 2987 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2988 | int af_bad; /* BAD ID for banned word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2989 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2990 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2991 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2992 | } afffile_T; |
| 2993 | |
| 2994 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2995 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2996 | struct affentry_S |
| 2997 | { |
| 2998 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2999 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 3000 | 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] | 3001 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 3002 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3003 | int ae_rare; /* rare affix */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3004 | }; |
| 3005 | |
| 3006 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 3007 | typedef struct affheader_S |
| 3008 | { |
| 3009 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3010 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3011 | int ah_combine; /* suffix may combine with prefix */ |
| 3012 | affentry_T *ah_first; /* first affix entry */ |
| 3013 | } affheader_T; |
| 3014 | |
| 3015 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 3016 | |
| 3017 | /* |
| 3018 | * Structure that is used to store the items in the word tree. This avoids |
| 3019 | * the need to keep track of each allocated thing, it's freed all at once |
| 3020 | * after ":mkspell" is done. |
| 3021 | */ |
| 3022 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 3023 | typedef struct sblock_S sblock_T; |
| 3024 | struct sblock_S |
| 3025 | { |
| 3026 | sblock_T *sb_next; /* next block in list */ |
| 3027 | int sb_used; /* nr of bytes already in use */ |
| 3028 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3029 | }; |
| 3030 | |
| 3031 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3032 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3033 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3034 | typedef struct wordnode_S wordnode_T; |
| 3035 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3036 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3037 | union /* shared to save space */ |
| 3038 | { |
| 3039 | char_u hashkey[6]; /* room for the hash key */ |
| 3040 | int index; /* index in written nodes (valid after first |
| 3041 | round) */ |
| 3042 | } wn_u1; |
| 3043 | union /* shared to save space */ |
| 3044 | { |
| 3045 | wordnode_T *next; /* next node with same hash key */ |
| 3046 | wordnode_T *wnode; /* parent node that will write this node */ |
| 3047 | } wn_u2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3048 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 3049 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 3050 | always sorted) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3051 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 3052 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3053 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 3054 | PREFIXTREE it's the prefcondnr */ |
| 3055 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3056 | }; |
| 3057 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3058 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3059 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3060 | /* |
| 3061 | * Info used while reading the spell files. |
| 3062 | */ |
| 3063 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3064 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3065 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3066 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3067 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3068 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3069 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3070 | sblock_T *si_blocks; /* memory blocks used */ |
| 3071 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3072 | int si_add; /* addition file */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3073 | int si_clear_chartab; /* when TRUE clear char tables */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3074 | int si_region; /* region mask */ |
| 3075 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3076 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3077 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3078 | int si_region_count; /* number of regions supported (1 when there |
| 3079 | are no regions) */ |
| 3080 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3081 | |
| 3082 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 3083 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3084 | char_u *si_sofofr; /* SOFOFROM text */ |
| 3085 | char_u *si_sofoto; /* SOFOTO text */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3086 | int si_followup; /* soundsalike: ? */ |
| 3087 | int si_collapse; /* soundsalike: ? */ |
| 3088 | int si_rem_accents; /* soundsalike: remove accents */ |
| 3089 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3090 | char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3091 | garray_T si_prefcond; /* table with conditions for postponed |
| 3092 | * prefixes, each stored as a string */ |
| 3093 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3094 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3095 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3096 | 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] | 3097 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3098 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 3099 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3100 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3101 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 3102 | 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] | 3103 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 3104 | 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] | 3105 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 3106 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 3107 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 3108 | static void free_blocks __ARGS((sblock_T *bl)); |
| 3109 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3110 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 3111 | 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] | 3112 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3113 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 3114 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3115 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3116 | static void clear_node __ARGS((wordnode_T *node)); |
| 3117 | 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] | 3118 | 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] | 3119 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3120 | |
| 3121 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3122 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3123 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3124 | */ |
| 3125 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3126 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3127 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3128 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3129 | { |
| 3130 | FILE *fd; |
| 3131 | afffile_T *aff; |
| 3132 | char_u rline[MAXLINELEN]; |
| 3133 | char_u *line; |
| 3134 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3135 | #define MAXITEMCNT 7 |
| 3136 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3137 | int itemcnt; |
| 3138 | char_u *p; |
| 3139 | int lnum = 0; |
| 3140 | affheader_T *cur_aff = NULL; |
| 3141 | int aff_todo = 0; |
| 3142 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3143 | char_u *low = NULL; |
| 3144 | char_u *fol = NULL; |
| 3145 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3146 | 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] | 3147 | int do_rep; |
| 3148 | int do_sal; |
| 3149 | int do_map; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3150 | int do_midword; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3151 | int do_sofo; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3152 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3153 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3154 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3155 | /* |
| 3156 | * Open the file. |
| 3157 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3158 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3159 | if (fd == NULL) |
| 3160 | { |
| 3161 | EMSG2(_(e_notopen), fname); |
| 3162 | return NULL; |
| 3163 | } |
| 3164 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3165 | if (spin->si_verbose || p_verbose > 2) |
| 3166 | { |
| 3167 | if (!spin->si_verbose) |
| 3168 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3169 | smsg((char_u *)_("Reading affix file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3170 | out_flush(); |
| 3171 | if (!spin->si_verbose) |
| 3172 | verbose_leave(); |
| 3173 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3174 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3175 | /* Only do REP lines when not done in another .aff file already. */ |
| 3176 | do_rep = spin->si_rep.ga_len == 0; |
| 3177 | |
| 3178 | /* Only do SAL lines when not done in another .aff file already. */ |
| 3179 | do_sal = spin->si_sal.ga_len == 0; |
| 3180 | |
| 3181 | /* Only do MAP lines when not done in another .aff file already. */ |
| 3182 | do_map = spin->si_map.ga_len == 0; |
| 3183 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3184 | /* Only do MIDWORD line when not done in another .aff file already */ |
| 3185 | do_midword = spin->si_midword == NULL; |
| 3186 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3187 | /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */ |
| 3188 | do_sofo = spin->si_sofofr == NULL; |
| 3189 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3190 | /* |
| 3191 | * Allocate and init the afffile_T structure. |
| 3192 | */ |
| 3193 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3194 | if (aff == NULL) |
| 3195 | return NULL; |
| 3196 | hash_init(&aff->af_pref); |
| 3197 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3198 | |
| 3199 | /* |
| 3200 | * Read all the lines in the file one by one. |
| 3201 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3202 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3203 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3204 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3205 | ++lnum; |
| 3206 | |
| 3207 | /* Skip comment lines. */ |
| 3208 | if (*rline == '#') |
| 3209 | continue; |
| 3210 | |
| 3211 | /* Convert from "SET" to 'encoding' when needed. */ |
| 3212 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3213 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3214 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3215 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3216 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3217 | if (pc == NULL) |
| 3218 | { |
| 3219 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3220 | fname, lnum, rline); |
| 3221 | continue; |
| 3222 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3223 | line = pc; |
| 3224 | } |
| 3225 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3226 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3227 | { |
| 3228 | pc = NULL; |
| 3229 | line = rline; |
| 3230 | } |
| 3231 | |
| 3232 | /* Split the line up in white separated items. Put a NUL after each |
| 3233 | * item. */ |
| 3234 | itemcnt = 0; |
| 3235 | for (p = line; ; ) |
| 3236 | { |
| 3237 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 3238 | ++p; |
| 3239 | if (*p == NUL) |
| 3240 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3241 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3242 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3243 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3244 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3245 | ++p; |
| 3246 | if (*p == NUL) |
| 3247 | break; |
| 3248 | *p++ = NUL; |
| 3249 | } |
| 3250 | |
| 3251 | /* Handle non-empty lines. */ |
| 3252 | if (itemcnt > 0) |
| 3253 | { |
| 3254 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 3255 | && aff->af_enc == NULL) |
| 3256 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3257 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3258 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 3259 | aff->af_enc = enc_canonize(items[1]); |
| 3260 | if (aff->af_enc != NULL && !spin->si_ascii |
| 3261 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 3262 | p_enc) == FAIL) |
| 3263 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 3264 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3265 | spin->si_conv.vc_fail = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3266 | #else |
| 3267 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3268 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3269 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3270 | else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2) |
| 3271 | { |
| 3272 | if (do_midword) |
| 3273 | spin->si_midword = vim_strsave(items[1]); |
| 3274 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3275 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 3276 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3277 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 3278 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3279 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3280 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3281 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3282 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3283 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 3284 | && aff->af_rar == 0) |
| 3285 | { |
| 3286 | aff->af_rar = items[1][0]; |
| 3287 | if (items[1][1] != NUL) |
| 3288 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3289 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3290 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 3291 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3292 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3293 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3294 | if (items[1][1] != NUL) |
| 3295 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3296 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3297 | else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2 |
| 3298 | && aff->af_bad == 0) |
| 3299 | { |
| 3300 | aff->af_bad = items[1][0]; |
| 3301 | if (items[1][1] != NUL) |
| 3302 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 3303 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3304 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 3305 | { |
| 3306 | aff->af_pfxpostpone = TRUE; |
| 3307 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3308 | else if ((STRCMP(items[0], "PFX") == 0 |
| 3309 | || STRCMP(items[0], "SFX") == 0) |
| 3310 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3311 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3312 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3313 | /* Myspell allows extra text after the item, but that might |
| 3314 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 3315 | if (itemcnt > 4 && *items[4] != '#') |
| 3316 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 3317 | fname, lnum, items[4]); |
| 3318 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3319 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3320 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 3321 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3322 | if (cur_aff == NULL) |
| 3323 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3324 | cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3325 | cur_aff->ah_key[1] = NUL; |
| 3326 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3327 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3328 | if (*items[2] == 'Y') |
| 3329 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3330 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3331 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 3332 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3333 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3334 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3335 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3336 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3337 | /* Use a new number in the .spl file later, to be able to |
| 3338 | * handle multiple .aff files. */ |
| 3339 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3340 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3341 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3342 | else |
| 3343 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3344 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3345 | hi = hash_find(tp, cur_aff->ah_key); |
| 3346 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3347 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3348 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 3349 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3350 | aff_todo = 0; |
| 3351 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3352 | else |
| 3353 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3354 | } |
| 3355 | else if ((STRCMP(items[0], "PFX") == 0 |
| 3356 | || STRCMP(items[0], "SFX") == 0) |
| 3357 | && aff_todo > 0 |
| 3358 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3359 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3360 | { |
| 3361 | affentry_T *aff_entry; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3362 | int rare = FALSE; |
| 3363 | int lasti = 5; |
| 3364 | |
| 3365 | /* Check for "rare" after the other info. */ |
| 3366 | if (itemcnt > 5 && STRICMP(items[5], "rare") == 0) |
| 3367 | { |
| 3368 | rare = TRUE; |
| 3369 | lasti = 6; |
| 3370 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3371 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3372 | /* Myspell allows extra text after the item, but that might |
| 3373 | * mean mistakes go unnoticed. Require a comment-starter. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3374 | if (itemcnt > lasti && *items[lasti] != '#') |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3375 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3376 | fname, lnum, items[lasti]); |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3377 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3378 | /* New item for an affix letter. */ |
| 3379 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3380 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 3381 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3382 | if (aff_entry == NULL) |
| 3383 | break; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3384 | aff_entry->ae_rare = rare; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3385 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3386 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3387 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 3388 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3389 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3390 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 3391 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3392 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3393 | /* Don't use an affix entry with non-ASCII characters when |
| 3394 | * "spin->si_ascii" is TRUE. */ |
| 3395 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3396 | || has_non_ascii(aff_entry->ae_add))) |
| 3397 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3398 | aff_entry->ae_next = cur_aff->ah_first; |
| 3399 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3400 | |
| 3401 | if (STRCMP(items[4], ".") != 0) |
| 3402 | { |
| 3403 | char_u buf[MAXLINELEN]; |
| 3404 | |
| 3405 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 3406 | items[4]); |
| 3407 | if (*items[0] == 'P') |
| 3408 | sprintf((char *)buf, "^%s", items[4]); |
| 3409 | else |
| 3410 | sprintf((char *)buf, "%s$", items[4]); |
| 3411 | aff_entry->ae_prog = vim_regcomp(buf, |
| 3412 | RE_MAGIC + RE_STRING); |
| 3413 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3414 | |
| 3415 | /* For postponed prefixes we need an entry in si_prefcond |
| 3416 | * for the condition. Use an existing one if possible. */ |
| 3417 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 3418 | && aff_entry->ae_chop == NULL) |
| 3419 | { |
| 3420 | int idx; |
| 3421 | char_u **pp; |
| 3422 | |
| 3423 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 3424 | --idx) |
| 3425 | { |
| 3426 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 3427 | if (str_equal(p, aff_entry->ae_cond)) |
| 3428 | break; |
| 3429 | } |
| 3430 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 3431 | { |
| 3432 | /* Not found, add a new condition. */ |
| 3433 | idx = spin->si_prefcond.ga_len++; |
| 3434 | pp = ((char_u **)spin->si_prefcond.ga_data) + idx; |
| 3435 | if (aff_entry->ae_cond == NULL) |
| 3436 | *pp = NULL; |
| 3437 | else |
| 3438 | *pp = getroom_save(&spin->si_blocks, |
| 3439 | aff_entry->ae_cond); |
| 3440 | } |
| 3441 | |
| 3442 | /* Add the prefix to the prefix tree. */ |
| 3443 | if (aff_entry->ae_add == NULL) |
| 3444 | p = (char_u *)""; |
| 3445 | else |
| 3446 | p = aff_entry->ae_add; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3447 | tree_add_word(p, spin->si_prefroot, rare ? -2 : -1, |
| 3448 | idx, cur_aff->ah_newID, &spin->si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3449 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3450 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3451 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3452 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 3453 | { |
| 3454 | if (fol != NULL) |
| 3455 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 3456 | fname, lnum); |
| 3457 | else |
| 3458 | fol = vim_strsave(items[1]); |
| 3459 | } |
| 3460 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 3461 | { |
| 3462 | if (low != NULL) |
| 3463 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 3464 | fname, lnum); |
| 3465 | else |
| 3466 | low = vim_strsave(items[1]); |
| 3467 | } |
| 3468 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 3469 | { |
| 3470 | if (upp != NULL) |
| 3471 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 3472 | fname, lnum); |
| 3473 | else |
| 3474 | upp = vim_strsave(items[1]); |
| 3475 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3476 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3477 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3478 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3479 | if (!isdigit(*items[1])) |
| 3480 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 3481 | fname, lnum); |
| 3482 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3483 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 3484 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3485 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3486 | if (do_rep) |
| 3487 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 3488 | } |
| 3489 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 3490 | { |
| 3491 | /* MAP item or count */ |
| 3492 | if (!found_map) |
| 3493 | { |
| 3494 | /* First line contains the count. */ |
| 3495 | found_map = TRUE; |
| 3496 | if (!isdigit(*items[1])) |
| 3497 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 3498 | fname, lnum); |
| 3499 | } |
| 3500 | else if (do_map) |
| 3501 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3502 | int c; |
| 3503 | |
| 3504 | /* Check that every character appears only once. */ |
| 3505 | for (p = items[1]; *p != NUL; ) |
| 3506 | { |
| 3507 | #ifdef FEAT_MBYTE |
| 3508 | c = mb_ptr2char_adv(&p); |
| 3509 | #else |
| 3510 | c = *p++; |
| 3511 | #endif |
| 3512 | if ((spin->si_map.ga_len > 0 |
| 3513 | && vim_strchr(spin->si_map.ga_data, c) |
| 3514 | != NULL) |
| 3515 | || vim_strchr(p, c) != NULL) |
| 3516 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 3517 | fname, lnum); |
| 3518 | } |
| 3519 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3520 | /* We simply concatenate all the MAP strings, separated by |
| 3521 | * slashes. */ |
| 3522 | ga_concat(&spin->si_map, items[1]); |
| 3523 | ga_append(&spin->si_map, '/'); |
| 3524 | } |
| 3525 | } |
| 3526 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 3527 | { |
| 3528 | if (do_sal) |
| 3529 | { |
| 3530 | /* SAL item (sounds-a-like) |
| 3531 | * Either one of the known keys or a from-to pair. */ |
| 3532 | if (STRCMP(items[1], "followup") == 0) |
| 3533 | spin->si_followup = sal_to_bool(items[2]); |
| 3534 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 3535 | spin->si_collapse = sal_to_bool(items[2]); |
| 3536 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 3537 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 3538 | else |
| 3539 | /* when "to" is "_" it means empty */ |
| 3540 | add_fromto(spin, &spin->si_sal, items[1], |
| 3541 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 3542 | : items[2]); |
| 3543 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3544 | } |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3545 | else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2 |
| 3546 | && (!do_sofo || spin->si_sofofr == NULL)) |
| 3547 | { |
| 3548 | if (do_sofo) |
| 3549 | spin->si_sofofr = vim_strsave(items[1]); |
| 3550 | } |
| 3551 | else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2 |
| 3552 | && (!do_sofo || spin->si_sofoto == NULL)) |
| 3553 | { |
| 3554 | if (do_sofo) |
| 3555 | spin->si_sofoto = vim_strsave(items[1]); |
| 3556 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3557 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3558 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 3559 | fname, lnum, items[0]); |
| 3560 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3563 | if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL)) |
| 3564 | smsg((char_u *)_("Missing SOFO%s line in %s"), |
| 3565 | spin->si_sofofr == NULL ? "FROM" : "TO", fname); |
| 3566 | if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0) |
| 3567 | smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname); |
| 3568 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3569 | if (fol != NULL || low != NULL || upp != NULL) |
| 3570 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3571 | if (spin->si_clear_chartab) |
| 3572 | { |
| 3573 | /* Clear the char type tables, don't want to use any of the |
| 3574 | * currently used spell properties. */ |
| 3575 | init_spell_chartab(); |
| 3576 | spin->si_clear_chartab = FALSE; |
| 3577 | } |
| 3578 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3579 | /* |
| 3580 | * Don't write a word table for an ASCII file, so that we don't check |
| 3581 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3582 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3583 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 3584 | */ |
| 3585 | if (!spin->si_ascii |
| 3586 | #ifdef FEAT_MBYTE |
| 3587 | && !enc_utf8 |
| 3588 | #endif |
| 3589 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3590 | { |
| 3591 | if (fol == NULL || low == NULL || upp == NULL) |
| 3592 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 3593 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3594 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3595 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3596 | |
| 3597 | vim_free(fol); |
| 3598 | vim_free(low); |
| 3599 | vim_free(upp); |
| 3600 | } |
| 3601 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3602 | vim_free(pc); |
| 3603 | fclose(fd); |
| 3604 | return aff; |
| 3605 | } |
| 3606 | |
| 3607 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3608 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 3609 | * NULL as equal. |
| 3610 | */ |
| 3611 | static int |
| 3612 | str_equal(s1, s2) |
| 3613 | char_u *s1; |
| 3614 | char_u *s2; |
| 3615 | { |
| 3616 | if (s1 == NULL || s2 == NULL) |
| 3617 | return s1 == s2; |
| 3618 | return STRCMP(s1, s2) == 0; |
| 3619 | } |
| 3620 | |
| 3621 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3622 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 3623 | * They are stored case-folded. |
| 3624 | */ |
| 3625 | static void |
| 3626 | add_fromto(spin, gap, from, to) |
| 3627 | spellinfo_T *spin; |
| 3628 | garray_T *gap; |
| 3629 | char_u *from; |
| 3630 | char_u *to; |
| 3631 | { |
| 3632 | fromto_T *ftp; |
| 3633 | char_u word[MAXWLEN]; |
| 3634 | |
| 3635 | if (ga_grow(gap, 1) == OK) |
| 3636 | { |
| 3637 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 3638 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 3639 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 3640 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 3641 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 3642 | ++gap->ga_len; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | /* |
| 3647 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 3648 | */ |
| 3649 | static int |
| 3650 | sal_to_bool(s) |
| 3651 | char_u *s; |
| 3652 | { |
| 3653 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 3654 | } |
| 3655 | |
| 3656 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3657 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 3658 | * When "s" is NULL FALSE is returned. |
| 3659 | */ |
| 3660 | static int |
| 3661 | has_non_ascii(s) |
| 3662 | char_u *s; |
| 3663 | { |
| 3664 | char_u *p; |
| 3665 | |
| 3666 | if (s != NULL) |
| 3667 | for (p = s; *p != NUL; ++p) |
| 3668 | if (*p >= 128) |
| 3669 | return TRUE; |
| 3670 | return FALSE; |
| 3671 | } |
| 3672 | |
| 3673 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3674 | * Free the structure filled by spell_read_aff(). |
| 3675 | */ |
| 3676 | static void |
| 3677 | spell_free_aff(aff) |
| 3678 | afffile_T *aff; |
| 3679 | { |
| 3680 | hashtab_T *ht; |
| 3681 | hashitem_T *hi; |
| 3682 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3683 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3684 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3685 | |
| 3686 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3687 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3688 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3689 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 3690 | { |
| 3691 | todo = ht->ht_used; |
| 3692 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3693 | { |
| 3694 | if (!HASHITEM_EMPTY(hi)) |
| 3695 | { |
| 3696 | --todo; |
| 3697 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3698 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3699 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | if (ht == &aff->af_suff) |
| 3703 | break; |
| 3704 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3705 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3706 | hash_clear(&aff->af_pref); |
| 3707 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3711 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3712 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3713 | */ |
| 3714 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3715 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3716 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3717 | spellinfo_T *spin; |
| 3718 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3719 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3720 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3721 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3722 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3723 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3724 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3725 | char_u *pc; |
| 3726 | char_u *w; |
| 3727 | int l; |
| 3728 | hash_T hash; |
| 3729 | hashitem_T *hi; |
| 3730 | FILE *fd; |
| 3731 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3732 | int non_ascii = 0; |
| 3733 | int retval = OK; |
| 3734 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3735 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3736 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3737 | /* |
| 3738 | * Open the file. |
| 3739 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3740 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3741 | if (fd == NULL) |
| 3742 | { |
| 3743 | EMSG2(_(e_notopen), fname); |
| 3744 | return FAIL; |
| 3745 | } |
| 3746 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3747 | /* The hashtable is only used to detect duplicated words. */ |
| 3748 | hash_init(&ht); |
| 3749 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3750 | spin->si_foldwcount = 0; |
| 3751 | spin->si_keepwcount = 0; |
| 3752 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3753 | if (spin->si_verbose || p_verbose > 2) |
| 3754 | { |
| 3755 | if (!spin->si_verbose) |
| 3756 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3757 | smsg((char_u *)_("Reading dictionary file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3758 | out_flush(); |
| 3759 | if (!spin->si_verbose) |
| 3760 | verbose_leave(); |
| 3761 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3762 | |
| 3763 | /* Read and ignore the first line: word count. */ |
| 3764 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3765 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3766 | EMSG2(_("E760: No word count in %s"), fname); |
| 3767 | |
| 3768 | /* |
| 3769 | * Read all the lines in the file one by one. |
| 3770 | * The words are converted to 'encoding' here, before being added to |
| 3771 | * the hashtable. |
| 3772 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3773 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3774 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3775 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3776 | ++lnum; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3777 | if (line[0] == '#') |
| 3778 | continue; /* comment line */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3779 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3780 | /* Remove CR, LF and white space from the end. White space halfway |
| 3781 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3782 | l = STRLEN(line); |
| 3783 | while (l > 0 && line[l - 1] <= ' ') |
| 3784 | --l; |
| 3785 | if (l == 0) |
| 3786 | continue; /* empty line */ |
| 3787 | line[l] = NUL; |
| 3788 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3789 | /* Find the optional affix names. */ |
| 3790 | afflist = vim_strchr(line, '/'); |
| 3791 | if (afflist != NULL) |
| 3792 | *afflist++ = NUL; |
| 3793 | |
| 3794 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3795 | if (spin->si_ascii && has_non_ascii(line)) |
| 3796 | { |
| 3797 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3798 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3799 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3800 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3801 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3802 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3803 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3804 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3805 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3806 | if (pc == NULL) |
| 3807 | { |
| 3808 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3809 | fname, lnum, line); |
| 3810 | continue; |
| 3811 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3812 | w = pc; |
| 3813 | } |
| 3814 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3815 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3816 | { |
| 3817 | pc = NULL; |
| 3818 | w = line; |
| 3819 | } |
| 3820 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3821 | /* This takes time, print a message now and then. */ |
| 3822 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3823 | { |
| 3824 | vim_snprintf((char *)message, sizeof(message), |
| 3825 | _("line %6d, word %6d - %s"), |
| 3826 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3827 | msg_start(); |
| 3828 | msg_puts_long_attr(message, 0); |
| 3829 | msg_clr_eos(); |
| 3830 | msg_didout = FALSE; |
| 3831 | msg_col = 0; |
| 3832 | out_flush(); |
| 3833 | } |
| 3834 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3835 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 3836 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3837 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3838 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3839 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3840 | if (retval == FAIL) |
| 3841 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3842 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3843 | hash = hash_hash(dw); |
| 3844 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3845 | if (!HASHITEM_EMPTY(hi)) |
| 3846 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3847 | fname, lnum, dw); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3848 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3849 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3850 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3851 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3852 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3853 | if (afflist != NULL) |
| 3854 | { |
| 3855 | /* Check for affix name that stands for keep-case word and stands |
| 3856 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3857 | if (affile->af_kep != NUL |
| 3858 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 3859 | flags |= WF_KEEPCAP | WF_FIXCAP; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3860 | if (affile->af_rar != NUL |
| 3861 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 3862 | flags |= WF_RARE; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3863 | if (affile->af_bad != NUL |
| 3864 | && vim_strchr(afflist, affile->af_bad) != NULL) |
| 3865 | flags |= WF_BANNED; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3866 | |
| 3867 | if (affile->af_pfxpostpone) |
| 3868 | /* Need to store the list of prefix IDs with the word. */ |
| 3869 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3872 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3873 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3874 | retval = FAIL; |
| 3875 | |
| 3876 | if (afflist != NULL) |
| 3877 | { |
| 3878 | /* Find all matching suffixes and add the resulting words. |
| 3879 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3880 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3881 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3882 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3883 | retval = FAIL; |
| 3884 | |
| 3885 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3886 | if (store_aff_word(dw, spin, afflist, affile, |
| 3887 | &affile->af_pref, NULL, |
| 3888 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3889 | retval = FAIL; |
| 3890 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3893 | if (spin->si_ascii && non_ascii > 0) |
| 3894 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3895 | non_ascii); |
| 3896 | hash_clear(&ht); |
| 3897 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3898 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3899 | return retval; |
| 3900 | } |
| 3901 | |
| 3902 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3903 | * Get the list of prefix IDs from the affix list "afflist". |
| 3904 | * Used for PFXPOSTPONE. |
| 3905 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 3906 | * or when out of memory. |
| 3907 | */ |
| 3908 | static char_u * |
| 3909 | get_pfxlist(affile, afflist, blp) |
| 3910 | afffile_T *affile; |
| 3911 | char_u *afflist; |
| 3912 | sblock_T **blp; |
| 3913 | { |
| 3914 | char_u *p; |
| 3915 | int cnt; |
| 3916 | int round; |
| 3917 | char_u *res = NULL; |
| 3918 | char_u key[2]; |
| 3919 | hashitem_T *hi; |
| 3920 | |
| 3921 | key[1] = NUL; |
| 3922 | |
| 3923 | /* round 1: count the number of prefix IDs. |
| 3924 | * round 2: move prefix IDs to "res" */ |
| 3925 | for (round = 1; round <= 2; ++round) |
| 3926 | { |
| 3927 | cnt = 0; |
| 3928 | for (p = afflist; *p != NUL; ++p) |
| 3929 | { |
| 3930 | key[0] = *p; |
| 3931 | hi = hash_find(&affile->af_pref, key); |
| 3932 | if (!HASHITEM_EMPTY(hi)) |
| 3933 | { |
| 3934 | /* This is a prefix ID, use the new number. */ |
| 3935 | if (round == 2) |
| 3936 | res[cnt] = HI2AH(hi)->ah_newID; |
| 3937 | ++cnt; |
| 3938 | } |
| 3939 | } |
| 3940 | if (round == 1 && cnt > 0) |
| 3941 | res = getroom(blp, cnt + 1); |
| 3942 | if (res == NULL) |
| 3943 | break; |
| 3944 | } |
| 3945 | |
| 3946 | if (res != NULL) |
| 3947 | res[cnt] = NUL; |
| 3948 | return res; |
| 3949 | } |
| 3950 | |
| 3951 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3952 | * Apply affixes to a word and store the resulting words. |
| 3953 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 3954 | * prefixes or suffixes. |
| 3955 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 3956 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3957 | * |
| 3958 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3959 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3960 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3961 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3962 | char_u *word; /* basic word start */ |
| 3963 | spellinfo_T *spin; /* spell info */ |
| 3964 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3965 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3966 | hashtab_T *ht; |
| 3967 | hashtab_T *xht; |
| 3968 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3969 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3970 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3971 | { |
| 3972 | int todo; |
| 3973 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3974 | affheader_T *ah; |
| 3975 | affentry_T *ae; |
| 3976 | regmatch_T regmatch; |
| 3977 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3978 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3979 | int i; |
| 3980 | char_u *p; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3981 | int use_flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3982 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3983 | todo = ht->ht_used; |
| 3984 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3985 | { |
| 3986 | if (!HASHITEM_EMPTY(hi)) |
| 3987 | { |
| 3988 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3989 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3990 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3991 | /* Check that the affix combines, if required, and that the word |
| 3992 | * supports this affix. */ |
| 3993 | if ((!comb || ah->ah_combine) |
| 3994 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3995 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3996 | /* Loop over all affix entries with this name. */ |
| 3997 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3998 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3999 | /* Check the condition. It's not logical to match case |
| 4000 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4001 | * Myspell. |
| 4002 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 4003 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4004 | regmatch.regprog = ae->ae_prog; |
| 4005 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4006 | if ((xht != NULL || !affile->af_pfxpostpone |
| 4007 | || ae->ae_chop != NULL) |
| 4008 | && (ae->ae_prog == NULL |
| 4009 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4010 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4011 | /* Match. Remove the chop and add the affix. */ |
| 4012 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4013 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4014 | /* prefix: chop/add at the start of the word */ |
| 4015 | if (ae->ae_add == NULL) |
| 4016 | *newword = NUL; |
| 4017 | else |
| 4018 | STRCPY(newword, ae->ae_add); |
| 4019 | p = word; |
| 4020 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4021 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4022 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4023 | #ifdef FEAT_MBYTE |
| 4024 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4025 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4026 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4027 | for ( ; i > 0; --i) |
| 4028 | mb_ptr_adv(p); |
| 4029 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4030 | else |
| 4031 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4032 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4033 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4034 | STRCAT(newword, p); |
| 4035 | } |
| 4036 | else |
| 4037 | { |
| 4038 | /* suffix: chop/add at the end of the word */ |
| 4039 | STRCPY(newword, word); |
| 4040 | if (ae->ae_chop != NULL) |
| 4041 | { |
| 4042 | /* Remove chop string. */ |
| 4043 | p = newword + STRLEN(newword); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4044 | i = MB_CHARLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4045 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4046 | mb_ptr_back(newword, p); |
| 4047 | *p = NUL; |
| 4048 | } |
| 4049 | if (ae->ae_add != NULL) |
| 4050 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4053 | /* Obey the "rare" flag of the affix. */ |
| 4054 | if (ae->ae_rare) |
| 4055 | use_flags = flags | WF_RARE; |
| 4056 | else |
| 4057 | use_flags = flags; |
| 4058 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4059 | /* Store the modified word. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4060 | if (store_word(newword, spin, use_flags, |
| 4061 | spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4062 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4063 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4064 | /* When added a suffix and combining is allowed also |
| 4065 | * try adding prefixes additionally. */ |
| 4066 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4067 | if (store_aff_word(newword, spin, afflist, affile, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4068 | xht, NULL, TRUE, use_flags, pfxlist) |
| 4069 | == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4070 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4077 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4081 | * Read a file with a list of words. |
| 4082 | */ |
| 4083 | static int |
| 4084 | spell_read_wordfile(fname, spin) |
| 4085 | char_u *fname; |
| 4086 | spellinfo_T *spin; |
| 4087 | { |
| 4088 | FILE *fd; |
| 4089 | long lnum = 0; |
| 4090 | char_u rline[MAXLINELEN]; |
| 4091 | char_u *line; |
| 4092 | char_u *pc = NULL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4093 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4094 | int l; |
| 4095 | int retval = OK; |
| 4096 | int did_word = FALSE; |
| 4097 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4098 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4099 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4100 | |
| 4101 | /* |
| 4102 | * Open the file. |
| 4103 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4104 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4105 | if (fd == NULL) |
| 4106 | { |
| 4107 | EMSG2(_(e_notopen), fname); |
| 4108 | return FAIL; |
| 4109 | } |
| 4110 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4111 | if (spin->si_verbose || p_verbose > 2) |
| 4112 | { |
| 4113 | if (!spin->si_verbose) |
| 4114 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4115 | smsg((char_u *)_("Reading word file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4116 | out_flush(); |
| 4117 | if (!spin->si_verbose) |
| 4118 | verbose_leave(); |
| 4119 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4120 | |
| 4121 | /* |
| 4122 | * Read all the lines in the file one by one. |
| 4123 | */ |
| 4124 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 4125 | { |
| 4126 | line_breakcheck(); |
| 4127 | ++lnum; |
| 4128 | |
| 4129 | /* Skip comment lines. */ |
| 4130 | if (*rline == '#') |
| 4131 | continue; |
| 4132 | |
| 4133 | /* Remove CR, LF and white space from the end. */ |
| 4134 | l = STRLEN(rline); |
| 4135 | while (l > 0 && rline[l - 1] <= ' ') |
| 4136 | --l; |
| 4137 | if (l == 0) |
| 4138 | continue; /* empty or blank line */ |
| 4139 | rline[l] = NUL; |
| 4140 | |
| 4141 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 4142 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4143 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4144 | if (spin->si_conv.vc_type != CONV_NONE) |
| 4145 | { |
| 4146 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 4147 | if (pc == NULL) |
| 4148 | { |
| 4149 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 4150 | fname, lnum, rline); |
| 4151 | continue; |
| 4152 | } |
| 4153 | line = pc; |
| 4154 | } |
| 4155 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4156 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4157 | { |
| 4158 | pc = NULL; |
| 4159 | line = rline; |
| 4160 | } |
| 4161 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4162 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4163 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4164 | ++line; |
| 4165 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4166 | { |
| 4167 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4168 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 4169 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4170 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4171 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 4172 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4173 | else |
| 4174 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4175 | #ifdef FEAT_MBYTE |
| 4176 | char_u *enc; |
| 4177 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4178 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4179 | line += 10; |
| 4180 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4181 | if (enc != NULL && !spin->si_ascii |
| 4182 | && convert_setup(&spin->si_conv, enc, |
| 4183 | p_enc) == FAIL) |
| 4184 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4185 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4186 | vim_free(enc); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4187 | spin->si_conv.vc_fail = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4188 | #else |
| 4189 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 4190 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4191 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4192 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4193 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4194 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4195 | if (STRNCMP(line, "regions=", 8) == 0) |
| 4196 | { |
| 4197 | if (spin->si_region_count > 1) |
| 4198 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 4199 | fname, lnum, line); |
| 4200 | else |
| 4201 | { |
| 4202 | line += 8; |
| 4203 | if (STRLEN(line) > 16) |
| 4204 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 4205 | fname, lnum, line); |
| 4206 | else |
| 4207 | { |
| 4208 | spin->si_region_count = STRLEN(line) / 2; |
| 4209 | STRCPY(spin->si_region_name, line); |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 4210 | |
| 4211 | /* Adjust the mask for a word valid in all regions. */ |
| 4212 | spin->si_region = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | continue; |
| 4216 | } |
| 4217 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4218 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
| 4219 | fname, lnum, line - 1); |
| 4220 | continue; |
| 4221 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4222 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4223 | flags = 0; |
| 4224 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4225 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4226 | /* Check for flags and region after a slash. */ |
| 4227 | p = vim_strchr(line, '/'); |
| 4228 | if (p != NULL) |
| 4229 | { |
| 4230 | *p++ = NUL; |
| 4231 | while (*p != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4232 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4233 | if (*p == '=') /* keep-case word */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 4234 | flags |= WF_KEEPCAP | WF_FIXCAP; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4235 | else if (*p == '!') /* Bad, bad, wicked word. */ |
| 4236 | flags |= WF_BANNED; |
| 4237 | else if (*p == '?') /* Rare word. */ |
| 4238 | flags |= WF_RARE; |
| 4239 | else if (VIM_ISDIGIT(*p)) /* region number(s) */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4240 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4241 | if ((flags & WF_REGION) == 0) /* first one */ |
| 4242 | regionmask = 0; |
| 4243 | flags |= WF_REGION; |
| 4244 | |
| 4245 | l = *p - '0'; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4246 | if (l > spin->si_region_count) |
| 4247 | { |
| 4248 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4249 | fname, lnum, p); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4250 | break; |
| 4251 | } |
| 4252 | regionmask |= 1 << (l - 1); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4253 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4254 | else |
| 4255 | { |
| 4256 | smsg((char_u *)_("Unrecognized flags in %s line %d: %s"), |
| 4257 | fname, lnum, p); |
| 4258 | break; |
| 4259 | } |
| 4260 | ++p; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4261 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 4265 | if (spin->si_ascii && has_non_ascii(line)) |
| 4266 | { |
| 4267 | ++non_ascii; |
| 4268 | continue; |
| 4269 | } |
| 4270 | |
| 4271 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4272 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4273 | { |
| 4274 | retval = FAIL; |
| 4275 | break; |
| 4276 | } |
| 4277 | did_word = TRUE; |
| 4278 | } |
| 4279 | |
| 4280 | vim_free(pc); |
| 4281 | fclose(fd); |
| 4282 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4283 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 4284 | { |
| 4285 | if (p_verbose > 2) |
| 4286 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4287 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 4288 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4289 | if (p_verbose > 2) |
| 4290 | verbose_leave(); |
| 4291 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4292 | return retval; |
| 4293 | } |
| 4294 | |
| 4295 | /* |
| 4296 | * Get part of an sblock_T, "len" bytes long. |
| 4297 | * This avoids calling free() for every little struct we use. |
| 4298 | * The memory is cleared to all zeros. |
| 4299 | * Returns NULL when out of memory. |
| 4300 | */ |
| 4301 | static void * |
| 4302 | getroom(blp, len) |
| 4303 | sblock_T **blp; |
| 4304 | size_t len; /* length needed */ |
| 4305 | { |
| 4306 | char_u *p; |
| 4307 | sblock_T *bl = *blp; |
| 4308 | |
| 4309 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 4310 | { |
| 4311 | /* Allocate a block of memory. This is not freed until much later. */ |
| 4312 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 4313 | if (bl == NULL) |
| 4314 | return NULL; |
| 4315 | bl->sb_next = *blp; |
| 4316 | *blp = bl; |
| 4317 | bl->sb_used = 0; |
| 4318 | } |
| 4319 | |
| 4320 | p = bl->sb_data + bl->sb_used; |
| 4321 | bl->sb_used += len; |
| 4322 | |
| 4323 | return p; |
| 4324 | } |
| 4325 | |
| 4326 | /* |
| 4327 | * Make a copy of a string into memory allocated with getroom(). |
| 4328 | */ |
| 4329 | static char_u * |
| 4330 | getroom_save(blp, s) |
| 4331 | sblock_T **blp; |
| 4332 | char_u *s; |
| 4333 | { |
| 4334 | char_u *sc; |
| 4335 | |
| 4336 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 4337 | if (sc != NULL) |
| 4338 | STRCPY(sc, s); |
| 4339 | return sc; |
| 4340 | } |
| 4341 | |
| 4342 | |
| 4343 | /* |
| 4344 | * Free the list of allocated sblock_T. |
| 4345 | */ |
| 4346 | static void |
| 4347 | free_blocks(bl) |
| 4348 | sblock_T *bl; |
| 4349 | { |
| 4350 | sblock_T *next; |
| 4351 | |
| 4352 | while (bl != NULL) |
| 4353 | { |
| 4354 | next = bl->sb_next; |
| 4355 | vim_free(bl); |
| 4356 | bl = next; |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | /* |
| 4361 | * Allocate the root of a word tree. |
| 4362 | */ |
| 4363 | static wordnode_T * |
| 4364 | wordtree_alloc(blp) |
| 4365 | sblock_T **blp; |
| 4366 | { |
| 4367 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 4368 | } |
| 4369 | |
| 4370 | /* |
| 4371 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4372 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 4373 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4374 | * For a keep-case word also store it in the keep-case tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4375 | * When "pfxlist" is not NULL store the word for each prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4376 | */ |
| 4377 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4378 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4379 | char_u *word; |
| 4380 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4381 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4382 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4383 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4384 | { |
| 4385 | int len = STRLEN(word); |
| 4386 | int ct = captype(word, word + len); |
| 4387 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4388 | int res = OK; |
| 4389 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4390 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4391 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4392 | for (p = pfxlist; res == OK; ++p) |
| 4393 | { |
| 4394 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 4395 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4396 | if (p == NULL || *p == NUL) |
| 4397 | break; |
| 4398 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4399 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4400 | |
| 4401 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4402 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4403 | for (p = pfxlist; res == OK; ++p) |
| 4404 | { |
| 4405 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 4406 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4407 | if (p == NULL || *p == NUL) |
| 4408 | break; |
| 4409 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4410 | ++spin->si_keepwcount; |
| 4411 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4412 | return res; |
| 4413 | } |
| 4414 | |
| 4415 | /* |
| 4416 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4417 | * When "flags" < 0 we are adding to the prefix tree where flags is used for |
| 4418 | * "rare" and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4419 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4420 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4421 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4422 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4423 | char_u *word; |
| 4424 | wordnode_T *root; |
| 4425 | int flags; |
| 4426 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4427 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4428 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4429 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4430 | wordnode_T *node = root; |
| 4431 | wordnode_T *np; |
| 4432 | wordnode_T **prev = NULL; |
| 4433 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4434 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4435 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 4436 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4437 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4438 | /* Look for the sibling that has the same character. They are sorted |
| 4439 | * 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] | 4440 | * higher byte value. For zero bytes (end of word) the sorting is |
| 4441 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4442 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4443 | while (node != NULL |
| 4444 | && (node->wn_byte < word[i] |
| 4445 | || (node->wn_byte == NUL |
| 4446 | && (flags < 0 |
| 4447 | ? node->wn_prefixID < prefixID |
| 4448 | : node->wn_flags < (flags & 0xff) |
| 4449 | || (node->wn_flags == (flags & 0xff) |
| 4450 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4451 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4452 | prev = &node->wn_sibling; |
| 4453 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4454 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4455 | if (node == NULL |
| 4456 | || node->wn_byte != word[i] |
| 4457 | || (word[i] == NUL |
| 4458 | && (flags < 0 |
| 4459 | || node->wn_flags != (flags & 0xff) |
| 4460 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4461 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4462 | /* Allocate a new node. */ |
| 4463 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 4464 | if (np == NULL) |
| 4465 | return FAIL; |
| 4466 | np->wn_byte = word[i]; |
| 4467 | *prev = np; |
| 4468 | np->wn_sibling = node; |
| 4469 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4470 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4471 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4472 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4473 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4474 | node->wn_flags = flags; |
| 4475 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4476 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4477 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4478 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4479 | prev = &node->wn_child; |
| 4480 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4487 | * Compress a tree: find tails that are identical and can be shared. |
| 4488 | */ |
| 4489 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4490 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4491 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4492 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4493 | { |
| 4494 | hashtab_T ht; |
| 4495 | int n; |
| 4496 | int tot = 0; |
| 4497 | |
| 4498 | if (root != NULL) |
| 4499 | { |
| 4500 | hash_init(&ht); |
| 4501 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4502 | if (spin->si_verbose || p_verbose > 2) |
| 4503 | { |
| 4504 | if (!spin->si_verbose) |
| 4505 | verbose_enter(); |
| 4506 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4507 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4508 | if (p_verbose > 2) |
| 4509 | verbose_leave(); |
| 4510 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4511 | hash_clear(&ht); |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | /* |
| 4516 | * Compress a node, its siblings and its children, depth first. |
| 4517 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4518 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4519 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4520 | node_compress(node, ht, tot) |
| 4521 | wordnode_T *node; |
| 4522 | hashtab_T *ht; |
| 4523 | int *tot; /* total count of nodes before compressing, |
| 4524 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4525 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4526 | wordnode_T *np; |
| 4527 | wordnode_T *tp; |
| 4528 | wordnode_T *child; |
| 4529 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4530 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4531 | int len = 0; |
| 4532 | unsigned nr, n; |
| 4533 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4534 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4535 | /* |
| 4536 | * Go through the list of siblings. Compress each child and then try |
| 4537 | * finding an identical child to replace it. |
| 4538 | * Note that with "child" we mean not just the node that is pointed to, |
| 4539 | * but the whole list of siblings, of which the node is the first. |
| 4540 | */ |
| 4541 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4542 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4543 | ++len; |
| 4544 | if ((child = np->wn_child) != NULL) |
| 4545 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4546 | /* Compress the child. This fills hashkey. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4547 | compressed += node_compress(child, ht, tot); |
| 4548 | |
| 4549 | /* Try to find an identical child. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4550 | hash = hash_hash(child->wn_u1.hashkey); |
| 4551 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4552 | tp = NULL; |
| 4553 | if (!HASHITEM_EMPTY(hi)) |
| 4554 | { |
| 4555 | /* There are children with an identical hash value. Now check |
| 4556 | * if there is one that is really identical. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4557 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4558 | if (node_equal(child, tp)) |
| 4559 | { |
| 4560 | /* Found one! Now use that child in place of the |
| 4561 | * current one. This means the current child is |
| 4562 | * dropped from the tree. */ |
| 4563 | np->wn_child = tp; |
| 4564 | ++compressed; |
| 4565 | break; |
| 4566 | } |
| 4567 | if (tp == NULL) |
| 4568 | { |
| 4569 | /* No other child with this hash value equals the child of |
| 4570 | * the node, add it to the linked list after the first |
| 4571 | * item. */ |
| 4572 | tp = HI2WN(hi); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4573 | child->wn_u2.next = tp->wn_u2.next; |
| 4574 | tp->wn_u2.next = child; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | else |
| 4578 | /* No other child has this hash value, add it to the |
| 4579 | * hashtable. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4580 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4581 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4582 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4583 | *tot += len; |
| 4584 | |
| 4585 | /* |
| 4586 | * Make a hash key for the node and its siblings, so that we can quickly |
| 4587 | * find a lookalike node. This must be done after compressing the sibling |
| 4588 | * list, otherwise the hash key would become invalid by the compression. |
| 4589 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4590 | node->wn_u1.hashkey[0] = len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4591 | nr = 0; |
| 4592 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4593 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4594 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4595 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 4596 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4597 | else |
| 4598 | /* byte node: use the byte value and the child pointer */ |
| 4599 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 4600 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4601 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4602 | |
| 4603 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 4604 | n = nr & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4605 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4606 | n = (nr >> 8) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4607 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4608 | n = (nr >> 16) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4609 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4610 | n = (nr >> 24) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4611 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 4612 | node->wn_u1.hashkey[5] = NUL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4613 | |
| 4614 | return compressed; |
| 4615 | } |
| 4616 | |
| 4617 | /* |
| 4618 | * Return TRUE when two nodes have identical siblings and children. |
| 4619 | */ |
| 4620 | static int |
| 4621 | node_equal(n1, n2) |
| 4622 | wordnode_T *n1; |
| 4623 | wordnode_T *n2; |
| 4624 | { |
| 4625 | wordnode_T *p1; |
| 4626 | wordnode_T *p2; |
| 4627 | |
| 4628 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 4629 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 4630 | if (p1->wn_byte != p2->wn_byte |
| 4631 | || (p1->wn_byte == NUL |
| 4632 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4633 | || p1->wn_region != p2->wn_region |
| 4634 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4635 | : (p1->wn_child != p2->wn_child))) |
| 4636 | break; |
| 4637 | |
| 4638 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /* |
| 4642 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 4643 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4644 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4645 | put_bytes(fd, nr, len) |
| 4646 | FILE *fd; |
| 4647 | long_u nr; |
| 4648 | int len; |
| 4649 | { |
| 4650 | int i; |
| 4651 | |
| 4652 | for (i = len - 1; i >= 0; --i) |
| 4653 | putc((int)(nr >> (i * 8)), fd); |
| 4654 | } |
| 4655 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4656 | static int |
| 4657 | #ifdef __BORLANDC__ |
| 4658 | _RTLENTRYF |
| 4659 | #endif |
| 4660 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 4661 | |
| 4662 | /* |
| 4663 | * Function given to qsort() to sort the REP items on "from" string. |
| 4664 | */ |
| 4665 | static int |
| 4666 | #ifdef __BORLANDC__ |
| 4667 | _RTLENTRYF |
| 4668 | #endif |
| 4669 | rep_compare(s1, s2) |
| 4670 | const void *s1; |
| 4671 | const void *s2; |
| 4672 | { |
| 4673 | fromto_T *p1 = (fromto_T *)s1; |
| 4674 | fromto_T *p2 = (fromto_T *)s2; |
| 4675 | |
| 4676 | return STRCMP(p1->ft_from, p2->ft_from); |
| 4677 | } |
| 4678 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4679 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4680 | * Write the Vim spell file "fname". |
| 4681 | */ |
| 4682 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4683 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4684 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4685 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4686 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4687 | FILE *fd; |
| 4688 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4689 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4690 | wordnode_T *tree; |
| 4691 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4692 | int i; |
| 4693 | int l; |
| 4694 | garray_T *gap; |
| 4695 | fromto_T *ftp; |
| 4696 | char_u *p; |
| 4697 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4698 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4699 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4700 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4701 | { |
| 4702 | EMSG2(_(e_notopen), fname); |
| 4703 | return; |
| 4704 | } |
| 4705 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4706 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4707 | * <charflagslen> <charflags> |
| 4708 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4709 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4710 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4711 | |
| 4712 | /* <fileID> */ |
| 4713 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 4714 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4715 | |
| 4716 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4717 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4718 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4719 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 4720 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 4721 | (size_t)1, fd); |
| 4722 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4723 | } |
| 4724 | else |
| 4725 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4726 | putc(0, fd); |
| 4727 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4730 | /* |
| 4731 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4732 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 4733 | * 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] | 4734 | * 'encoding'. |
| 4735 | * Also skip this for an .add.spl file, the main spell file must contain |
| 4736 | * the table (avoids that it conflicts). File is shorter too. |
| 4737 | */ |
| 4738 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4739 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4740 | putc(0, fd); |
| 4741 | putc(0, fd); |
| 4742 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4743 | } |
| 4744 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4745 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4746 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4747 | |
| 4748 | if (spin->si_midword == NULL) |
| 4749 | put_bytes(fd, 0L, 2); /* <midwordlen> */ |
| 4750 | else |
| 4751 | { |
| 4752 | i = STRLEN(spin->si_midword); |
| 4753 | put_bytes(fd, (long_u)i, 2); /* <midwordlen> */ |
| 4754 | fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */ |
| 4755 | } |
| 4756 | |
| 4757 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4758 | /* Write the prefix conditions. */ |
| 4759 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 4760 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4761 | /* <SUGGEST> : <repcount> <rep> ... |
| 4762 | * <salflags> <salcount> <sal> ... |
| 4763 | * <maplen> <mapstr> */ |
| 4764 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4765 | /* Sort the REP items. */ |
| 4766 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 4767 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4768 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4769 | /* round 1: REP items |
| 4770 | * round 2: SAL items (unless SOFO is used) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4771 | for (round = 1; round <= 2; ++round) |
| 4772 | { |
| 4773 | if (round == 1) |
| 4774 | gap = &spin->si_rep; |
| 4775 | else |
| 4776 | { |
| 4777 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4778 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4779 | i = 0; |
| 4780 | if (spin->si_followup) |
| 4781 | i |= SAL_F0LLOWUP; |
| 4782 | if (spin->si_collapse) |
| 4783 | i |= SAL_COLLAPSE; |
| 4784 | if (spin->si_rem_accents) |
| 4785 | i |= SAL_REM_ACCENTS; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4786 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 4787 | i |= SAL_SOFO; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4788 | putc(i, fd); /* <salflags> */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4789 | if (i & SAL_SOFO) |
| 4790 | break; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 4794 | for (i = 0; i < gap->ga_len; ++i) |
| 4795 | { |
| 4796 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 4797 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 4798 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 4799 | for (rr = 1; rr <= 2; ++rr) |
| 4800 | { |
| 4801 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 4802 | l = STRLEN(p); |
| 4803 | putc(l, fd); |
| 4804 | fwrite(p, l, (size_t)1, fd); |
| 4805 | } |
| 4806 | } |
| 4807 | } |
| 4808 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 4809 | /* SOFOFROM and SOFOTO */ |
| 4810 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 4811 | { |
| 4812 | put_bytes(fd, 1L, 2); /* <salcount> */ |
| 4813 | |
| 4814 | l = STRLEN(spin->si_sofofr); |
| 4815 | put_bytes(fd, (long_u)l, 2); /* <salfromlen> */ |
| 4816 | fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */ |
| 4817 | |
| 4818 | l = STRLEN(spin->si_sofoto); |
| 4819 | put_bytes(fd, (long_u)l, 2); /* <saltolen> */ |
| 4820 | fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */ |
| 4821 | } |
| 4822 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4823 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 4824 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 4825 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 4826 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4827 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4828 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4829 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4830 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4831 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4832 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4833 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4834 | if (round == 1) |
| 4835 | tree = spin->si_foldroot; |
| 4836 | else if (round == 2) |
| 4837 | tree = spin->si_keeproot; |
| 4838 | else |
| 4839 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4840 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4841 | /* Clear the index and wnode fields in the tree. */ |
| 4842 | clear_node(tree); |
| 4843 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4844 | /* Count the number of nodes. Needed to be able to allocate the |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4845 | * memory when reading the nodes. Also fills in index for shared |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4846 | * nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4847 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4848 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4849 | /* number of nodes in 4 bytes */ |
| 4850 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4851 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4852 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4853 | /* Write the nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4854 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4857 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4861 | * Clear the index and wnode fields of "node", it siblings and its |
| 4862 | * children. This is needed because they are a union with other items to save |
| 4863 | * space. |
| 4864 | */ |
| 4865 | static void |
| 4866 | clear_node(node) |
| 4867 | wordnode_T *node; |
| 4868 | { |
| 4869 | wordnode_T *np; |
| 4870 | |
| 4871 | if (node != NULL) |
| 4872 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4873 | { |
| 4874 | np->wn_u1.index = 0; |
| 4875 | np->wn_u2.wnode = NULL; |
| 4876 | |
| 4877 | if (np->wn_byte != NUL) |
| 4878 | clear_node(np->wn_child); |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | |
| 4883 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4884 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4885 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4886 | * This first writes the list of possible bytes (siblings). Then for each |
| 4887 | * byte recursively write the children. |
| 4888 | * |
| 4889 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 4890 | * are made about the indexes (so that we don't have to write them in the |
| 4891 | * file). |
| 4892 | * |
| 4893 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4894 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4895 | static int |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4896 | put_node(fd, node, index, regionmask, prefixtree) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4897 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4898 | wordnode_T *node; |
| 4899 | int index; |
| 4900 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4901 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4902 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4903 | int newindex = index; |
| 4904 | int siblingcount = 0; |
| 4905 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4906 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4907 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4908 | /* If "node" is zero the tree is empty. */ |
| 4909 | if (node == NULL) |
| 4910 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4911 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4912 | /* Store the index where this node is written. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4913 | node->wn_u1.index = index; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4914 | |
| 4915 | /* Count the number of siblings. */ |
| 4916 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4917 | ++siblingcount; |
| 4918 | |
| 4919 | /* Write the sibling count. */ |
| 4920 | if (fd != NULL) |
| 4921 | putc(siblingcount, fd); /* <siblingcount> */ |
| 4922 | |
| 4923 | /* Write each sibling byte and optionally extra info. */ |
| 4924 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4925 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4926 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4927 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4928 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4929 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4930 | /* For a NUL byte (end of word) write the flags etc. */ |
| 4931 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4932 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4933 | /* In PREFIXTREE write the required prefixID and the |
| 4934 | * associated condition nr (stored in wn_region). */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4935 | if (np->wn_flags == (char_u)-2) |
| 4936 | putc(BY_FLAGS, fd); /* <byte> rare */ |
| 4937 | else |
| 4938 | putc(BY_NOFLAGS, fd); /* <byte> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4939 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4940 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4941 | } |
| 4942 | else |
| 4943 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4944 | /* For word trees we write the flag/region items. */ |
| 4945 | flags = np->wn_flags; |
| 4946 | if (regionmask != 0 && np->wn_region != regionmask) |
| 4947 | flags |= WF_REGION; |
| 4948 | if (np->wn_prefixID != 0) |
| 4949 | flags |= WF_PFX; |
| 4950 | if (flags == 0) |
| 4951 | { |
| 4952 | /* word without flags or region */ |
| 4953 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 4954 | } |
| 4955 | else |
| 4956 | { |
| 4957 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4958 | putc(flags, fd); /* <flags> */ |
| 4959 | if (flags & WF_REGION) |
| 4960 | putc(np->wn_region, fd); /* <region> */ |
| 4961 | if (flags & WF_PFX) |
| 4962 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4963 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4964 | } |
| 4965 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4966 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4967 | else |
| 4968 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4969 | if (np->wn_child->wn_u1.index != 0 |
| 4970 | && np->wn_child->wn_u2.wnode != node) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4971 | { |
| 4972 | /* The child is written elsewhere, write the reference. */ |
| 4973 | if (fd != NULL) |
| 4974 | { |
| 4975 | putc(BY_INDEX, fd); /* <byte> */ |
| 4976 | /* <nodeidx> */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4977 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4978 | } |
| 4979 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4980 | else if (np->wn_child->wn_u2.wnode == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4981 | /* We will write the child below and give it an index. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4982 | np->wn_child->wn_u2.wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4983 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4984 | if (fd != NULL) |
| 4985 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 4986 | { |
| 4987 | EMSG(_(e_write)); |
| 4988 | return 0; |
| 4989 | } |
| 4990 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4991 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4992 | |
| 4993 | /* Space used in the array when reading: one for each sibling and one for |
| 4994 | * the count. */ |
| 4995 | newindex += siblingcount + 1; |
| 4996 | |
| 4997 | /* Recursively dump the children of each sibling. */ |
| 4998 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4999 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 5000 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5001 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5002 | |
| 5003 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | |
| 5007 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5008 | * ":mkspell [-ascii] outfile infile ..." |
| 5009 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5010 | */ |
| 5011 | void |
| 5012 | ex_mkspell(eap) |
| 5013 | exarg_T *eap; |
| 5014 | { |
| 5015 | int fcount; |
| 5016 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5017 | char_u *arg = eap->arg; |
| 5018 | int ascii = FALSE; |
| 5019 | |
| 5020 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 5021 | { |
| 5022 | ascii = TRUE; |
| 5023 | arg = skipwhite(arg + 6); |
| 5024 | } |
| 5025 | |
| 5026 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 5027 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 5028 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5029 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5030 | FreeWild(fcount, fnames); |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | /* |
| 5035 | * Create a Vim spell file from one or more word lists. |
| 5036 | * "fnames[0]" is the output file name. |
| 5037 | * "fnames[fcount - 1]" is the last input file name. |
| 5038 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 5039 | * and ".spl" is appended to make the output file name. |
| 5040 | */ |
| 5041 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5042 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5043 | int fcount; |
| 5044 | char_u **fnames; |
| 5045 | int ascii; /* -ascii argument given */ |
| 5046 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5047 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5048 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5049 | char_u fname[MAXPATHL]; |
| 5050 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5051 | char_u **innames; |
| 5052 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5053 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5054 | int i; |
| 5055 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5056 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5057 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5058 | spellinfo_T spin; |
| 5059 | |
| 5060 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5061 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5062 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5063 | spin.si_followup = TRUE; |
| 5064 | spin.si_rem_accents = TRUE; |
| 5065 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 5066 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 5067 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5068 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5069 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5070 | /* default: fnames[0] is output file, following are input files */ |
| 5071 | innames = &fnames[1]; |
| 5072 | incount = fcount - 1; |
| 5073 | |
| 5074 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5075 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5076 | len = STRLEN(fnames[0]); |
| 5077 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 5078 | { |
| 5079 | /* For ":mkspell path/en.latin1.add" output file is |
| 5080 | * "path/en.latin1.add.spl". */ |
| 5081 | innames = &fnames[0]; |
| 5082 | incount = 1; |
| 5083 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 5084 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5085 | else if (fcount == 1) |
| 5086 | { |
| 5087 | /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */ |
| 5088 | innames = &fnames[0]; |
| 5089 | incount = 1; |
| 5090 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 5091 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 5092 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5093 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 5094 | { |
| 5095 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5096 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5097 | } |
| 5098 | else |
| 5099 | /* Name should be language, make the file name from it. */ |
| 5100 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 5101 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 5102 | |
| 5103 | /* Check for .ascii.spl. */ |
| 5104 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 5105 | spin.si_ascii = TRUE; |
| 5106 | |
| 5107 | /* Check for .add.spl. */ |
| 5108 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 5109 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5112 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5113 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5114 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 5115 | EMSG(_("E751: Output file name must not have region name")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5116 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5117 | EMSG(_("E754: Only up to 8 regions supported")); |
| 5118 | else |
| 5119 | { |
| 5120 | /* Check for overwriting before doing things that may take a lot of |
| 5121 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5122 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5123 | { |
| 5124 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5125 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5126 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5127 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5128 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5129 | EMSG2(_(e_isadir2), wfname); |
| 5130 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | /* |
| 5134 | * Init the aff and dic pointers. |
| 5135 | * Get the region names if there are more than 2 arguments. |
| 5136 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5137 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5138 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5139 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5140 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5141 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5142 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5143 | len = STRLEN(innames[i]); |
| 5144 | if (STRLEN(gettail(innames[i])) < 5 |
| 5145 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5146 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5147 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 5148 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5149 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5150 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 5151 | spin.si_region_name[i * 2 + 1] = |
| 5152 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5153 | } |
| 5154 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5155 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5156 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5157 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 5158 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5159 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 5160 | if (spin.si_foldroot == NULL |
| 5161 | || spin.si_keeproot == NULL |
| 5162 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5163 | { |
| 5164 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5165 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5168 | /* When not producing a .add.spl file clear the character table when |
| 5169 | * we encounter one in the .aff file. This means we dump the current |
| 5170 | * one in the .spl file if the .aff file doesn't define one. That's |
| 5171 | * better than guessing the contents, the table will match a |
| 5172 | * previously loaded spell file. */ |
| 5173 | if (!spin.si_add) |
| 5174 | spin.si_clear_chartab = TRUE; |
| 5175 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5176 | /* |
| 5177 | * Read all the .aff and .dic files. |
| 5178 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5179 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5180 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5181 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5182 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5183 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5184 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5185 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5186 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5187 | if (mch_stat((char *)fname, &st) >= 0) |
| 5188 | { |
| 5189 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 5190 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5191 | afile[i] = spell_read_aff(fname, &spin); |
| 5192 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5193 | error = TRUE; |
| 5194 | else |
| 5195 | { |
| 5196 | /* Read the .dic file and store the words in the trees. */ |
| 5197 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5198 | innames[i]); |
| 5199 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5200 | error = TRUE; |
| 5201 | } |
| 5202 | } |
| 5203 | else |
| 5204 | { |
| 5205 | /* No .aff file, try reading the file as a word list. Store |
| 5206 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5207 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5208 | error = TRUE; |
| 5209 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5210 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5211 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5212 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5213 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5214 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5217 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5218 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5219 | /* |
| 5220 | * Remove the dummy NUL from the start of the tree root. |
| 5221 | */ |
| 5222 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 5223 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5224 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5225 | |
| 5226 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5227 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5228 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5229 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5230 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5231 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5232 | verbose_enter(); |
| 5233 | MSG(_("Compressing word tree...")); |
| 5234 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5235 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5236 | verbose_leave(); |
| 5237 | } |
| 5238 | wordtree_compress(spin.si_foldroot, &spin); |
| 5239 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5240 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5243 | if (!error) |
| 5244 | { |
| 5245 | /* |
| 5246 | * Write the info in the spell file. |
| 5247 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5248 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5249 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5250 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5251 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5252 | smsg((char_u *)_("Writing spell file %s ..."), wfname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5253 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5254 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5255 | verbose_leave(); |
| 5256 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5257 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5258 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5259 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5260 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5261 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5262 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5263 | verbose_enter(); |
| 5264 | MSG(_("Done!")); |
| 5265 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5266 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5267 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5268 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5269 | verbose_leave(); |
| 5270 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5271 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5272 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5273 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5277 | ga_clear(&spin.si_rep); |
| 5278 | ga_clear(&spin.si_sal); |
| 5279 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5280 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5281 | vim_free(spin.si_midword); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5282 | vim_free(spin.si_sofofr); |
| 5283 | vim_free(spin.si_sofoto); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5284 | |
| 5285 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5286 | for (i = 0; i < incount; ++i) |
| 5287 | if (afile[i] != NULL) |
| 5288 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5289 | |
| 5290 | /* Free all the bits and pieces at once. */ |
| 5291 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5292 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5295 | |
| 5296 | /* |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5297 | * ":[count]spellgood {word}" |
| 5298 | * ":[count]spellwrong {word}" |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5299 | */ |
| 5300 | void |
| 5301 | ex_spell(eap) |
| 5302 | exarg_T *eap; |
| 5303 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5304 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong, |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5305 | eap->forceit ? 0 : (int)eap->line2); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | /* |
| 5309 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 5310 | */ |
| 5311 | void |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5312 | spell_add_word(word, len, bad, index) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5313 | char_u *word; |
| 5314 | int len; |
| 5315 | int bad; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5316 | int index; /* "zG" and "zW": zero, otherwise index in |
| 5317 | 'spellfile' */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5318 | { |
| 5319 | FILE *fd; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5320 | buf_T *buf = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5321 | int new_spf = FALSE; |
| 5322 | struct stat st; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5323 | char_u *fname; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5324 | char_u fnamebuf[MAXPATHL]; |
| 5325 | char_u line[MAXWLEN * 2]; |
| 5326 | long fpos, fpos_next = 0; |
| 5327 | int i; |
| 5328 | char_u *spf; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5329 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5330 | if (index == 0) /* use internal wordlist */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5331 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5332 | if (int_wordlist == NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5333 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5334 | int_wordlist = vim_tempname('s'); |
| 5335 | if (int_wordlist == NULL) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5336 | return; |
| 5337 | } |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5338 | fname = int_wordlist; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5339 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5340 | else |
| 5341 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5342 | /* If 'spellfile' isn't set figure out a good default value. */ |
| 5343 | if (*curbuf->b_p_spf == NUL) |
| 5344 | { |
| 5345 | init_spellfile(); |
| 5346 | new_spf = TRUE; |
| 5347 | } |
| 5348 | |
| 5349 | if (*curbuf->b_p_spf == NUL) |
| 5350 | { |
| 5351 | EMSG(_("E764: 'spellfile' is not set")); |
| 5352 | return; |
| 5353 | } |
| 5354 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5355 | for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i) |
| 5356 | { |
| 5357 | copy_option_part(&spf, fnamebuf, MAXPATHL, ","); |
| 5358 | if (i == index) |
| 5359 | break; |
| 5360 | if (*spf == NUL) |
| 5361 | { |
| 5362 | EMSGN(_("E765: 'spellfile' does not have %ld enties"), index); |
| 5363 | return; |
| 5364 | } |
| 5365 | } |
| 5366 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5367 | /* Check that the user isn't editing the .add file somewhere. */ |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5368 | buf = buflist_findname_exp(fnamebuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5369 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 5370 | buf = NULL; |
| 5371 | if (buf != NULL && bufIsChanged(buf)) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5372 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5373 | EMSG(_(e_bufloaded)); |
| 5374 | return; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5375 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5376 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 5377 | fname = fnamebuf; |
| 5378 | } |
| 5379 | |
| 5380 | if (bad) |
| 5381 | { |
| 5382 | /* When the word also appears as good word we need to remove that one, |
| 5383 | * since its flags sort before the one with WF_BANNED. */ |
| 5384 | fd = mch_fopen((char *)fname, "r"); |
| 5385 | if (fd != NULL) |
| 5386 | { |
| 5387 | while (!vim_fgets(line, MAXWLEN * 2, fd)) |
| 5388 | { |
| 5389 | fpos = fpos_next; |
| 5390 | fpos_next = ftell(fd); |
| 5391 | if (STRNCMP(word, line, len) == 0 |
| 5392 | && (line[len] == '/' || line[len] < ' ')) |
| 5393 | { |
| 5394 | /* Found duplicate word. Remove it by writing a '#' at |
| 5395 | * the start of the line. Mixing reading and writing |
| 5396 | * doesn't work for all systems, close the file first. */ |
| 5397 | fclose(fd); |
| 5398 | fd = mch_fopen((char *)fname, "r+"); |
| 5399 | if (fd == NULL) |
| 5400 | break; |
| 5401 | if (fseek(fd, fpos, SEEK_SET) == 0) |
| 5402 | fputc('#', fd); |
| 5403 | fseek(fd, fpos_next, SEEK_SET); |
| 5404 | } |
| 5405 | } |
| 5406 | fclose(fd); |
| 5407 | } |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | fd = mch_fopen((char *)fname, "a"); |
| 5411 | if (fd == NULL && new_spf) |
| 5412 | { |
| 5413 | /* We just initialized the 'spellfile' option and can't open the file. |
| 5414 | * We may need to create the "spell" directory first. We already |
| 5415 | * checked the runtime directory is writable in init_spellfile(). */ |
| 5416 | STRCPY(NameBuff, fname); |
| 5417 | *gettail_sep(NameBuff) = NUL; |
| 5418 | if (mch_stat((char *)NameBuff, &st) < 0) |
| 5419 | { |
| 5420 | /* The directory doesn't exist. Try creating it and opening the |
| 5421 | * file again. */ |
| 5422 | vim_mkdir(NameBuff, 0755); |
| 5423 | fd = mch_fopen((char *)fname, "a"); |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | if (fd == NULL) |
| 5428 | EMSG2(_(e_notopen), fname); |
| 5429 | else |
| 5430 | { |
| 5431 | if (bad) |
| 5432 | fprintf(fd, "%.*s/!\n", len, word); |
| 5433 | else |
| 5434 | fprintf(fd, "%.*s\n", len, word); |
| 5435 | fclose(fd); |
| 5436 | |
| 5437 | /* Update the .add.spl file. */ |
| 5438 | mkspell(1, &fname, FALSE, TRUE, TRUE); |
| 5439 | |
| 5440 | /* If the .add file is edited somewhere, reload it. */ |
| 5441 | if (buf != NULL) |
| 5442 | buf_reload(buf); |
| 5443 | |
| 5444 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | /* |
| 5449 | * Initialize 'spellfile' for the current buffer. |
| 5450 | */ |
| 5451 | static void |
| 5452 | init_spellfile() |
| 5453 | { |
| 5454 | char_u buf[MAXPATHL]; |
| 5455 | int l; |
| 5456 | slang_T *sl; |
| 5457 | char_u *rtp; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5458 | char_u *lend; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5459 | |
| 5460 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 5461 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5462 | /* Find the end of the language name. Exclude the region. */ |
| 5463 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 5464 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| 5465 | ; |
| 5466 | |
| 5467 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 5468 | * are allowed to write. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5469 | rtp = p_rtp; |
| 5470 | while (*rtp != NUL) |
| 5471 | { |
| 5472 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 5473 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 5474 | if (filewritable(buf) == 2) |
| 5475 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5476 | /* Use the first language name from 'spelllang' and the |
| 5477 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5478 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 5479 | l = STRLEN(buf); |
| 5480 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5481 | "/spell/%.*s.%s.add", |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5482 | (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5483 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 5484 | ? (char_u *)"ascii" : spell_enc()); |
| 5485 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 5486 | break; |
| 5487 | } |
| 5488 | } |
| 5489 | } |
| 5490 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5491 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5492 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5493 | /* |
| 5494 | * Init the chartab used for spelling for ASCII. |
| 5495 | * EBCDIC is not supported! |
| 5496 | */ |
| 5497 | static void |
| 5498 | clear_spell_chartab(sp) |
| 5499 | spelltab_T *sp; |
| 5500 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5501 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5502 | |
| 5503 | /* Init everything to FALSE. */ |
| 5504 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 5505 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 5506 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5507 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5508 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5509 | sp->st_upper[i] = i; |
| 5510 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5511 | |
| 5512 | /* We include digits. A word shouldn't start with a digit, but handling |
| 5513 | * that is done separately. */ |
| 5514 | for (i = '0'; i <= '9'; ++i) |
| 5515 | sp->st_isw[i] = TRUE; |
| 5516 | for (i = 'A'; i <= 'Z'; ++i) |
| 5517 | { |
| 5518 | sp->st_isw[i] = TRUE; |
| 5519 | sp->st_isu[i] = TRUE; |
| 5520 | sp->st_fold[i] = i + 0x20; |
| 5521 | } |
| 5522 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5523 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5524 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5525 | sp->st_upper[i] = i - 0x20; |
| 5526 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | /* |
| 5530 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 5531 | * Called once while starting up and when 'encoding' changes. |
| 5532 | * The default is to use isalpha(), but the spell file should define the word |
| 5533 | * characters to make it possible that 'encoding' differs from the current |
| 5534 | * locale. |
| 5535 | */ |
| 5536 | void |
| 5537 | init_spell_chartab() |
| 5538 | { |
| 5539 | int i; |
| 5540 | |
| 5541 | did_set_spelltab = FALSE; |
| 5542 | clear_spell_chartab(&spelltab); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5543 | #ifdef FEAT_MBYTE |
| 5544 | if (enc_dbcs) |
| 5545 | { |
| 5546 | /* DBCS: assume double-wide characters are word characters. */ |
| 5547 | for (i = 128; i <= 255; ++i) |
| 5548 | if (MB_BYTE2LEN(i) == 2) |
| 5549 | spelltab.st_isw[i] = TRUE; |
| 5550 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5551 | else if (enc_utf8) |
| 5552 | { |
| 5553 | for (i = 128; i < 256; ++i) |
| 5554 | { |
| 5555 | spelltab.st_isu[i] = utf_isupper(i); |
| 5556 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 5557 | spelltab.st_fold[i] = utf_fold(i); |
| 5558 | spelltab.st_upper[i] = utf_toupper(i); |
| 5559 | } |
| 5560 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5561 | else |
| 5562 | #endif |
| 5563 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5564 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5565 | for (i = 128; i < 256; ++i) |
| 5566 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5567 | if (MB_ISUPPER(i)) |
| 5568 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5569 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5570 | spelltab.st_isu[i] = TRUE; |
| 5571 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 5572 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5573 | else if (MB_ISLOWER(i)) |
| 5574 | { |
| 5575 | spelltab.st_isw[i] = TRUE; |
| 5576 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 5577 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5578 | } |
| 5579 | } |
| 5580 | } |
| 5581 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5582 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 5583 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 5584 | |
| 5585 | /* |
| 5586 | * Set the spell character tables from strings in the affix file. |
| 5587 | */ |
| 5588 | static int |
| 5589 | set_spell_chartab(fol, low, upp) |
| 5590 | char_u *fol; |
| 5591 | char_u *low; |
| 5592 | char_u *upp; |
| 5593 | { |
| 5594 | /* We build the new tables here first, so that we can compare with the |
| 5595 | * previous one. */ |
| 5596 | spelltab_T new_st; |
| 5597 | char_u *pf = fol, *pl = low, *pu = upp; |
| 5598 | int f, l, u; |
| 5599 | |
| 5600 | clear_spell_chartab(&new_st); |
| 5601 | |
| 5602 | while (*pf != NUL) |
| 5603 | { |
| 5604 | if (*pl == NUL || *pu == NUL) |
| 5605 | { |
| 5606 | EMSG(_(e_affform)); |
| 5607 | return FAIL; |
| 5608 | } |
| 5609 | #ifdef FEAT_MBYTE |
| 5610 | f = mb_ptr2char_adv(&pf); |
| 5611 | l = mb_ptr2char_adv(&pl); |
| 5612 | u = mb_ptr2char_adv(&pu); |
| 5613 | #else |
| 5614 | f = *pf++; |
| 5615 | l = *pl++; |
| 5616 | u = *pu++; |
| 5617 | #endif |
| 5618 | /* Every character that appears is a word character. */ |
| 5619 | if (f < 256) |
| 5620 | new_st.st_isw[f] = TRUE; |
| 5621 | if (l < 256) |
| 5622 | new_st.st_isw[l] = TRUE; |
| 5623 | if (u < 256) |
| 5624 | new_st.st_isw[u] = TRUE; |
| 5625 | |
| 5626 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 5627 | * case-folding */ |
| 5628 | if (l < 256 && l != f) |
| 5629 | { |
| 5630 | if (f >= 256) |
| 5631 | { |
| 5632 | EMSG(_(e_affrange)); |
| 5633 | return FAIL; |
| 5634 | } |
| 5635 | new_st.st_fold[l] = f; |
| 5636 | } |
| 5637 | |
| 5638 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5639 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 5640 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5641 | if (u < 256 && u != f) |
| 5642 | { |
| 5643 | if (f >= 256) |
| 5644 | { |
| 5645 | EMSG(_(e_affrange)); |
| 5646 | return FAIL; |
| 5647 | } |
| 5648 | new_st.st_fold[u] = f; |
| 5649 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5650 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5651 | } |
| 5652 | } |
| 5653 | |
| 5654 | if (*pl != NUL || *pu != NUL) |
| 5655 | { |
| 5656 | EMSG(_(e_affform)); |
| 5657 | return FAIL; |
| 5658 | } |
| 5659 | |
| 5660 | return set_spell_finish(&new_st); |
| 5661 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5662 | |
| 5663 | /* |
| 5664 | * Set the spell character tables from strings in the .spl file. |
| 5665 | */ |
| 5666 | static int |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5667 | set_spell_charflags(flags, cnt, fol) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5668 | char_u *flags; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5669 | int cnt; /* length of "flags" */ |
| 5670 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5671 | { |
| 5672 | /* We build the new tables here first, so that we can compare with the |
| 5673 | * previous one. */ |
| 5674 | spelltab_T new_st; |
| 5675 | int i; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5676 | char_u *p = fol; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5677 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5678 | |
| 5679 | clear_spell_chartab(&new_st); |
| 5680 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5681 | for (i = 0; i < 128; ++i) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5682 | { |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5683 | if (i < cnt) |
| 5684 | { |
| 5685 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 5686 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
| 5687 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5688 | |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5689 | if (*p != NUL) |
| 5690 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5691 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5692 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5693 | #else |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5694 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5695 | #endif |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 5696 | new_st.st_fold[i + 128] = c; |
| 5697 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 5698 | new_st.st_upper[c] = i + 128; |
| 5699 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | return set_spell_finish(&new_st); |
| 5703 | } |
| 5704 | |
| 5705 | static int |
| 5706 | set_spell_finish(new_st) |
| 5707 | spelltab_T *new_st; |
| 5708 | { |
| 5709 | int i; |
| 5710 | |
| 5711 | if (did_set_spelltab) |
| 5712 | { |
| 5713 | /* check that it's the same table */ |
| 5714 | for (i = 0; i < 256; ++i) |
| 5715 | { |
| 5716 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 5717 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5718 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 5719 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5720 | { |
| 5721 | EMSG(_("E763: Word characters differ between spell files")); |
| 5722 | return FAIL; |
| 5723 | } |
| 5724 | } |
| 5725 | } |
| 5726 | else |
| 5727 | { |
| 5728 | /* copy the new spelltab into the one being used */ |
| 5729 | spelltab = *new_st; |
| 5730 | did_set_spelltab = TRUE; |
| 5731 | } |
| 5732 | |
| 5733 | return OK; |
| 5734 | } |
| 5735 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5736 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5737 | * Return TRUE if "p" points to a word character. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5738 | * 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] | 5739 | * 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] | 5740 | * 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] | 5741 | */ |
| 5742 | static int |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5743 | spell_iswordp(p, buf) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5744 | char_u *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5745 | buf_T *buf; /* buffer used */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5746 | { |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5747 | #ifdef FEAT_MBYTE |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5748 | char_u *s; |
| 5749 | int l; |
| 5750 | int c; |
| 5751 | |
| 5752 | if (has_mbyte) |
| 5753 | { |
| 5754 | l = MB_BYTE2LEN(*p); |
| 5755 | s = p; |
| 5756 | if (l == 1) |
| 5757 | { |
| 5758 | /* be quick for ASCII */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5759 | if (buf->b_spell_ismw[*p]) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5760 | { |
| 5761 | s = p + 1; /* skip a mid-word character */ |
| 5762 | l = MB_BYTE2LEN(*s); |
| 5763 | } |
| 5764 | } |
| 5765 | else |
| 5766 | { |
| 5767 | c = mb_ptr2char(p); |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5768 | if (c < 256 ? buf->b_spell_ismw[c] |
| 5769 | : (buf->b_spell_ismw_mb != NULL |
| 5770 | && vim_strchr(buf->b_spell_ismw_mb, c) != NULL)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5771 | { |
| 5772 | s = p + l; |
| 5773 | l = MB_BYTE2LEN(*s); |
| 5774 | } |
| 5775 | } |
| 5776 | |
| 5777 | if (l > 1) |
| 5778 | return mb_get_class(s) >= 2; |
| 5779 | return spelltab.st_isw[*s]; |
| 5780 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5781 | #endif |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5782 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5783 | return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]]; |
| 5784 | } |
| 5785 | |
| 5786 | /* |
| 5787 | * Return TRUE if "p" points to a word character. |
| 5788 | * Unlike spell_iswordp() this doesn't check for "midword" characters. |
| 5789 | */ |
| 5790 | static int |
| 5791 | spell_iswordp_nmw(p) |
| 5792 | char_u *p; |
| 5793 | { |
| 5794 | #ifdef FEAT_MBYTE |
| 5795 | if (has_mbyte && MB_BYTE2LEN(*p) > 1) |
| 5796 | return mb_get_class(p) >= 2; |
| 5797 | #endif |
| 5798 | |
| 5799 | return spelltab.st_isw[*p]; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5800 | } |
| 5801 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5802 | #ifdef FEAT_MBYTE |
| 5803 | /* |
| 5804 | * Return TRUE if "p" points to a word character. |
| 5805 | * Wide version of spell_iswordp(). |
| 5806 | */ |
| 5807 | static int |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5808 | spell_iswordp_w(p, buf) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5809 | int *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5810 | buf_T *buf; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5811 | { |
| 5812 | int *s; |
| 5813 | |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 5814 | if (*p < 256 ? buf->b_spell_ismw[*p] |
| 5815 | : (buf->b_spell_ismw_mb != NULL |
| 5816 | && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5817 | s = p + 1; |
| 5818 | else |
| 5819 | s = p; |
| 5820 | |
| 5821 | if (mb_char2len(*s) > 1) |
| 5822 | { |
| 5823 | if (enc_utf8) |
| 5824 | return utf_class(*s) >= 2; |
| 5825 | if (enc_dbcs) |
| 5826 | return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2; |
| 5827 | return 0; |
| 5828 | } |
| 5829 | return spelltab.st_isw[*s]; |
| 5830 | } |
| 5831 | #endif |
| 5832 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5833 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5834 | * Write the table with prefix conditions to the .spl file. |
| 5835 | */ |
| 5836 | static void |
| 5837 | write_spell_prefcond(fd, gap) |
| 5838 | FILE *fd; |
| 5839 | garray_T *gap; |
| 5840 | { |
| 5841 | int i; |
| 5842 | char_u *p; |
| 5843 | int len; |
| 5844 | |
| 5845 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 5846 | |
| 5847 | for (i = 0; i < gap->ga_len; ++i) |
| 5848 | { |
| 5849 | /* <prefcond> : <condlen> <condstr> */ |
| 5850 | p = ((char_u **)gap->ga_data)[i]; |
| 5851 | if (p == NULL) |
| 5852 | fputc(0, fd); |
| 5853 | else |
| 5854 | { |
| 5855 | len = STRLEN(p); |
| 5856 | fputc(len, fd); |
| 5857 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5863 | * Write the current tables into the .spl file. |
| 5864 | * This makes sure the same characters are recognized as word characters when |
| 5865 | * generating an when using a spell file. |
| 5866 | */ |
| 5867 | static void |
| 5868 | write_spell_chartab(fd) |
| 5869 | FILE *fd; |
| 5870 | { |
| 5871 | char_u charbuf[256 * 4]; |
| 5872 | int len = 0; |
| 5873 | int flags; |
| 5874 | int i; |
| 5875 | |
| 5876 | fputc(128, fd); /* <charflagslen> */ |
| 5877 | for (i = 128; i < 256; ++i) |
| 5878 | { |
| 5879 | flags = 0; |
| 5880 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5881 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5882 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5883 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5884 | fputc(flags, fd); /* <charflags> */ |
| 5885 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5886 | #ifdef FEAT_MBYTE |
| 5887 | if (has_mbyte) |
| 5888 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 5889 | else |
| 5890 | #endif |
| 5891 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 5895 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 5896 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5897 | |
| 5898 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5899 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 5900 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5901 | * When using a multi-byte 'encoding' the length may change! |
| 5902 | * Returns FAIL when something wrong. |
| 5903 | */ |
| 5904 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5905 | spell_casefold(str, len, buf, buflen) |
| 5906 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5907 | int len; |
| 5908 | char_u *buf; |
| 5909 | int buflen; |
| 5910 | { |
| 5911 | int i; |
| 5912 | |
| 5913 | if (len >= buflen) |
| 5914 | { |
| 5915 | buf[0] = NUL; |
| 5916 | return FAIL; /* result will not fit */ |
| 5917 | } |
| 5918 | |
| 5919 | #ifdef FEAT_MBYTE |
| 5920 | if (has_mbyte) |
| 5921 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5922 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5923 | char_u *p; |
| 5924 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5925 | |
| 5926 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5927 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5928 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5929 | if (outi + MB_MAXBYTES > buflen) |
| 5930 | { |
| 5931 | buf[outi] = NUL; |
| 5932 | return FAIL; |
| 5933 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5934 | c = mb_ptr2char_adv(&p); |
| 5935 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5936 | } |
| 5937 | buf[outi] = NUL; |
| 5938 | } |
| 5939 | else |
| 5940 | #endif |
| 5941 | { |
| 5942 | /* Be quick for non-multibyte encodings. */ |
| 5943 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5944 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5945 | buf[i] = NUL; |
| 5946 | } |
| 5947 | |
| 5948 | return OK; |
| 5949 | } |
| 5950 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5951 | #define SPS_BEST 1 |
| 5952 | #define SPS_FAST 2 |
| 5953 | #define SPS_DOUBLE 4 |
| 5954 | |
| 5955 | static int sps_flags = SPS_BEST; |
| 5956 | |
| 5957 | /* |
| 5958 | * Check the 'spellsuggest' option. Return FAIL if it's wrong. |
| 5959 | * Sets "sps_flags". |
| 5960 | */ |
| 5961 | int |
| 5962 | spell_check_sps() |
| 5963 | { |
| 5964 | char_u *p; |
| 5965 | char_u buf[MAXPATHL]; |
| 5966 | int f; |
| 5967 | |
| 5968 | sps_flags = 0; |
| 5969 | |
| 5970 | for (p = p_sps; *p != NUL; ) |
| 5971 | { |
| 5972 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 5973 | |
| 5974 | f = 0; |
| 5975 | if (STRCMP(buf, "best") == 0) |
| 5976 | f = SPS_BEST; |
| 5977 | else if (STRCMP(buf, "fast") == 0) |
| 5978 | f = SPS_FAST; |
| 5979 | else if (STRCMP(buf, "double") == 0) |
| 5980 | f = SPS_DOUBLE; |
| 5981 | else if (STRNCMP(buf, "expr:", 5) != 0 |
| 5982 | && STRNCMP(buf, "file:", 5) != 0) |
| 5983 | f = -1; |
| 5984 | |
| 5985 | if (f == -1 || (sps_flags != 0 && f != 0)) |
| 5986 | { |
| 5987 | sps_flags = SPS_BEST; |
| 5988 | return FAIL; |
| 5989 | } |
| 5990 | if (f != 0) |
| 5991 | sps_flags = f; |
| 5992 | } |
| 5993 | |
| 5994 | if (sps_flags == 0) |
| 5995 | sps_flags = SPS_BEST; |
| 5996 | |
| 5997 | return OK; |
| 5998 | } |
| 5999 | |
| 6000 | /* Remember what "z?" replaced. */ |
| 6001 | static char_u *repl_from = NULL; |
| 6002 | static char_u *repl_to = NULL; |
| 6003 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6004 | /* |
| 6005 | * "z?": Find badly spelled word under or after the cursor. |
| 6006 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6007 | */ |
| 6008 | void |
| 6009 | spell_suggest() |
| 6010 | { |
| 6011 | char_u *line; |
| 6012 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6013 | char_u wcopy[MAXWLEN + 2]; |
| 6014 | char_u *p; |
| 6015 | int i; |
| 6016 | int c; |
| 6017 | suginfo_T sug; |
| 6018 | suggest_T *stp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6019 | int mouse_used; |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6020 | int need_cap; |
| 6021 | regmatch_T regmatch; |
| 6022 | int endcol; |
| 6023 | char_u *line_copy = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6024 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6025 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6026 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL |
| 6027 | || curwin->w_cursor.col > prev_cursor.col) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6028 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6029 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 6030 | return; |
| 6031 | |
| 6032 | /* No bad word or it starts after the cursor: use the word under the |
| 6033 | * cursor. */ |
| 6034 | curwin->w_cursor = prev_cursor; |
| 6035 | line = ml_get_curline(); |
| 6036 | p = line + curwin->w_cursor.col; |
| 6037 | /* Backup to before start of word. */ |
| 6038 | while (p > line && SPELL_ISWORDP(p)) |
| 6039 | mb_ptr_back(line, p); |
| 6040 | /* Forward to start of word. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6041 | while (*p != NUL && !SPELL_ISWORDP(p)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6042 | mb_ptr_adv(p); |
| 6043 | |
| 6044 | if (!SPELL_ISWORDP(p)) /* No word found. */ |
| 6045 | { |
| 6046 | beep_flush(); |
| 6047 | return; |
| 6048 | } |
| 6049 | curwin->w_cursor.col = p - line; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6052 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6053 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6054 | |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6055 | /* Figure out if the word should be capitalised. */ |
| 6056 | need_cap = FALSE; |
| 6057 | if (curbuf->b_cap_prog != NULL) |
| 6058 | { |
| 6059 | endcol = 0; |
| 6060 | if (skipwhite(line) - line == curwin->w_cursor.col) |
| 6061 | { |
| 6062 | /* At start of line, check if previous line is empty or sentence |
| 6063 | * ends there. */ |
| 6064 | if (curwin->w_cursor.lnum == 1) |
| 6065 | need_cap = TRUE; |
| 6066 | else |
| 6067 | { |
| 6068 | line = ml_get(curwin->w_cursor.lnum - 1); |
| 6069 | if (*skipwhite(line) == NUL) |
| 6070 | need_cap = TRUE; |
| 6071 | else |
| 6072 | { |
| 6073 | /* Append a space in place of the line break. */ |
| 6074 | line_copy = concat_str(line, (char_u *)" "); |
| 6075 | line = line_copy; |
| 6076 | endcol = STRLEN(line); |
| 6077 | } |
| 6078 | } |
| 6079 | } |
| 6080 | else |
| 6081 | endcol = curwin->w_cursor.col; |
| 6082 | |
| 6083 | if (endcol > 0) |
| 6084 | { |
| 6085 | /* Check if sentence ends before the bad word. */ |
| 6086 | regmatch.regprog = curbuf->b_cap_prog; |
| 6087 | regmatch.rm_ic = FALSE; |
| 6088 | p = line + endcol; |
| 6089 | for (;;) |
| 6090 | { |
| 6091 | mb_ptr_back(line, p); |
| 6092 | if (p == line || SPELL_ISWORDP(p)) |
| 6093 | break; |
| 6094 | if (vim_regexec(®match, p, 0) |
| 6095 | && regmatch.endp[0] == line + endcol) |
| 6096 | { |
| 6097 | need_cap = TRUE; |
| 6098 | break; |
| 6099 | } |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | /* get the line again, we may have been using the previous one */ |
| 6104 | line = ml_get_curline(); |
| 6105 | vim_free(line_copy); |
| 6106 | } |
| 6107 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6108 | /* Get the list of suggestions */ |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6109 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, |
| 6110 | TRUE, need_cap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6111 | |
| 6112 | if (sug.su_ga.ga_len == 0) |
| 6113 | MSG(_("Sorry, no suggestions")); |
| 6114 | else |
| 6115 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6116 | vim_free(repl_from); |
| 6117 | repl_from = NULL; |
| 6118 | vim_free(repl_to); |
| 6119 | repl_to = NULL; |
| 6120 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6121 | /* List the suggestions. */ |
| 6122 | msg_start(); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6123 | lines_left = Rows; /* avoid more prompt */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6124 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 6125 | sug.su_badlen, sug.su_badptr); |
| 6126 | msg_puts(IObuff); |
| 6127 | msg_clr_eos(); |
| 6128 | msg_putchar('\n'); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6129 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6130 | msg_scroll = TRUE; |
| 6131 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 6132 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6133 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6134 | |
| 6135 | /* The suggested word may replace only part of the bad word, add |
| 6136 | * the not replaced part. */ |
| 6137 | STRCPY(wcopy, stp->st_word); |
| 6138 | if (sug.su_badlen > stp->st_orglen) |
| 6139 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 6140 | sug.su_badptr + stp->st_orglen, |
| 6141 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6142 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy); |
| 6143 | msg_puts(IObuff); |
| 6144 | |
| 6145 | /* The word may replace more than "su_badlen". */ |
| 6146 | if (sug.su_badlen < stp->st_orglen) |
| 6147 | { |
| 6148 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 6149 | stp->st_orglen, sug.su_badptr); |
| 6150 | msg_puts(IObuff); |
| 6151 | } |
| 6152 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6153 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6154 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6155 | /* Add the score. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6156 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6157 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"), |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6158 | stp->st_salscore ? "s " : "", |
| 6159 | stp->st_score, stp->st_altscore); |
| 6160 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6161 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"), |
| 6162 | stp->st_score); |
| 6163 | msg_advance(30); |
| 6164 | msg_puts(IObuff); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6165 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6166 | msg_putchar('\n'); |
| 6167 | } |
| 6168 | |
| 6169 | /* Ask for choice. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6170 | i = prompt_for_number(&mouse_used); |
| 6171 | if (mouse_used) |
| 6172 | i -= lines_left; |
| 6173 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6174 | 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] | 6175 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6176 | /* Save the from and to text for :spellrepall. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6177 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6178 | repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); |
| 6179 | repl_to = vim_strsave(stp->st_word); |
| 6180 | |
| 6181 | /* Replace the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6182 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 6183 | if (p != NULL) |
| 6184 | { |
| 6185 | c = sug.su_badptr - line; |
| 6186 | mch_memmove(p, line, c); |
| 6187 | STRCPY(p + c, stp->st_word); |
| 6188 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 6189 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 6190 | curwin->w_cursor.col = c; |
| 6191 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6192 | |
| 6193 | /* For redo we use a change-word command. */ |
| 6194 | ResetRedobuff(); |
| 6195 | AppendToRedobuff((char_u *)"ciw"); |
| 6196 | AppendToRedobuff(stp->st_word); |
| 6197 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6198 | } |
| 6199 | } |
| 6200 | else |
| 6201 | curwin->w_cursor = prev_cursor; |
| 6202 | } |
| 6203 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6204 | spell_find_cleanup(&sug); |
| 6205 | } |
| 6206 | |
| 6207 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6208 | * ":spellrepall" |
| 6209 | */ |
| 6210 | /*ARGSUSED*/ |
| 6211 | void |
| 6212 | ex_spellrepall(eap) |
| 6213 | exarg_T *eap; |
| 6214 | { |
| 6215 | pos_T pos = curwin->w_cursor; |
| 6216 | char_u *frompat; |
| 6217 | int addlen; |
| 6218 | char_u *line; |
| 6219 | char_u *p; |
| 6220 | int didone = FALSE; |
| 6221 | int save_ws = p_ws; |
| 6222 | |
| 6223 | if (repl_from == NULL || repl_to == NULL) |
| 6224 | { |
| 6225 | EMSG(_("E752: No previous spell replacement")); |
| 6226 | return; |
| 6227 | } |
| 6228 | addlen = STRLEN(repl_to) - STRLEN(repl_from); |
| 6229 | |
| 6230 | frompat = alloc(STRLEN(repl_from) + 7); |
| 6231 | if (frompat == NULL) |
| 6232 | return; |
| 6233 | sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); |
| 6234 | p_ws = FALSE; |
| 6235 | |
| 6236 | curwin->w_cursor.lnum = 0; |
| 6237 | while (!got_int) |
| 6238 | { |
| 6239 | if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0 |
| 6240 | || u_save_cursor() == FAIL) |
| 6241 | break; |
| 6242 | |
| 6243 | /* Only replace when the right word isn't there yet. This happens |
| 6244 | * when changing "etc" to "etc.". */ |
| 6245 | line = ml_get_curline(); |
| 6246 | if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, |
| 6247 | repl_to, STRLEN(repl_to)) != 0) |
| 6248 | { |
| 6249 | p = alloc(STRLEN(line) + addlen + 1); |
| 6250 | if (p == NULL) |
| 6251 | break; |
| 6252 | mch_memmove(p, line, curwin->w_cursor.col); |
| 6253 | STRCPY(p + curwin->w_cursor.col, repl_to); |
| 6254 | STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); |
| 6255 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 6256 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 6257 | didone = TRUE; |
| 6258 | } |
| 6259 | curwin->w_cursor.col += STRLEN(repl_to); |
| 6260 | } |
| 6261 | |
| 6262 | p_ws = save_ws; |
| 6263 | curwin->w_cursor = pos; |
| 6264 | vim_free(frompat); |
| 6265 | |
| 6266 | if (!didone) |
| 6267 | EMSG2(_("E753: Not found: %s"), repl_from); |
| 6268 | } |
| 6269 | |
| 6270 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6271 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 6272 | * a list of allocated strings. |
| 6273 | */ |
| 6274 | void |
| 6275 | spell_suggest_list(gap, word, maxcount) |
| 6276 | garray_T *gap; |
| 6277 | char_u *word; |
| 6278 | int maxcount; /* maximum nr of suggestions */ |
| 6279 | { |
| 6280 | suginfo_T sug; |
| 6281 | int i; |
| 6282 | suggest_T *stp; |
| 6283 | char_u *wcopy; |
| 6284 | |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6285 | spell_find_suggest(word, &sug, maxcount, FALSE, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6286 | |
| 6287 | /* Make room in "gap". */ |
| 6288 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 6289 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 6290 | return; |
| 6291 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6292 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6293 | { |
| 6294 | stp = &SUG(sug.su_ga, i); |
| 6295 | |
| 6296 | /* The suggested word may replace only part of "word", add the not |
| 6297 | * replaced part. */ |
| 6298 | wcopy = alloc(STRLEN(stp->st_word) |
| 6299 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 6300 | if (wcopy == NULL) |
| 6301 | break; |
| 6302 | STRCPY(wcopy, stp->st_word); |
| 6303 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 6304 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 6305 | } |
| 6306 | |
| 6307 | spell_find_cleanup(&sug); |
| 6308 | } |
| 6309 | |
| 6310 | /* |
| 6311 | * Find spell suggestions for the word at the start of "badptr". |
| 6312 | * Return the suggestions in "su->su_ga". |
| 6313 | * The maximum number of suggestions is "maxcount". |
| 6314 | * Note: does use info for the current window. |
| 6315 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 6316 | */ |
| 6317 | static void |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6318 | spell_find_suggest(badptr, su, maxcount, banbadword, need_cap) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6319 | char_u *badptr; |
| 6320 | suginfo_T *su; |
| 6321 | int maxcount; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6322 | int banbadword; /* don't include badword in suggestions */ |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6323 | int need_cap; /* word should start with capital */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6324 | { |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6325 | int attr = 0; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6326 | char_u buf[MAXPATHL]; |
| 6327 | char_u *p; |
| 6328 | int do_combine = FALSE; |
| 6329 | char_u *sps_copy; |
| 6330 | #ifdef FEAT_EVAL |
| 6331 | static int expr_busy = FALSE; |
| 6332 | #endif |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6333 | int c; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6334 | |
| 6335 | /* |
| 6336 | * Set the info in "*su". |
| 6337 | */ |
| 6338 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 6339 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 6340 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 6341 | if (*badptr == NUL) |
| 6342 | return; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6343 | hash_init(&su->su_banned); |
| 6344 | |
| 6345 | su->su_badptr = badptr; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6346 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6347 | su->su_maxcount = maxcount; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6348 | su->su_maxscore = SCORE_MAXINIT; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6349 | |
| 6350 | if (su->su_badlen >= MAXWLEN) |
| 6351 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 6352 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 6353 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 6354 | su->su_fbadword, MAXWLEN); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6355 | /* get caps flags for bad word */ |
| 6356 | 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] | 6357 | if (need_cap) |
| 6358 | su->su_badflags |= WF_ONECAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6359 | |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6360 | /* If the word is not capitalised and spell_check() doesn't consider the |
| 6361 | * word to be bad then it might need to be capitalised. Add a suggestion |
| 6362 | * for that. */ |
| 6363 | #ifdef FEAT_MBYTE |
| 6364 | c = mb_ptr2char(su->su_badptr); |
| 6365 | #else |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 6366 | c = *su->su_badptr; |
Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 6367 | #endif |
| 6368 | if (!SPELL_ISUPPER(c) && attr == 0) |
| 6369 | { |
| 6370 | make_case_word(su->su_badword, buf, WF_ONECAP); |
| 6371 | add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, |
| 6372 | 0, TRUE); |
| 6373 | } |
| 6374 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6375 | /* Ban the bad word itself. It may appear in another region. */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6376 | if (banbadword) |
| 6377 | add_banned(su, su->su_badword); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6378 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6379 | /* Make a copy of 'spellsuggest', because the expression may change it. */ |
| 6380 | sps_copy = vim_strsave(p_sps); |
| 6381 | if (sps_copy == NULL) |
| 6382 | return; |
| 6383 | |
| 6384 | /* Loop over the items in 'spellsuggest'. */ |
| 6385 | for (p = sps_copy; *p != NUL; ) |
| 6386 | { |
| 6387 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 6388 | |
| 6389 | if (STRNCMP(buf, "expr:", 5) == 0) |
| 6390 | { |
| 6391 | #ifdef FEAT_EVAL |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6392 | /* Evaluate an expression. Skip this when called recursively, |
| 6393 | * when using spellsuggest() in the expression. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6394 | if (!expr_busy) |
| 6395 | { |
| 6396 | expr_busy = TRUE; |
| 6397 | spell_suggest_expr(su, buf + 5); |
| 6398 | expr_busy = FALSE; |
| 6399 | } |
| 6400 | #endif |
| 6401 | } |
| 6402 | else if (STRNCMP(buf, "file:", 5) == 0) |
| 6403 | /* Use list of suggestions in a file. */ |
| 6404 | spell_suggest_file(su, buf + 5); |
| 6405 | else |
| 6406 | { |
| 6407 | /* Use internal method. */ |
| 6408 | spell_suggest_intern(su); |
| 6409 | if (sps_flags & SPS_DOUBLE) |
| 6410 | do_combine = TRUE; |
| 6411 | } |
| 6412 | } |
| 6413 | |
| 6414 | vim_free(sps_copy); |
| 6415 | |
| 6416 | if (do_combine) |
| 6417 | /* Combine the two list of suggestions. This must be done last, |
| 6418 | * because sorting changes the order again. */ |
| 6419 | score_combine(su); |
| 6420 | } |
| 6421 | |
| 6422 | #ifdef FEAT_EVAL |
| 6423 | /* |
| 6424 | * Find suggestions by evaluating expression "expr". |
| 6425 | */ |
| 6426 | static void |
| 6427 | spell_suggest_expr(su, expr) |
| 6428 | suginfo_T *su; |
| 6429 | char_u *expr; |
| 6430 | { |
| 6431 | list_T *list; |
| 6432 | listitem_T *li; |
| 6433 | int score; |
| 6434 | char_u *p; |
| 6435 | |
| 6436 | /* The work is split up in a few parts to avoid having to export |
| 6437 | * suginfo_T. |
| 6438 | * First evaluate the expression and get the resulting list. */ |
| 6439 | list = eval_spell_expr(su->su_badword, expr); |
| 6440 | if (list != NULL) |
| 6441 | { |
| 6442 | /* Loop over the items in the list. */ |
| 6443 | for (li = list->lv_first; li != NULL; li = li->li_next) |
| 6444 | if (li->li_tv.v_type == VAR_LIST) |
| 6445 | { |
| 6446 | /* Get the word and the score from the items. */ |
| 6447 | score = get_spellword(li->li_tv.vval.v_list, &p); |
| 6448 | if (score >= 0) |
| 6449 | add_suggestion(su, &su->su_ga, p, |
| 6450 | su->su_badlen, score, 0, TRUE); |
| 6451 | } |
| 6452 | list_unref(list); |
| 6453 | } |
| 6454 | |
| 6455 | /* Sort the suggestions and truncate at "maxcount". */ |
| 6456 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6457 | } |
| 6458 | #endif |
| 6459 | |
| 6460 | /* |
| 6461 | * Find suggestions a file "fname". |
| 6462 | */ |
| 6463 | static void |
| 6464 | spell_suggest_file(su, fname) |
| 6465 | suginfo_T *su; |
| 6466 | char_u *fname; |
| 6467 | { |
| 6468 | FILE *fd; |
| 6469 | char_u line[MAXWLEN * 2]; |
| 6470 | char_u *p; |
| 6471 | int len; |
| 6472 | char_u cword[MAXWLEN]; |
| 6473 | |
| 6474 | /* Open the file. */ |
| 6475 | fd = mch_fopen((char *)fname, "r"); |
| 6476 | if (fd == NULL) |
| 6477 | { |
| 6478 | EMSG2(_(e_notopen), fname); |
| 6479 | return; |
| 6480 | } |
| 6481 | |
| 6482 | /* Read it line by line. */ |
| 6483 | while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) |
| 6484 | { |
| 6485 | line_breakcheck(); |
| 6486 | |
| 6487 | p = vim_strchr(line, '/'); |
| 6488 | if (p == NULL) |
| 6489 | continue; /* No Tab found, just skip the line. */ |
| 6490 | *p++ = NUL; |
| 6491 | if (STRICMP(su->su_badword, line) == 0) |
| 6492 | { |
| 6493 | /* Match! Isolate the good word, until CR or NL. */ |
| 6494 | for (len = 0; p[len] >= ' '; ++len) |
| 6495 | ; |
| 6496 | p[len] = NUL; |
| 6497 | |
| 6498 | /* If the suggestion doesn't have specific case duplicate the case |
| 6499 | * of the bad word. */ |
| 6500 | if (captype(p, NULL) == 0) |
| 6501 | { |
| 6502 | make_case_word(p, cword, su->su_badflags); |
| 6503 | p = cword; |
| 6504 | } |
| 6505 | |
| 6506 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| 6507 | SCORE_FILE, 0, TRUE); |
| 6508 | } |
| 6509 | } |
| 6510 | |
| 6511 | fclose(fd); |
| 6512 | |
| 6513 | /* Sort the suggestions and truncate at "maxcount". */ |
| 6514 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 6515 | } |
| 6516 | |
| 6517 | /* |
| 6518 | * Find suggestions for the internal method indicated by "sps_flags". |
| 6519 | */ |
| 6520 | static void |
| 6521 | spell_suggest_intern(su) |
| 6522 | suginfo_T *su; |
| 6523 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6524 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6525 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6526 | * |
| 6527 | * Set a maximum score to limit the combination of operations that is |
| 6528 | * tried. |
| 6529 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6530 | suggest_try_special(su); |
| 6531 | |
| 6532 | /* |
| 6533 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 6534 | * from the .aff file and inserting a space (split the word). |
| 6535 | */ |
| 6536 | suggest_try_change(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6537 | |
| 6538 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 6539 | if (sps_flags & SPS_DOUBLE) |
| 6540 | score_comp_sal(su); |
| 6541 | |
| 6542 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6543 | * 3. Try finding sound-a-like words. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6544 | * |
| 6545 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 6546 | * very slow and often doesn't find new suggestions. |
| 6547 | */ |
| 6548 | if ((sps_flags & SPS_DOUBLE) |
| 6549 | || (!(sps_flags & SPS_FAST) |
| 6550 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 6551 | { |
| 6552 | /* Allow a higher score now. */ |
| 6553 | su->su_maxscore = SCORE_MAXMAX; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6554 | suggest_try_soundalike(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | /* When CTRL-C was hit while searching do show the results. */ |
| 6558 | ui_breakcheck(); |
| 6559 | if (got_int) |
| 6560 | { |
| 6561 | (void)vgetc(); |
| 6562 | got_int = FALSE; |
| 6563 | } |
| 6564 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6565 | if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6566 | { |
| 6567 | if (sps_flags & SPS_BEST) |
| 6568 | /* Adjust the word score for how it sounds like. */ |
| 6569 | rescore_suggestions(su); |
| 6570 | |
| 6571 | /* Sort the suggestions and truncate at "maxcount". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6572 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | /* |
| 6577 | * Free the info put in "*su" by spell_find_suggest(). |
| 6578 | */ |
| 6579 | static void |
| 6580 | spell_find_cleanup(su) |
| 6581 | suginfo_T *su; |
| 6582 | { |
| 6583 | int i; |
| 6584 | |
| 6585 | /* Free the suggestions. */ |
| 6586 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6587 | vim_free(SUG(su->su_ga, i).st_word); |
| 6588 | ga_clear(&su->su_ga); |
| 6589 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6590 | vim_free(SUG(su->su_sga, i).st_word); |
| 6591 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6592 | |
| 6593 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6594 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
| 6597 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6598 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 6599 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 6600 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6601 | */ |
| 6602 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6603 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6604 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6605 | char_u *wcopy; |
| 6606 | int upper; /* TRUE: first letter made upper case */ |
| 6607 | { |
| 6608 | char_u *p; |
| 6609 | int c; |
| 6610 | int l; |
| 6611 | |
| 6612 | p = word; |
| 6613 | #ifdef FEAT_MBYTE |
| 6614 | if (has_mbyte) |
| 6615 | c = mb_ptr2char_adv(&p); |
| 6616 | else |
| 6617 | #endif |
| 6618 | c = *p++; |
| 6619 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6620 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6621 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6622 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6623 | #ifdef FEAT_MBYTE |
| 6624 | if (has_mbyte) |
| 6625 | l = mb_char2bytes(c, wcopy); |
| 6626 | else |
| 6627 | #endif |
| 6628 | { |
| 6629 | l = 1; |
| 6630 | wcopy[0] = c; |
| 6631 | } |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6632 | vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
| 6635 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6636 | * Make a copy of "word" with all the letters upper cased into |
| 6637 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6638 | */ |
| 6639 | static void |
| 6640 | allcap_copy(word, wcopy) |
| 6641 | char_u *word; |
| 6642 | char_u *wcopy; |
| 6643 | { |
| 6644 | char_u *s; |
| 6645 | char_u *d; |
| 6646 | int c; |
| 6647 | |
| 6648 | d = wcopy; |
| 6649 | for (s = word; *s != NUL; ) |
| 6650 | { |
| 6651 | #ifdef FEAT_MBYTE |
| 6652 | if (has_mbyte) |
| 6653 | c = mb_ptr2char_adv(&s); |
| 6654 | else |
| 6655 | #endif |
| 6656 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6657 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6658 | |
| 6659 | #ifdef FEAT_MBYTE |
| 6660 | if (has_mbyte) |
| 6661 | { |
| 6662 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 6663 | break; |
| 6664 | d += mb_char2bytes(c, d); |
| 6665 | } |
| 6666 | else |
| 6667 | #endif |
| 6668 | { |
| 6669 | if (d - wcopy >= MAXWLEN - 1) |
| 6670 | break; |
| 6671 | *d++ = c; |
| 6672 | } |
| 6673 | } |
| 6674 | *d = NUL; |
| 6675 | } |
| 6676 | |
| 6677 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6678 | * Try finding suggestions by recognizing specific situations. |
| 6679 | */ |
| 6680 | static void |
| 6681 | suggest_try_special(su) |
| 6682 | suginfo_T *su; |
| 6683 | { |
| 6684 | char_u *p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6685 | size_t len; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6686 | int c; |
| 6687 | char_u word[MAXWLEN]; |
| 6688 | |
| 6689 | /* |
| 6690 | * Recognize a word that is repeated: "the the". |
| 6691 | */ |
| 6692 | p = skiptowhite(su->su_fbadword); |
| 6693 | len = p - su->su_fbadword; |
| 6694 | p = skipwhite(p); |
| 6695 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 6696 | { |
| 6697 | /* Include badflags: if the badword is onecap or allcap |
| 6698 | * use that for the goodword too: "The the" -> "The". */ |
| 6699 | c = su->su_fbadword[len]; |
| 6700 | su->su_fbadword[len] = NUL; |
| 6701 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 6702 | su->su_fbadword[len] = c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6703 | 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] | 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6708 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6709 | * |
| 6710 | * This uses a state machine. At each node in the tree we try various |
| 6711 | * operations. When trying if an operation work "depth" is increased and the |
| 6712 | * stack[] is used to store info. This allows combinations, thus insert one |
| 6713 | * character, replace one and delete another. The number of changes is |
| 6714 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6715 | */ |
| 6716 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6717 | suggest_try_change(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6718 | suginfo_T *su; |
| 6719 | { |
| 6720 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 6721 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 6722 | trystate_T stack[MAXWLEN]; |
| 6723 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 6724 | * to for word split) */ |
| 6725 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 6726 | int splitoff = 0; /* index in tword after last split */ |
| 6727 | trystate_T *sp; |
| 6728 | int newscore; |
| 6729 | langp_T *lp; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6730 | char_u *byts, *fbyts, *pbyts; |
| 6731 | idx_T *idxs, *fidxs, *pidxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6732 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6733 | int c, c2, c3; |
| 6734 | int n = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6735 | int flags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6736 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6737 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6738 | int len; |
| 6739 | char_u *p; |
| 6740 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6741 | int fl = 0, tl; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6742 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6743 | |
| 6744 | /* 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] | 6745 | * to find matches (esp. REP items). Append some more text, changing |
| 6746 | * chars after the bad word may help. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6747 | STRCPY(fword, su->su_fbadword); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6748 | n = STRLEN(fword); |
| 6749 | p = su->su_badptr + su->su_badlen; |
| 6750 | (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6751 | |
| 6752 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6753 | lp->lp_slang != NULL; ++lp) |
| 6754 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6755 | /* |
| 6756 | * Go through the whole case-fold tree, try changes at each node. |
| 6757 | * "tword[]" contains the word collected from nodes in the tree. |
| 6758 | * "fword[]" the word we are trying to match with (initially the bad |
| 6759 | * word). |
| 6760 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6761 | depth = 0; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6762 | sp = &stack[0]; |
| 6763 | sp->ts_state = STATE_START; |
| 6764 | sp->ts_score = 0; |
| 6765 | sp->ts_curi = 1; |
| 6766 | sp->ts_fidx = 0; |
| 6767 | sp->ts_fidxtry = 0; |
| 6768 | sp->ts_twordlen = 0; |
| 6769 | sp->ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6770 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6771 | sp->ts_tcharlen = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6772 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6773 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6774 | /* |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6775 | * When there are postponed prefixes we need to use these first. At |
| 6776 | * the end of the prefix we continue in the case-fold tree. |
| 6777 | */ |
| 6778 | fbyts = lp->lp_slang->sl_fbyts; |
| 6779 | fidxs = lp->lp_slang->sl_fidxs; |
| 6780 | pbyts = lp->lp_slang->sl_pbyts; |
| 6781 | pidxs = lp->lp_slang->sl_pidxs; |
| 6782 | if (pbyts != NULL) |
| 6783 | { |
| 6784 | byts = pbyts; |
| 6785 | idxs = pidxs; |
| 6786 | sp->ts_prefixdepth = PREFIXTREE; |
| 6787 | sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
| 6788 | } |
| 6789 | else |
| 6790 | { |
| 6791 | byts = fbyts; |
| 6792 | idxs = fidxs; |
| 6793 | sp->ts_prefixdepth = NOPREFIX; |
| 6794 | } |
| 6795 | |
| 6796 | /* |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6797 | * Loop to find all suggestions. At each round we either: |
| 6798 | * - For the current state try one operation, advance "ts_curi", |
| 6799 | * increase "depth". |
| 6800 | * - When a state is done go to the next, set "ts_state". |
| 6801 | * - When all states are tried decrease "depth". |
| 6802 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6803 | while (depth >= 0 && !got_int) |
| 6804 | { |
| 6805 | sp = &stack[depth]; |
| 6806 | switch (sp->ts_state) |
| 6807 | { |
| 6808 | case STATE_START: |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6809 | case STATE_NOPREFIX: |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6810 | /* |
| 6811 | * Start of node: Deal with NUL bytes, which means |
| 6812 | * tword[] may end here. |
| 6813 | */ |
| 6814 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 6815 | len = byts[arridx]; /* bytes in this node */ |
| 6816 | arridx += sp->ts_curi; /* index of current byte */ |
| 6817 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6818 | if (sp->ts_prefixdepth == PREFIXTREE) |
| 6819 | { |
| 6820 | /* Skip over the NUL bytes, we use them later. */ |
| 6821 | for (n = 0; n < len && byts[arridx + n] == 0; ++n) |
| 6822 | ; |
| 6823 | sp->ts_curi += n; |
| 6824 | |
| 6825 | /* At end of a prefix or at start of prefixtree: check for |
| 6826 | * following word. */ |
| 6827 | if (byts[arridx] == 0 || sp->ts_state == STATE_NOPREFIX) |
| 6828 | { |
| 6829 | sp->ts_state = STATE_START; |
| 6830 | ++depth; |
| 6831 | stack[depth] = stack[depth - 1]; |
| 6832 | sp = &stack[depth]; |
| 6833 | sp->ts_prefixdepth = depth - 1; |
| 6834 | byts = fbyts; |
| 6835 | idxs = fidxs; |
| 6836 | sp->ts_state = STATE_START; |
| 6837 | sp->ts_curi = 1; /* start just after length byte */ |
| 6838 | sp->ts_arridx = 0; |
| 6839 | |
| 6840 | /* Move the prefix to preword[] so that |
| 6841 | * find_keepcap_word() works. */ |
| 6842 | prewordlen = splitoff = sp->ts_twordlen; |
| 6843 | mch_memmove(preword, tword, splitoff); |
| 6844 | break; |
| 6845 | } |
| 6846 | |
| 6847 | /* Always past NUL bytes now. */ |
| 6848 | sp->ts_state = STATE_ENDNUL; |
| 6849 | break; |
| 6850 | } |
| 6851 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6852 | if (sp->ts_curi > len || byts[arridx] != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6853 | { |
| 6854 | /* Past bytes in node and/or past NUL bytes. */ |
| 6855 | sp->ts_state = STATE_ENDNUL; |
| 6856 | break; |
| 6857 | } |
| 6858 | |
| 6859 | /* |
| 6860 | * End of word in tree. |
| 6861 | */ |
| 6862 | ++sp->ts_curi; /* eat one NUL byte */ |
| 6863 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6864 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6865 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6866 | if (sp->ts_prefixdepth < MAXWLEN) |
| 6867 | { |
| 6868 | /* There was a prefix before the word. Check that the |
| 6869 | * prefix can be used with this word. */ |
| 6870 | /* Count the length of the NULs in the prefix. If there |
| 6871 | * are none this must be the first try without a prefix. |
| 6872 | */ |
| 6873 | n = stack[sp->ts_prefixdepth].ts_arridx; |
| 6874 | len = pbyts[n++]; |
| 6875 | for (c = 0; c < len && pbyts[n + c] == 0; ++c) |
| 6876 | ; |
| 6877 | if (c > 0) |
| 6878 | { |
| 6879 | /* The prefix ID is stored two bytes above the flags. */ |
| 6880 | c = valid_word_prefix(c, n, (unsigned)flags >> 16, |
| 6881 | tword + splitoff, lp->lp_slang); |
| 6882 | if (c == 0) |
| 6883 | break; |
| 6884 | |
| 6885 | /* Use the WF_RARE flag for a rare prefix. */ |
| 6886 | if (c & WF_RAREPFX) |
| 6887 | flags |= WF_RARE; |
| 6888 | } |
| 6889 | } |
| 6890 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6891 | /* |
| 6892 | * Form the word with proper case in preword. |
| 6893 | * If there is a word from a previous split, append. |
| 6894 | */ |
| 6895 | tword[sp->ts_twordlen] = NUL; |
| 6896 | if (flags & WF_KEEPCAP) |
| 6897 | /* Must find the word in the keep-case tree. */ |
| 6898 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 6899 | preword + prewordlen); |
| 6900 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6901 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6902 | /* Include badflags: if the badword is onecap or allcap |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6903 | * use that for the goodword too. But if the badword is |
| 6904 | * allcap and it's only one char long use onecap. */ |
| 6905 | c = su->su_badflags; |
| 6906 | if ((c & WF_ALLCAP) |
| 6907 | #ifdef FEAT_MBYTE |
| 6908 | && su->su_badlen == mb_ptr2len_check(su->su_badptr) |
| 6909 | #else |
| 6910 | && su->su_badlen == 1 |
| 6911 | #endif |
| 6912 | ) |
| 6913 | c = WF_ONECAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6914 | make_case_word(tword + splitoff, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6915 | preword + prewordlen, flags | c); |
| 6916 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6917 | |
| 6918 | /* Don't use a banned word. It may appear again as a good |
| 6919 | * word, thus remember it. */ |
| 6920 | if (flags & WF_BANNED) |
| 6921 | { |
| 6922 | add_banned(su, preword + prewordlen); |
| 6923 | break; |
| 6924 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6925 | if (was_banned(su, preword + prewordlen) |
| 6926 | || was_banned(su, preword)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6927 | break; |
| 6928 | |
| 6929 | newscore = 0; |
| 6930 | if ((flags & WF_REGION) |
| 6931 | && (((unsigned)flags >> 8) & lp->lp_region) == 0) |
| 6932 | newscore += SCORE_REGION; |
| 6933 | if (flags & WF_RARE) |
| 6934 | newscore += SCORE_RARE; |
| 6935 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6936 | if (!spell_valid_case(su->su_badflags, |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6937 | captype(preword + prewordlen, NULL))) |
| 6938 | newscore += SCORE_ICASE; |
| 6939 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6940 | if ((fword[sp->ts_fidx] == NUL |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6941 | || !spell_iswordp(fword + sp->ts_fidx, curbuf)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6942 | && sp->ts_fidx >= sp->ts_fidxtry) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6943 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6944 | /* The badword also ends: add suggestions. Give a penalty |
| 6945 | * when changing non-word char to word char, e.g., "thes," |
| 6946 | * -> "these". */ |
| 6947 | p = fword + sp->ts_fidx; |
| 6948 | #ifdef FEAT_MBYTE |
| 6949 | if (has_mbyte) |
| 6950 | mb_ptr_back(fword, p); |
| 6951 | else |
| 6952 | #endif |
| 6953 | --p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6954 | if (!spell_iswordp(p, curbuf)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6955 | { |
| 6956 | p = preword + STRLEN(preword); |
| 6957 | #ifdef FEAT_MBYTE |
| 6958 | if (has_mbyte) |
| 6959 | mb_ptr_back(preword, p); |
| 6960 | else |
| 6961 | #endif |
| 6962 | --p; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6963 | if (spell_iswordp(p, curbuf)) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6964 | newscore += SCORE_NONWORD; |
| 6965 | } |
| 6966 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6967 | add_suggestion(su, &su->su_ga, preword, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6968 | sp->ts_fidx - repextra, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6969 | sp->ts_score + newscore, 0, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6970 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6971 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 6972 | #ifdef FEAT_MBYTE |
| 6973 | /* Don't split halfway a character. */ |
| 6974 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 6975 | #endif |
| 6976 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6977 | { |
| 6978 | /* The word in the tree ends but the badword |
| 6979 | * continues: try inserting a space and check that a valid |
| 6980 | * words starts at fword[sp->ts_fidx]. */ |
| 6981 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 6982 | { |
| 6983 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 6984 | sp->ts_save_prewordlen = prewordlen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6985 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6986 | sp->ts_save_splitoff = splitoff; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6987 | sp->ts_state = STATE_SPLITUNDO; |
| 6988 | |
| 6989 | ++depth; |
| 6990 | sp = &stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6991 | |
| 6992 | /* Append a space to preword. */ |
| 6993 | STRCAT(preword, " "); |
| 6994 | prewordlen = STRLEN(preword); |
| 6995 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 6996 | |
| 6997 | /* If the badword has a non-word character at this |
| 6998 | * position skip it. That means replacing the |
| 6999 | * non-word character with a space. */ |
| 7000 | if (!spell_iswordp_nmw(fword + sp->ts_fidx)) |
| 7001 | { |
| 7002 | sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; |
| 7003 | #ifdef FEAT_MBYTE |
| 7004 | if (has_mbyte) |
| 7005 | sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
| 7006 | else |
| 7007 | #endif |
| 7008 | ++sp->ts_fidx; |
| 7009 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7010 | #ifdef FEAT_MBYTE |
| 7011 | if (has_mbyte) |
| 7012 | { |
| 7013 | int i = 0; |
| 7014 | |
| 7015 | /* Case-folding may change the number of bytes: |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7016 | * Count nr of chars in fword[ts_fidx] and |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7017 | * advance that many chars in su->su_badptr. */ |
| 7018 | for (p = fword; p < fword + sp->ts_fidx; |
| 7019 | mb_ptr_adv(p)) |
| 7020 | ++i; |
| 7021 | for (p = su->su_badptr; i > 0; mb_ptr_adv(p)) |
| 7022 | --i; |
| 7023 | } |
| 7024 | else |
| 7025 | #endif |
| 7026 | p = su->su_badptr + sp->ts_fidx; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7027 | su->su_badflags = captype(p, su->su_badptr |
| 7028 | + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7029 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7030 | /* Restart at top of the tree. */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 7031 | sp->ts_arridx = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7032 | } |
| 7033 | } |
| 7034 | break; |
| 7035 | |
| 7036 | case STATE_SPLITUNDO: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7037 | /* Undo the changes done for word split. */ |
| 7038 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7039 | splitoff = sp->ts_save_splitoff; |
| 7040 | prewordlen = sp->ts_save_prewordlen; |
| 7041 | |
| 7042 | /* Continue looking for NUL bytes. */ |
| 7043 | sp->ts_state = STATE_START; |
| 7044 | break; |
| 7045 | |
| 7046 | case STATE_ENDNUL: |
| 7047 | /* Past the NUL bytes in the node. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7048 | if (fword[sp->ts_fidx] == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7049 | { |
| 7050 | /* The badword ends, can't use the bytes in this node. */ |
| 7051 | sp->ts_state = STATE_DEL; |
| 7052 | break; |
| 7053 | } |
| 7054 | sp->ts_state = STATE_PLAIN; |
| 7055 | /*FALLTHROUGH*/ |
| 7056 | |
| 7057 | case STATE_PLAIN: |
| 7058 | /* |
| 7059 | * Go over all possible bytes at this node, add each to |
| 7060 | * tword[] and use child node. "ts_curi" is the index. |
| 7061 | */ |
| 7062 | arridx = sp->ts_arridx; |
| 7063 | if (sp->ts_curi > byts[arridx]) |
| 7064 | { |
| 7065 | /* Done all bytes at this node, do next state. When still |
| 7066 | * at already changed bytes skip the other tricks. */ |
| 7067 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 7068 | sp->ts_state = STATE_DEL; |
| 7069 | else |
| 7070 | sp->ts_state = STATE_FINAL; |
| 7071 | } |
| 7072 | else |
| 7073 | { |
| 7074 | arridx += sp->ts_curi++; |
| 7075 | c = byts[arridx]; |
| 7076 | |
| 7077 | /* Normal byte, go one level deeper. If it's not equal to |
| 7078 | * the byte in the bad word adjust the score. But don't |
| 7079 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7080 | if (c == fword[sp->ts_fidx] |
| 7081 | #ifdef FEAT_MBYTE |
| 7082 | || (sp->ts_tcharlen > 0 |
| 7083 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7084 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7085 | ) |
| 7086 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7087 | else |
| 7088 | newscore = SCORE_SUBST; |
| 7089 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 7090 | && try_deeper(su, stack, depth, newscore)) |
| 7091 | { |
| 7092 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7093 | sp = &stack[depth]; |
| 7094 | ++sp->ts_fidx; |
| 7095 | tword[sp->ts_twordlen++] = c; |
| 7096 | sp->ts_arridx = idxs[arridx]; |
| 7097 | #ifdef FEAT_MBYTE |
| 7098 | if (newscore == SCORE_SUBST) |
| 7099 | sp->ts_isdiff = DIFF_YES; |
| 7100 | if (has_mbyte) |
| 7101 | { |
| 7102 | /* Multi-byte characters are a bit complicated to |
| 7103 | * handle: They differ when any of the bytes |
| 7104 | * differ and then their length may also differ. */ |
| 7105 | if (sp->ts_tcharlen == 0) |
| 7106 | { |
| 7107 | /* First byte. */ |
| 7108 | sp->ts_tcharidx = 0; |
| 7109 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 7110 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 7111 | sp->ts_isdiff = (newscore != 0) |
| 7112 | ? DIFF_YES : DIFF_NONE; |
| 7113 | } |
| 7114 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 7115 | /* When inserting trail bytes don't advance in |
| 7116 | * the bad word. */ |
| 7117 | --sp->ts_fidx; |
| 7118 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 7119 | { |
| 7120 | /* Last byte of character. */ |
| 7121 | if (sp->ts_isdiff == DIFF_YES) |
| 7122 | { |
| 7123 | /* Correct ts_fidx for the byte length of |
| 7124 | * the character (we didn't check that |
| 7125 | * before). */ |
| 7126 | sp->ts_fidx = sp->ts_fcharstart |
| 7127 | + MB_BYTE2LEN( |
| 7128 | fword[sp->ts_fcharstart]); |
| 7129 | |
| 7130 | /* For a similar character adjust score |
| 7131 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 7132 | if (lp->lp_slang->sl_has_map |
| 7133 | && similar_chars(lp->lp_slang, |
| 7134 | mb_ptr2char(tword |
| 7135 | + sp->ts_twordlen |
| 7136 | - sp->ts_tcharlen), |
| 7137 | mb_ptr2char(fword |
| 7138 | + sp->ts_fcharstart))) |
| 7139 | sp->ts_score -= |
| 7140 | SCORE_SUBST - SCORE_SIMILAR; |
| 7141 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7142 | else if (sp->ts_isdiff == DIFF_INSERT |
| 7143 | && sp->ts_twordlen > sp->ts_tcharlen) |
| 7144 | { |
| 7145 | /* If the previous character was the same, |
| 7146 | * thus doubling a character, give a bonus |
| 7147 | * to the score. */ |
| 7148 | p = tword + sp->ts_twordlen |
| 7149 | - sp->ts_tcharlen; |
| 7150 | c = mb_ptr2char(p); |
| 7151 | mb_ptr_back(tword, p); |
| 7152 | if (c == mb_ptr2char(p)) |
| 7153 | sp->ts_score -= SCORE_INS |
| 7154 | - SCORE_INSDUP; |
| 7155 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7156 | |
| 7157 | /* Starting a new char, reset the length. */ |
| 7158 | sp->ts_tcharlen = 0; |
| 7159 | } |
| 7160 | } |
| 7161 | else |
| 7162 | #endif |
| 7163 | { |
| 7164 | /* If we found a similar char adjust the score. |
| 7165 | * We do this after calling try_deeper() because |
| 7166 | * it's slow. */ |
| 7167 | if (newscore != 0 |
| 7168 | && lp->lp_slang->sl_has_map |
| 7169 | && similar_chars(lp->lp_slang, |
| 7170 | c, fword[sp->ts_fidx - 1])) |
| 7171 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 7172 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7173 | } |
| 7174 | } |
| 7175 | break; |
| 7176 | |
| 7177 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7178 | #ifdef FEAT_MBYTE |
| 7179 | /* When past the first byte of a multi-byte char don't try |
| 7180 | * delete/insert/swap a character. */ |
| 7181 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 7182 | { |
| 7183 | sp->ts_state = STATE_FINAL; |
| 7184 | break; |
| 7185 | } |
| 7186 | #endif |
| 7187 | /* |
| 7188 | * Try skipping one character in the bad word (delete it). |
| 7189 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7190 | sp->ts_state = STATE_INS; |
| 7191 | sp->ts_curi = 1; |
| 7192 | if (fword[sp->ts_fidx] != NUL |
| 7193 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 7194 | { |
| 7195 | ++depth; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7196 | |
| 7197 | /* Advance over the character in fword[]. Give a bonus to |
| 7198 | * the score if the same character is following "nn" -> |
| 7199 | * "n". */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7200 | #ifdef FEAT_MBYTE |
| 7201 | if (has_mbyte) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7202 | { |
| 7203 | c = mb_ptr2char(fword + sp->ts_fidx); |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7204 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7205 | if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) |
| 7206 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 7207 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7208 | else |
| 7209 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7210 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7211 | ++stack[depth].ts_fidx; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7212 | if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) |
| 7213 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 7214 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7215 | break; |
| 7216 | } |
| 7217 | /*FALLTHROUGH*/ |
| 7218 | |
| 7219 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7220 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7221 | * node. */ |
| 7222 | n = sp->ts_arridx; |
| 7223 | if (sp->ts_curi > byts[n]) |
| 7224 | { |
| 7225 | /* Done all bytes at this node, do next state. */ |
| 7226 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7227 | } |
| 7228 | else |
| 7229 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7230 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7231 | n += sp->ts_curi++; |
| 7232 | c = byts[n]; |
| 7233 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 7234 | { |
| 7235 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7236 | sp = &stack[depth]; |
| 7237 | tword[sp->ts_twordlen++] = c; |
| 7238 | sp->ts_arridx = idxs[n]; |
| 7239 | #ifdef FEAT_MBYTE |
| 7240 | if (has_mbyte) |
| 7241 | { |
| 7242 | fl = MB_BYTE2LEN(c); |
| 7243 | if (fl > 1) |
| 7244 | { |
| 7245 | /* There are following bytes for the same |
| 7246 | * character. We must find all bytes before |
| 7247 | * trying delete/insert/swap/etc. */ |
| 7248 | sp->ts_tcharlen = fl; |
| 7249 | sp->ts_tcharidx = 1; |
| 7250 | sp->ts_isdiff = DIFF_INSERT; |
| 7251 | } |
| 7252 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7253 | else |
| 7254 | fl = 1; |
| 7255 | if (fl == 1) |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7256 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7257 | { |
| 7258 | /* If the previous character was the same, thus |
| 7259 | * doubling a character, give a bonus to the |
| 7260 | * score. */ |
| 7261 | if (sp->ts_twordlen >= 2 |
| 7262 | && tword[sp->ts_twordlen - 2] == c) |
| 7263 | sp->ts_score -= SCORE_INS - SCORE_INSDUP; |
| 7264 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7265 | } |
| 7266 | } |
| 7267 | break; |
| 7268 | |
| 7269 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7270 | /* |
| 7271 | * Swap two bytes in the bad word: "12" -> "21". |
| 7272 | * We change "fword" here, it's changed back afterwards. |
| 7273 | */ |
| 7274 | p = fword + sp->ts_fidx; |
| 7275 | c = *p; |
| 7276 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7277 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7278 | /* End of word, can't swap or replace. */ |
| 7279 | sp->ts_state = STATE_FINAL; |
| 7280 | break; |
| 7281 | } |
| 7282 | #ifdef FEAT_MBYTE |
| 7283 | if (has_mbyte) |
| 7284 | { |
| 7285 | n = mb_ptr2len_check(p); |
| 7286 | c = mb_ptr2char(p); |
| 7287 | c2 = mb_ptr2char(p + n); |
| 7288 | } |
| 7289 | else |
| 7290 | #endif |
| 7291 | c2 = p[1]; |
| 7292 | if (c == c2) |
| 7293 | { |
| 7294 | /* Characters are identical, swap won't do anything. */ |
| 7295 | sp->ts_state = STATE_SWAP3; |
| 7296 | break; |
| 7297 | } |
| 7298 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 7299 | { |
| 7300 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7301 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7302 | #ifdef FEAT_MBYTE |
| 7303 | if (has_mbyte) |
| 7304 | { |
| 7305 | fl = mb_char2len(c2); |
| 7306 | mch_memmove(p, p + n, fl); |
| 7307 | mb_char2bytes(c, p + fl); |
| 7308 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 7309 | } |
| 7310 | else |
| 7311 | #endif |
| 7312 | { |
| 7313 | p[0] = c2; |
| 7314 | p[1] = c; |
| 7315 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 7316 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7317 | } |
| 7318 | else |
| 7319 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 7320 | sp->ts_state = STATE_REP_INI; |
| 7321 | break; |
| 7322 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7323 | case STATE_UNSWAP: |
| 7324 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 7325 | p = fword + sp->ts_fidx; |
| 7326 | #ifdef FEAT_MBYTE |
| 7327 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7328 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7329 | n = MB_BYTE2LEN(*p); |
| 7330 | c = mb_ptr2char(p + n); |
| 7331 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 7332 | mb_char2bytes(c, p); |
| 7333 | } |
| 7334 | else |
| 7335 | #endif |
| 7336 | { |
| 7337 | c = *p; |
| 7338 | *p = p[1]; |
| 7339 | p[1] = c; |
| 7340 | } |
| 7341 | /*FALLTHROUGH*/ |
| 7342 | |
| 7343 | case STATE_SWAP3: |
| 7344 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 7345 | * "fword" here, it's changed back afterwards. */ |
| 7346 | p = fword + sp->ts_fidx; |
| 7347 | #ifdef FEAT_MBYTE |
| 7348 | if (has_mbyte) |
| 7349 | { |
| 7350 | n = mb_ptr2len_check(p); |
| 7351 | c = mb_ptr2char(p); |
| 7352 | fl = mb_ptr2len_check(p + n); |
| 7353 | c2 = mb_ptr2char(p + n); |
| 7354 | c3 = mb_ptr2char(p + n + fl); |
| 7355 | } |
| 7356 | else |
| 7357 | #endif |
| 7358 | { |
| 7359 | c = *p; |
| 7360 | c2 = p[1]; |
| 7361 | c3 = p[2]; |
| 7362 | } |
| 7363 | |
| 7364 | /* When characters are identical: "121" then SWAP3 result is |
| 7365 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 7366 | * result is same as SWAP on next char: "112". Thus skip all |
| 7367 | * swapping. Also skip when c3 is NUL. */ |
| 7368 | if (c == c3 || c3 == NUL) |
| 7369 | { |
| 7370 | sp->ts_state = STATE_REP_INI; |
| 7371 | break; |
| 7372 | } |
| 7373 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7374 | { |
| 7375 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7376 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7377 | #ifdef FEAT_MBYTE |
| 7378 | if (has_mbyte) |
| 7379 | { |
| 7380 | tl = mb_char2len(c3); |
| 7381 | mch_memmove(p, p + n + fl, tl); |
| 7382 | mb_char2bytes(c2, p + tl); |
| 7383 | mb_char2bytes(c, p + fl + tl); |
| 7384 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 7385 | } |
| 7386 | else |
| 7387 | #endif |
| 7388 | { |
| 7389 | p[0] = p[2]; |
| 7390 | p[2] = c; |
| 7391 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7392 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7393 | } |
| 7394 | else |
| 7395 | sp->ts_state = STATE_REP_INI; |
| 7396 | break; |
| 7397 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7398 | case STATE_UNSWAP3: |
| 7399 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 7400 | p = fword + sp->ts_fidx; |
| 7401 | #ifdef FEAT_MBYTE |
| 7402 | if (has_mbyte) |
| 7403 | { |
| 7404 | n = MB_BYTE2LEN(*p); |
| 7405 | c2 = mb_ptr2char(p + n); |
| 7406 | fl = MB_BYTE2LEN(p[n]); |
| 7407 | c = mb_ptr2char(p + n + fl); |
| 7408 | tl = MB_BYTE2LEN(p[n + fl]); |
| 7409 | mch_memmove(p + fl + tl, p, n); |
| 7410 | mb_char2bytes(c, p); |
| 7411 | mb_char2bytes(c2, p + tl); |
| 7412 | } |
| 7413 | else |
| 7414 | #endif |
| 7415 | { |
| 7416 | c = *p; |
| 7417 | *p = p[2]; |
| 7418 | p[2] = c; |
| 7419 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7420 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7421 | /* Rotate three characters left: "123" -> "231". We change |
| 7422 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7423 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7424 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7425 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7426 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7427 | p = fword + sp->ts_fidx; |
| 7428 | #ifdef FEAT_MBYTE |
| 7429 | if (has_mbyte) |
| 7430 | { |
| 7431 | n = mb_ptr2len_check(p); |
| 7432 | c = mb_ptr2char(p); |
| 7433 | fl = mb_ptr2len_check(p + n); |
| 7434 | fl += mb_ptr2len_check(p + n + fl); |
| 7435 | mch_memmove(p, p + n, fl); |
| 7436 | mb_char2bytes(c, p + fl); |
| 7437 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 7438 | } |
| 7439 | else |
| 7440 | #endif |
| 7441 | { |
| 7442 | c = *p; |
| 7443 | *p = p[1]; |
| 7444 | p[1] = p[2]; |
| 7445 | p[2] = c; |
| 7446 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7447 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7448 | } |
| 7449 | else |
| 7450 | sp->ts_state = STATE_REP_INI; |
| 7451 | break; |
| 7452 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7453 | case STATE_UNROT3L: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7454 | /* Undo ROT3L: "231" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7455 | p = fword + sp->ts_fidx; |
| 7456 | #ifdef FEAT_MBYTE |
| 7457 | if (has_mbyte) |
| 7458 | { |
| 7459 | n = MB_BYTE2LEN(*p); |
| 7460 | n += MB_BYTE2LEN(p[n]); |
| 7461 | c = mb_ptr2char(p + n); |
| 7462 | tl = MB_BYTE2LEN(p[n]); |
| 7463 | mch_memmove(p + tl, p, n); |
| 7464 | mb_char2bytes(c, p); |
| 7465 | } |
| 7466 | else |
| 7467 | #endif |
| 7468 | { |
| 7469 | c = p[2]; |
| 7470 | p[2] = p[1]; |
| 7471 | p[1] = *p; |
| 7472 | *p = c; |
| 7473 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7474 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7475 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7476 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7477 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 7478 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7479 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7480 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7481 | p = fword + sp->ts_fidx; |
| 7482 | #ifdef FEAT_MBYTE |
| 7483 | if (has_mbyte) |
| 7484 | { |
| 7485 | n = mb_ptr2len_check(p); |
| 7486 | n += mb_ptr2len_check(p + n); |
| 7487 | c = mb_ptr2char(p + n); |
| 7488 | tl = mb_ptr2len_check(p + n); |
| 7489 | mch_memmove(p + tl, p, n); |
| 7490 | mb_char2bytes(c, p); |
| 7491 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 7492 | } |
| 7493 | else |
| 7494 | #endif |
| 7495 | { |
| 7496 | c = p[2]; |
| 7497 | p[2] = p[1]; |
| 7498 | p[1] = *p; |
| 7499 | *p = c; |
| 7500 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 7501 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7502 | } |
| 7503 | else |
| 7504 | sp->ts_state = STATE_REP_INI; |
| 7505 | break; |
| 7506 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7507 | case STATE_UNROT3R: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7508 | /* Undo ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7509 | p = fword + sp->ts_fidx; |
| 7510 | #ifdef FEAT_MBYTE |
| 7511 | if (has_mbyte) |
| 7512 | { |
| 7513 | c = mb_ptr2char(p); |
| 7514 | tl = MB_BYTE2LEN(*p); |
| 7515 | n = MB_BYTE2LEN(p[tl]); |
| 7516 | n += MB_BYTE2LEN(p[tl + n]); |
| 7517 | mch_memmove(p, p + tl, n); |
| 7518 | mb_char2bytes(c, p + n); |
| 7519 | } |
| 7520 | else |
| 7521 | #endif |
| 7522 | { |
| 7523 | c = *p; |
| 7524 | *p = p[1]; |
| 7525 | p[1] = p[2]; |
| 7526 | p[2] = c; |
| 7527 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7528 | /*FALLTHROUGH*/ |
| 7529 | |
| 7530 | case STATE_REP_INI: |
| 7531 | /* Check if matching with REP items from the .aff file would |
| 7532 | * work. Quickly skip if there are no REP items or the score |
| 7533 | * is going to be too high anyway. */ |
| 7534 | gap = &lp->lp_slang->sl_rep; |
| 7535 | if (gap->ga_len == 0 |
| 7536 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 7537 | { |
| 7538 | sp->ts_state = STATE_FINAL; |
| 7539 | break; |
| 7540 | } |
| 7541 | |
| 7542 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7543 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7544 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 7545 | if (sp->ts_curi < 0) |
| 7546 | { |
| 7547 | sp->ts_state = STATE_FINAL; |
| 7548 | break; |
| 7549 | } |
| 7550 | |
| 7551 | sp->ts_state = STATE_REP; |
| 7552 | /*FALLTHROUGH*/ |
| 7553 | |
| 7554 | case STATE_REP: |
| 7555 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7556 | * match replace the characters and check if the resulting |
| 7557 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7558 | p = fword + sp->ts_fidx; |
| 7559 | |
| 7560 | gap = &lp->lp_slang->sl_rep; |
| 7561 | while (sp->ts_curi < gap->ga_len) |
| 7562 | { |
| 7563 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 7564 | if (*ftp->ft_from != *p) |
| 7565 | { |
| 7566 | /* past possible matching entries */ |
| 7567 | sp->ts_curi = gap->ga_len; |
| 7568 | break; |
| 7569 | } |
| 7570 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 7571 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 7572 | { |
| 7573 | /* Need to undo this afterwards. */ |
| 7574 | sp->ts_state = STATE_REP_UNDO; |
| 7575 | |
| 7576 | /* Change the "from" to the "to" string. */ |
| 7577 | ++depth; |
| 7578 | fl = STRLEN(ftp->ft_from); |
| 7579 | tl = STRLEN(ftp->ft_to); |
| 7580 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7581 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7582 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7583 | repextra += tl - fl; |
| 7584 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7585 | mch_memmove(p, ftp->ft_to, tl); |
| 7586 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7587 | #ifdef FEAT_MBYTE |
| 7588 | stack[depth].ts_tcharlen = 0; |
| 7589 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7590 | break; |
| 7591 | } |
| 7592 | } |
| 7593 | |
| 7594 | if (sp->ts_curi >= gap->ga_len) |
| 7595 | /* No (more) matches. */ |
| 7596 | sp->ts_state = STATE_FINAL; |
| 7597 | |
| 7598 | break; |
| 7599 | |
| 7600 | case STATE_REP_UNDO: |
| 7601 | /* Undo a REP replacement and continue with the next one. */ |
| 7602 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 7603 | + sp->ts_curi - 1; |
| 7604 | fl = STRLEN(ftp->ft_from); |
| 7605 | tl = STRLEN(ftp->ft_to); |
| 7606 | p = fword + sp->ts_fidx; |
| 7607 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7608 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7609 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7610 | repextra -= tl - fl; |
| 7611 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7612 | mch_memmove(p, ftp->ft_from, fl); |
| 7613 | sp->ts_state = STATE_REP; |
| 7614 | break; |
| 7615 | |
| 7616 | default: |
| 7617 | /* Did all possible states at this level, go up one level. */ |
| 7618 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7619 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7620 | if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE) |
| 7621 | { |
| 7622 | /* Continue in or go back to the prefix tree. */ |
| 7623 | byts = pbyts; |
| 7624 | idxs = pidxs; |
| 7625 | splitoff = 0; |
| 7626 | } |
| 7627 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7628 | /* Don't check for CTRL-C too often, it takes time. */ |
| 7629 | line_breakcheck(); |
| 7630 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7631 | } |
| 7632 | } |
| 7633 | } |
| 7634 | |
| 7635 | /* |
| 7636 | * Try going one level deeper in the tree. |
| 7637 | */ |
| 7638 | static int |
| 7639 | try_deeper(su, stack, depth, score_add) |
| 7640 | suginfo_T *su; |
| 7641 | trystate_T *stack; |
| 7642 | int depth; |
| 7643 | int score_add; |
| 7644 | { |
| 7645 | int newscore; |
| 7646 | |
| 7647 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 7648 | newscore = stack[depth].ts_score + score_add; |
| 7649 | if (newscore >= su->su_maxscore) |
| 7650 | return FALSE; |
| 7651 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7652 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7653 | stack[depth + 1].ts_state = STATE_START; |
| 7654 | stack[depth + 1].ts_score = newscore; |
| 7655 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7656 | return TRUE; |
| 7657 | } |
| 7658 | |
| 7659 | /* |
| 7660 | * "fword" is a good word with case folded. Find the matching keep-case |
| 7661 | * words and put it in "kword". |
| 7662 | * Theoretically there could be several keep-case words that result in the |
| 7663 | * same case-folded word, but we only find one... |
| 7664 | */ |
| 7665 | static void |
| 7666 | find_keepcap_word(slang, fword, kword) |
| 7667 | slang_T *slang; |
| 7668 | char_u *fword; |
| 7669 | char_u *kword; |
| 7670 | { |
| 7671 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 7672 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7673 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7674 | |
| 7675 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7676 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7677 | int round[MAXWLEN]; |
| 7678 | int fwordidx[MAXWLEN]; |
| 7679 | int uwordidx[MAXWLEN]; |
| 7680 | int kwordlen[MAXWLEN]; |
| 7681 | |
| 7682 | int flen, ulen; |
| 7683 | int l; |
| 7684 | int len; |
| 7685 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7686 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7687 | char_u *p; |
| 7688 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7689 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7690 | |
| 7691 | if (byts == NULL) |
| 7692 | { |
| 7693 | /* array is empty: "cannot happen" */ |
| 7694 | *kword = NUL; |
| 7695 | return; |
| 7696 | } |
| 7697 | |
| 7698 | /* Make an all-cap version of "fword". */ |
| 7699 | allcap_copy(fword, uword); |
| 7700 | |
| 7701 | /* |
| 7702 | * Each character needs to be tried both case-folded and upper-case. |
| 7703 | * All this gets very complicated if we keep in mind that changing case |
| 7704 | * may change the byte length of a multi-byte character... |
| 7705 | */ |
| 7706 | depth = 0; |
| 7707 | arridx[0] = 0; |
| 7708 | round[0] = 0; |
| 7709 | fwordidx[0] = 0; |
| 7710 | uwordidx[0] = 0; |
| 7711 | kwordlen[0] = 0; |
| 7712 | while (depth >= 0) |
| 7713 | { |
| 7714 | if (fword[fwordidx[depth]] == NUL) |
| 7715 | { |
| 7716 | /* We are at the end of "fword". If the tree allows a word to end |
| 7717 | * here we have found a match. */ |
| 7718 | if (byts[arridx[depth] + 1] == 0) |
| 7719 | { |
| 7720 | kword[kwordlen[depth]] = NUL; |
| 7721 | return; |
| 7722 | } |
| 7723 | |
| 7724 | /* kword is getting too long, continue one level up */ |
| 7725 | --depth; |
| 7726 | } |
| 7727 | else if (++round[depth] > 2) |
| 7728 | { |
| 7729 | /* tried both fold-case and upper-case character, continue one |
| 7730 | * level up */ |
| 7731 | --depth; |
| 7732 | } |
| 7733 | else |
| 7734 | { |
| 7735 | /* |
| 7736 | * round[depth] == 1: Try using the folded-case character. |
| 7737 | * round[depth] == 2: Try using the upper-case character. |
| 7738 | */ |
| 7739 | #ifdef FEAT_MBYTE |
| 7740 | if (has_mbyte) |
| 7741 | { |
| 7742 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 7743 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 7744 | } |
| 7745 | else |
| 7746 | #endif |
| 7747 | ulen = flen = 1; |
| 7748 | if (round[depth] == 1) |
| 7749 | { |
| 7750 | p = fword + fwordidx[depth]; |
| 7751 | l = flen; |
| 7752 | } |
| 7753 | else |
| 7754 | { |
| 7755 | p = uword + uwordidx[depth]; |
| 7756 | l = ulen; |
| 7757 | } |
| 7758 | |
| 7759 | for (tryidx = arridx[depth]; l > 0; --l) |
| 7760 | { |
| 7761 | /* Perform a binary search in the list of accepted bytes. */ |
| 7762 | len = byts[tryidx++]; |
| 7763 | c = *p++; |
| 7764 | lo = tryidx; |
| 7765 | hi = tryidx + len - 1; |
| 7766 | while (lo < hi) |
| 7767 | { |
| 7768 | m = (lo + hi) / 2; |
| 7769 | if (byts[m] > c) |
| 7770 | hi = m - 1; |
| 7771 | else if (byts[m] < c) |
| 7772 | lo = m + 1; |
| 7773 | else |
| 7774 | { |
| 7775 | lo = hi = m; |
| 7776 | break; |
| 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | /* Stop if there is no matching byte. */ |
| 7781 | if (hi < lo || byts[lo] != c) |
| 7782 | break; |
| 7783 | |
| 7784 | /* Continue at the child (if there is one). */ |
| 7785 | tryidx = idxs[lo]; |
| 7786 | } |
| 7787 | |
| 7788 | if (l == 0) |
| 7789 | { |
| 7790 | /* |
| 7791 | * Found the matching char. Copy it to "kword" and go a |
| 7792 | * level deeper. |
| 7793 | */ |
| 7794 | if (round[depth] == 1) |
| 7795 | { |
| 7796 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 7797 | flen); |
| 7798 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 7799 | } |
| 7800 | else |
| 7801 | { |
| 7802 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 7803 | ulen); |
| 7804 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 7805 | } |
| 7806 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 7807 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 7808 | |
| 7809 | ++depth; |
| 7810 | arridx[depth] = tryidx; |
| 7811 | round[depth] = 0; |
| 7812 | } |
| 7813 | } |
| 7814 | } |
| 7815 | |
| 7816 | /* Didn't find it: "cannot happen". */ |
| 7817 | *kword = NUL; |
| 7818 | } |
| 7819 | |
| 7820 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7821 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 7822 | * su->su_sga. |
| 7823 | */ |
| 7824 | static void |
| 7825 | score_comp_sal(su) |
| 7826 | suginfo_T *su; |
| 7827 | { |
| 7828 | langp_T *lp; |
| 7829 | char_u badsound[MAXWLEN]; |
| 7830 | int i; |
| 7831 | suggest_T *stp; |
| 7832 | suggest_T *sstp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7833 | int score; |
| 7834 | |
| 7835 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 7836 | return; |
| 7837 | |
| 7838 | /* Use the sound-folding of the first language that supports it. */ |
| 7839 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7840 | lp->lp_slang != NULL; ++lp) |
| 7841 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7842 | { |
| 7843 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7844 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7845 | |
| 7846 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7847 | { |
| 7848 | stp = &SUG(su->su_ga, i); |
| 7849 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7850 | /* Case-fold the suggested word, sound-fold it and compute the |
| 7851 | * sound-a-like score. */ |
| 7852 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7853 | if (score < SCORE_MAXMAX) |
| 7854 | { |
| 7855 | /* Add the suggestion. */ |
| 7856 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 7857 | sstp->st_word = vim_strsave(stp->st_word); |
| 7858 | if (sstp->st_word != NULL) |
| 7859 | { |
| 7860 | sstp->st_score = score; |
| 7861 | sstp->st_altscore = 0; |
| 7862 | sstp->st_orglen = stp->st_orglen; |
| 7863 | ++su->su_sga.ga_len; |
| 7864 | } |
| 7865 | } |
| 7866 | } |
| 7867 | break; |
| 7868 | } |
| 7869 | } |
| 7870 | |
| 7871 | /* |
| 7872 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 7873 | * They are intwined. |
| 7874 | */ |
| 7875 | static void |
| 7876 | score_combine(su) |
| 7877 | suginfo_T *su; |
| 7878 | { |
| 7879 | int i; |
| 7880 | int j; |
| 7881 | garray_T ga; |
| 7882 | garray_T *gap; |
| 7883 | langp_T *lp; |
| 7884 | suggest_T *stp; |
| 7885 | char_u *p; |
| 7886 | char_u badsound[MAXWLEN]; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7887 | int round; |
| 7888 | |
| 7889 | /* Add the alternate score to su_ga. */ |
| 7890 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7891 | lp->lp_slang != NULL; ++lp) |
| 7892 | { |
| 7893 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7894 | { |
| 7895 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7896 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7897 | |
| 7898 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7899 | { |
| 7900 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7901 | stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang, |
| 7902 | badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7903 | if (stp->st_altscore == SCORE_MAXMAX) |
| 7904 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 7905 | else |
| 7906 | stp->st_score = (stp->st_score * 3 |
| 7907 | + stp->st_altscore) / 4; |
| 7908 | stp->st_salscore = FALSE; |
| 7909 | } |
| 7910 | break; |
| 7911 | } |
| 7912 | } |
| 7913 | |
| 7914 | /* Add the alternate score to su_sga. */ |
| 7915 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 7916 | { |
| 7917 | stp = &SUG(su->su_sga, i); |
| 7918 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 7919 | if (stp->st_score == SCORE_MAXMAX) |
| 7920 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 7921 | else |
| 7922 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 7923 | stp->st_salscore = TRUE; |
| 7924 | } |
| 7925 | |
| 7926 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 7927 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 7928 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 7929 | |
| 7930 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 7931 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 7932 | return; |
| 7933 | |
| 7934 | stp = &SUG(ga, 0); |
| 7935 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 7936 | { |
| 7937 | /* round 1: get a suggestion from su_ga |
| 7938 | * round 2: get a suggestion from su_sga */ |
| 7939 | for (round = 1; round <= 2; ++round) |
| 7940 | { |
| 7941 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 7942 | if (i < gap->ga_len) |
| 7943 | { |
| 7944 | /* Don't add a word if it's already there. */ |
| 7945 | p = SUG(*gap, i).st_word; |
| 7946 | for (j = 0; j < ga.ga_len; ++j) |
| 7947 | if (STRCMP(stp[j].st_word, p) == 0) |
| 7948 | break; |
| 7949 | if (j == ga.ga_len) |
| 7950 | stp[ga.ga_len++] = SUG(*gap, i); |
| 7951 | else |
| 7952 | vim_free(p); |
| 7953 | } |
| 7954 | } |
| 7955 | } |
| 7956 | |
| 7957 | ga_clear(&su->su_ga); |
| 7958 | ga_clear(&su->su_sga); |
| 7959 | |
| 7960 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 7961 | if (ga.ga_len > su->su_maxcount) |
| 7962 | { |
| 7963 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 7964 | vim_free(stp[i].st_word); |
| 7965 | ga.ga_len = su->su_maxcount; |
| 7966 | } |
| 7967 | |
| 7968 | su->su_ga = ga; |
| 7969 | } |
| 7970 | |
| 7971 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7972 | * For the goodword in "stp" compute the soundalike score compared to the |
| 7973 | * badword. |
| 7974 | */ |
| 7975 | static int |
| 7976 | stp_sal_score(stp, su, slang, badsound) |
| 7977 | suggest_T *stp; |
| 7978 | suginfo_T *su; |
| 7979 | slang_T *slang; |
| 7980 | char_u *badsound; /* sound-folded badword */ |
| 7981 | { |
| 7982 | char_u *p; |
| 7983 | char_u badsound2[MAXWLEN]; |
| 7984 | char_u fword[MAXWLEN]; |
| 7985 | char_u goodsound[MAXWLEN]; |
| 7986 | |
| 7987 | if (stp->st_orglen <= su->su_badlen) |
| 7988 | p = badsound; |
| 7989 | else |
| 7990 | { |
| 7991 | /* soundfold the bad word with more characters following */ |
| 7992 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 7993 | |
| 7994 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 7995 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 7996 | * removing the space. Don't do it when the good word also contains a |
| 7997 | * space. */ |
| 7998 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 7999 | && *skiptowhite(stp->st_word) == NUL) |
| 8000 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| 8001 | mch_memmove(p, p + 1, STRLEN(p)); |
| 8002 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8003 | spell_soundfold(slang, fword, TRUE, badsound2); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8004 | p = badsound2; |
| 8005 | } |
| 8006 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8007 | /* Sound-fold the word and compute the score for the difference. */ |
| 8008 | spell_soundfold(slang, stp->st_word, FALSE, goodsound); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8009 | |
| 8010 | return soundalike_score(goodsound, p); |
| 8011 | } |
| 8012 | |
| 8013 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8014 | * Find suggestions by comparing the word in a sound-a-like form. |
| 8015 | */ |
| 8016 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8017 | suggest_try_soundalike(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8018 | suginfo_T *su; |
| 8019 | { |
| 8020 | char_u salword[MAXWLEN]; |
| 8021 | char_u tword[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8022 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8023 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8024 | int curi[MAXWLEN]; |
| 8025 | langp_T *lp; |
| 8026 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8027 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8028 | int depth; |
| 8029 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8030 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8031 | int round; |
| 8032 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8033 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8034 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8035 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8036 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8037 | lp->lp_slang != NULL; ++lp) |
| 8038 | { |
| 8039 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8040 | { |
| 8041 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8042 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8043 | |
| 8044 | /* |
| 8045 | * Go through the whole tree, soundfold each word and compare. |
| 8046 | * round 1: use the case-folded tree. |
| 8047 | * round 2: use the keep-case tree. |
| 8048 | */ |
| 8049 | for (round = 1; round <= 2; ++round) |
| 8050 | { |
| 8051 | if (round == 1) |
| 8052 | { |
| 8053 | byts = lp->lp_slang->sl_fbyts; |
| 8054 | idxs = lp->lp_slang->sl_fidxs; |
| 8055 | } |
| 8056 | else |
| 8057 | { |
| 8058 | byts = lp->lp_slang->sl_kbyts; |
| 8059 | idxs = lp->lp_slang->sl_kidxs; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 8060 | if (byts == NULL) /* no keep-case words */ |
| 8061 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8062 | } |
| 8063 | |
| 8064 | depth = 0; |
| 8065 | arridx[0] = 0; |
| 8066 | curi[0] = 1; |
| 8067 | while (depth >= 0 && !got_int) |
| 8068 | { |
| 8069 | if (curi[depth] > byts[arridx[depth]]) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8070 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8071 | /* Done all bytes at this node, go up one level. */ |
| 8072 | --depth; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8073 | line_breakcheck(); |
| 8074 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8075 | else |
| 8076 | { |
| 8077 | /* Do one more byte at this node. */ |
| 8078 | n = arridx[depth] + curi[depth]; |
| 8079 | ++curi[depth]; |
| 8080 | c = byts[n]; |
| 8081 | if (c == 0) |
| 8082 | { |
| 8083 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8084 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8085 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 8086 | { |
| 8087 | tword[depth] = NUL; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8088 | /* Sound-fold. Only in keep-case tree need to |
| 8089 | * case-fold the word. */ |
| 8090 | spell_soundfold(lp->lp_slang, tword, |
| 8091 | round == 1, tsalword); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8092 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8093 | /* Compute the edit distance between the |
| 8094 | * sound-a-like words. */ |
| 8095 | sound_score = soundalike_score(salword, |
| 8096 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8097 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8098 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8099 | char_u cword[MAXWLEN]; |
| 8100 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8101 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8102 | |
Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 8103 | flags |= su->su_badflags; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8104 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8105 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8106 | /* Need to fix case according to |
| 8107 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8108 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8109 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8110 | } |
| 8111 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8112 | p = tword; |
| 8113 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8114 | if (sps_flags & SPS_DOUBLE) |
| 8115 | add_suggestion(su, &su->su_sga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8116 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8117 | sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8118 | else |
| 8119 | { |
| 8120 | /* Compute the score. */ |
| 8121 | score = spell_edit_score( |
| 8122 | su->su_badword, p); |
| 8123 | if (sps_flags & SPS_BEST) |
| 8124 | /* give a bonus for the good word |
| 8125 | * sounding the same as the bad |
| 8126 | * word */ |
| 8127 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8128 | su->su_badlen, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8129 | RESCORE(score, sound_score), |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8130 | sound_score, TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8131 | else |
| 8132 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8133 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8134 | score + sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8135 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8136 | } |
| 8137 | } |
| 8138 | |
| 8139 | /* Skip over other NUL bytes. */ |
| 8140 | while (byts[n + 1] == 0) |
| 8141 | { |
| 8142 | ++n; |
| 8143 | ++curi[depth]; |
| 8144 | } |
| 8145 | } |
| 8146 | else |
| 8147 | { |
| 8148 | /* Normal char, go one level deeper. */ |
| 8149 | tword[depth++] = c; |
| 8150 | arridx[depth] = idxs[n]; |
| 8151 | curi[depth] = 1; |
| 8152 | } |
| 8153 | } |
| 8154 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8155 | } |
| 8156 | } |
| 8157 | } |
| 8158 | } |
| 8159 | |
| 8160 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8161 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8162 | */ |
| 8163 | static void |
| 8164 | make_case_word(fword, cword, flags) |
| 8165 | char_u *fword; |
| 8166 | char_u *cword; |
| 8167 | int flags; |
| 8168 | { |
| 8169 | if (flags & WF_ALLCAP) |
| 8170 | /* Make it all upper-case */ |
| 8171 | allcap_copy(fword, cword); |
| 8172 | else if (flags & WF_ONECAP) |
| 8173 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8174 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8175 | else |
| 8176 | /* Use goodword as-is. */ |
| 8177 | STRCPY(cword, fword); |
| 8178 | } |
| 8179 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8180 | /* |
| 8181 | * Use map string "map" for languages "lp". |
| 8182 | */ |
| 8183 | static void |
| 8184 | set_map_str(lp, map) |
| 8185 | slang_T *lp; |
| 8186 | char_u *map; |
| 8187 | { |
| 8188 | char_u *p; |
| 8189 | int headc = 0; |
| 8190 | int c; |
| 8191 | int i; |
| 8192 | |
| 8193 | if (*map == NUL) |
| 8194 | { |
| 8195 | lp->sl_has_map = FALSE; |
| 8196 | return; |
| 8197 | } |
| 8198 | lp->sl_has_map = TRUE; |
| 8199 | |
| 8200 | /* Init the array and hash table empty. */ |
| 8201 | for (i = 0; i < 256; ++i) |
| 8202 | lp->sl_map_array[i] = 0; |
| 8203 | #ifdef FEAT_MBYTE |
| 8204 | hash_init(&lp->sl_map_hash); |
| 8205 | #endif |
| 8206 | |
| 8207 | /* |
| 8208 | * The similar characters are stored separated with slashes: |
| 8209 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 8210 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 8211 | */ |
| 8212 | for (p = map; *p != NUL; ) |
| 8213 | { |
| 8214 | #ifdef FEAT_MBYTE |
| 8215 | c = mb_ptr2char_adv(&p); |
| 8216 | #else |
| 8217 | c = *p++; |
| 8218 | #endif |
| 8219 | if (c == '/') |
| 8220 | headc = 0; |
| 8221 | else |
| 8222 | { |
| 8223 | if (headc == 0) |
| 8224 | headc = c; |
| 8225 | |
| 8226 | #ifdef FEAT_MBYTE |
| 8227 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 8228 | * the hash table. Each entry is the char, a NUL the headchar and |
| 8229 | * a NUL. */ |
| 8230 | if (c >= 256) |
| 8231 | { |
| 8232 | int cl = mb_char2len(c); |
| 8233 | int headcl = mb_char2len(headc); |
| 8234 | char_u *b; |
| 8235 | hash_T hash; |
| 8236 | hashitem_T *hi; |
| 8237 | |
| 8238 | b = alloc((unsigned)(cl + headcl + 2)); |
| 8239 | if (b == NULL) |
| 8240 | return; |
| 8241 | mb_char2bytes(c, b); |
| 8242 | b[cl] = NUL; |
| 8243 | mb_char2bytes(headc, b + cl + 1); |
| 8244 | b[cl + 1 + headcl] = NUL; |
| 8245 | hash = hash_hash(b); |
| 8246 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 8247 | if (HASHITEM_EMPTY(hi)) |
| 8248 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 8249 | else |
| 8250 | { |
| 8251 | /* This should have been checked when generating the .spl |
| 8252 | * file. */ |
| 8253 | EMSG(_("E999: duplicate char in MAP entry")); |
| 8254 | vim_free(b); |
| 8255 | } |
| 8256 | } |
| 8257 | else |
| 8258 | #endif |
| 8259 | lp->sl_map_array[c] = headc; |
| 8260 | } |
| 8261 | } |
| 8262 | } |
| 8263 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8264 | /* |
| 8265 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 8266 | * lines in the .aff file. |
| 8267 | */ |
| 8268 | static int |
| 8269 | similar_chars(slang, c1, c2) |
| 8270 | slang_T *slang; |
| 8271 | int c1; |
| 8272 | int c2; |
| 8273 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8274 | int m1, m2; |
| 8275 | #ifdef FEAT_MBYTE |
| 8276 | char_u buf[MB_MAXBYTES]; |
| 8277 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8278 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8279 | if (c1 >= 256) |
| 8280 | { |
| 8281 | buf[mb_char2bytes(c1, buf)] = 0; |
| 8282 | hi = hash_find(&slang->sl_map_hash, buf); |
| 8283 | if (HASHITEM_EMPTY(hi)) |
| 8284 | m1 = 0; |
| 8285 | else |
| 8286 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 8287 | } |
| 8288 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8289 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 8290 | m1 = slang->sl_map_array[c1]; |
| 8291 | if (m1 == 0) |
| 8292 | return FALSE; |
| 8293 | |
| 8294 | |
| 8295 | #ifdef FEAT_MBYTE |
| 8296 | if (c2 >= 256) |
| 8297 | { |
| 8298 | buf[mb_char2bytes(c2, buf)] = 0; |
| 8299 | hi = hash_find(&slang->sl_map_hash, buf); |
| 8300 | if (HASHITEM_EMPTY(hi)) |
| 8301 | m2 = 0; |
| 8302 | else |
| 8303 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 8304 | } |
| 8305 | else |
| 8306 | #endif |
| 8307 | m2 = slang->sl_map_array[c2]; |
| 8308 | |
| 8309 | return m1 == m2; |
| 8310 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8311 | |
| 8312 | /* |
| 8313 | * Add a suggestion to the list of suggestions. |
| 8314 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 8315 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 8316 | * with spell_edit_score(). |
| 8317 | */ |
| 8318 | static void |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8319 | add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8320 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8321 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8322 | char_u *goodword; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8323 | int badlen; /* length of bad word used */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8324 | int score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8325 | int altscore; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8326 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8327 | { |
| 8328 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8329 | int i; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8330 | char_u *p = NULL; |
| 8331 | int c = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8332 | |
| 8333 | /* Check that the word wasn't banned. */ |
| 8334 | if (was_banned(su, goodword)) |
| 8335 | return; |
| 8336 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8337 | /* If past "su_badlen" and the rest is identical stop at "su_badlen". |
| 8338 | * Remove the common part from "goodword". */ |
| 8339 | i = badlen - su->su_badlen; |
| 8340 | if (i > 0) |
| 8341 | { |
| 8342 | /* This assumes there was no case folding or it didn't change the |
| 8343 | * length... */ |
| 8344 | p = goodword + STRLEN(goodword) - i; |
| 8345 | if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0) |
| 8346 | { |
| 8347 | badlen = su->su_badlen; |
| 8348 | c = *p; |
| 8349 | *p = NUL; |
| 8350 | } |
| 8351 | else |
| 8352 | p = NULL; |
| 8353 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8354 | else if (i < 0) |
| 8355 | { |
| 8356 | /* When replacing part of the word check that we actually change |
| 8357 | * something. For "the the" a suggestion can be replacing the first |
| 8358 | * "the" with itself, since "the" wasn't banned. */ |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8359 | if (badlen == (int)STRLEN(goodword) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8360 | && STRNCMP(su->su_badword, goodword, badlen) == 0) |
| 8361 | return; |
| 8362 | } |
| 8363 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8364 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8365 | if (score <= su->su_maxscore) |
| 8366 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8367 | /* Check if the word is already there. Also check the length that is |
| 8368 | * being replaced "thes," -> "these" is a different suggestion from |
| 8369 | * "thes" -> "these". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8370 | stp = &SUG(*gap, 0); |
| 8371 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8372 | if (STRCMP(stp[i].st_word, goodword) == 0 |
| 8373 | && stp[i].st_orglen == badlen) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8374 | { |
| 8375 | /* Found it. Remember the lowest score. */ |
| 8376 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8377 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8378 | stp[i].st_score = score; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8379 | stp[i].st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8380 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8381 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8382 | break; |
| 8383 | } |
| 8384 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8385 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8386 | { |
| 8387 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8388 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8389 | stp->st_word = vim_strsave(goodword); |
| 8390 | if (stp->st_word != NULL) |
| 8391 | { |
| 8392 | stp->st_score = score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8393 | stp->st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8394 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8395 | stp->st_orglen = badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8396 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8397 | |
| 8398 | /* If we have too many suggestions now, sort the list and keep |
| 8399 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8400 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 8401 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 8402 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8403 | } |
| 8404 | } |
| 8405 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8406 | |
| 8407 | if (p != NULL) |
| 8408 | *p = c; /* restore "goodword" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8409 | } |
| 8410 | |
| 8411 | /* |
| 8412 | * Add a word to be banned. |
| 8413 | */ |
| 8414 | static void |
| 8415 | add_banned(su, word) |
| 8416 | suginfo_T *su; |
| 8417 | char_u *word; |
| 8418 | { |
| 8419 | char_u *s = vim_strsave(word); |
| 8420 | hash_T hash; |
| 8421 | hashitem_T *hi; |
| 8422 | |
| 8423 | if (s != NULL) |
| 8424 | { |
| 8425 | hash = hash_hash(s); |
| 8426 | hi = hash_lookup(&su->su_banned, s, hash); |
| 8427 | if (HASHITEM_EMPTY(hi)) |
| 8428 | hash_add_item(&su->su_banned, hi, s, hash); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8429 | else |
| 8430 | vim_free(s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8431 | } |
| 8432 | } |
| 8433 | |
| 8434 | /* |
| 8435 | * Return TRUE if a word appears in the list of banned words. |
| 8436 | */ |
| 8437 | static int |
| 8438 | was_banned(su, word) |
| 8439 | suginfo_T *su; |
| 8440 | char_u *word; |
| 8441 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8442 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 8443 | |
| 8444 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8445 | } |
| 8446 | |
| 8447 | /* |
| 8448 | * Free the banned words in "su". |
| 8449 | */ |
| 8450 | static void |
| 8451 | free_banned(su) |
| 8452 | suginfo_T *su; |
| 8453 | { |
| 8454 | int todo; |
| 8455 | hashitem_T *hi; |
| 8456 | |
| 8457 | todo = su->su_banned.ht_used; |
| 8458 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 8459 | { |
| 8460 | if (!HASHITEM_EMPTY(hi)) |
| 8461 | { |
| 8462 | vim_free(hi->hi_key); |
| 8463 | --todo; |
| 8464 | } |
| 8465 | } |
| 8466 | hash_clear(&su->su_banned); |
| 8467 | } |
| 8468 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8469 | /* |
| 8470 | * Recompute the score if sound-folding is possible. This is slow, |
| 8471 | * thus only done for the final results. |
| 8472 | */ |
| 8473 | static void |
| 8474 | rescore_suggestions(su) |
| 8475 | suginfo_T *su; |
| 8476 | { |
| 8477 | langp_T *lp; |
| 8478 | suggest_T *stp; |
| 8479 | char_u sal_badword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8480 | int i; |
| 8481 | |
| 8482 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8483 | lp->lp_slang != NULL; ++lp) |
| 8484 | { |
| 8485 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8486 | { |
| 8487 | /* soundfold the bad word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8488 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8489 | |
| 8490 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 8491 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8492 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8493 | if (!stp->st_had_bonus) |
| 8494 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8495 | stp->st_altscore = stp_sal_score(stp, su, |
| 8496 | lp->lp_slang, sal_badword); |
| 8497 | if (stp->st_altscore == SCORE_MAXMAX) |
| 8498 | stp->st_altscore = SCORE_BIG; |
| 8499 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8500 | } |
| 8501 | } |
| 8502 | break; |
| 8503 | } |
| 8504 | } |
| 8505 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8506 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8507 | static int |
| 8508 | #ifdef __BORLANDC__ |
| 8509 | _RTLENTRYF |
| 8510 | #endif |
| 8511 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 8512 | |
| 8513 | /* |
| 8514 | * Function given to qsort() to sort the suggestions on st_score. |
| 8515 | */ |
| 8516 | static int |
| 8517 | #ifdef __BORLANDC__ |
| 8518 | _RTLENTRYF |
| 8519 | #endif |
| 8520 | sug_compare(s1, s2) |
| 8521 | const void *s1; |
| 8522 | const void *s2; |
| 8523 | { |
| 8524 | suggest_T *p1 = (suggest_T *)s1; |
| 8525 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8526 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8527 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8528 | if (n == 0) |
| 8529 | return p1->st_altscore - p2->st_altscore; |
| 8530 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | /* |
| 8534 | * Cleanup the suggestions: |
| 8535 | * - Sort on score. |
| 8536 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8537 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8538 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8539 | static int |
| 8540 | cleanup_suggestions(gap, maxscore, keep) |
| 8541 | garray_T *gap; |
| 8542 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8543 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8544 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8545 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8546 | int i; |
| 8547 | |
| 8548 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8549 | 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] | 8550 | |
| 8551 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8552 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8553 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8554 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8555 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8556 | gap->ga_len = keep; |
| 8557 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8558 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8559 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8560 | } |
| 8561 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8562 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 8563 | /* |
| 8564 | * Soundfold a string, for soundfold(). |
| 8565 | * Result is in allocated memory, NULL for an error. |
| 8566 | */ |
| 8567 | char_u * |
| 8568 | eval_soundfold(word) |
| 8569 | char_u *word; |
| 8570 | { |
| 8571 | langp_T *lp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8572 | char_u sound[MAXWLEN]; |
| 8573 | |
| 8574 | if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) |
| 8575 | /* Use the sound-folding of the first language that supports it. */ |
| 8576 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 8577 | lp->lp_slang != NULL; ++lp) |
| 8578 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 8579 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8580 | /* soundfold the word */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8581 | spell_soundfold(lp->lp_slang, word, FALSE, sound); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8582 | return vim_strsave(sound); |
| 8583 | } |
| 8584 | |
| 8585 | /* No language with sound folding, return word as-is. */ |
| 8586 | return vim_strsave(word); |
| 8587 | } |
| 8588 | #endif |
| 8589 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8590 | /* |
| 8591 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 8592 | */ |
| 8593 | static void |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8594 | spell_soundfold(slang, inword, folded, res) |
| 8595 | slang_T *slang; |
| 8596 | char_u *inword; |
| 8597 | int folded; /* "inword" is already case-folded */ |
| 8598 | char_u *res; |
| 8599 | { |
| 8600 | char_u fword[MAXWLEN]; |
| 8601 | char_u *word; |
| 8602 | |
| 8603 | if (slang->sl_sofo) |
| 8604 | /* SOFOFROM and SOFOTO used */ |
| 8605 | spell_soundfold_sofo(slang, inword, res); |
| 8606 | else |
| 8607 | { |
| 8608 | /* SAL items used. Requires the word to be case-folded. */ |
| 8609 | if (folded) |
| 8610 | word = inword; |
| 8611 | else |
| 8612 | { |
| 8613 | (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN); |
| 8614 | word = fword; |
| 8615 | } |
| 8616 | |
| 8617 | #ifdef FEAT_MBYTE |
| 8618 | if (has_mbyte) |
| 8619 | spell_soundfold_wsal(slang, word, res); |
| 8620 | else |
| 8621 | #endif |
| 8622 | spell_soundfold_sal(slang, word, res); |
| 8623 | } |
| 8624 | } |
| 8625 | |
| 8626 | /* |
| 8627 | * Perform sound folding of "inword" into "res" according to SOFOFROM and |
| 8628 | * SOFOTO lines. |
| 8629 | */ |
| 8630 | static void |
| 8631 | spell_soundfold_sofo(slang, inword, res) |
| 8632 | slang_T *slang; |
| 8633 | char_u *inword; |
| 8634 | char_u *res; |
| 8635 | { |
| 8636 | char_u *s; |
| 8637 | int ri = 0; |
| 8638 | int c; |
| 8639 | |
| 8640 | #ifdef FEAT_MBYTE |
| 8641 | if (has_mbyte) |
| 8642 | { |
| 8643 | int prevc = 0; |
| 8644 | int *ip; |
| 8645 | |
| 8646 | /* The sl_sal_first[] table contains the translation for chars up to |
| 8647 | * 255, sl_sal the rest. */ |
| 8648 | for (s = inword; *s != NUL; ) |
| 8649 | { |
| 8650 | c = mb_ptr2char_adv(&s); |
| 8651 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 8652 | c = ' '; |
| 8653 | else if (c < 256) |
| 8654 | c = slang->sl_sal_first[c]; |
| 8655 | else |
| 8656 | { |
| 8657 | ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; |
| 8658 | if (ip == NULL) /* empty list, can't match */ |
| 8659 | c = NUL; |
| 8660 | else |
| 8661 | for (;;) /* find "c" in the list */ |
| 8662 | { |
| 8663 | if (*ip == 0) /* not found */ |
| 8664 | { |
| 8665 | c = NUL; |
| 8666 | break; |
| 8667 | } |
| 8668 | if (*ip == c) /* match! */ |
| 8669 | { |
| 8670 | c = ip[1]; |
| 8671 | break; |
| 8672 | } |
| 8673 | ip += 2; |
| 8674 | } |
| 8675 | } |
| 8676 | |
| 8677 | if (c != NUL && c != prevc) |
| 8678 | { |
| 8679 | ri += mb_char2bytes(c, res + ri); |
| 8680 | if (ri + MB_MAXBYTES > MAXWLEN) |
| 8681 | break; |
| 8682 | prevc = c; |
| 8683 | } |
| 8684 | } |
| 8685 | } |
| 8686 | else |
| 8687 | #endif |
| 8688 | { |
| 8689 | /* The sl_sal_first[] table contains the translation. */ |
| 8690 | for (s = inword; (c = *s) != NUL; ++s) |
| 8691 | { |
| 8692 | if (vim_iswhite(c)) |
| 8693 | c = ' '; |
| 8694 | else |
| 8695 | c = slang->sl_sal_first[c]; |
| 8696 | if (c != NUL && (ri == 0 || res[ri - 1] != c)) |
| 8697 | res[ri++] = c; |
| 8698 | } |
| 8699 | } |
| 8700 | |
| 8701 | res[ri] = NUL; |
| 8702 | } |
| 8703 | |
| 8704 | static void |
| 8705 | spell_soundfold_sal(slang, inword, res) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8706 | slang_T *slang; |
| 8707 | char_u *inword; |
| 8708 | char_u *res; |
| 8709 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8710 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8711 | char_u word[MAXWLEN]; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8712 | char_u *s = inword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8713 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8714 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8715 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8716 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8717 | int n, k = 0; |
| 8718 | int z0; |
| 8719 | int k0; |
| 8720 | int n0; |
| 8721 | int c; |
| 8722 | int pri; |
| 8723 | int p0 = -333; |
| 8724 | int c0; |
| 8725 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8726 | /* Remove accents, if wanted. We actually remove all non-word characters. |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8727 | * 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] | 8728 | if (slang->sl_rem_accents) |
| 8729 | { |
| 8730 | t = word; |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8731 | while (*s != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8732 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8733 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8734 | { |
| 8735 | *t++ = ' '; |
| 8736 | s = skipwhite(s); |
| 8737 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8738 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8739 | { |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8740 | if (spell_iswordp_nmw(s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8741 | *t++ = *s; |
| 8742 | ++s; |
| 8743 | } |
| 8744 | } |
| 8745 | *t = NUL; |
| 8746 | } |
| 8747 | else |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8748 | STRCPY(word, s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8749 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8750 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8751 | |
| 8752 | /* |
| 8753 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8754 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8755 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8756 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8757 | while ((c = word[i]) != NUL) |
| 8758 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8759 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8760 | n = slang->sl_sal_first[c]; |
| 8761 | z0 = 0; |
| 8762 | |
| 8763 | if (n >= 0) |
| 8764 | { |
| 8765 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8766 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8767 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8768 | /* Quickly skip entries that don't match the word. Most |
| 8769 | * entries are less then three chars, optimize for that. */ |
| 8770 | k = smp[n].sm_leadlen; |
| 8771 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8772 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8773 | if (word[i + 1] != s[1]) |
| 8774 | continue; |
| 8775 | if (k > 2) |
| 8776 | { |
| 8777 | for (j = 2; j < k; ++j) |
| 8778 | if (word[i + j] != s[j]) |
| 8779 | break; |
| 8780 | if (j < k) |
| 8781 | continue; |
| 8782 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8783 | } |
| 8784 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8785 | if ((pf = smp[n].sm_oneof) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8786 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8787 | /* Check for match with one of the chars in "sm_oneof". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8788 | while (*pf != NUL && *pf != word[i + k]) |
| 8789 | ++pf; |
| 8790 | if (*pf == NUL) |
| 8791 | continue; |
| 8792 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8793 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8794 | s = smp[n].sm_rules; |
| 8795 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8796 | |
| 8797 | p0 = *s; |
| 8798 | k0 = k; |
| 8799 | while (*s == '-' && k > 1) |
| 8800 | { |
| 8801 | k--; |
| 8802 | s++; |
| 8803 | } |
| 8804 | if (*s == '<') |
| 8805 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8806 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8807 | { |
| 8808 | /* determine priority */ |
| 8809 | pri = *s - '0'; |
| 8810 | s++; |
| 8811 | } |
| 8812 | if (*s == '^' && *(s + 1) == '^') |
| 8813 | s++; |
| 8814 | |
| 8815 | if (*s == NUL |
| 8816 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8817 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8818 | || spell_iswordp(word + i - 1, curbuf))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8819 | && (*(s + 1) != '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8820 | || (!spell_iswordp(word + i + k0, curbuf)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8821 | || (*s == '$' && i > 0 |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8822 | && spell_iswordp(word + i - 1, curbuf) |
| 8823 | && (!spell_iswordp(word + i + k0, curbuf)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8824 | { |
| 8825 | /* search for followup rules, if: */ |
| 8826 | /* followup and k > 1 and NO '-' in searchstring */ |
| 8827 | c0 = word[i + k - 1]; |
| 8828 | n0 = slang->sl_sal_first[c0]; |
| 8829 | |
| 8830 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8831 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8832 | { |
| 8833 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8834 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8835 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8836 | /* Quickly skip entries that don't match the word. |
| 8837 | * */ |
| 8838 | k0 = smp[n0].sm_leadlen; |
| 8839 | if (k0 > 1) |
| 8840 | { |
| 8841 | if (word[i + k] != s[1]) |
| 8842 | continue; |
| 8843 | if (k0 > 2) |
| 8844 | { |
| 8845 | pf = word + i + k + 1; |
| 8846 | for (j = 2; j < k0; ++j) |
| 8847 | if (*pf++ != s[j]) |
| 8848 | break; |
| 8849 | if (j < k0) |
| 8850 | continue; |
| 8851 | } |
| 8852 | } |
| 8853 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8854 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8855 | if ((pf = smp[n0].sm_oneof) != NULL) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8856 | { |
| 8857 | /* Check for match with one of the chars in |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8858 | * "sm_oneof". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8859 | while (*pf != NUL && *pf != word[i + k0]) |
| 8860 | ++pf; |
| 8861 | if (*pf == NUL) |
| 8862 | continue; |
| 8863 | ++k0; |
| 8864 | } |
| 8865 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8866 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8867 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8868 | while (*s == '-') |
| 8869 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8870 | /* "k0" gets NOT reduced because |
| 8871 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8872 | s++; |
| 8873 | } |
| 8874 | if (*s == '<') |
| 8875 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8876 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8877 | { |
| 8878 | p0 = *s - '0'; |
| 8879 | s++; |
| 8880 | } |
| 8881 | |
| 8882 | if (*s == NUL |
| 8883 | /* *s == '^' cuts */ |
| 8884 | || (*s == '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 8885 | && !spell_iswordp(word + i + k0, |
| 8886 | curbuf))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8887 | { |
| 8888 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8889 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8890 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8891 | |
| 8892 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8893 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8894 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8895 | /* rule fits; stop search */ |
| 8896 | break; |
| 8897 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8900 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8901 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8902 | } |
| 8903 | |
| 8904 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8905 | s = smp[n].sm_to; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 8906 | if (s == NULL) |
| 8907 | s = (char_u *)""; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8908 | pf = smp[n].sm_rules; |
| 8909 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8910 | if (p0 == 1 && z == 0) |
| 8911 | { |
| 8912 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8913 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 8914 | || res[reslen - 1] == *s)) |
| 8915 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8916 | z0 = 1; |
| 8917 | z = 1; |
| 8918 | k0 = 0; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8919 | while (*s != NUL && word[i + k0] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8920 | { |
| 8921 | word[i + k0] = *s; |
| 8922 | k0++; |
| 8923 | s++; |
| 8924 | } |
| 8925 | if (k > k0) |
| 8926 | mch_memmove(word + i + k0, word + i + k, |
| 8927 | STRLEN(word + i + k) + 1); |
| 8928 | |
| 8929 | /* new "actual letter" */ |
| 8930 | c = word[i]; |
| 8931 | } |
| 8932 | else |
| 8933 | { |
| 8934 | /* no '<' rule used */ |
| 8935 | i += k - 1; |
| 8936 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8937 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8938 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8939 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8940 | res[reslen++] = *s; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8941 | s++; |
| 8942 | } |
| 8943 | /* new "actual letter" */ |
| 8944 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8945 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8946 | { |
| 8947 | if (c != NUL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8948 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8949 | mch_memmove(word, word + i + 1, |
| 8950 | STRLEN(word + i + 1) + 1); |
| 8951 | i = 0; |
| 8952 | z0 = 1; |
| 8953 | } |
| 8954 | } |
| 8955 | break; |
| 8956 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8957 | } |
| 8958 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8959 | else if (vim_iswhite(c)) |
| 8960 | { |
| 8961 | c = ' '; |
| 8962 | k = 1; |
| 8963 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8964 | |
| 8965 | if (z0 == 0) |
| 8966 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8967 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 8968 | && (!slang->sl_collapse || reslen == 0 |
| 8969 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8970 | /* condense only double letters */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8971 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8972 | |
| 8973 | i++; |
| 8974 | z = 0; |
| 8975 | k = 0; |
| 8976 | } |
| 8977 | } |
| 8978 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8979 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8982 | #ifdef FEAT_MBYTE |
| 8983 | /* |
| 8984 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 8985 | * Multi-byte version of spell_soundfold(). |
| 8986 | */ |
| 8987 | static void |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8988 | spell_soundfold_wsal(slang, inword, res) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8989 | slang_T *slang; |
| 8990 | char_u *inword; |
| 8991 | char_u *res; |
| 8992 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8993 | salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8994 | int word[MAXWLEN]; |
| 8995 | int wres[MAXWLEN]; |
| 8996 | int l; |
| 8997 | char_u *s; |
| 8998 | int *ws; |
| 8999 | char_u *t; |
| 9000 | int *pf; |
| 9001 | int i, j, z; |
| 9002 | int reslen; |
| 9003 | int n, k = 0; |
| 9004 | int z0; |
| 9005 | int k0; |
| 9006 | int n0; |
| 9007 | int c; |
| 9008 | int pri; |
| 9009 | int p0 = -333; |
| 9010 | int c0; |
| 9011 | int did_white = FALSE; |
| 9012 | |
| 9013 | /* |
| 9014 | * Convert the multi-byte string to a wide-character string. |
| 9015 | * Remove accents, if wanted. We actually remove all non-word characters. |
| 9016 | * But keep white space. |
| 9017 | */ |
| 9018 | n = 0; |
| 9019 | for (s = inword; *s != NUL; ) |
| 9020 | { |
| 9021 | t = s; |
| 9022 | c = mb_ptr2char_adv(&s); |
| 9023 | if (slang->sl_rem_accents) |
| 9024 | { |
| 9025 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 9026 | { |
| 9027 | if (did_white) |
| 9028 | continue; |
| 9029 | c = ' '; |
| 9030 | did_white = TRUE; |
| 9031 | } |
| 9032 | else |
| 9033 | { |
| 9034 | did_white = FALSE; |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9035 | if (!spell_iswordp_nmw(t)) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9036 | continue; |
| 9037 | } |
| 9038 | } |
| 9039 | word[n++] = c; |
| 9040 | } |
| 9041 | word[n] = NUL; |
| 9042 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9043 | /* |
| 9044 | * This comes from Aspell phonet.cpp. |
| 9045 | * Converted from C++ to C. Added support for multi-byte chars. |
| 9046 | * Changed to keep spaces. |
| 9047 | */ |
| 9048 | i = reslen = z = 0; |
| 9049 | while ((c = word[i]) != NUL) |
| 9050 | { |
| 9051 | /* Start with the first rule that has the character in the word. */ |
| 9052 | n = slang->sl_sal_first[c & 0xff]; |
| 9053 | z0 = 0; |
| 9054 | |
| 9055 | if (n >= 0) |
| 9056 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9057 | /* check all rules for the same index byte */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9058 | for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n) |
| 9059 | { |
| 9060 | /* Quickly skip entries that don't match the word. Most |
| 9061 | * entries are less then three chars, optimize for that. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9062 | if (c != ws[0]) |
| 9063 | continue; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9064 | k = smp[n].sm_leadlen; |
| 9065 | if (k > 1) |
| 9066 | { |
| 9067 | if (word[i + 1] != ws[1]) |
| 9068 | continue; |
| 9069 | if (k > 2) |
| 9070 | { |
| 9071 | for (j = 2; j < k; ++j) |
| 9072 | if (word[i + j] != ws[j]) |
| 9073 | break; |
| 9074 | if (j < k) |
| 9075 | continue; |
| 9076 | } |
| 9077 | } |
| 9078 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9079 | if ((pf = smp[n].sm_oneof_w) != NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9080 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9081 | /* Check for match with one of the chars in "sm_oneof". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9082 | while (*pf != NUL && *pf != word[i + k]) |
| 9083 | ++pf; |
| 9084 | if (*pf == NUL) |
| 9085 | continue; |
| 9086 | ++k; |
| 9087 | } |
| 9088 | s = smp[n].sm_rules; |
| 9089 | pri = 5; /* default priority */ |
| 9090 | |
| 9091 | p0 = *s; |
| 9092 | k0 = k; |
| 9093 | while (*s == '-' && k > 1) |
| 9094 | { |
| 9095 | k--; |
| 9096 | s++; |
| 9097 | } |
| 9098 | if (*s == '<') |
| 9099 | s++; |
| 9100 | if (VIM_ISDIGIT(*s)) |
| 9101 | { |
| 9102 | /* determine priority */ |
| 9103 | pri = *s - '0'; |
| 9104 | s++; |
| 9105 | } |
| 9106 | if (*s == '^' && *(s + 1) == '^') |
| 9107 | s++; |
| 9108 | |
| 9109 | if (*s == NUL |
| 9110 | || (*s == '^' |
| 9111 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9112 | || spell_iswordp_w(word + i - 1, curbuf))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9113 | && (*(s + 1) != '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9114 | || (!spell_iswordp_w(word + i + k0, curbuf)))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9115 | || (*s == '$' && i > 0 |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9116 | && spell_iswordp_w(word + i - 1, curbuf) |
| 9117 | && (!spell_iswordp_w(word + i + k0, curbuf)))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9118 | { |
| 9119 | /* search for followup rules, if: */ |
| 9120 | /* followup and k > 1 and NO '-' in searchstring */ |
| 9121 | c0 = word[i + k - 1]; |
| 9122 | n0 = slang->sl_sal_first[c0 & 0xff]; |
| 9123 | |
| 9124 | if (slang->sl_followup && k > 1 && n0 >= 0 |
| 9125 | && p0 != '-' && word[i + k] != NUL) |
| 9126 | { |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9127 | /* Test follow-up rule for "word[i + k]"; loop over |
| 9128 | * all entries with the same index byte. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9129 | for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
| 9130 | == (c0 & 0xff); ++n0) |
| 9131 | { |
| 9132 | /* Quickly skip entries that don't match the word. |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9133 | */ |
| 9134 | if (c0 != ws[0]) |
| 9135 | continue; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9136 | k0 = smp[n0].sm_leadlen; |
| 9137 | if (k0 > 1) |
| 9138 | { |
| 9139 | if (word[i + k] != ws[1]) |
| 9140 | continue; |
| 9141 | if (k0 > 2) |
| 9142 | { |
| 9143 | pf = word + i + k + 1; |
| 9144 | for (j = 2; j < k0; ++j) |
| 9145 | if (*pf++ != ws[j]) |
| 9146 | break; |
| 9147 | if (j < k0) |
| 9148 | continue; |
| 9149 | } |
| 9150 | } |
| 9151 | k0 += k - 1; |
| 9152 | |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9153 | if ((pf = smp[n0].sm_oneof_w) != NULL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9154 | { |
| 9155 | /* Check for match with one of the chars in |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 9156 | * "sm_oneof". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9157 | while (*pf != NUL && *pf != word[i + k0]) |
| 9158 | ++pf; |
| 9159 | if (*pf == NUL) |
| 9160 | continue; |
| 9161 | ++k0; |
| 9162 | } |
| 9163 | |
| 9164 | p0 = 5; |
| 9165 | s = smp[n0].sm_rules; |
| 9166 | while (*s == '-') |
| 9167 | { |
| 9168 | /* "k0" gets NOT reduced because |
| 9169 | * "if (k0 == k)" */ |
| 9170 | s++; |
| 9171 | } |
| 9172 | if (*s == '<') |
| 9173 | s++; |
| 9174 | if (VIM_ISDIGIT(*s)) |
| 9175 | { |
| 9176 | p0 = *s - '0'; |
| 9177 | s++; |
| 9178 | } |
| 9179 | |
| 9180 | if (*s == NUL |
| 9181 | /* *s == '^' cuts */ |
| 9182 | || (*s == '$' |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9183 | && !spell_iswordp_w(word + i + k0, |
| 9184 | curbuf))) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9185 | { |
| 9186 | if (k0 == k) |
| 9187 | /* this is just a piece of the string */ |
| 9188 | continue; |
| 9189 | |
| 9190 | if (p0 < pri) |
| 9191 | /* priority too low */ |
| 9192 | continue; |
| 9193 | /* rule fits; stop search */ |
| 9194 | break; |
| 9195 | } |
| 9196 | } |
| 9197 | |
| 9198 | if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) |
| 9199 | == (c0 & 0xff)) |
| 9200 | continue; |
| 9201 | } |
| 9202 | |
| 9203 | /* replace string */ |
| 9204 | ws = smp[n].sm_to_w; |
| 9205 | s = smp[n].sm_rules; |
| 9206 | p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; |
| 9207 | if (p0 == 1 && z == 0) |
| 9208 | { |
| 9209 | /* rule with '<' is used */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 9210 | if (reslen > 0 && ws != NULL && *ws != NUL |
| 9211 | && (wres[reslen - 1] == c |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9212 | || wres[reslen - 1] == *ws)) |
| 9213 | reslen--; |
| 9214 | z0 = 1; |
| 9215 | z = 1; |
| 9216 | k0 = 0; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 9217 | if (ws != NULL) |
| 9218 | while (*ws != NUL && word[i + k0] != NUL) |
| 9219 | { |
| 9220 | word[i + k0] = *ws; |
| 9221 | k0++; |
| 9222 | ws++; |
| 9223 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9224 | if (k > k0) |
| 9225 | mch_memmove(word + i + k0, word + i + k, |
| 9226 | sizeof(int) * (STRLEN(word + i + k) + 1)); |
| 9227 | |
| 9228 | /* new "actual letter" */ |
| 9229 | c = word[i]; |
| 9230 | } |
| 9231 | else |
| 9232 | { |
| 9233 | /* no '<' rule used */ |
| 9234 | i += k - 1; |
| 9235 | z = 0; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 9236 | if (ws != NULL) |
| 9237 | while (*ws != NUL && ws[1] != NUL |
| 9238 | && reslen < MAXWLEN) |
| 9239 | { |
| 9240 | if (reslen == 0 || wres[reslen - 1] != *ws) |
| 9241 | wres[reslen++] = *ws; |
| 9242 | ws++; |
| 9243 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9244 | /* new "actual letter" */ |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 9245 | if (ws == NULL) |
| 9246 | c = NUL; |
| 9247 | else |
| 9248 | c = *ws; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9249 | if (strstr((char *)s, "^^") != NULL) |
| 9250 | { |
| 9251 | if (c != NUL) |
| 9252 | wres[reslen++] = c; |
| 9253 | mch_memmove(word, word + i + 1, |
| 9254 | sizeof(int) * (STRLEN(word + i + 1) + 1)); |
| 9255 | i = 0; |
| 9256 | z0 = 1; |
| 9257 | } |
| 9258 | } |
| 9259 | break; |
| 9260 | } |
| 9261 | } |
| 9262 | } |
| 9263 | else if (vim_iswhite(c)) |
| 9264 | { |
| 9265 | c = ' '; |
| 9266 | k = 1; |
| 9267 | } |
| 9268 | |
| 9269 | if (z0 == 0) |
| 9270 | { |
| 9271 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 9272 | && (!slang->sl_collapse || reslen == 0 |
| 9273 | || wres[reslen - 1] != c)) |
| 9274 | /* condense only double letters */ |
| 9275 | wres[reslen++] = c; |
| 9276 | |
| 9277 | i++; |
| 9278 | z = 0; |
| 9279 | k = 0; |
| 9280 | } |
| 9281 | } |
| 9282 | |
| 9283 | /* Convert wide characters in "wres" to a multi-byte string in "res". */ |
| 9284 | l = 0; |
| 9285 | for (n = 0; n < reslen; ++n) |
| 9286 | { |
| 9287 | l += mb_char2bytes(wres[n], res + l); |
| 9288 | if (l + MB_MAXBYTES > MAXWLEN) |
| 9289 | break; |
| 9290 | } |
| 9291 | res[l] = NUL; |
| 9292 | } |
| 9293 | #endif |
| 9294 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9295 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9296 | * Compute a score for two sound-a-like words. |
| 9297 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 9298 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 9299 | * avoiding checks that will not be possible. |
| 9300 | */ |
| 9301 | static int |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9302 | soundalike_score(goodstart, badstart) |
| 9303 | char_u *goodstart; /* sound-folded good word */ |
| 9304 | char_u *badstart; /* sound-folded bad word */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9305 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9306 | char_u *goodsound = goodstart; |
| 9307 | char_u *badsound = badstart; |
| 9308 | int goodlen; |
| 9309 | int badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9310 | int n; |
| 9311 | char_u *pl, *ps; |
| 9312 | char_u *pl2, *ps2; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9313 | int score = 0; |
| 9314 | |
| 9315 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 9316 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 9317 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 9318 | { |
| 9319 | score = SCORE_DEL / 2; |
| 9320 | if (*badsound == '*') |
| 9321 | ++badsound; |
| 9322 | else |
| 9323 | ++goodsound; |
| 9324 | } |
| 9325 | |
| 9326 | goodlen = STRLEN(goodsound); |
| 9327 | badlen = STRLEN(badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9328 | |
| 9329 | /* Return quickly if the lenghts are too different to be fixed by two |
| 9330 | * changes. */ |
| 9331 | n = goodlen - badlen; |
| 9332 | if (n < -2 || n > 2) |
| 9333 | return SCORE_MAXMAX; |
| 9334 | |
| 9335 | if (n > 0) |
| 9336 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9337 | pl = goodsound; /* goodsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9338 | ps = badsound; |
| 9339 | } |
| 9340 | else |
| 9341 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9342 | pl = badsound; /* badsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9343 | ps = goodsound; |
| 9344 | } |
| 9345 | |
| 9346 | /* Skip over the identical part. */ |
| 9347 | while (*pl == *ps && *pl != NUL) |
| 9348 | { |
| 9349 | ++pl; |
| 9350 | ++ps; |
| 9351 | } |
| 9352 | |
| 9353 | switch (n) |
| 9354 | { |
| 9355 | case -2: |
| 9356 | case 2: |
| 9357 | /* |
| 9358 | * Must delete two characters from "pl". |
| 9359 | */ |
| 9360 | ++pl; /* first delete */ |
| 9361 | while (*pl == *ps) |
| 9362 | { |
| 9363 | ++pl; |
| 9364 | ++ps; |
| 9365 | } |
| 9366 | /* strings must be equal after second delete */ |
| 9367 | if (STRCMP(pl + 1, ps) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9368 | return score + SCORE_DEL * 2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9369 | |
| 9370 | /* Failed to compare. */ |
| 9371 | break; |
| 9372 | |
| 9373 | case -1: |
| 9374 | case 1: |
| 9375 | /* |
| 9376 | * Minimal one delete from "pl" required. |
| 9377 | */ |
| 9378 | |
| 9379 | /* 1: delete */ |
| 9380 | pl2 = pl + 1; |
| 9381 | ps2 = ps; |
| 9382 | while (*pl2 == *ps2) |
| 9383 | { |
| 9384 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9385 | return score + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9386 | ++pl2; |
| 9387 | ++ps2; |
| 9388 | } |
| 9389 | |
| 9390 | /* 2: delete then swap, then rest must be equal */ |
| 9391 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9392 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9393 | return score + SCORE_DEL + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9394 | |
| 9395 | /* 3: delete then substitute, then the rest must be equal */ |
| 9396 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9397 | return score + SCORE_DEL + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9398 | |
| 9399 | /* 4: first swap then delete */ |
| 9400 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 9401 | { |
| 9402 | pl2 = pl + 2; /* swap, skip two chars */ |
| 9403 | ps2 = ps + 2; |
| 9404 | while (*pl2 == *ps2) |
| 9405 | { |
| 9406 | ++pl2; |
| 9407 | ++ps2; |
| 9408 | } |
| 9409 | /* delete a char and then strings must be equal */ |
| 9410 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9411 | return score + SCORE_SWAP + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9412 | } |
| 9413 | |
| 9414 | /* 5: first substitute then delete */ |
| 9415 | pl2 = pl + 1; /* substitute, skip one char */ |
| 9416 | ps2 = ps + 1; |
| 9417 | while (*pl2 == *ps2) |
| 9418 | { |
| 9419 | ++pl2; |
| 9420 | ++ps2; |
| 9421 | } |
| 9422 | /* delete a char and then strings must be equal */ |
| 9423 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9424 | return score + SCORE_SUBST + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9425 | |
| 9426 | /* Failed to compare. */ |
| 9427 | break; |
| 9428 | |
| 9429 | case 0: |
| 9430 | /* |
| 9431 | * Lenghts are equal, thus changes must result in same length: An |
| 9432 | * insert is only possible in combination with a delete. |
| 9433 | * 1: check if for identical strings |
| 9434 | */ |
| 9435 | if (*pl == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9436 | return score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9437 | |
| 9438 | /* 2: swap */ |
| 9439 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 9440 | { |
| 9441 | pl2 = pl + 2; /* swap, skip two chars */ |
| 9442 | ps2 = ps + 2; |
| 9443 | while (*pl2 == *ps2) |
| 9444 | { |
| 9445 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9446 | return score + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9447 | ++pl2; |
| 9448 | ++ps2; |
| 9449 | } |
| 9450 | /* 3: swap and swap again */ |
| 9451 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9452 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9453 | return score + SCORE_SWAP + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9454 | |
| 9455 | /* 4: swap and substitute */ |
| 9456 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9457 | return score + SCORE_SWAP + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9458 | } |
| 9459 | |
| 9460 | /* 5: substitute */ |
| 9461 | pl2 = pl + 1; |
| 9462 | ps2 = ps + 1; |
| 9463 | while (*pl2 == *ps2) |
| 9464 | { |
| 9465 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9466 | return score + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9467 | ++pl2; |
| 9468 | ++ps2; |
| 9469 | } |
| 9470 | |
| 9471 | /* 6: substitute and swap */ |
| 9472 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 9473 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9474 | return score + SCORE_SUBST + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9475 | |
| 9476 | /* 7: substitute and substitute */ |
| 9477 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9478 | return score + SCORE_SUBST + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9479 | |
| 9480 | /* 8: insert then delete */ |
| 9481 | pl2 = pl; |
| 9482 | ps2 = ps + 1; |
| 9483 | while (*pl2 == *ps2) |
| 9484 | { |
| 9485 | ++pl2; |
| 9486 | ++ps2; |
| 9487 | } |
| 9488 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9489 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9490 | |
| 9491 | /* 9: delete then insert */ |
| 9492 | pl2 = pl + 1; |
| 9493 | ps2 = ps; |
| 9494 | while (*pl2 == *ps2) |
| 9495 | { |
| 9496 | ++pl2; |
| 9497 | ++ps2; |
| 9498 | } |
| 9499 | if (STRCMP(pl2, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9500 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9501 | |
| 9502 | /* Failed to compare. */ |
| 9503 | break; |
| 9504 | } |
| 9505 | |
| 9506 | return SCORE_MAXMAX; |
| 9507 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9508 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9509 | /* |
| 9510 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9511 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9512 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9513 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9514 | * It has been converted from C++ to C and modified to support multi-byte |
| 9515 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9516 | */ |
| 9517 | static int |
| 9518 | spell_edit_score(badword, goodword) |
| 9519 | char_u *badword; |
| 9520 | char_u *goodword; |
| 9521 | { |
| 9522 | int *cnt; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9523 | int badlen, goodlen; /* lenghts including NUL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9524 | int j, i; |
| 9525 | int t; |
| 9526 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9527 | int pbc, pgc; |
| 9528 | #ifdef FEAT_MBYTE |
| 9529 | char_u *p; |
| 9530 | int wbadword[MAXWLEN]; |
| 9531 | int wgoodword[MAXWLEN]; |
| 9532 | |
| 9533 | if (has_mbyte) |
| 9534 | { |
| 9535 | /* Get the characters from the multi-byte strings and put them in an |
| 9536 | * int array for easy access. */ |
| 9537 | for (p = badword, badlen = 0; *p != NUL; ) |
| 9538 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
| 9539 | ++badlen; |
| 9540 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 9541 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
| 9542 | ++goodlen; |
| 9543 | } |
| 9544 | else |
| 9545 | #endif |
| 9546 | { |
| 9547 | badlen = STRLEN(badword) + 1; |
| 9548 | goodlen = STRLEN(goodword) + 1; |
| 9549 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9550 | |
| 9551 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 9552 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9553 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 9554 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9555 | if (cnt == NULL) |
| 9556 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9557 | |
| 9558 | CNT(0, 0) = 0; |
| 9559 | for (j = 1; j <= goodlen; ++j) |
| 9560 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 9561 | |
| 9562 | for (i = 1; i <= badlen; ++i) |
| 9563 | { |
| 9564 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 9565 | for (j = 1; j <= goodlen; ++j) |
| 9566 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9567 | #ifdef FEAT_MBYTE |
| 9568 | if (has_mbyte) |
| 9569 | { |
| 9570 | bc = wbadword[i - 1]; |
| 9571 | gc = wgoodword[j - 1]; |
| 9572 | } |
| 9573 | else |
| 9574 | #endif |
| 9575 | { |
| 9576 | bc = badword[i - 1]; |
| 9577 | gc = goodword[j - 1]; |
| 9578 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9579 | if (bc == gc) |
| 9580 | CNT(i, j) = CNT(i - 1, j - 1); |
| 9581 | else |
| 9582 | { |
| 9583 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9584 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9585 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 9586 | else |
| 9587 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 9588 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9589 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9590 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9591 | #ifdef FEAT_MBYTE |
| 9592 | if (has_mbyte) |
| 9593 | { |
| 9594 | pbc = wbadword[i - 2]; |
| 9595 | pgc = wgoodword[j - 2]; |
| 9596 | } |
| 9597 | else |
| 9598 | #endif |
| 9599 | { |
| 9600 | pbc = badword[i - 2]; |
| 9601 | pgc = goodword[j - 2]; |
| 9602 | } |
| 9603 | if (bc == pgc && pbc == gc) |
| 9604 | { |
| 9605 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 9606 | if (t < CNT(i, j)) |
| 9607 | CNT(i, j) = t; |
| 9608 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9609 | } |
| 9610 | t = SCORE_DEL + CNT(i - 1, j); |
| 9611 | if (t < CNT(i, j)) |
| 9612 | CNT(i, j) = t; |
| 9613 | t = SCORE_INS + CNT(i, j - 1); |
| 9614 | if (t < CNT(i, j)) |
| 9615 | CNT(i, j) = t; |
| 9616 | } |
| 9617 | } |
| 9618 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 9619 | |
| 9620 | i = CNT(badlen - 1, goodlen - 1); |
| 9621 | vim_free(cnt); |
| 9622 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9623 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9624 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9625 | /* |
| 9626 | * ":spelldump" |
| 9627 | */ |
| 9628 | /*ARGSUSED*/ |
| 9629 | void |
| 9630 | ex_spelldump(eap) |
| 9631 | exarg_T *eap; |
| 9632 | { |
| 9633 | buf_T *buf = curbuf; |
| 9634 | langp_T *lp; |
| 9635 | slang_T *slang; |
| 9636 | idx_T arridx[MAXWLEN]; |
| 9637 | int curi[MAXWLEN]; |
| 9638 | char_u word[MAXWLEN]; |
| 9639 | int c; |
| 9640 | char_u *byts; |
| 9641 | idx_T *idxs; |
| 9642 | linenr_T lnum = 0; |
| 9643 | int round; |
| 9644 | int depth; |
| 9645 | int n; |
| 9646 | int flags; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9647 | char_u *region_names = NULL; /* region names being used */ |
| 9648 | int do_region = TRUE; /* dump region names and numbers */ |
| 9649 | char_u *p; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9650 | |
| 9651 | if (no_spell_checking()) |
| 9652 | return; |
| 9653 | |
| 9654 | /* Create a new empty buffer by splitting the window. */ |
| 9655 | do_cmdline_cmd((char_u *)"new"); |
| 9656 | if (!bufempty() || !buf_valid(buf)) |
| 9657 | return; |
| 9658 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9659 | /* Find out if we can support regions: All languages must support the same |
| 9660 | * regions or none at all. */ |
| 9661 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 9662 | { |
| 9663 | p = lp->lp_slang->sl_regions; |
| 9664 | if (p[0] != 0) |
| 9665 | { |
| 9666 | if (region_names == NULL) /* first language with regions */ |
| 9667 | region_names = p; |
| 9668 | else if (STRCMP(region_names, p) != 0) |
| 9669 | { |
| 9670 | do_region = FALSE; /* region names are different */ |
| 9671 | break; |
| 9672 | } |
| 9673 | } |
| 9674 | } |
| 9675 | |
| 9676 | if (do_region && region_names != NULL) |
| 9677 | { |
| 9678 | vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); |
| 9679 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 9680 | } |
| 9681 | else |
| 9682 | do_region = FALSE; |
| 9683 | |
| 9684 | /* |
| 9685 | * Loop over all files loaded for the entries in 'spelllang'. |
| 9686 | */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9687 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 9688 | { |
| 9689 | slang = lp->lp_slang; |
| 9690 | |
| 9691 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 9692 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 9693 | |
| 9694 | /* round 1: case-folded tree |
| 9695 | * round 2: keep-case tree */ |
| 9696 | for (round = 1; round <= 2; ++round) |
| 9697 | { |
| 9698 | if (round == 1) |
| 9699 | { |
| 9700 | byts = slang->sl_fbyts; |
| 9701 | idxs = slang->sl_fidxs; |
| 9702 | } |
| 9703 | else |
| 9704 | { |
| 9705 | byts = slang->sl_kbyts; |
| 9706 | idxs = slang->sl_kidxs; |
| 9707 | } |
| 9708 | if (byts == NULL) |
| 9709 | continue; /* array is empty */ |
| 9710 | |
| 9711 | depth = 0; |
| 9712 | arridx[0] = 0; |
| 9713 | curi[0] = 1; |
| 9714 | while (depth >= 0 && !got_int) |
| 9715 | { |
| 9716 | if (curi[depth] > byts[arridx[depth]]) |
| 9717 | { |
| 9718 | /* Done all bytes at this node, go up one level. */ |
| 9719 | --depth; |
| 9720 | line_breakcheck(); |
| 9721 | } |
| 9722 | else |
| 9723 | { |
| 9724 | /* Do one more byte at this node. */ |
| 9725 | n = arridx[depth] + curi[depth]; |
| 9726 | ++curi[depth]; |
| 9727 | c = byts[n]; |
| 9728 | if (c == 0) |
| 9729 | { |
| 9730 | /* End of word, deal with the word. |
| 9731 | * Don't use keep-case words in the fold-case tree, |
| 9732 | * they will appear in the keep-case tree. |
| 9733 | * Only use the word when the region matches. */ |
| 9734 | flags = (int)idxs[n]; |
| 9735 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9736 | && (do_region |
| 9737 | || (flags & WF_REGION) == 0 |
| 9738 | || (((unsigned)flags >> 8) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9739 | & lp->lp_region) != 0)) |
| 9740 | { |
| 9741 | word[depth] = NUL; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9742 | if (!do_region) |
| 9743 | flags &= ~WF_REGION; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9744 | |
| 9745 | /* Dump the basic word if there is no prefix or |
| 9746 | * when it's the first one. */ |
| 9747 | c = (unsigned)flags >> 16; |
| 9748 | if (c == 0 || curi[depth] == 2) |
| 9749 | dump_word(word, round, flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9750 | |
| 9751 | /* Apply the prefix, if there is one. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 9752 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9753 | lnum = apply_prefixes(slang, word, round, |
| 9754 | flags, lnum); |
| 9755 | } |
| 9756 | } |
| 9757 | else |
| 9758 | { |
| 9759 | /* Normal char, go one level deeper. */ |
| 9760 | word[depth++] = c; |
| 9761 | arridx[depth] = idxs[n]; |
| 9762 | curi[depth] = 1; |
| 9763 | } |
| 9764 | } |
| 9765 | } |
| 9766 | } |
| 9767 | } |
| 9768 | |
| 9769 | /* Delete the empty line that we started with. */ |
| 9770 | if (curbuf->b_ml.ml_line_count > 1) |
| 9771 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 9772 | |
| 9773 | redraw_later(NOT_VALID); |
| 9774 | } |
| 9775 | |
| 9776 | /* |
| 9777 | * Dump one word: apply case modifications and append a line to the buffer. |
| 9778 | */ |
| 9779 | static void |
| 9780 | dump_word(word, round, flags, lnum) |
| 9781 | char_u *word; |
| 9782 | int round; |
| 9783 | int flags; |
| 9784 | linenr_T lnum; |
| 9785 | { |
| 9786 | int keepcap = FALSE; |
| 9787 | char_u *p; |
| 9788 | char_u cword[MAXWLEN]; |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9789 | char_u badword[MAXWLEN + 10]; |
| 9790 | int i; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9791 | |
| 9792 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
| 9793 | { |
| 9794 | /* Need to fix case according to "flags". */ |
| 9795 | make_case_word(word, cword, flags); |
| 9796 | p = cword; |
| 9797 | } |
| 9798 | else |
| 9799 | { |
| 9800 | p = word; |
Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame^] | 9801 | if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0 |
| 9802 | || (flags & WF_FIXCAP) != 0)) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9803 | keepcap = TRUE; |
| 9804 | } |
| 9805 | |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9806 | /* Add flags and regions after a slash. */ |
| 9807 | if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9808 | { |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9809 | STRCPY(badword, p); |
| 9810 | STRCAT(badword, "/"); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9811 | if (keepcap) |
| 9812 | STRCAT(badword, "="); |
| 9813 | if (flags & WF_BANNED) |
| 9814 | STRCAT(badword, "!"); |
| 9815 | else if (flags & WF_RARE) |
| 9816 | STRCAT(badword, "?"); |
Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9817 | if (flags & WF_REGION) |
| 9818 | for (i = 0; i < 7; ++i) |
| 9819 | if (flags & (0x100 << i)) |
| 9820 | sprintf((char *)badword + STRLEN(badword), "%d", i + 1); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9821 | p = badword; |
| 9822 | } |
| 9823 | |
| 9824 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 9825 | } |
| 9826 | |
| 9827 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9828 | * For ":spelldump": Find matching prefixes for "word". Prepend each to |
| 9829 | * "word" and append a line to the buffer. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9830 | * Return the updated line number. |
| 9831 | */ |
| 9832 | static linenr_T |
| 9833 | apply_prefixes(slang, word, round, flags, startlnum) |
| 9834 | slang_T *slang; |
| 9835 | char_u *word; /* case-folded word */ |
| 9836 | int round; |
| 9837 | int flags; /* flags with prefix ID */ |
| 9838 | linenr_T startlnum; |
| 9839 | { |
| 9840 | idx_T arridx[MAXWLEN]; |
| 9841 | int curi[MAXWLEN]; |
| 9842 | char_u prefix[MAXWLEN]; |
| 9843 | int c; |
| 9844 | char_u *byts; |
| 9845 | idx_T *idxs; |
| 9846 | linenr_T lnum = startlnum; |
| 9847 | int depth; |
| 9848 | int n; |
| 9849 | int len; |
| 9850 | int prefid = (unsigned)flags >> 16; |
| 9851 | int i; |
| 9852 | |
| 9853 | byts = slang->sl_pbyts; |
| 9854 | idxs = slang->sl_pidxs; |
| 9855 | if (byts != NULL) /* array not is empty */ |
| 9856 | { |
| 9857 | /* |
| 9858 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
| 9859 | * When at the end of a prefix check that it supports "prefid". |
| 9860 | */ |
| 9861 | depth = 0; |
| 9862 | arridx[0] = 0; |
| 9863 | curi[0] = 1; |
| 9864 | while (depth >= 0 && !got_int) |
| 9865 | { |
| 9866 | len = arridx[depth]; |
| 9867 | if (curi[depth] > byts[len]) |
| 9868 | { |
| 9869 | /* Done all bytes at this node, go up one level. */ |
| 9870 | --depth; |
| 9871 | line_breakcheck(); |
| 9872 | } |
| 9873 | else |
| 9874 | { |
| 9875 | /* Do one more byte at this node. */ |
| 9876 | n = len + curi[depth]; |
| 9877 | ++curi[depth]; |
| 9878 | c = byts[n]; |
| 9879 | if (c == 0) |
| 9880 | { |
| 9881 | /* End of prefix, find out how many IDs there are. */ |
| 9882 | for (i = 1; i < len; ++i) |
| 9883 | if (byts[n + i] != 0) |
| 9884 | break; |
| 9885 | curi[depth] += i - 1; |
| 9886 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9887 | i = valid_word_prefix(i, n, prefid, word, slang); |
| 9888 | if (i != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9889 | { |
Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9890 | vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9891 | dump_word(prefix, round, |
| 9892 | (i & WF_RAREPFX) ? (flags | WF_RARE) |
| 9893 | : flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9894 | } |
| 9895 | } |
| 9896 | else |
| 9897 | { |
| 9898 | /* Normal char, go one level deeper. */ |
| 9899 | prefix[depth++] = c; |
| 9900 | arridx[depth] = idxs[n]; |
| 9901 | curi[depth] = 1; |
| 9902 | } |
| 9903 | } |
| 9904 | } |
| 9905 | } |
| 9906 | |
| 9907 | return lnum; |
| 9908 | } |
| 9909 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9910 | #endif /* FEAT_SYN_HL */ |