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 |
| 137 | * |
| 138 | * <sal> : <salfromlen> <salfrom> <saltolen> <salto> |
| 139 | * |
| 140 | * <salfromlen> 1 byte length of <salfrom> |
| 141 | * |
| 142 | * <salfrom> N bytes "from" part of soundsalike |
| 143 | * |
| 144 | * <saltolen> 1 byte length of <salto> |
| 145 | * |
| 146 | * <salto> N bytes "to" part of soundsalike |
| 147 | * |
| 148 | * <maplen> 2 bytes length of <mapstr>, MSB first |
| 149 | * |
| 150 | * <mapstr> N bytes String with sequences of similar characters, |
| 151 | * separated by slashes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 152 | * |
| 153 | * |
| 154 | * <LWORDTREE>: <wordtree> |
| 155 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 156 | * <KWORDTREE>: <wordtree> |
| 157 | * |
| 158 | * <PREFIXTREE>: <wordtree> |
| 159 | * |
| 160 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 161 | * <wordtree>: <nodecount> <nodedata> ... |
| 162 | * |
| 163 | * <nodecount> 4 bytes Number of nodes following. MSB first. |
| 164 | * |
| 165 | * <nodedata>: <siblingcount> <sibling> ... |
| 166 | * |
| 167 | * <siblingcount> 1 byte Number of siblings in this node. The siblings |
| 168 | * follow in sorted order. |
| 169 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 170 | * <sibling>: <byte> [ <nodeidx> <xbyte> |
| 171 | * | <flags> [<region>] [<prefixID>] |
| 172 | * | <prefixID> <prefcondnr> ] |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 173 | * |
| 174 | * <byte> 1 byte Byte value of the sibling. Special cases: |
| 175 | * BY_NOFLAGS: End of word without flags and for all |
| 176 | * regions. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 177 | * For PREFIXTREE <prefixID> and |
| 178 | * <prefcondnr> follow. |
| 179 | * BY_FLAGS: End of word, <flags> follow. |
| 180 | * For PREFIXTREE <prefixID> and |
| 181 | * <prefcondnr> follow for rare prefix. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 182 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
| 183 | * and <xbyte> follow. |
| 184 | * |
| 185 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 186 | * |
| 187 | * <xbyte> 1 byte byte value of the sibling. |
| 188 | * |
| 189 | * <flags> 1 byte bitmask of: |
| 190 | * WF_ALLCAP word must have only capitals |
| 191 | * WF_ONECAP first char of word must be capital |
| 192 | * WF_RARE rare word |
| 193 | * WF_REGION <region> follows |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 194 | * WF_PFX <prefixID> follows |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 195 | * |
| 196 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 197 | * omitted it's valid in all regions. |
| 198 | * Lowest bit is for region 1. |
| 199 | * |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 200 | * <prefixID> 1 byte ID of prefix that can be used with this word. For |
| 201 | * PREFIXTREE used for the required prefix ID. |
| 202 | * |
| 203 | * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list |
| 204 | * from HEADER. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 205 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 206 | * All text characters are in 'encoding', but stored as single bytes. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 207 | */ |
| 208 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 209 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 210 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 211 | #endif |
| 212 | |
| 213 | #include "vim.h" |
| 214 | |
| 215 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 216 | |
| 217 | #ifdef HAVE_FCNTL_H |
| 218 | # include <fcntl.h> |
| 219 | #endif |
| 220 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 221 | #define MAXWLEN 250 /* Assume max. word len is this many bytes. |
| 222 | Some places assume a word length fits in a |
| 223 | byte, thus it can't be above 255. */ |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 224 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 225 | /* Type used for indexes in the word tree need to be at least 3 bytes. If int |
| 226 | * is 8 bytes we could use something smaller, but what? */ |
| 227 | #if SIZEOF_INT > 2 |
| 228 | typedef int idx_T; |
| 229 | #else |
| 230 | typedef long idx_T; |
| 231 | #endif |
| 232 | |
| 233 | /* Flags used for a word. Only the lowest byte can be used, the region byte |
| 234 | * comes above it. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 235 | #define WF_REGION 0x01 /* region byte follows */ |
| 236 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 237 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 238 | #define WF_RARE 0x08 /* rare word */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 239 | #define WF_BANNED 0x10 /* bad word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 240 | #define WF_PFX 0x20 /* prefix ID list follows */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 241 | #define WF_KEEPCAP 0x80 /* keep-case word */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 242 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 243 | #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 244 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 245 | #define WF_RAREPFX 0x1000000 /* in sl_pidxs: flag for rare postponed |
| 246 | prefix; must be above prefixID (one byte) |
| 247 | and prefcondnr (two bytes) */ |
| 248 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 249 | #define BY_NOFLAGS 0 /* end of word without flags or region */ |
| 250 | #define BY_FLAGS 1 /* end of word, flag byte follows */ |
| 251 | #define BY_INDEX 2 /* child is shared, index follows */ |
| 252 | #define BY_SPECIAL BY_INDEX /* hightest special byte value */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 253 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 254 | /* 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] | 255 | * and si_sal. Not for sl_sal! |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 256 | * One replacement: from "ft_from" to "ft_to". */ |
| 257 | typedef struct fromto_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 258 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 259 | char_u *ft_from; |
| 260 | char_u *ft_to; |
| 261 | } fromto_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 262 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 263 | /* Info from "SAL" entries in ".aff" file used in sl_sal. |
| 264 | * The info is split for quick processing by spell_soundfold(). |
| 265 | * Note that "sm_oneof" and "sm_rules" point into sm_lead. */ |
| 266 | typedef struct salitem_S |
| 267 | { |
| 268 | char_u *sm_lead; /* leading letters */ |
| 269 | int sm_leadlen; /* length of "sm_lead" */ |
| 270 | char_u *sm_oneoff; /* letters from () or NULL */ |
| 271 | char_u *sm_rules; /* rules like ^, $, priority */ |
| 272 | char_u *sm_to; /* replacement. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 273 | #ifdef FEAT_MBYTE |
| 274 | int *sm_lead_w; /* wide character copy of "sm_lead" */ |
| 275 | int *sm_oneoff_w; /* wide character copy of "sm_oneoff" */ |
| 276 | int *sm_to_w; /* wide character copy of "sm_to" */ |
| 277 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 278 | } salitem_T; |
| 279 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 280 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 281 | * Structure used to store words and other info for one language, loaded from |
| 282 | * a .spl file. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 283 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 284 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 285 | * |
| 286 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 287 | * the number of possible bytes, sorted on byte value: |
| 288 | * <len> <byte1> <byte2> ... |
| 289 | * The "idxs" array stores the index of the child node corresponding to the |
| 290 | * byte in "byts". |
| 291 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
| 292 | * the flags and region for the word. There may be several zeros in sequence |
| 293 | * for alternative flag/region combinations. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 294 | */ |
| 295 | typedef struct slang_S slang_T; |
| 296 | struct slang_S |
| 297 | { |
| 298 | slang_T *sl_next; /* next language */ |
| 299 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 300 | char_u *sl_fname; /* name of .spl file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 301 | int sl_add; /* TRUE if it's a .add file. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 302 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 303 | char_u *sl_fbyts; /* case-folded word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 304 | idx_T *sl_fidxs; /* case-folded word indexes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 305 | char_u *sl_kbyts; /* keep-case word bytes */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 306 | idx_T *sl_kidxs; /* keep-case word indexes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 307 | char_u *sl_pbyts; /* prefix tree word bytes */ |
| 308 | idx_T *sl_pidxs; /* prefix tree word indexes */ |
| 309 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 310 | 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] | 311 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 312 | int sl_prefixcnt; /* number of items in "sl_prefprog" */ |
| 313 | regprog_T **sl_prefprog; /* table with regprogs for prefixes */ |
| 314 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 315 | garray_T sl_rep; /* list of fromto_T entries from REP lines */ |
| 316 | short sl_rep_first[256]; /* indexes where byte first appears, -1 if |
| 317 | there is none */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 318 | garray_T sl_sal; /* list of salitem_T entries from SAL lines */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 319 | short sl_sal_first[256]; /* indexes where byte first appears, -1 if |
| 320 | there is none */ |
| 321 | int sl_followup; /* SAL followup */ |
| 322 | int sl_collapse; /* SAL collapse_result */ |
| 323 | int sl_rem_accents; /* SAL remove_accents */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 324 | int sl_has_map; /* TRUE if there is a MAP line */ |
| 325 | #ifdef FEAT_MBYTE |
| 326 | hashtab_T sl_map_hash; /* MAP for multi-byte chars */ |
| 327 | int sl_map_array[256]; /* MAP for first 256 chars */ |
| 328 | #else |
| 329 | char_u sl_map_array[256]; /* MAP for first 256 chars */ |
| 330 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 333 | /* First language that is loaded, start of the linked list of loaded |
| 334 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 335 | static slang_T *first_lang = NULL; |
| 336 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 337 | /* Flags used in .spl file for soundsalike flags. */ |
| 338 | #define SAL_F0LLOWUP 1 |
| 339 | #define SAL_COLLAPSE 2 |
| 340 | #define SAL_REM_ACCENTS 4 |
| 341 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 342 | /* |
| 343 | * Structure used in "b_langp", filled from 'spelllang'. |
| 344 | */ |
| 345 | typedef struct langp_S |
| 346 | { |
| 347 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 348 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 349 | } langp_T; |
| 350 | |
| 351 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 352 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 353 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 354 | |
| 355 | /* Result values. Lower number is accepted over higher one. */ |
| 356 | #define SP_BANNED -1 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 357 | #define SP_OK 0 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 358 | #define SP_RARE 1 |
| 359 | #define SP_LOCAL 2 |
| 360 | #define SP_BAD 3 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 361 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 362 | #define VIMSPELLMAGIC "VIMspell08" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 363 | #define VIMSPELLMAGICL 10 |
| 364 | |
| 365 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 366 | * Information used when looking for suggestions. |
| 367 | */ |
| 368 | typedef struct suginfo_S |
| 369 | { |
| 370 | garray_T su_ga; /* suggestions, contains "suggest_T" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 371 | int su_maxcount; /* max. number of suggestions displayed */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 372 | int su_maxscore; /* maximum score for adding to su_ga */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 373 | garray_T su_sga; /* like su_ga, sound-folded scoring */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 374 | char_u *su_badptr; /* start of bad word in line */ |
| 375 | int su_badlen; /* length of detected bad word in line */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 376 | int su_badflags; /* caps flags for bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 377 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 378 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| 379 | hashtab_T su_banned; /* table with banned words */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 380 | } suginfo_T; |
| 381 | |
| 382 | /* One word suggestion. Used in "si_ga". */ |
| 383 | typedef struct suggest_S |
| 384 | { |
| 385 | char_u *st_word; /* suggested word, allocated string */ |
| 386 | int st_orglen; /* length of replaced text */ |
| 387 | int st_score; /* lower is better */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 388 | int st_altscore; /* used when st_score compares equal */ |
| 389 | int st_salscore; /* st_score is for soundalike */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 390 | int st_had_bonus; /* bonus already included in score */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 391 | } suggest_T; |
| 392 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 393 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 394 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 395 | /* Number of suggestions kept when cleaning up. When rescore_suggestions() is |
| 396 | * called the score may change, thus we need to keep more than what is |
| 397 | * displayed. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 398 | #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] | 399 | |
| 400 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 401 | * of suggestions that are not going to be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 402 | #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 403 | |
| 404 | /* score for various changes */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 405 | #define SCORE_SPLIT 149 /* split bad word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 406 | #define SCORE_ICASE 52 /* slightly different case */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 407 | #define SCORE_REGION 70 /* word is for different region */ |
| 408 | #define SCORE_RARE 180 /* rare word */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 409 | #define SCORE_SWAP 90 /* swap two characters */ |
| 410 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| 411 | #define SCORE_REP 87 /* REP replacement */ |
| 412 | #define SCORE_SUBST 93 /* substitute a character */ |
| 413 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 414 | #define SCORE_DEL 94 /* delete a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 415 | #define SCORE_DELDUP 64 /* delete a duplicated character */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 416 | #define SCORE_INS 96 /* insert a character */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 417 | #define SCORE_INSDUP 66 /* insert a duplicate character */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 418 | #define SCORE_NONWORD 103 /* change non-word to word char */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 419 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 420 | #define SCORE_FILE 30 /* suggestion from a file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 421 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 422 | * 350 allows for about three changes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 423 | |
| 424 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 425 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 426 | |
| 427 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 428 | * Structure to store info for word matching. |
| 429 | */ |
| 430 | typedef struct matchinf_S |
| 431 | { |
| 432 | langp_T *mi_lp; /* info for language and region */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 433 | |
| 434 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 435 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 436 | char_u *mi_end; /* end of matching word so far */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 437 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 438 | char_u *mi_cend; /* char after what was used for |
| 439 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 440 | |
| 441 | /* case-folded text */ |
| 442 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 443 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 444 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 445 | /* for when checking word after a prefix */ |
| 446 | int mi_prefarridx; /* index in sl_pidxs with list of |
| 447 | prefixID/condition */ |
| 448 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 449 | int mi_prefixlen; /* byte length of prefix */ |
| 450 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 451 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 452 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 453 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 454 | } matchinf_T; |
| 455 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 456 | /* |
| 457 | * The tables used for recognizing word characters according to spelling. |
| 458 | * These are only used for the first 256 characters of 'encoding'. |
| 459 | */ |
| 460 | typedef struct spelltab_S |
| 461 | { |
| 462 | char_u st_isw[256]; /* flags: is word char */ |
| 463 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 464 | char_u st_fold[256]; /* chars: folded case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 465 | char_u st_upper[256]; /* chars: upper case */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 466 | } spelltab_T; |
| 467 | |
| 468 | static spelltab_T spelltab; |
| 469 | static int did_set_spelltab; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 470 | static char_u spell_ismw[256]; /* flags: is midword char */ |
| 471 | #ifdef FEAT_MBYTE |
| 472 | static char_u *spell_ismw_mb = NULL; /* multi-byte midword chars */ |
| 473 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 474 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 475 | #define CF_WORD 0x01 |
| 476 | #define CF_UPPER 0x02 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 477 | |
| 478 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 479 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 480 | static int spell_iswordp __ARGS((char_u *p)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 481 | static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 482 | |
| 483 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 484 | * Return TRUE if "p" points to a word character. Like spell_iswordp() but |
| 485 | * without the special handling of a single quote. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 486 | * Checking for a word character is done very often, avoid the function call |
| 487 | * overhead. |
| 488 | */ |
| 489 | #ifdef FEAT_MBYTE |
| 490 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 491 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 492 | #else |
| 493 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 494 | #endif |
| 495 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 496 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 497 | * 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] | 498 | */ |
| 499 | typedef enum |
| 500 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 501 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 502 | * ends); if badword ends there is a match, otherwise |
| 503 | * try splitting word. */ |
| 504 | STATE_SPLITUNDO, /* Undo splitting. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 505 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 506 | STATE_PLAIN, /* Use each byte of the node. */ |
| 507 | STATE_DEL, /* Delete a byte from the bad word. */ |
| 508 | STATE_INS, /* Insert a byte in the bad word. */ |
| 509 | STATE_SWAP, /* Swap two bytes. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 510 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 511 | STATE_SWAP3, /* Swap two characters over three. */ |
| 512 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 513 | STATE_UNROT3L, /* Undo rotate three characters left */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 514 | STATE_UNROT3R, /* Undo rotate three characters right */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 515 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 516 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 517 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 518 | STATE_FINAL /* End of this node. */ |
| 519 | } state_T; |
| 520 | |
| 521 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 522 | * Struct to keep the state at each level in suggest_try_change(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 523 | */ |
| 524 | typedef struct trystate_S |
| 525 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 526 | state_T ts_state; /* state at this level, STATE_ */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 527 | int ts_score; /* score */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 528 | idx_T ts_arridx; /* index in tree array, start of node */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 529 | short ts_curi; /* index in list of child nodes */ |
| 530 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 531 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 532 | char_u ts_twordlen; /* valid length of tword[] */ |
| 533 | #ifdef FEAT_MBYTE |
| 534 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 535 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 536 | char_u ts_isdiff; /* DIFF_ values */ |
| 537 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 538 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 539 | char_u ts_save_prewordlen; /* saved "prewordlen" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 540 | char_u ts_save_splitoff; /* su_splitoff saved here */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 541 | char_u ts_save_badflags; /* su_badflags saved here */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 542 | } trystate_T; |
| 543 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 544 | /* values for ts_isdiff */ |
| 545 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 546 | #define DIFF_YES 1 /* different byte found */ |
| 547 | #define DIFF_INSERT 2 /* inserting character */ |
| 548 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 549 | /* mode values for find_word */ |
| 550 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 551 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 552 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 553 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 554 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 555 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 556 | static void slang_clear __ARGS((slang_T *lp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 557 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 558 | 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] | 559 | static void find_prefix __ARGS((matchinf_T *mip)); |
| 560 | static int fold_more __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 561 | static int spell_valid_case __ARGS((int origflags, int treeflags)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 562 | static int no_spell_checking __ARGS((void)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 563 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 564 | static char_u *spell_enc __ARGS((void)); |
| 565 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 566 | static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 567 | #ifdef FEAT_MBYTE |
| 568 | static int *mb_str2wide __ARGS((char_u *s)); |
| 569 | #endif |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 570 | static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 571 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 572 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 573 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 574 | 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] | 575 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 576 | static void write_spell_chartab __ARGS((FILE *fd)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 577 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 578 | static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 579 | #ifdef FEAT_EVAL |
| 580 | static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr)); |
| 581 | #endif |
| 582 | static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname)); |
| 583 | static void spell_suggest_intern __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 584 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 585 | 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] | 586 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 587 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 588 | static void suggest_try_change __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 589 | static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add)); |
| 590 | 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] | 591 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 592 | static void score_combine __ARGS((suginfo_T *su)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 593 | 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] | 594 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 595 | 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] | 596 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 597 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 598 | 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] | 599 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| 600 | static int was_banned __ARGS((suginfo_T *su, char_u *word)); |
| 601 | static void free_banned __ARGS((suginfo_T *su)); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 602 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 603 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 604 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 605 | #ifdef FEAT_MBYTE |
| 606 | static void spell_soundfold_w __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| 607 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 608 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 609 | static int spell_edit_score __ARGS((char_u *badword, char_u *goodword)); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 610 | static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum)); |
| 611 | 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] | 612 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 613 | /* |
| 614 | * Use our own character-case definitions, because the current locale may |
| 615 | * differ from what the .spl file uses. |
| 616 | * These must not be called with negative number! |
| 617 | */ |
| 618 | #ifndef FEAT_MBYTE |
| 619 | /* Non-multi-byte implementation. */ |
| 620 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 621 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 622 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 623 | #else |
| 624 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 625 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 626 | * the "w" library function for characters above 255 if available. */ |
| 627 | # ifdef HAVE_TOWLOWER |
| 628 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 629 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 630 | # else |
| 631 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 632 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 633 | # endif |
| 634 | |
| 635 | # ifdef HAVE_TOWUPPER |
| 636 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 637 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 638 | # else |
| 639 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 640 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 641 | # endif |
| 642 | |
| 643 | # ifdef HAVE_ISWUPPER |
| 644 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 645 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 646 | # else |
| 647 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 648 | : (c) < 256 ? spelltab.st_isu[c] : (c)) |
| 649 | # endif |
| 650 | #endif |
| 651 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 652 | |
| 653 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 657 | * "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] | 658 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 659 | * or when it's OK it remains unchanged. |
| 660 | * This must only be called when 'spelllang' is not empty. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 661 | * |
| 662 | * "sug" is normally NULL. When looking for suggestions it points to |
| 663 | * suginfo_T. It's passed as a void pointer to keep the struct local. |
| 664 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 665 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 666 | * caller can skip over the word. |
| 667 | */ |
| 668 | int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 669 | spell_check(wp, ptr, attrp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 670 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 671 | char_u *ptr; |
| 672 | int *attrp; |
| 673 | { |
| 674 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 675 | be passed to functions quickly. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 676 | int nrlen = 0; /* found a number first */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 677 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 678 | /* A word never starts at a space or a control character. Return quickly |
| 679 | * then, skipping over the character. */ |
| 680 | if (*ptr <= ' ') |
| 681 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 682 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 683 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 684 | * 0X99FF. But when a word character follows do check spelling to find |
| 685 | * "3GPP". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 686 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 687 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 688 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 689 | mi.mi_end = skiphex(ptr + 2); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 690 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 691 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 692 | mi.mi_end = skipdigits(ptr); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 693 | nrlen = mi.mi_end - ptr; |
| 694 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 695 | if (!spell_iswordp(mi.mi_end)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 696 | return (int)(mi.mi_end - ptr); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 697 | |
| 698 | /* Try including the digits in the word. */ |
| 699 | mi.mi_fend = ptr + nrlen; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 700 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 701 | else |
| 702 | mi.mi_fend = ptr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 703 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 704 | /* 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] | 705 | mi.mi_word = ptr; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 706 | if (spell_iswordp(mi.mi_fend)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 707 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 708 | do |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 709 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 710 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 711 | } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 712 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 713 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 714 | /* We always use the characters up to the next non-word character, |
| 715 | * also for bad words. */ |
| 716 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 717 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 718 | /* Check caps type later. */ |
| 719 | mi.mi_capflags = 0; |
| 720 | mi.mi_cend = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 721 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 722 | /* Include one non-word character so that we can check for the |
| 723 | * word end. */ |
| 724 | if (*mi.mi_fend != NUL) |
| 725 | mb_ptr_adv(mi.mi_fend); |
| 726 | |
| 727 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 728 | MAXWLEN + 1); |
| 729 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 730 | |
| 731 | /* The word is bad unless we recognize it. */ |
| 732 | mi.mi_result = SP_BAD; |
| 733 | |
| 734 | /* |
| 735 | * Loop over the languages specified in 'spelllang'. |
| 736 | * We check them all, because a matching word may be longer than an |
| 737 | * already found matching word. |
| 738 | */ |
| 739 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 740 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 741 | { |
| 742 | /* Check for a matching word in case-folded words. */ |
| 743 | find_word(&mi, FIND_FOLDWORD); |
| 744 | |
| 745 | /* Check for a matching word in keep-case words. */ |
| 746 | find_word(&mi, FIND_KEEPWORD); |
| 747 | |
| 748 | /* Check for matching prefixes. */ |
| 749 | find_prefix(&mi); |
| 750 | } |
| 751 | |
| 752 | if (mi.mi_result != SP_OK) |
| 753 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 754 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 755 | * rare and local words, e.g., "3GPP". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 756 | if (nrlen > 0) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 757 | { |
| 758 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 759 | return nrlen; |
| 760 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 761 | |
| 762 | /* When we are at a non-word character there is no error, just |
| 763 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 764 | else if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 765 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 766 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 767 | if (has_mbyte) |
| 768 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 769 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 770 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 771 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 772 | |
| 773 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 774 | *attrp = highlight_attr[HLF_SPB]; |
| 775 | else if (mi.mi_result == SP_RARE) |
| 776 | *attrp = highlight_attr[HLF_SPR]; |
| 777 | else |
| 778 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 781 | return (int)(mi.mi_end - ptr); |
| 782 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 783 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 784 | /* |
| 785 | * Check if the word at "mip->mi_word" is in the tree. |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 786 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 787 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 788 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 789 | * tree. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 790 | * |
| 791 | * For a match mip->mi_result is updated. |
| 792 | */ |
| 793 | static void |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 794 | find_word(mip, mode) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 795 | matchinf_T *mip; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 796 | int mode; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 797 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 798 | idx_T arridx = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 799 | int endlen[MAXWLEN]; /* length at possible word endings */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 800 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 801 | int endidxcnt = 0; |
| 802 | int len; |
| 803 | int wlen = 0; |
| 804 | int flen; |
| 805 | int c; |
| 806 | char_u *ptr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 807 | idx_T lo, hi, m; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 808 | #ifdef FEAT_MBYTE |
| 809 | char_u *s; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 810 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 811 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 812 | int res = SP_BAD; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 813 | slang_T *slang = mip->mi_lp->lp_slang; |
| 814 | unsigned flags; |
| 815 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 816 | idx_T *idxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 817 | int prefid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 818 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 819 | if (mode == FIND_KEEPWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 820 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 821 | /* Check for word with matching case in keep-case tree. */ |
| 822 | ptr = mip->mi_word; |
| 823 | flen = 9999; /* no case folding, always enough bytes */ |
| 824 | byts = slang->sl_kbyts; |
| 825 | idxs = slang->sl_kidxs; |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | /* Check for case-folded in case-folded tree. */ |
| 830 | ptr = mip->mi_fword; |
| 831 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 832 | byts = slang->sl_fbyts; |
| 833 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 834 | |
| 835 | if (mode == FIND_PREFIX) |
| 836 | { |
| 837 | /* Skip over the prefix. */ |
| 838 | wlen = mip->mi_prefixlen; |
| 839 | flen -= mip->mi_prefixlen; |
| 840 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 843 | if (byts == NULL) |
| 844 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 845 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 846 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 847 | * Repeat advancing in the tree until: |
| 848 | * - there is a byte that doesn't match, |
| 849 | * - we reach the end of the tree, |
| 850 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 851 | */ |
| 852 | for (;;) |
| 853 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 854 | if (flen <= 0 && *mip->mi_fend != NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 855 | flen = fold_more(mip); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 856 | |
| 857 | len = byts[arridx++]; |
| 858 | |
| 859 | /* If the first possible byte is a zero the word could end here. |
| 860 | * Remember this index, we first check for the longest word. */ |
| 861 | if (byts[arridx] == 0) |
| 862 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 863 | if (endidxcnt == MAXWLEN) |
| 864 | { |
| 865 | /* Must be a corrupted spell file. */ |
| 866 | EMSG(_(e_format)); |
| 867 | return; |
| 868 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 869 | endlen[endidxcnt] = wlen; |
| 870 | endidx[endidxcnt++] = arridx++; |
| 871 | --len; |
| 872 | |
| 873 | /* Skip over the zeros, there can be several flag/region |
| 874 | * combinations. */ |
| 875 | while (len > 0 && byts[arridx] == 0) |
| 876 | { |
| 877 | ++arridx; |
| 878 | --len; |
| 879 | } |
| 880 | if (len == 0) |
| 881 | break; /* no children, word must end here */ |
| 882 | } |
| 883 | |
| 884 | /* Stop looking at end of the line. */ |
| 885 | if (ptr[wlen] == NUL) |
| 886 | break; |
| 887 | |
| 888 | /* Perform a binary search in the list of accepted bytes. */ |
| 889 | c = ptr[wlen]; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 890 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 891 | c = ' '; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 892 | lo = arridx; |
| 893 | hi = arridx + len - 1; |
| 894 | while (lo < hi) |
| 895 | { |
| 896 | m = (lo + hi) / 2; |
| 897 | if (byts[m] > c) |
| 898 | hi = m - 1; |
| 899 | else if (byts[m] < c) |
| 900 | lo = m + 1; |
| 901 | else |
| 902 | { |
| 903 | lo = hi = m; |
| 904 | break; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /* Stop if there is no matching byte. */ |
| 909 | if (hi < lo || byts[lo] != c) |
| 910 | break; |
| 911 | |
| 912 | /* Continue at the child (if there is one). */ |
| 913 | arridx = idxs[lo]; |
| 914 | ++wlen; |
| 915 | --flen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 916 | |
| 917 | /* One space in the good word may stand for several spaces in the |
| 918 | * checked word. */ |
| 919 | if (c == ' ') |
| 920 | { |
| 921 | for (;;) |
| 922 | { |
| 923 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 924 | flen = fold_more(mip); |
| 925 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 926 | break; |
| 927 | ++wlen; |
| 928 | --flen; |
| 929 | } |
| 930 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | /* |
| 934 | * Verify that one of the possible endings is valid. Try the longest |
| 935 | * first. |
| 936 | */ |
| 937 | while (endidxcnt > 0) |
| 938 | { |
| 939 | --endidxcnt; |
| 940 | arridx = endidx[endidxcnt]; |
| 941 | wlen = endlen[endidxcnt]; |
| 942 | |
| 943 | #ifdef FEAT_MBYTE |
| 944 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 945 | continue; /* not at first byte of character */ |
| 946 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 947 | if (spell_iswordp(ptr + wlen)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 948 | continue; /* next char is a word character */ |
| 949 | |
| 950 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 951 | if (mode != FIND_KEEPWORD && has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 952 | { |
| 953 | /* Compute byte length in original word, length may change |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 954 | * when folding case. This can be slow, take a shortcut when the |
| 955 | * case-folded word is equal to the keep-case word. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 956 | p = mip->mi_word; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 957 | if (STRNCMP(ptr, p, wlen) != 0) |
| 958 | { |
| 959 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 960 | mb_ptr_adv(p); |
| 961 | wlen = p - mip->mi_word; |
| 962 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 963 | } |
| 964 | #endif |
| 965 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 966 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 967 | * prefix ID. |
| 968 | * Repeat this if there are more flags/region alternatives until there |
| 969 | * is a match. */ |
| 970 | res = SP_BAD; |
| 971 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 972 | --len, ++arridx) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 973 | { |
| 974 | flags = idxs[arridx]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 975 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 976 | /* For the fold-case tree check that the case of the checked word |
| 977 | * matches with what the word in the tree requires. |
| 978 | * For keep-case tree the case is always right. For prefixes we |
| 979 | * don't bother to check. */ |
| 980 | if (mode == FIND_FOLDWORD) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 981 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 982 | if (mip->mi_cend != mip->mi_word + wlen) |
| 983 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 984 | /* mi_capflags was set for a different word length, need |
| 985 | * to do it again. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 986 | mip->mi_cend = mip->mi_word + wlen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 987 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 990 | if (mip->mi_capflags == WF_KEEPCAP |
| 991 | || !spell_valid_case(mip->mi_capflags, flags)) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 992 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 995 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 996 | * 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] | 997 | * mip->mi_prefarridx that find_prefix() filled. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 998 | if (mode == FIND_PREFIX) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 999 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1000 | /* The prefix ID is stored two bytes above the flags. */ |
| 1001 | prefid = (unsigned)flags >> 16; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1002 | c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1003 | prefid, mip->mi_fword + mip->mi_prefixlen, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1004 | slang); |
| 1005 | if (c == 0) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1006 | continue; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1007 | |
| 1008 | /* Use the WF_RARE flag for a rare prefix. */ |
| 1009 | if (c & WF_RAREPFX) |
| 1010 | flags |= WF_RARE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | if (flags & WF_BANNED) |
| 1014 | res = SP_BANNED; |
| 1015 | else if (flags & WF_REGION) |
| 1016 | { |
| 1017 | /* Check region. */ |
| 1018 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 1019 | res = SP_OK; |
| 1020 | else |
| 1021 | res = SP_LOCAL; |
| 1022 | } |
| 1023 | else if (flags & WF_RARE) |
| 1024 | res = SP_RARE; |
| 1025 | else |
| 1026 | res = SP_OK; |
| 1027 | |
| 1028 | /* Always use the longest match and the best result. */ |
| 1029 | if (mip->mi_result > res) |
| 1030 | { |
| 1031 | mip->mi_result = res; |
| 1032 | mip->mi_end = mip->mi_word + wlen; |
| 1033 | } |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1034 | 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] | 1035 | mip->mi_end = mip->mi_word + wlen; |
| 1036 | |
| 1037 | if (res == SP_OK) |
| 1038 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1041 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1042 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1043 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1046 | /* |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1047 | * Return non-zero if the prefix indicated by "mip->mi_prefarridx" matches |
| 1048 | * with the prefix ID "prefid" for the word "word". |
| 1049 | * 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] | 1050 | */ |
| 1051 | static int |
| 1052 | valid_word_prefix(totprefcnt, arridx, prefid, word, slang) |
| 1053 | int totprefcnt; /* nr of prefix IDs */ |
| 1054 | int arridx; /* idx in sl_pidxs[] */ |
| 1055 | int prefid; |
| 1056 | char_u *word; |
| 1057 | slang_T *slang; |
| 1058 | { |
| 1059 | int prefcnt; |
| 1060 | int pidx; |
| 1061 | regprog_T *rp; |
| 1062 | regmatch_T regmatch; |
| 1063 | |
| 1064 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1065 | { |
| 1066 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1067 | |
| 1068 | /* Check the prefix ID. */ |
| 1069 | if (prefid != (pidx & 0xff)) |
| 1070 | continue; |
| 1071 | |
| 1072 | /* Check the condition, if there is one. The condition index is |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1073 | * stored in the two bytes above the prefix ID byte. */ |
| 1074 | rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1075 | if (rp != NULL) |
| 1076 | { |
| 1077 | regmatch.regprog = rp; |
| 1078 | regmatch.rm_ic = FALSE; |
| 1079 | if (!vim_regexec(®match, word, 0)) |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1083 | /* It's a match! Return the WF_RAREPFX flag. */ |
| 1084 | return pidx; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1085 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1086 | return 0; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1090 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1091 | * If it does, then check the following word. |
| 1092 | * |
| 1093 | * For a match mip->mi_result is updated. |
| 1094 | */ |
| 1095 | static void |
| 1096 | find_prefix(mip) |
| 1097 | matchinf_T *mip; |
| 1098 | { |
| 1099 | idx_T arridx = 0; |
| 1100 | int len; |
| 1101 | int wlen = 0; |
| 1102 | int flen; |
| 1103 | int c; |
| 1104 | char_u *ptr; |
| 1105 | idx_T lo, hi, m; |
| 1106 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1107 | char_u *byts; |
| 1108 | idx_T *idxs; |
| 1109 | |
| 1110 | /* We use the case-folded word here, since prefixes are always |
| 1111 | * case-folded. */ |
| 1112 | ptr = mip->mi_fword; |
| 1113 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 1114 | byts = slang->sl_pbyts; |
| 1115 | idxs = slang->sl_pidxs; |
| 1116 | |
| 1117 | if (byts == NULL) |
| 1118 | return; /* array is empty */ |
| 1119 | |
| 1120 | /* |
| 1121 | * Repeat advancing in the tree until: |
| 1122 | * - there is a byte that doesn't match, |
| 1123 | * - we reach the end of the tree, |
| 1124 | * - or we reach the end of the line. |
| 1125 | */ |
| 1126 | for (;;) |
| 1127 | { |
| 1128 | if (flen == 0 && *mip->mi_fend != NUL) |
| 1129 | flen = fold_more(mip); |
| 1130 | |
| 1131 | len = byts[arridx++]; |
| 1132 | |
| 1133 | /* If the first possible byte is a zero the prefix could end here. |
| 1134 | * Check if the following word matches and supports the prefix. */ |
| 1135 | if (byts[arridx] == 0) |
| 1136 | { |
| 1137 | /* There can be several prefixes with different conditions. We |
| 1138 | * try them all, since we don't know which one will give the |
| 1139 | * longest match. The word is the same each time, pass the list |
| 1140 | * of possible prefixes to find_word(). */ |
| 1141 | mip->mi_prefarridx = arridx; |
| 1142 | mip->mi_prefcnt = len; |
| 1143 | while (len > 0 && byts[arridx] == 0) |
| 1144 | { |
| 1145 | ++arridx; |
| 1146 | --len; |
| 1147 | } |
| 1148 | mip->mi_prefcnt -= len; |
| 1149 | |
| 1150 | /* Find the word that comes after the prefix. */ |
| 1151 | mip->mi_prefixlen = wlen; |
| 1152 | find_word(mip, FIND_PREFIX); |
| 1153 | |
| 1154 | |
| 1155 | if (len == 0) |
| 1156 | break; /* no children, word must end here */ |
| 1157 | } |
| 1158 | |
| 1159 | /* Stop looking at end of the line. */ |
| 1160 | if (ptr[wlen] == NUL) |
| 1161 | break; |
| 1162 | |
| 1163 | /* Perform a binary search in the list of accepted bytes. */ |
| 1164 | c = ptr[wlen]; |
| 1165 | lo = arridx; |
| 1166 | hi = arridx + len - 1; |
| 1167 | while (lo < hi) |
| 1168 | { |
| 1169 | m = (lo + hi) / 2; |
| 1170 | if (byts[m] > c) |
| 1171 | hi = m - 1; |
| 1172 | else if (byts[m] < c) |
| 1173 | lo = m + 1; |
| 1174 | else |
| 1175 | { |
| 1176 | lo = hi = m; |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /* Stop if there is no matching byte. */ |
| 1182 | if (hi < lo || byts[lo] != c) |
| 1183 | break; |
| 1184 | |
| 1185 | /* Continue at the child (if there is one). */ |
| 1186 | arridx = idxs[lo]; |
| 1187 | ++wlen; |
| 1188 | --flen; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Need to fold at least one more character. Do until next non-word character |
| 1194 | * for efficiency. |
| 1195 | * Return the length of the folded chars in bytes. |
| 1196 | */ |
| 1197 | static int |
| 1198 | fold_more(mip) |
| 1199 | matchinf_T *mip; |
| 1200 | { |
| 1201 | int flen; |
| 1202 | char_u *p; |
| 1203 | |
| 1204 | p = mip->mi_fend; |
| 1205 | do |
| 1206 | { |
| 1207 | mb_ptr_adv(mip->mi_fend); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1208 | } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* Include the non-word character so that we can check for the |
| 1211 | * word end. */ |
| 1212 | if (*mip->mi_fend != NUL) |
| 1213 | mb_ptr_adv(mip->mi_fend); |
| 1214 | |
| 1215 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 1216 | mip->mi_fword + mip->mi_fwordlen, |
| 1217 | MAXWLEN - mip->mi_fwordlen); |
| 1218 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 1219 | mip->mi_fwordlen += flen; |
| 1220 | return flen; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1224 | * Check case flags for a word. Return TRUE if the word has the requested |
| 1225 | * case. |
| 1226 | */ |
| 1227 | static int |
| 1228 | spell_valid_case(origflags, treeflags) |
| 1229 | int origflags; /* flags for the checked word. */ |
| 1230 | int treeflags; /* flags for the word in the spell tree */ |
| 1231 | { |
| 1232 | return (origflags == WF_ALLCAP |
| 1233 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
| 1234 | && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP))); |
| 1235 | } |
| 1236 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1237 | /* |
| 1238 | * Return TRUE if spell checking is not enabled. |
| 1239 | */ |
| 1240 | static int |
| 1241 | no_spell_checking() |
| 1242 | { |
| 1243 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 1244 | { |
| 1245 | EMSG(_("E756: Spell checking is not enabled")); |
| 1246 | return TRUE; |
| 1247 | } |
| 1248 | return FALSE; |
| 1249 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1250 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1251 | /* |
| 1252 | * Move to next spell error. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1253 | * "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] | 1254 | * Return OK if found, FAIL otherwise. |
| 1255 | */ |
| 1256 | int |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1257 | spell_move_to(dir, allwords, curline) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1258 | int dir; /* FORWARD or BACKWARD */ |
| 1259 | int allwords; /* TRUE for "[s" and "]s" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1260 | int curline; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1261 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1262 | linenr_T lnum; |
| 1263 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1264 | char_u *line; |
| 1265 | char_u *p; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1266 | char_u *endp; |
| 1267 | int attr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1268 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1269 | int has_syntax = syntax_present(curbuf); |
| 1270 | int col; |
| 1271 | int can_spell; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1272 | char_u *buf = NULL; |
| 1273 | int buflen = 0; |
| 1274 | int skip = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1275 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1276 | if (no_spell_checking()) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1277 | return FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1278 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1279 | /* |
| 1280 | * 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] | 1281 | * 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] | 1282 | * |
| 1283 | * When searching backwards, we continue in the line to find the last |
| 1284 | * bad word (in the cursor line: before the cursor). |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1285 | * |
| 1286 | * We concatenate the start of the next line, so that wrapped words work |
| 1287 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 1288 | * though... |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1289 | */ |
| 1290 | lnum = curwin->w_cursor.lnum; |
| 1291 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1292 | |
| 1293 | while (!got_int) |
| 1294 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1295 | line = ml_get(lnum); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1296 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1297 | len = STRLEN(line); |
| 1298 | if (buflen < len + MAXWLEN + 2) |
| 1299 | { |
| 1300 | vim_free(buf); |
| 1301 | buflen = len + MAXWLEN + 2; |
| 1302 | buf = alloc(buflen); |
| 1303 | if (buf == NULL) |
| 1304 | break; |
| 1305 | } |
| 1306 | |
| 1307 | /* Copy the line into "buf" and append the start of the next line if |
| 1308 | * possible. */ |
| 1309 | STRCPY(buf, line); |
| 1310 | if (lnum < curbuf->b_ml.ml_line_count) |
| 1311 | spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN); |
| 1312 | |
| 1313 | p = buf + skip; |
| 1314 | endp = buf + len; |
| 1315 | while (p < endp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1316 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1317 | /* When searching backward don't search after the cursor. */ |
| 1318 | if (dir == BACKWARD |
| 1319 | && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1320 | && (colnr_T)(p - buf) >= curwin->w_cursor.col) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | |
| 1323 | /* start of word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1324 | attr = 0; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1325 | len = spell_check(curwin, p, &attr); |
| 1326 | |
| 1327 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1328 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1329 | /* We found a bad word. Check the attribute. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1330 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1331 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1332 | /* When searching forward only accept a bad word after |
| 1333 | * the cursor. */ |
| 1334 | if (dir == BACKWARD |
| 1335 | || lnum > curwin->w_cursor.lnum |
| 1336 | || (lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1337 | && (colnr_T)(curline ? p - buf + len |
| 1338 | : p - buf) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1339 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1340 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1341 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1342 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1343 | col = p - buf; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1344 | (void)syn_get_id(lnum, (colnr_T)col, |
| 1345 | FALSE, &can_spell); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1346 | } |
| 1347 | else |
| 1348 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1349 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1350 | if (can_spell) |
| 1351 | { |
| 1352 | found_pos.lnum = lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1353 | found_pos.col = p - buf; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1354 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1355 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1356 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1357 | if (dir == FORWARD) |
| 1358 | { |
| 1359 | /* No need to search further. */ |
| 1360 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1361 | vim_free(buf); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1362 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1365 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1366 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1369 | /* advance to character after the word */ |
| 1370 | p += len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1373 | if (curline) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1374 | break; /* only check cursor line */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1375 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1376 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1377 | if (dir == BACKWARD) |
| 1378 | { |
| 1379 | if (found_pos.lnum != 0) |
| 1380 | { |
| 1381 | /* Use the last match in the line. */ |
| 1382 | curwin->w_cursor = found_pos; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1383 | vim_free(buf); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1384 | return OK; |
| 1385 | } |
| 1386 | if (lnum == 1) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1387 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1388 | --lnum; |
| 1389 | } |
| 1390 | else |
| 1391 | { |
| 1392 | if (lnum == curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1393 | break; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1394 | ++lnum; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* Skip the characters at the start of the next line that were |
| 1397 | * included in a match crossing line boundaries. */ |
| 1398 | if (attr == 0) |
| 1399 | skip = p - endp; |
| 1400 | else |
| 1401 | skip = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 1402 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1403 | |
| 1404 | line_breakcheck(); |
| 1405 | } |
| 1406 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1407 | vim_free(buf); |
| 1408 | return FAIL; |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * For spell checking: concatenate the start of the following line "line" into |
| 1413 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| 1414 | */ |
| 1415 | void |
| 1416 | spell_cat_line(buf, line, maxlen) |
| 1417 | char_u *buf; |
| 1418 | char_u *line; |
| 1419 | int maxlen; |
| 1420 | { |
| 1421 | char_u *p; |
| 1422 | int n; |
| 1423 | |
| 1424 | p = skipwhite(line); |
| 1425 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 1426 | p = skipwhite(p + 1); |
| 1427 | |
| 1428 | if (*p != NUL) |
| 1429 | { |
| 1430 | *buf = ' '; |
| 1431 | vim_strncpy(buf + 1, line, maxlen - 1); |
| 1432 | n = p - line; |
| 1433 | if (n >= maxlen) |
| 1434 | n = maxlen - 1; |
| 1435 | vim_memset(buf + 1, ' ', n); |
| 1436 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1440 | * Load word list(s) for "lang" from Vim spell file(s). |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1441 | * "lang" must be the language without the region: e.g., "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1442 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1443 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1444 | spell_load_lang(lang) |
| 1445 | char_u *lang; |
| 1446 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1447 | char_u fname_enc[85]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1448 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1449 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1450 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1451 | /* 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] | 1452 | * It's truncated when an error is detected. */ |
| 1453 | STRCPY(langcp, lang); |
| 1454 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1455 | /* |
| 1456 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 1457 | */ |
| 1458 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 1459 | "spell/%s.%s.spl", lang, spell_enc()); |
| 1460 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1461 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1462 | if (r == FAIL && *langcp != NUL) |
| 1463 | { |
| 1464 | /* Try loading the ASCII version. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1465 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1466 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1467 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1470 | if (r == FAIL) |
| 1471 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1472 | fname_enc + 6); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1473 | else if (*langcp != NUL) |
| 1474 | { |
| 1475 | /* Load all the additions. */ |
| 1476 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| 1477 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 1483 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 1484 | */ |
| 1485 | static char_u * |
| 1486 | spell_enc() |
| 1487 | { |
| 1488 | |
| 1489 | #ifdef FEAT_MBYTE |
| 1490 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1491 | return p_enc; |
| 1492 | #endif |
| 1493 | return (char_u *)"latin1"; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * Allocate a new slang_T. |
| 1498 | * Caller must fill "sl_next". |
| 1499 | */ |
| 1500 | static slang_T * |
| 1501 | slang_alloc(lang) |
| 1502 | char_u *lang; |
| 1503 | { |
| 1504 | slang_T *lp; |
| 1505 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1506 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1507 | if (lp != NULL) |
| 1508 | { |
| 1509 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1510 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1511 | ga_init2(&lp->sl_sal, sizeof(salitem_T), 10); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1512 | } |
| 1513 | return lp; |
| 1514 | } |
| 1515 | |
| 1516 | /* |
| 1517 | * Free the contents of an slang_T and the structure itself. |
| 1518 | */ |
| 1519 | static void |
| 1520 | slang_free(lp) |
| 1521 | slang_T *lp; |
| 1522 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1523 | vim_free(lp->sl_name); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1524 | vim_free(lp->sl_fname); |
| 1525 | slang_clear(lp); |
| 1526 | vim_free(lp); |
| 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * Clear an slang_T so that the file can be reloaded. |
| 1531 | */ |
| 1532 | static void |
| 1533 | slang_clear(lp) |
| 1534 | slang_T *lp; |
| 1535 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1536 | garray_T *gap; |
| 1537 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1538 | salitem_T *smp; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1539 | int i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1540 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1541 | vim_free(lp->sl_fbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1542 | lp->sl_fbyts = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1543 | vim_free(lp->sl_kbyts); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1544 | lp->sl_kbyts = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1545 | vim_free(lp->sl_pbyts); |
| 1546 | lp->sl_pbyts = NULL; |
| 1547 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1548 | vim_free(lp->sl_fidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1549 | lp->sl_fidxs = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1550 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1551 | lp->sl_kidxs = NULL; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1552 | vim_free(lp->sl_pidxs); |
| 1553 | lp->sl_pidxs = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1554 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1555 | gap = &lp->sl_rep; |
| 1556 | while (gap->ga_len > 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1557 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1558 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 1559 | vim_free(ftp->ft_from); |
| 1560 | vim_free(ftp->ft_to); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1561 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1562 | ga_clear(gap); |
| 1563 | |
| 1564 | gap = &lp->sl_sal; |
| 1565 | while (gap->ga_len > 0) |
| 1566 | { |
| 1567 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 1568 | vim_free(smp->sm_lead); |
| 1569 | vim_free(smp->sm_to); |
| 1570 | } |
| 1571 | ga_clear(gap); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1572 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1573 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 1574 | vim_free(lp->sl_prefprog[i]); |
| 1575 | vim_free(lp->sl_prefprog); |
| 1576 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 1577 | #ifdef FEAT_MBYTE |
| 1578 | { |
| 1579 | int todo = lp->sl_map_hash.ht_used; |
| 1580 | hashitem_T *hi; |
| 1581 | |
| 1582 | for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi) |
| 1583 | if (!HASHITEM_EMPTY(hi)) |
| 1584 | { |
| 1585 | --todo; |
| 1586 | vim_free(hi->hi_key); |
| 1587 | } |
| 1588 | } |
| 1589 | hash_clear(&lp->sl_map_hash); |
| 1590 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1594 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1595 | * Invoked through do_in_runtimepath(). |
| 1596 | */ |
| 1597 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1598 | spell_load_cb(fname, cookie) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1599 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1600 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1601 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1602 | (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * Load one spell file and store the info into a slang_T. |
| 1607 | * |
| 1608 | * This is invoked in two ways: |
| 1609 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 1610 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 1611 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 1612 | * points to the existing slang_T. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1613 | * 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] | 1614 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1615 | static slang_T * |
| 1616 | spell_load_file(fname, lang, old_lp, silent) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1617 | char_u *fname; |
| 1618 | char_u *lang; |
| 1619 | slang_T *old_lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1620 | int silent; /* no error if file doesn't exist */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1621 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1622 | FILE *fd; |
| 1623 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1624 | char_u *p; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1625 | char_u *bp; |
| 1626 | idx_T *ip; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1627 | int i; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1628 | int n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1629 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1630 | int round; |
| 1631 | char_u *save_sourcing_name = sourcing_name; |
| 1632 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1633 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1634 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1635 | slang_T *lp = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1636 | garray_T *gap; |
| 1637 | fromto_T *ftp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1638 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1639 | int rr; |
| 1640 | short *first; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1641 | idx_T idx; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1642 | int c = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1643 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1644 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1645 | if (fd == NULL) |
| 1646 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1647 | if (!silent) |
| 1648 | EMSG2(_(e_notopen), fname); |
| 1649 | else if (p_verbose > 2) |
| 1650 | { |
| 1651 | verbose_enter(); |
| 1652 | smsg((char_u *)e_notopen, fname); |
| 1653 | verbose_leave(); |
| 1654 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1655 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1656 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1657 | if (p_verbose > 2) |
| 1658 | { |
| 1659 | verbose_enter(); |
| 1660 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 1661 | verbose_leave(); |
| 1662 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1663 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 1664 | if (old_lp == NULL) |
| 1665 | { |
| 1666 | lp = slang_alloc(lang); |
| 1667 | if (lp == NULL) |
| 1668 | goto endFAIL; |
| 1669 | |
| 1670 | /* Remember the file name, used to reload the file when it's updated. */ |
| 1671 | lp->sl_fname = vim_strsave(fname); |
| 1672 | if (lp->sl_fname == NULL) |
| 1673 | goto endFAIL; |
| 1674 | |
| 1675 | /* Check for .add.spl. */ |
| 1676 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 1677 | } |
| 1678 | else |
| 1679 | lp = old_lp; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1680 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1681 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1682 | sourcing_name = fname; |
| 1683 | sourcing_lnum = 0; |
| 1684 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1685 | /* <HEADER>: <fileID> |
| 1686 | * <regioncnt> <regionname> ... |
| 1687 | * <charflagslen> <charflags> |
| 1688 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1689 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1690 | * <prefcondcnt> <prefcond> ... |
| 1691 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1692 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1693 | buf[i] = getc(fd); /* <fileID> */ |
| 1694 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1695 | { |
| 1696 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1697 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1701 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1702 | { |
| 1703 | truncerr: |
| 1704 | EMSG(_("E758: Truncated spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1705 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1706 | } |
| 1707 | if (cnt > 8) |
| 1708 | { |
| 1709 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1710 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1711 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1712 | } |
| 1713 | for (i = 0; i < cnt; ++i) |
| 1714 | { |
| 1715 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1716 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1717 | } |
| 1718 | lp->sl_regions[cnt * 2] = NUL; |
| 1719 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1720 | cnt = getc(fd); /* <charflagslen> */ |
| 1721 | if (cnt > 0) |
| 1722 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1723 | p = alloc((unsigned)cnt); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1724 | if (p == NULL) |
| 1725 | goto endFAIL; |
| 1726 | for (i = 0; i < cnt; ++i) |
| 1727 | p[i] = getc(fd); /* <charflags> */ |
| 1728 | |
| 1729 | ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */ |
| 1730 | if (ccnt <= 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1731 | { |
| 1732 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1733 | goto formerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1734 | } |
| 1735 | fol = alloc((unsigned)ccnt + 1); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1736 | if (fol == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1737 | { |
| 1738 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1739 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1740 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1741 | for (i = 0; i < ccnt; ++i) |
| 1742 | fol[i] = getc(fd); /* <fchars> */ |
| 1743 | fol[i] = NUL; |
| 1744 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1745 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1746 | i = set_spell_charflags(p, cnt, fol); |
| 1747 | vim_free(p); |
| 1748 | vim_free(fol); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1749 | #if 0 /* tolerate the differences */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1750 | if (i == FAIL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1751 | goto formerr; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1752 | #endif |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1753 | } |
| 1754 | else |
| 1755 | { |
| 1756 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 1757 | cnt = (getc(fd) << 8) + getc(fd); |
| 1758 | if (cnt != 0) |
| 1759 | goto formerr; |
| 1760 | } |
| 1761 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1762 | /* <midwordlen> <midword> */ |
| 1763 | cnt = (getc(fd) << 8) + getc(fd); |
| 1764 | if (cnt < 0) |
| 1765 | goto truncerr; |
| 1766 | if (cnt > 0) |
| 1767 | { |
| 1768 | for (i = 0; i < cnt; ++i) |
| 1769 | if (i < MAXWLEN) /* truncate at reasonable length */ |
| 1770 | buf[i] = getc(fd); |
| 1771 | if (i < MAXWLEN) |
| 1772 | buf[i] = NUL; |
| 1773 | else |
| 1774 | buf[MAXWLEN] = NUL; |
| 1775 | |
| 1776 | /* The midword characters add up to any midword characters from other |
| 1777 | * .spel files. */ |
| 1778 | for (p = buf; *p != NUL; ) |
| 1779 | #ifdef FEAT_MBYTE |
| 1780 | if (has_mbyte) |
| 1781 | { |
| 1782 | c = mb_ptr2char(p); |
| 1783 | i = mb_ptr2len_check(p); |
| 1784 | if (c < 256) |
| 1785 | spell_ismw[c] = TRUE; |
| 1786 | else if (spell_ismw_mb == NULL) |
| 1787 | /* First multi-byte char in "spell_ismw_mb". */ |
| 1788 | spell_ismw_mb = vim_strnsave(p, i); |
| 1789 | else |
| 1790 | { |
| 1791 | /* Append multi-byte chars to "spell_ismw_mb". */ |
| 1792 | n = STRLEN(spell_ismw_mb); |
| 1793 | bp = vim_strnsave(spell_ismw_mb, n + i); |
| 1794 | if (bp != NULL) |
| 1795 | { |
| 1796 | vim_free(spell_ismw_mb); |
| 1797 | spell_ismw_mb = bp; |
| 1798 | vim_strncpy(bp + n, p, i); |
| 1799 | } |
| 1800 | } |
| 1801 | p += i; |
| 1802 | } |
| 1803 | else |
| 1804 | #endif |
| 1805 | spell_ismw[*p++] = TRUE; |
| 1806 | } |
| 1807 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1808 | /* <prefcondcnt> <prefcond> ... */ |
| 1809 | cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */ |
| 1810 | if (cnt > 0) |
| 1811 | { |
| 1812 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 1813 | (unsigned)sizeof(regprog_T *) * cnt); |
| 1814 | if (lp->sl_prefprog == NULL) |
| 1815 | goto endFAIL; |
| 1816 | lp->sl_prefixcnt = cnt; |
| 1817 | |
| 1818 | for (i = 0; i < cnt; ++i) |
| 1819 | { |
| 1820 | /* <prefcond> : <condlen> <condstr> */ |
| 1821 | n = getc(fd); /* <condlen> */ |
| 1822 | if (n < 0) |
| 1823 | goto formerr; |
| 1824 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 1825 | * compile the regexp program used to check for the condition. */ |
| 1826 | if (n > 0) |
| 1827 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1828 | buf[0] = '^'; /* always match at one position only */ |
| 1829 | p = buf + 1; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1830 | while (n-- > 0) |
| 1831 | *p++ = getc(fd); /* <condstr> */ |
| 1832 | *p = NUL; |
| 1833 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1839 | /* <SUGGEST> : <repcount> <rep> ... |
| 1840 | * <salflags> <salcount> <sal> ... |
| 1841 | * <maplen> <mapstr> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1842 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1843 | cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */ |
| 1844 | if (cnt < 0) |
| 1845 | goto formerr; |
| 1846 | |
| 1847 | gap = &lp->sl_rep; |
| 1848 | if (ga_grow(gap, cnt) == FAIL) |
| 1849 | goto endFAIL; |
| 1850 | |
| 1851 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 1852 | for (; gap->ga_len < cnt; ++gap->ga_len) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1853 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1854 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
| 1855 | for (rr = 1; rr <= 2; ++rr) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1856 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1857 | ccnt = getc(fd); |
| 1858 | if (ccnt < 0) |
| 1859 | { |
| 1860 | if (rr == 2) |
| 1861 | vim_free(ftp->ft_from); |
| 1862 | goto formerr; |
| 1863 | } |
| 1864 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1865 | { |
| 1866 | if (rr == 2) |
| 1867 | vim_free(ftp->ft_from); |
| 1868 | goto endFAIL; |
| 1869 | } |
| 1870 | for (i = 0; i < ccnt; ++i) |
| 1871 | p[i] = getc(fd); /* <repfrom> or <repto> */ |
| 1872 | p[i] = NUL; |
| 1873 | if (rr == 1) |
| 1874 | ftp->ft_from = p; |
| 1875 | else |
| 1876 | ftp->ft_to = p; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | /* Fill the first-index table. */ |
| 1881 | first = lp->sl_rep_first; |
| 1882 | for (i = 0; i < 256; ++i) |
| 1883 | first[i] = -1; |
| 1884 | for (i = 0; i < gap->ga_len; ++i) |
| 1885 | { |
| 1886 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 1887 | if (first[*ftp->ft_from] == -1) |
| 1888 | first[*ftp->ft_from] = i; |
| 1889 | } |
| 1890 | |
| 1891 | i = getc(fd); /* <salflags> */ |
| 1892 | if (i & SAL_F0LLOWUP) |
| 1893 | lp->sl_followup = TRUE; |
| 1894 | if (i & SAL_COLLAPSE) |
| 1895 | lp->sl_collapse = TRUE; |
| 1896 | if (i & SAL_REM_ACCENTS) |
| 1897 | lp->sl_rem_accents = TRUE; |
| 1898 | |
| 1899 | cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */ |
| 1900 | if (cnt < 0) |
| 1901 | goto formerr; |
| 1902 | |
| 1903 | gap = &lp->sl_sal; |
| 1904 | if (ga_grow(gap, cnt) == FAIL) |
| 1905 | goto endFAIL; |
| 1906 | |
| 1907 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 1908 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 1909 | { |
| 1910 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 1911 | ccnt = getc(fd); /* <salfromlen> */ |
| 1912 | if (ccnt < 0) |
| 1913 | goto formerr; |
| 1914 | if ((p = alloc(ccnt + 2)) == NULL) |
| 1915 | goto endFAIL; |
| 1916 | smp->sm_lead = p; |
| 1917 | |
| 1918 | /* Read up to the first special char into sm_lead. */ |
| 1919 | for (i = 0; i < ccnt; ++i) |
| 1920 | { |
| 1921 | c = getc(fd); /* <salfrom> */ |
| 1922 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
| 1923 | break; |
| 1924 | *p++ = c; |
| 1925 | } |
| 1926 | smp->sm_leadlen = p - smp->sm_lead; |
| 1927 | *p++ = NUL; |
| 1928 | |
| 1929 | /* Put optional chars in sm_oneoff, if any. */ |
| 1930 | if (c == '(') |
| 1931 | { |
| 1932 | smp->sm_oneoff = p; |
| 1933 | for (++i; i < ccnt; ++i) |
| 1934 | { |
| 1935 | c = getc(fd); /* <salfrom> */ |
| 1936 | if (c == ')') |
| 1937 | break; |
| 1938 | *p++ = c; |
| 1939 | } |
| 1940 | *p++ = NUL; |
| 1941 | if (++i < ccnt) |
| 1942 | c = getc(fd); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1943 | } |
| 1944 | else |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1945 | smp->sm_oneoff = NULL; |
| 1946 | |
| 1947 | /* Any following chars go in sm_rules. */ |
| 1948 | smp->sm_rules = p; |
| 1949 | if (i < ccnt) |
| 1950 | *p++ = c; |
| 1951 | for (++i; i < ccnt; ++i) |
| 1952 | *p++ = getc(fd); /* <salfrom> */ |
| 1953 | *p++ = NUL; |
| 1954 | |
| 1955 | ccnt = getc(fd); /* <saltolen> */ |
| 1956 | if (ccnt < 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1957 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1958 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1959 | goto formerr; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1960 | } |
| 1961 | if ((p = alloc(ccnt + 1)) == NULL) |
| 1962 | { |
| 1963 | vim_free(smp->sm_lead); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1964 | goto endFAIL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1965 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1966 | smp->sm_to = p; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1967 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1968 | for (i = 0; i < ccnt; ++i) |
| 1969 | *p++ = getc(fd); /* <salto> */ |
| 1970 | *p++ = NUL; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 1971 | |
| 1972 | #ifdef FEAT_MBYTE |
| 1973 | if (has_mbyte) |
| 1974 | { |
| 1975 | /* convert the multi-byte strings to wide char strings */ |
| 1976 | smp->sm_lead_w = mb_str2wide(smp->sm_lead); |
| 1977 | smp->sm_leadlen = mb_charlen(smp->sm_lead); |
| 1978 | if (smp->sm_oneoff == NULL) |
| 1979 | smp->sm_oneoff_w = NULL; |
| 1980 | else |
| 1981 | smp->sm_oneoff_w = mb_str2wide(smp->sm_oneoff); |
| 1982 | smp->sm_to_w = mb_str2wide(smp->sm_to); |
| 1983 | if (smp->sm_lead_w == NULL |
| 1984 | || (smp->sm_oneoff_w == NULL && smp->sm_oneoff != NULL) |
| 1985 | || smp->sm_to_w == NULL) |
| 1986 | { |
| 1987 | vim_free(smp->sm_lead); |
| 1988 | vim_free(smp->sm_to); |
| 1989 | vim_free(smp->sm_lead_w); |
| 1990 | vim_free(smp->sm_oneoff_w); |
| 1991 | vim_free(smp->sm_to_w); |
| 1992 | goto endFAIL; |
| 1993 | } |
| 1994 | } |
| 1995 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | /* Fill the first-index table. */ |
| 1999 | first = lp->sl_sal_first; |
| 2000 | for (i = 0; i < 256; ++i) |
| 2001 | first[i] = -1; |
| 2002 | for (i = 0; i < gap->ga_len; ++i) |
| 2003 | { |
| 2004 | smp = &((salitem_T *)gap->ga_data)[i]; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2005 | #ifdef FEAT_MBYTE |
| 2006 | if (has_mbyte) |
| 2007 | /* Use the lowest byte of the first character. */ |
| 2008 | c = *smp->sm_lead_w & 0xff; |
| 2009 | else |
| 2010 | #endif |
| 2011 | c = *smp->sm_lead; |
| 2012 | if (first[c] == -1) |
| 2013 | { |
| 2014 | first[c] = i; |
| 2015 | #ifdef FEAT_MBYTE |
| 2016 | if (has_mbyte) |
| 2017 | { |
| 2018 | int j; |
| 2019 | |
| 2020 | /* Make sure all entries with this byte are following each |
| 2021 | * other. Move the ones down that are in the wrong position. |
| 2022 | * Do keep the right sequence. */ |
| 2023 | while (i + 1 < gap->ga_len && (*smp[1].sm_lead_w & 0xff) == c) |
| 2024 | { |
| 2025 | ++i; |
| 2026 | ++smp; |
| 2027 | } |
| 2028 | for (j = 1; i + j < gap->ga_len; ++j) |
| 2029 | if ((*smp[j].sm_lead_w & 0xff) == c) |
| 2030 | { |
| 2031 | salitem_T tsal; |
| 2032 | |
| 2033 | ++i; |
| 2034 | ++smp; |
| 2035 | --j; |
| 2036 | tsal = smp[j]; |
| 2037 | mch_memmove(smp + 1, smp, sizeof(salitem_T) * j); |
| 2038 | *smp = tsal; |
| 2039 | } |
| 2040 | } |
| 2041 | #endif |
| 2042 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */ |
| 2046 | if (cnt < 0) |
| 2047 | goto formerr; |
| 2048 | p = alloc(cnt + 1); |
| 2049 | if (p == NULL) |
| 2050 | goto endFAIL; |
| 2051 | for (i = 0; i < cnt; ++i) |
| 2052 | p[i] = getc(fd); /* <mapstr> */ |
| 2053 | p[i] = NUL; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 2054 | set_map_str(lp, p); |
| 2055 | vim_free(p); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2056 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2057 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2058 | /* round 1: <LWORDTREE> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2059 | * round 2: <KWORDTREE> |
| 2060 | * round 3: <PREFIXTREE> */ |
| 2061 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2062 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2063 | /* The tree size was computed when writing the file, so that we can |
| 2064 | * allocate it as one long block. <nodecount> */ |
| 2065 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2066 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2067 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2068 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2069 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2070 | /* Allocate the byte array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2071 | bp = lalloc((long_u)len, TRUE); |
| 2072 | if (bp == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2073 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2074 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2075 | lp->sl_fbyts = bp; |
| 2076 | else if (round == 2) |
| 2077 | lp->sl_kbyts = bp; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2078 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2079 | lp->sl_pbyts = bp; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2080 | |
| 2081 | /* Allocate the index array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2082 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 2083 | if (ip == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2084 | goto endFAIL; |
| 2085 | if (round == 1) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2086 | lp->sl_fidxs = ip; |
| 2087 | else if (round == 2) |
| 2088 | lp->sl_kidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2089 | else |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2090 | lp->sl_pidxs = ip; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2091 | |
| 2092 | /* Read the tree and store it in the array. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2093 | 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] | 2094 | if (idx == -1) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2095 | goto truncerr; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2096 | if (idx < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2097 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2098 | } |
| 2099 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2100 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2101 | /* For a new file link it in the list of spell files. */ |
| 2102 | if (old_lp == NULL) |
| 2103 | { |
| 2104 | lp->sl_next = first_lang; |
| 2105 | first_lang = lp; |
| 2106 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2107 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2108 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2109 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2110 | endFAIL: |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2111 | if (lang != NULL) |
| 2112 | /* truncating the name signals the error to spell_load_lang() */ |
| 2113 | *lang = NUL; |
| 2114 | if (lp != NULL && old_lp == NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2115 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2116 | slang_free(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2117 | lp = NULL; |
| 2118 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2119 | |
| 2120 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2121 | if (fd != NULL) |
| 2122 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2123 | sourcing_name = save_sourcing_name; |
| 2124 | sourcing_lnum = save_sourcing_lnum; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2125 | |
| 2126 | return lp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2129 | #ifdef FEAT_MBYTE |
| 2130 | /* |
| 2131 | * Turn a multi-byte string into a wide character string. |
| 2132 | * Return it in allocated memory (NULL for out-of-memory) |
| 2133 | */ |
| 2134 | static int * |
| 2135 | mb_str2wide(s) |
| 2136 | char_u *s; |
| 2137 | { |
| 2138 | int *res; |
| 2139 | char_u *p; |
| 2140 | int i = 0; |
| 2141 | |
| 2142 | res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1)); |
| 2143 | if (res != NULL) |
| 2144 | { |
| 2145 | for (p = s; *p != NUL; ) |
| 2146 | res[i++] = mb_ptr2char_adv(&p); |
| 2147 | res[i] = NUL; |
| 2148 | } |
| 2149 | return res; |
| 2150 | } |
| 2151 | #endif |
| 2152 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2153 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2154 | * Read one row of siblings from the spell file and store it in the byte array |
| 2155 | * "byts" and index array "idxs". Recursively read the children. |
| 2156 | * |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2157 | * NOTE: The code here must match put_node(). |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2158 | * |
| 2159 | * Returns the index follosing the siblings. |
| 2160 | * Returns -1 if the file is shorter than expected. |
| 2161 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2162 | */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2163 | static idx_T |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2164 | read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2165 | FILE *fd; |
| 2166 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2167 | idx_T *idxs; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2168 | int maxidx; /* size of arrays */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2169 | idx_T startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2170 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 2171 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2172 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2173 | int len; |
| 2174 | int i; |
| 2175 | int n; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2176 | idx_T idx = startidx; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2177 | int c; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2178 | int c2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2179 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2180 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2181 | len = getc(fd); /* <siblingcount> */ |
| 2182 | if (len <= 0) |
| 2183 | return -1; |
| 2184 | |
| 2185 | if (startidx + len >= maxidx) |
| 2186 | return -2; |
| 2187 | byts[idx++] = len; |
| 2188 | |
| 2189 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 2190 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2191 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2192 | c = getc(fd); /* <byte> */ |
| 2193 | if (c < 0) |
| 2194 | return -1; |
| 2195 | if (c <= BY_SPECIAL) |
| 2196 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2197 | if (c == BY_NOFLAGS && !prefixtree) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2198 | { |
| 2199 | /* No flags, all regions. */ |
| 2200 | idxs[idx] = 0; |
| 2201 | c = 0; |
| 2202 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2203 | else if (c == BY_FLAGS || c == BY_NOFLAGS) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2204 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2205 | if (prefixtree) |
| 2206 | { |
| 2207 | /* Read the prefix ID and the condition nr. In idxs[] |
| 2208 | * store the prefix ID in the low byte, the condition |
| 2209 | * index shifted up 8 bits. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2210 | c2 = getc(fd); /* <prefixID> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2211 | n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */ |
| 2212 | if (n >= maxprefcondnr) |
| 2213 | return -2; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2214 | c2 += (n << 8); |
| 2215 | if (c == BY_NOFLAGS) |
| 2216 | c = c2; |
| 2217 | else |
| 2218 | c = c2 | WF_RAREPFX; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2219 | } |
| 2220 | else |
| 2221 | { |
| 2222 | /* Read flags and optional region and prefix ID. In |
| 2223 | * idxs[] the flags go in the low byte, region above that |
| 2224 | * and prefix ID above the region. */ |
| 2225 | c = getc(fd); /* <flags> */ |
| 2226 | if (c & WF_REGION) |
| 2227 | c = (getc(fd) << 8) + c; /* <region> */ |
| 2228 | if (c & WF_PFX) |
| 2229 | c = (getc(fd) << 16) + c; /* <prefixID> */ |
| 2230 | } |
| 2231 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2232 | idxs[idx] = c; |
| 2233 | c = 0; |
| 2234 | } |
| 2235 | else /* c == BY_INDEX */ |
| 2236 | { |
| 2237 | /* <nodeidx> */ |
| 2238 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 2239 | if (n < 0 || n >= maxidx) |
| 2240 | return -2; |
| 2241 | idxs[idx] = n + SHARED_MASK; |
| 2242 | c = getc(fd); /* <xbyte> */ |
| 2243 | } |
| 2244 | } |
| 2245 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2248 | /* Recursively read the children for non-shared siblings. |
| 2249 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 2250 | * remove SHARED_MASK) */ |
| 2251 | for (i = 1; i <= len; ++i) |
| 2252 | if (byts[startidx + i] != 0) |
| 2253 | { |
| 2254 | if (idxs[startidx + i] & SHARED_MASK) |
| 2255 | idxs[startidx + i] &= ~SHARED_MASK; |
| 2256 | else |
| 2257 | { |
| 2258 | idxs[startidx + i] = idx; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2259 | idx = read_tree(fd, byts, idxs, maxidx, idx, |
| 2260 | prefixtree, maxprefcondnr); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2261 | if (idx < 0) |
| 2262 | break; |
| 2263 | } |
| 2264 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2265 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2266 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * Parse 'spelllang' and set buf->b_langp accordingly. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2271 | * Returns NULL if it's OK, an error message otherwise. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2272 | */ |
| 2273 | char_u * |
| 2274 | did_set_spelllang(buf) |
| 2275 | buf_T *buf; |
| 2276 | { |
| 2277 | garray_T ga; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2278 | char_u *splp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2279 | char_u *region; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2280 | int filename; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2281 | int region_mask; |
| 2282 | slang_T *lp; |
| 2283 | int c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2284 | char_u lang[MAXWLEN + 1]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2285 | char_u spf_name[MAXPATHL]; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2286 | int load_spf; |
| 2287 | int len; |
| 2288 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2289 | |
| 2290 | ga_init2(&ga, sizeof(langp_T), 2); |
| 2291 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2292 | /* Make the name of the .spl file associated with 'spellfile'. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2293 | if (*buf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2294 | load_spf = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2295 | else |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2296 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2297 | vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl", |
| 2298 | buf->b_p_spf); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2299 | load_spf = TRUE; |
| 2300 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2301 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2302 | /* loop over comma separated language names. */ |
| 2303 | for (splp = buf->b_p_spl; *splp != NUL; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2304 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2305 | /* Get one language name. */ |
| 2306 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 2307 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2308 | region = NULL; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2309 | len = STRLEN(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2310 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2311 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 2312 | * If there is a region name let "region" point to it and remove it |
| 2313 | * from the name. */ |
| 2314 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 2315 | { |
| 2316 | filename = TRUE; |
| 2317 | |
| 2318 | /* Check if we loaded this language before. */ |
| 2319 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2320 | if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME) |
| 2321 | break; |
| 2322 | } |
| 2323 | else |
| 2324 | { |
| 2325 | filename = FALSE; |
| 2326 | if (len > 3 && lang[len - 3] == '_') |
| 2327 | { |
| 2328 | region = lang + len - 2; |
| 2329 | len -= 3; |
| 2330 | lang[len] = NUL; |
| 2331 | } |
| 2332 | |
| 2333 | /* Check if we loaded this language before. */ |
| 2334 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2335 | if (STRICMP(lang, lp->sl_name) == 0) |
| 2336 | break; |
| 2337 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2338 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2339 | /* If not found try loading the language now. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2340 | if (lp == NULL) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2341 | { |
| 2342 | if (filename) |
| 2343 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 2344 | else |
| 2345 | spell_load_lang(lang); |
| 2346 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2347 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2348 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2349 | * Loop over the languages, there can be several files for "lang". |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2350 | */ |
| 2351 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2352 | if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME |
| 2353 | : STRICMP(lang, lp->sl_name) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2354 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2355 | region_mask = REGION_ALL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2356 | if (!filename && region != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2357 | { |
| 2358 | /* find region in sl_regions */ |
| 2359 | c = find_region(lp->sl_regions, region); |
| 2360 | if (c == REGION_ALL) |
| 2361 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2362 | if (!lp->sl_add) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2363 | smsg((char_u *) |
| 2364 | _("Warning: region %s not supported"), |
| 2365 | region); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2366 | } |
| 2367 | else |
| 2368 | region_mask = 1 << c; |
| 2369 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2370 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2371 | if (ga_grow(&ga, 1) == FAIL) |
| 2372 | { |
| 2373 | ga_clear(&ga); |
| 2374 | return e_outofmem; |
| 2375 | } |
| 2376 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2377 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2378 | ++ga.ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2379 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2380 | /* Check if this is the spell file related to 'spellfile'. */ |
| 2381 | if (load_spf && fullpathcmp(spf_name, lp->sl_fname, FALSE) |
| 2382 | == FPC_SAME) |
| 2383 | load_spf = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2384 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2387 | /* |
| 2388 | * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath', |
| 2389 | * then it's probably loaded above already. Otherwise load it here. |
| 2390 | */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2391 | if (load_spf) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2392 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2393 | /* Check if it was loaded already. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2394 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2395 | if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME) |
| 2396 | break; |
| 2397 | if (lp == NULL) |
| 2398 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2399 | /* Not loaded, try loading it now. The language name includes the |
| 2400 | * region name, the region is ignored otherwise. */ |
| 2401 | vim_strncpy(lang, gettail(buf->b_p_spf), MAXWLEN); |
| 2402 | p = vim_strchr(lang, '.'); |
| 2403 | if (p != NULL) |
| 2404 | *p = NUL; /* truncate at ".encoding.add" */ |
| 2405 | lp = spell_load_file(spf_name, lang, NULL, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2406 | } |
| 2407 | if (lp != NULL && ga_grow(&ga, 1) == OK) |
| 2408 | { |
| 2409 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2410 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL; |
| 2411 | ++ga.ga_len; |
| 2412 | } |
| 2413 | } |
| 2414 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2415 | /* Add a NULL entry to mark the end of the list. */ |
| 2416 | if (ga_grow(&ga, 1) == FAIL) |
| 2417 | { |
| 2418 | ga_clear(&ga); |
| 2419 | return e_outofmem; |
| 2420 | } |
| 2421 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2422 | ++ga.ga_len; |
| 2423 | |
| 2424 | /* Everything is fine, store the new b_langp value. */ |
| 2425 | ga_clear(&buf->b_langp); |
| 2426 | buf->b_langp = ga; |
| 2427 | |
| 2428 | return NULL; |
| 2429 | } |
| 2430 | |
| 2431 | /* |
| 2432 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2433 | * Each region is simply stored as the two characters of it's name. |
| 2434 | * Returns the index if found, REGION_ALL if not found. |
| 2435 | */ |
| 2436 | static int |
| 2437 | find_region(rp, region) |
| 2438 | char_u *rp; |
| 2439 | char_u *region; |
| 2440 | { |
| 2441 | int i; |
| 2442 | |
| 2443 | for (i = 0; ; i += 2) |
| 2444 | { |
| 2445 | if (rp[i] == NUL) |
| 2446 | return REGION_ALL; |
| 2447 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2448 | break; |
| 2449 | } |
| 2450 | return i / 2; |
| 2451 | } |
| 2452 | |
| 2453 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2454 | * Return case type of word: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2455 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2456 | * Word WF_ONECAP |
| 2457 | * W WORD WF_ALLCAP |
| 2458 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2459 | */ |
| 2460 | static int |
| 2461 | captype(word, end) |
| 2462 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2463 | char_u *end; /* When NULL use up to NUL byte. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2464 | { |
| 2465 | char_u *p; |
| 2466 | int c; |
| 2467 | int firstcap; |
| 2468 | int allcap; |
| 2469 | int past_second = FALSE; /* past second word char */ |
| 2470 | |
| 2471 | /* find first letter */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2472 | for (p = word; !spell_iswordp(p); mb_ptr_adv(p)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2473 | if (end == NULL ? *p == NUL : p >= end) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2474 | return 0; /* only non-word characters, illegal word */ |
| 2475 | #ifdef FEAT_MBYTE |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2476 | if (has_mbyte) |
| 2477 | c = mb_ptr2char_adv(&p); |
| 2478 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2479 | #endif |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2480 | c = *p++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2481 | firstcap = allcap = SPELL_ISUPPER(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2482 | |
| 2483 | /* |
| 2484 | * Need to check all letters to find a word with mixed upper/lower. |
| 2485 | * But a word with an upper char only at start is a ONECAP. |
| 2486 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2487 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2488 | if (spell_iswordp(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2489 | { |
| 2490 | #ifdef FEAT_MBYTE |
| 2491 | c = mb_ptr2char(p); |
| 2492 | #else |
| 2493 | c = *p; |
| 2494 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2495 | if (!SPELL_ISUPPER(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2496 | { |
| 2497 | /* UUl -> KEEPCAP */ |
| 2498 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2499 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2500 | allcap = FALSE; |
| 2501 | } |
| 2502 | else if (!allcap) |
| 2503 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2504 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2505 | past_second = TRUE; |
| 2506 | } |
| 2507 | |
| 2508 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2509 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2510 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2511 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2512 | return 0; |
| 2513 | } |
| 2514 | |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2515 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 2516 | /* |
| 2517 | * Free all languages. |
| 2518 | */ |
| 2519 | void |
| 2520 | spell_free_all() |
| 2521 | { |
| 2522 | slang_T *lp; |
| 2523 | buf_T *buf; |
| 2524 | |
| 2525 | /* Go through all buffers and handle 'spelllang'. */ |
| 2526 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2527 | ga_clear(&buf->b_langp); |
| 2528 | |
| 2529 | while (first_lang != NULL) |
| 2530 | { |
| 2531 | lp = first_lang; |
| 2532 | first_lang = lp->sl_next; |
| 2533 | slang_free(lp); |
| 2534 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2535 | |
| 2536 | init_spell_chartab(); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2537 | } |
| 2538 | # endif |
| 2539 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2540 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2541 | /* |
| 2542 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2543 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2544 | */ |
| 2545 | void |
| 2546 | spell_reload() |
| 2547 | { |
| 2548 | buf_T *buf; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2549 | win_T *wp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2550 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 2551 | /* Initialize the table for spell_iswordp(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2552 | init_spell_chartab(); |
| 2553 | |
| 2554 | /* Unload all allocated memory. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 2555 | spell_free_all(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2556 | |
| 2557 | /* Go through all buffers and handle 'spelllang'. */ |
| 2558 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2559 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2560 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 2561 | * window for this buffer in which 'spell' is set. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2562 | if (*buf->b_p_spl != NUL) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2563 | { |
| 2564 | FOR_ALL_WINDOWS(wp) |
| 2565 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 2566 | { |
| 2567 | (void)did_set_spelllang(buf); |
| 2568 | # ifdef FEAT_WINDOWS |
| 2569 | break; |
| 2570 | # endif |
| 2571 | } |
| 2572 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2573 | } |
| 2574 | } |
| 2575 | # endif |
| 2576 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2577 | /* |
| 2578 | * Reload the spell file "fname" if it's loaded. |
| 2579 | */ |
| 2580 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2581 | spell_reload_one(fname, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2582 | char_u *fname; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2583 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2584 | { |
| 2585 | slang_T *lp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2586 | int didit = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2587 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2588 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2589 | if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME) |
| 2590 | { |
| 2591 | slang_clear(lp); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2592 | (void)spell_load_file(fname, NULL, lp, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2593 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2594 | didit = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2595 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2596 | |
| 2597 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| 2598 | * 'spelllang' to get it loaded. */ |
| 2599 | if (added_word && !didit) |
| 2600 | did_set_spelllang(curbuf); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2604 | /* |
| 2605 | * Functions for ":mkspell". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2606 | */ |
| 2607 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2608 | #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] | 2609 | and .dic file. */ |
| 2610 | /* |
| 2611 | * Main structure to store the contents of a ".aff" file. |
| 2612 | */ |
| 2613 | typedef struct afffile_S |
| 2614 | { |
| 2615 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2616 | int af_rar; /* RAR ID for rare word */ |
| 2617 | int af_kep; /* KEP ID for keep-case word */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2618 | int af_bad; /* BAD ID for banned word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2619 | int af_pfxpostpone; /* postpone prefixes without chop string */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2620 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2621 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2622 | } afffile_T; |
| 2623 | |
| 2624 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2625 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2626 | struct affentry_S |
| 2627 | { |
| 2628 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2629 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 2630 | 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] | 2631 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 2632 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2633 | int ae_rare; /* rare affix */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2634 | }; |
| 2635 | |
| 2636 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 2637 | typedef struct affheader_S |
| 2638 | { |
| 2639 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2640 | int ah_newID; /* prefix ID after renumbering */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2641 | int ah_combine; /* suffix may combine with prefix */ |
| 2642 | affentry_T *ah_first; /* first affix entry */ |
| 2643 | } affheader_T; |
| 2644 | |
| 2645 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 2646 | |
| 2647 | /* |
| 2648 | * Structure that is used to store the items in the word tree. This avoids |
| 2649 | * the need to keep track of each allocated thing, it's freed all at once |
| 2650 | * after ":mkspell" is done. |
| 2651 | */ |
| 2652 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 2653 | typedef struct sblock_S sblock_T; |
| 2654 | struct sblock_S |
| 2655 | { |
| 2656 | sblock_T *sb_next; /* next block in list */ |
| 2657 | int sb_used; /* nr of bytes already in use */ |
| 2658 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2659 | }; |
| 2660 | |
| 2661 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2662 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2663 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2664 | typedef struct wordnode_S wordnode_T; |
| 2665 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2666 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2667 | union /* shared to save space */ |
| 2668 | { |
| 2669 | char_u hashkey[6]; /* room for the hash key */ |
| 2670 | int index; /* index in written nodes (valid after first |
| 2671 | round) */ |
| 2672 | } wn_u1; |
| 2673 | union /* shared to save space */ |
| 2674 | { |
| 2675 | wordnode_T *next; /* next node with same hash key */ |
| 2676 | wordnode_T *wnode; /* parent node that will write this node */ |
| 2677 | } wn_u2; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2678 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 2679 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 2680 | always sorted) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2681 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 2682 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2683 | short wn_region; /* when wn_byte is NUL: region mask; for |
| 2684 | PREFIXTREE it's the prefcondnr */ |
| 2685 | char_u wn_prefixID; /* supported/required prefix ID or 0 */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2686 | }; |
| 2687 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2688 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2689 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2690 | /* |
| 2691 | * Info used while reading the spell files. |
| 2692 | */ |
| 2693 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2694 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2695 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2696 | long si_foldwcount; /* nr of words in si_foldroot */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2697 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2698 | long si_keepwcount; /* nr of words in si_keeproot */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2699 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2700 | sblock_T *si_blocks; /* memory blocks used */ |
| 2701 | int si_ascii; /* handling only ASCII words */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2702 | int si_add; /* addition file */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2703 | int si_clear_chartab; /* when TRUE clear char tables */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2704 | int si_region; /* region mask */ |
| 2705 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2706 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2707 | int si_verbose; /* verbose messages */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2708 | int si_region_count; /* number of regions supported (1 when there |
| 2709 | are no regions) */ |
| 2710 | char_u si_region_name[16]; /* region names (if count > 1) */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2711 | |
| 2712 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| 2713 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
| 2714 | int si_followup; /* soundsalike: ? */ |
| 2715 | int si_collapse; /* soundsalike: ? */ |
| 2716 | int si_rem_accents; /* soundsalike: remove accents */ |
| 2717 | garray_T si_map; /* MAP info concatenated */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2718 | char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2719 | garray_T si_prefcond; /* table with conditions for postponed |
| 2720 | * prefixes, each stored as a string */ |
| 2721 | int si_newID; /* current value for ah_newID */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2722 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2723 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2724 | 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] | 2725 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2726 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 2727 | static int sal_to_bool __ARGS((char_u *s)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2728 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2729 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 2730 | 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] | 2731 | static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp)); |
| 2732 | 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] | 2733 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 2734 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 2735 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 2736 | static void free_blocks __ARGS((sblock_T *bl)); |
| 2737 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2738 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist)); |
| 2739 | 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] | 2740 | static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2741 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 2742 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2743 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2744 | static void clear_node __ARGS((wordnode_T *node)); |
| 2745 | 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] | 2746 | 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] | 2747 | static void init_spellfile __ARGS((void)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2748 | |
| 2749 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2750 | * Read the affix file "fname". |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 2751 | * Returns an afffile_T, NULL for complete failure. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2752 | */ |
| 2753 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2754 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2755 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2756 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2757 | { |
| 2758 | FILE *fd; |
| 2759 | afffile_T *aff; |
| 2760 | char_u rline[MAXLINELEN]; |
| 2761 | char_u *line; |
| 2762 | char_u *pc = NULL; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2763 | #define MAXITEMCNT 7 |
| 2764 | char_u *(items[MAXITEMCNT]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2765 | int itemcnt; |
| 2766 | char_u *p; |
| 2767 | int lnum = 0; |
| 2768 | affheader_T *cur_aff = NULL; |
| 2769 | int aff_todo = 0; |
| 2770 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2771 | char_u *low = NULL; |
| 2772 | char_u *fol = NULL; |
| 2773 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2774 | 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] | 2775 | int do_rep; |
| 2776 | int do_sal; |
| 2777 | int do_map; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2778 | int do_midword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2779 | int found_map = FALSE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2780 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2781 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2782 | /* |
| 2783 | * Open the file. |
| 2784 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2785 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2786 | if (fd == NULL) |
| 2787 | { |
| 2788 | EMSG2(_(e_notopen), fname); |
| 2789 | return NULL; |
| 2790 | } |
| 2791 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2792 | if (spin->si_verbose || p_verbose > 2) |
| 2793 | { |
| 2794 | if (!spin->si_verbose) |
| 2795 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2796 | smsg((char_u *)_("Reading affix file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2797 | out_flush(); |
| 2798 | if (!spin->si_verbose) |
| 2799 | verbose_leave(); |
| 2800 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2801 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2802 | /* Only do REP lines when not done in another .aff file already. */ |
| 2803 | do_rep = spin->si_rep.ga_len == 0; |
| 2804 | |
| 2805 | /* Only do SAL lines when not done in another .aff file already. */ |
| 2806 | do_sal = spin->si_sal.ga_len == 0; |
| 2807 | |
| 2808 | /* Only do MAP lines when not done in another .aff file already. */ |
| 2809 | do_map = spin->si_map.ga_len == 0; |
| 2810 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2811 | /* Only do MIDWORD line when not done in another .aff file already */ |
| 2812 | do_midword = spin->si_midword == NULL; |
| 2813 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2814 | /* |
| 2815 | * Allocate and init the afffile_T structure. |
| 2816 | */ |
| 2817 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2818 | if (aff == NULL) |
| 2819 | return NULL; |
| 2820 | hash_init(&aff->af_pref); |
| 2821 | hash_init(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2822 | |
| 2823 | /* |
| 2824 | * Read all the lines in the file one by one. |
| 2825 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2826 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2827 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2828 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2829 | ++lnum; |
| 2830 | |
| 2831 | /* Skip comment lines. */ |
| 2832 | if (*rline == '#') |
| 2833 | continue; |
| 2834 | |
| 2835 | /* Convert from "SET" to 'encoding' when needed. */ |
| 2836 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2837 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2838 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2839 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2840 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2841 | if (pc == NULL) |
| 2842 | { |
| 2843 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 2844 | fname, lnum, rline); |
| 2845 | continue; |
| 2846 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2847 | line = pc; |
| 2848 | } |
| 2849 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2850 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2851 | { |
| 2852 | pc = NULL; |
| 2853 | line = rline; |
| 2854 | } |
| 2855 | |
| 2856 | /* Split the line up in white separated items. Put a NUL after each |
| 2857 | * item. */ |
| 2858 | itemcnt = 0; |
| 2859 | for (p = line; ; ) |
| 2860 | { |
| 2861 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 2862 | ++p; |
| 2863 | if (*p == NUL) |
| 2864 | break; |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2865 | if (itemcnt == MAXITEMCNT) /* too many items */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2866 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2867 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2868 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2869 | ++p; |
| 2870 | if (*p == NUL) |
| 2871 | break; |
| 2872 | *p++ = NUL; |
| 2873 | } |
| 2874 | |
| 2875 | /* Handle non-empty lines. */ |
| 2876 | if (itemcnt > 0) |
| 2877 | { |
| 2878 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 2879 | && aff->af_enc == NULL) |
| 2880 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2881 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2882 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 2883 | aff->af_enc = enc_canonize(items[1]); |
| 2884 | if (aff->af_enc != NULL && !spin->si_ascii |
| 2885 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 2886 | p_enc) == FAIL) |
| 2887 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 2888 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2889 | #else |
| 2890 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 2891 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2892 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2893 | else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2) |
| 2894 | { |
| 2895 | if (do_midword) |
| 2896 | spin->si_midword = vim_strsave(items[1]); |
| 2897 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2898 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 2899 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2900 | /* ignored, we always split */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2901 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2902 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2903 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2904 | /* ignored, we look in the tree for what chars may appear */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2905 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2906 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 2907 | && aff->af_rar == 0) |
| 2908 | { |
| 2909 | aff->af_rar = items[1][0]; |
| 2910 | if (items[1][1] != NUL) |
| 2911 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2912 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2913 | else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2 |
| 2914 | && aff->af_kep == 0) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2915 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2916 | aff->af_kep = items[1][0]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2917 | if (items[1][1] != NUL) |
| 2918 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2919 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2920 | else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2 |
| 2921 | && aff->af_bad == 0) |
| 2922 | { |
| 2923 | aff->af_bad = items[1][0]; |
| 2924 | if (items[1][1] != NUL) |
| 2925 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 2926 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2927 | else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1) |
| 2928 | { |
| 2929 | aff->af_pfxpostpone = TRUE; |
| 2930 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2931 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2932 | || STRCMP(items[0], "SFX") == 0) |
| 2933 | && aff_todo == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2934 | && itemcnt >= 4) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2935 | { |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2936 | /* Myspell allows extra text after the item, but that might |
| 2937 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| 2938 | if (itemcnt > 4 && *items[4] != '#') |
| 2939 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
| 2940 | fname, lnum, items[4]); |
| 2941 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2942 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2943 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 2944 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2945 | if (cur_aff == NULL) |
| 2946 | break; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2947 | cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2948 | cur_aff->ah_key[1] = NUL; |
| 2949 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2950 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2951 | if (*items[2] == 'Y') |
| 2952 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2953 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2954 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 2955 | fname, lnum, items[2]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2956 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2957 | if (*items[0] == 'P') |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2958 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2959 | tp = &aff->af_pref; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2960 | /* Use a new number in the .spl file later, to be able to |
| 2961 | * handle multiple .aff files. */ |
| 2962 | if (aff->af_pfxpostpone) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2963 | cur_aff->ah_newID = ++spin->si_newID; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2964 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2965 | else |
| 2966 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2967 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 2968 | hi = hash_find(tp, cur_aff->ah_key); |
| 2969 | if (!HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2970 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2971 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 2972 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2973 | aff_todo = 0; |
| 2974 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2975 | else |
| 2976 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2977 | } |
| 2978 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2979 | || STRCMP(items[0], "SFX") == 0) |
| 2980 | && aff_todo > 0 |
| 2981 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2982 | && itemcnt >= 5) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2983 | { |
| 2984 | affentry_T *aff_entry; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2985 | int rare = FALSE; |
| 2986 | int lasti = 5; |
| 2987 | |
| 2988 | /* Check for "rare" after the other info. */ |
| 2989 | if (itemcnt > 5 && STRICMP(items[5], "rare") == 0) |
| 2990 | { |
| 2991 | rare = TRUE; |
| 2992 | lasti = 6; |
| 2993 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2994 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2995 | /* Myspell allows extra text after the item, but that might |
| 2996 | * mean mistakes go unnoticed. Require a comment-starter. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2997 | if (itemcnt > lasti && *items[lasti] != '#') |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 2998 | smsg((char_u *)_("Trailing text in %s line %d: %s"), |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 2999 | fname, lnum, items[lasti]); |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3000 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3001 | /* New item for an affix letter. */ |
| 3002 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3003 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 3004 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3005 | if (aff_entry == NULL) |
| 3006 | break; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3007 | aff_entry->ae_rare = rare; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3008 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3009 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3010 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 3011 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3012 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3013 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 3014 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3015 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3016 | /* Don't use an affix entry with non-ASCII characters when |
| 3017 | * "spin->si_ascii" is TRUE. */ |
| 3018 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3019 | || has_non_ascii(aff_entry->ae_add))) |
| 3020 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3021 | aff_entry->ae_next = cur_aff->ah_first; |
| 3022 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3023 | |
| 3024 | if (STRCMP(items[4], ".") != 0) |
| 3025 | { |
| 3026 | char_u buf[MAXLINELEN]; |
| 3027 | |
| 3028 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 3029 | items[4]); |
| 3030 | if (*items[0] == 'P') |
| 3031 | sprintf((char *)buf, "^%s", items[4]); |
| 3032 | else |
| 3033 | sprintf((char *)buf, "%s$", items[4]); |
| 3034 | aff_entry->ae_prog = vim_regcomp(buf, |
| 3035 | RE_MAGIC + RE_STRING); |
| 3036 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3037 | |
| 3038 | /* For postponed prefixes we need an entry in si_prefcond |
| 3039 | * for the condition. Use an existing one if possible. */ |
| 3040 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 3041 | && aff_entry->ae_chop == NULL) |
| 3042 | { |
| 3043 | int idx; |
| 3044 | char_u **pp; |
| 3045 | |
| 3046 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 3047 | --idx) |
| 3048 | { |
| 3049 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 3050 | if (str_equal(p, aff_entry->ae_cond)) |
| 3051 | break; |
| 3052 | } |
| 3053 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 3054 | { |
| 3055 | /* Not found, add a new condition. */ |
| 3056 | idx = spin->si_prefcond.ga_len++; |
| 3057 | pp = ((char_u **)spin->si_prefcond.ga_data) + idx; |
| 3058 | if (aff_entry->ae_cond == NULL) |
| 3059 | *pp = NULL; |
| 3060 | else |
| 3061 | *pp = getroom_save(&spin->si_blocks, |
| 3062 | aff_entry->ae_cond); |
| 3063 | } |
| 3064 | |
| 3065 | /* Add the prefix to the prefix tree. */ |
| 3066 | if (aff_entry->ae_add == NULL) |
| 3067 | p = (char_u *)""; |
| 3068 | else |
| 3069 | p = aff_entry->ae_add; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3070 | tree_add_word(p, spin->si_prefroot, rare ? -2 : -1, |
| 3071 | idx, cur_aff->ah_newID, &spin->si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3072 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3073 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3074 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3075 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 3076 | { |
| 3077 | if (fol != NULL) |
| 3078 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 3079 | fname, lnum); |
| 3080 | else |
| 3081 | fol = vim_strsave(items[1]); |
| 3082 | } |
| 3083 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 3084 | { |
| 3085 | if (low != NULL) |
| 3086 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 3087 | fname, lnum); |
| 3088 | else |
| 3089 | low = vim_strsave(items[1]); |
| 3090 | } |
| 3091 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 3092 | { |
| 3093 | if (upp != NULL) |
| 3094 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 3095 | fname, lnum); |
| 3096 | else |
| 3097 | upp = vim_strsave(items[1]); |
| 3098 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3099 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3100 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3101 | /* Ignore REP count */; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3102 | if (!isdigit(*items[1])) |
| 3103 | smsg((char_u *)_("Expected REP count in %s line %d"), |
| 3104 | fname, lnum); |
| 3105 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3106 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 3107 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3108 | /* REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3109 | if (do_rep) |
| 3110 | add_fromto(spin, &spin->si_rep, items[1], items[2]); |
| 3111 | } |
| 3112 | else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2) |
| 3113 | { |
| 3114 | /* MAP item or count */ |
| 3115 | if (!found_map) |
| 3116 | { |
| 3117 | /* First line contains the count. */ |
| 3118 | found_map = TRUE; |
| 3119 | if (!isdigit(*items[1])) |
| 3120 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 3121 | fname, lnum); |
| 3122 | } |
| 3123 | else if (do_map) |
| 3124 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3125 | int c; |
| 3126 | |
| 3127 | /* Check that every character appears only once. */ |
| 3128 | for (p = items[1]; *p != NUL; ) |
| 3129 | { |
| 3130 | #ifdef FEAT_MBYTE |
| 3131 | c = mb_ptr2char_adv(&p); |
| 3132 | #else |
| 3133 | c = *p++; |
| 3134 | #endif |
| 3135 | if ((spin->si_map.ga_len > 0 |
| 3136 | && vim_strchr(spin->si_map.ga_data, c) |
| 3137 | != NULL) |
| 3138 | || vim_strchr(p, c) != NULL) |
| 3139 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 3140 | fname, lnum); |
| 3141 | } |
| 3142 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3143 | /* We simply concatenate all the MAP strings, separated by |
| 3144 | * slashes. */ |
| 3145 | ga_concat(&spin->si_map, items[1]); |
| 3146 | ga_append(&spin->si_map, '/'); |
| 3147 | } |
| 3148 | } |
| 3149 | else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3) |
| 3150 | { |
| 3151 | if (do_sal) |
| 3152 | { |
| 3153 | /* SAL item (sounds-a-like) |
| 3154 | * Either one of the known keys or a from-to pair. */ |
| 3155 | if (STRCMP(items[1], "followup") == 0) |
| 3156 | spin->si_followup = sal_to_bool(items[2]); |
| 3157 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 3158 | spin->si_collapse = sal_to_bool(items[2]); |
| 3159 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 3160 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 3161 | else |
| 3162 | /* when "to" is "_" it means empty */ |
| 3163 | add_fromto(spin, &spin->si_sal, items[1], |
| 3164 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 3165 | : items[2]); |
| 3166 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3167 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3168 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3169 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 3170 | fname, lnum, items[0]); |
| 3171 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3174 | if (fol != NULL || low != NULL || upp != NULL) |
| 3175 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3176 | if (spin->si_clear_chartab) |
| 3177 | { |
| 3178 | /* Clear the char type tables, don't want to use any of the |
| 3179 | * currently used spell properties. */ |
| 3180 | init_spell_chartab(); |
| 3181 | spin->si_clear_chartab = FALSE; |
| 3182 | } |
| 3183 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3184 | /* |
| 3185 | * Don't write a word table for an ASCII file, so that we don't check |
| 3186 | * for conflicts with a word table that matches 'encoding'. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3187 | * Don't write one for utf-8 either, we use utf_*() and |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3188 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 3189 | */ |
| 3190 | if (!spin->si_ascii |
| 3191 | #ifdef FEAT_MBYTE |
| 3192 | && !enc_utf8 |
| 3193 | #endif |
| 3194 | ) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3195 | { |
| 3196 | if (fol == NULL || low == NULL || upp == NULL) |
| 3197 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 3198 | else |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3199 | (void)set_spell_chartab(fol, low, upp); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 3200 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3201 | |
| 3202 | vim_free(fol); |
| 3203 | vim_free(low); |
| 3204 | vim_free(upp); |
| 3205 | } |
| 3206 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3207 | vim_free(pc); |
| 3208 | fclose(fd); |
| 3209 | return aff; |
| 3210 | } |
| 3211 | |
| 3212 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3213 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 3214 | * NULL as equal. |
| 3215 | */ |
| 3216 | static int |
| 3217 | str_equal(s1, s2) |
| 3218 | char_u *s1; |
| 3219 | char_u *s2; |
| 3220 | { |
| 3221 | if (s1 == NULL || s2 == NULL) |
| 3222 | return s1 == s2; |
| 3223 | return STRCMP(s1, s2) == 0; |
| 3224 | } |
| 3225 | |
| 3226 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3227 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 3228 | * They are stored case-folded. |
| 3229 | */ |
| 3230 | static void |
| 3231 | add_fromto(spin, gap, from, to) |
| 3232 | spellinfo_T *spin; |
| 3233 | garray_T *gap; |
| 3234 | char_u *from; |
| 3235 | char_u *to; |
| 3236 | { |
| 3237 | fromto_T *ftp; |
| 3238 | char_u word[MAXWLEN]; |
| 3239 | |
| 3240 | if (ga_grow(gap, 1) == OK) |
| 3241 | { |
| 3242 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| 3243 | (void)spell_casefold(from, STRLEN(from), word, MAXWLEN); |
| 3244 | ftp->ft_from = getroom_save(&spin->si_blocks, word); |
| 3245 | (void)spell_casefold(to, STRLEN(to), word, MAXWLEN); |
| 3246 | ftp->ft_to = getroom_save(&spin->si_blocks, word); |
| 3247 | ++gap->ga_len; |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 3253 | */ |
| 3254 | static int |
| 3255 | sal_to_bool(s) |
| 3256 | char_u *s; |
| 3257 | { |
| 3258 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 3259 | } |
| 3260 | |
| 3261 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3262 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 3263 | * When "s" is NULL FALSE is returned. |
| 3264 | */ |
| 3265 | static int |
| 3266 | has_non_ascii(s) |
| 3267 | char_u *s; |
| 3268 | { |
| 3269 | char_u *p; |
| 3270 | |
| 3271 | if (s != NULL) |
| 3272 | for (p = s; *p != NUL; ++p) |
| 3273 | if (*p >= 128) |
| 3274 | return TRUE; |
| 3275 | return FALSE; |
| 3276 | } |
| 3277 | |
| 3278 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3279 | * Free the structure filled by spell_read_aff(). |
| 3280 | */ |
| 3281 | static void |
| 3282 | spell_free_aff(aff) |
| 3283 | afffile_T *aff; |
| 3284 | { |
| 3285 | hashtab_T *ht; |
| 3286 | hashitem_T *hi; |
| 3287 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3288 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3289 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3290 | |
| 3291 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3292 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3293 | /* All this trouble to free the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3294 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 3295 | { |
| 3296 | todo = ht->ht_used; |
| 3297 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3298 | { |
| 3299 | if (!HASHITEM_EMPTY(hi)) |
| 3300 | { |
| 3301 | --todo; |
| 3302 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3303 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3304 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3305 | } |
| 3306 | } |
| 3307 | if (ht == &aff->af_suff) |
| 3308 | break; |
| 3309 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3310 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3311 | hash_clear(&aff->af_pref); |
| 3312 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3316 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3317 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3318 | */ |
| 3319 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3320 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3321 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3322 | spellinfo_T *spin; |
| 3323 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3324 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3325 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3326 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3327 | char_u *afflist; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3328 | char_u *pfxlist; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3329 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3330 | char_u *pc; |
| 3331 | char_u *w; |
| 3332 | int l; |
| 3333 | hash_T hash; |
| 3334 | hashitem_T *hi; |
| 3335 | FILE *fd; |
| 3336 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3337 | int non_ascii = 0; |
| 3338 | int retval = OK; |
| 3339 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3340 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3341 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3342 | /* |
| 3343 | * Open the file. |
| 3344 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3345 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3346 | if (fd == NULL) |
| 3347 | { |
| 3348 | EMSG2(_(e_notopen), fname); |
| 3349 | return FAIL; |
| 3350 | } |
| 3351 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3352 | /* The hashtable is only used to detect duplicated words. */ |
| 3353 | hash_init(&ht); |
| 3354 | |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 3355 | spin->si_foldwcount = 0; |
| 3356 | spin->si_keepwcount = 0; |
| 3357 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3358 | if (spin->si_verbose || p_verbose > 2) |
| 3359 | { |
| 3360 | if (!spin->si_verbose) |
| 3361 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3362 | smsg((char_u *)_("Reading dictionary file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3363 | out_flush(); |
| 3364 | if (!spin->si_verbose) |
| 3365 | verbose_leave(); |
| 3366 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3367 | |
| 3368 | /* Read and ignore the first line: word count. */ |
| 3369 | (void)vim_fgets(line, MAXLINELEN, fd); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3370 | if (!vim_isdigit(*skipwhite(line))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3371 | EMSG2(_("E760: No word count in %s"), fname); |
| 3372 | |
| 3373 | /* |
| 3374 | * Read all the lines in the file one by one. |
| 3375 | * The words are converted to 'encoding' here, before being added to |
| 3376 | * the hashtable. |
| 3377 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3378 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3379 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3380 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3381 | ++lnum; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 3382 | if (line[0] == '#') |
| 3383 | continue; /* comment line */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3384 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3385 | /* Remove CR, LF and white space from the end. White space halfway |
| 3386 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3387 | l = STRLEN(line); |
| 3388 | while (l > 0 && line[l - 1] <= ' ') |
| 3389 | --l; |
| 3390 | if (l == 0) |
| 3391 | continue; /* empty line */ |
| 3392 | line[l] = NUL; |
| 3393 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3394 | /* Find the optional affix names. */ |
| 3395 | afflist = vim_strchr(line, '/'); |
| 3396 | if (afflist != NULL) |
| 3397 | *afflist++ = NUL; |
| 3398 | |
| 3399 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3400 | if (spin->si_ascii && has_non_ascii(line)) |
| 3401 | { |
| 3402 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3403 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3404 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3405 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3406 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3407 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3408 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3409 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3410 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3411 | if (pc == NULL) |
| 3412 | { |
| 3413 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3414 | fname, lnum, line); |
| 3415 | continue; |
| 3416 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3417 | w = pc; |
| 3418 | } |
| 3419 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3420 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3421 | { |
| 3422 | pc = NULL; |
| 3423 | w = line; |
| 3424 | } |
| 3425 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3426 | /* This takes time, print a message now and then. */ |
| 3427 | if (spin->si_verbose && (lnum & 0x3ff) == 0) |
| 3428 | { |
| 3429 | vim_snprintf((char *)message, sizeof(message), |
| 3430 | _("line %6d, word %6d - %s"), |
| 3431 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 3432 | msg_start(); |
| 3433 | msg_puts_long_attr(message, 0); |
| 3434 | msg_clr_eos(); |
| 3435 | msg_didout = FALSE; |
| 3436 | msg_col = 0; |
| 3437 | out_flush(); |
| 3438 | } |
| 3439 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3440 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 3441 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3442 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3443 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3444 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3445 | if (retval == FAIL) |
| 3446 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3447 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3448 | hash = hash_hash(dw); |
| 3449 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3450 | if (!HASHITEM_EMPTY(hi)) |
| 3451 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3452 | fname, lnum, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3453 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3454 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3455 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3456 | flags = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3457 | pfxlist = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3458 | if (afflist != NULL) |
| 3459 | { |
| 3460 | /* Check for affix name that stands for keep-case word and stands |
| 3461 | * for rare word (if defined). */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3462 | if (affile->af_kep != NUL |
| 3463 | && vim_strchr(afflist, affile->af_kep) != NULL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3464 | flags |= WF_KEEPCAP; |
| 3465 | if (affile->af_rar != NUL |
| 3466 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 3467 | flags |= WF_RARE; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 3468 | if (affile->af_bad != NUL |
| 3469 | && vim_strchr(afflist, affile->af_bad) != NULL) |
| 3470 | flags |= WF_BANNED; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3471 | |
| 3472 | if (affile->af_pfxpostpone) |
| 3473 | /* Need to store the list of prefix IDs with the word. */ |
| 3474 | pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3477 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3478 | if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3479 | retval = FAIL; |
| 3480 | |
| 3481 | if (afflist != NULL) |
| 3482 | { |
| 3483 | /* Find all matching suffixes and add the resulting words. |
| 3484 | * Additionally do matching prefixes that combine. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3485 | if (store_aff_word(dw, spin, afflist, affile, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3486 | &affile->af_suff, &affile->af_pref, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3487 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3488 | retval = FAIL; |
| 3489 | |
| 3490 | /* Find all matching prefixes and add the resulting words. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3491 | if (store_aff_word(dw, spin, afflist, affile, |
| 3492 | &affile->af_pref, NULL, |
| 3493 | FALSE, flags, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3494 | retval = FAIL; |
| 3495 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3498 | if (spin->si_ascii && non_ascii > 0) |
| 3499 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3500 | non_ascii); |
| 3501 | hash_clear(&ht); |
| 3502 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3503 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3504 | return retval; |
| 3505 | } |
| 3506 | |
| 3507 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3508 | * Get the list of prefix IDs from the affix list "afflist". |
| 3509 | * Used for PFXPOSTPONE. |
| 3510 | * Returns a string allocated with getroom(). NULL when there are no prefixes |
| 3511 | * or when out of memory. |
| 3512 | */ |
| 3513 | static char_u * |
| 3514 | get_pfxlist(affile, afflist, blp) |
| 3515 | afffile_T *affile; |
| 3516 | char_u *afflist; |
| 3517 | sblock_T **blp; |
| 3518 | { |
| 3519 | char_u *p; |
| 3520 | int cnt; |
| 3521 | int round; |
| 3522 | char_u *res = NULL; |
| 3523 | char_u key[2]; |
| 3524 | hashitem_T *hi; |
| 3525 | |
| 3526 | key[1] = NUL; |
| 3527 | |
| 3528 | /* round 1: count the number of prefix IDs. |
| 3529 | * round 2: move prefix IDs to "res" */ |
| 3530 | for (round = 1; round <= 2; ++round) |
| 3531 | { |
| 3532 | cnt = 0; |
| 3533 | for (p = afflist; *p != NUL; ++p) |
| 3534 | { |
| 3535 | key[0] = *p; |
| 3536 | hi = hash_find(&affile->af_pref, key); |
| 3537 | if (!HASHITEM_EMPTY(hi)) |
| 3538 | { |
| 3539 | /* This is a prefix ID, use the new number. */ |
| 3540 | if (round == 2) |
| 3541 | res[cnt] = HI2AH(hi)->ah_newID; |
| 3542 | ++cnt; |
| 3543 | } |
| 3544 | } |
| 3545 | if (round == 1 && cnt > 0) |
| 3546 | res = getroom(blp, cnt + 1); |
| 3547 | if (res == NULL) |
| 3548 | break; |
| 3549 | } |
| 3550 | |
| 3551 | if (res != NULL) |
| 3552 | res[cnt] = NUL; |
| 3553 | return res; |
| 3554 | } |
| 3555 | |
| 3556 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3557 | * Apply affixes to a word and store the resulting words. |
| 3558 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 3559 | * prefixes or suffixes. |
| 3560 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 3561 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3562 | * |
| 3563 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3564 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3565 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3566 | store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3567 | char_u *word; /* basic word start */ |
| 3568 | spellinfo_T *spin; /* spell info */ |
| 3569 | char_u *afflist; /* list of names of supported affixes */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3570 | afffile_T *affile; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3571 | hashtab_T *ht; |
| 3572 | hashtab_T *xht; |
| 3573 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3574 | int flags; /* flags for the word */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3575 | char_u *pfxlist; /* list of prefix IDs */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3576 | { |
| 3577 | int todo; |
| 3578 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3579 | affheader_T *ah; |
| 3580 | affentry_T *ae; |
| 3581 | regmatch_T regmatch; |
| 3582 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3583 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3584 | int i; |
| 3585 | char_u *p; |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3586 | int use_flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3587 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3588 | todo = ht->ht_used; |
| 3589 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3590 | { |
| 3591 | if (!HASHITEM_EMPTY(hi)) |
| 3592 | { |
| 3593 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3594 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3595 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3596 | /* Check that the affix combines, if required, and that the word |
| 3597 | * supports this affix. */ |
| 3598 | if ((!comb || ah->ah_combine) |
| 3599 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3600 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3601 | /* Loop over all affix entries with this name. */ |
| 3602 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3603 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3604 | /* Check the condition. It's not logical to match case |
| 3605 | * here, but it is required for compatibility with |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3606 | * Myspell. |
| 3607 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| 3608 | * prefixes with a chop string. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3609 | regmatch.regprog = ae->ae_prog; |
| 3610 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3611 | if ((xht != NULL || !affile->af_pfxpostpone |
| 3612 | || ae->ae_chop != NULL) |
| 3613 | && (ae->ae_prog == NULL |
| 3614 | || vim_regexec(®match, word, (colnr_T)0))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3615 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3616 | /* Match. Remove the chop and add the affix. */ |
| 3617 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3618 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3619 | /* prefix: chop/add at the start of the word */ |
| 3620 | if (ae->ae_add == NULL) |
| 3621 | *newword = NUL; |
| 3622 | else |
| 3623 | STRCPY(newword, ae->ae_add); |
| 3624 | p = word; |
| 3625 | if (ae->ae_chop != NULL) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3626 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3627 | /* Skip chop string. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3628 | #ifdef FEAT_MBYTE |
| 3629 | if (has_mbyte) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3630 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3631 | i = mb_charlen(ae->ae_chop); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3632 | for ( ; i > 0; --i) |
| 3633 | mb_ptr_adv(p); |
| 3634 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3635 | else |
| 3636 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 3637 | p += STRLEN(ae->ae_chop); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3638 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3639 | STRCAT(newword, p); |
| 3640 | } |
| 3641 | else |
| 3642 | { |
| 3643 | /* suffix: chop/add at the end of the word */ |
| 3644 | STRCPY(newword, word); |
| 3645 | if (ae->ae_chop != NULL) |
| 3646 | { |
| 3647 | /* Remove chop string. */ |
| 3648 | p = newword + STRLEN(newword); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3649 | #ifdef FEAT_MBYTE |
| 3650 | if (has_mbyte) |
| 3651 | i = mb_charlen(ae->ae_chop); |
| 3652 | else |
| 3653 | #endif |
| 3654 | i = STRLEN(ae->ae_chop); |
| 3655 | for ( ; i > 0; --i) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3656 | mb_ptr_back(newword, p); |
| 3657 | *p = NUL; |
| 3658 | } |
| 3659 | if (ae->ae_add != NULL) |
| 3660 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3663 | /* Obey the "rare" flag of the affix. */ |
| 3664 | if (ae->ae_rare) |
| 3665 | use_flags = flags | WF_RARE; |
| 3666 | else |
| 3667 | use_flags = flags; |
| 3668 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3669 | /* Store the modified word. */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3670 | if (store_word(newword, spin, use_flags, |
| 3671 | spin->si_region, pfxlist) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3672 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3673 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3674 | /* When added a suffix and combining is allowed also |
| 3675 | * try adding prefixes additionally. */ |
| 3676 | if (xht != NULL && ah->ah_combine) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3677 | if (store_aff_word(newword, spin, afflist, affile, |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3678 | xht, NULL, TRUE, use_flags, pfxlist) |
| 3679 | == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3680 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 3687 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3691 | * Read a file with a list of words. |
| 3692 | */ |
| 3693 | static int |
| 3694 | spell_read_wordfile(fname, spin) |
| 3695 | char_u *fname; |
| 3696 | spellinfo_T *spin; |
| 3697 | { |
| 3698 | FILE *fd; |
| 3699 | long lnum = 0; |
| 3700 | char_u rline[MAXLINELEN]; |
| 3701 | char_u *line; |
| 3702 | char_u *pc = NULL; |
| 3703 | int l; |
| 3704 | int retval = OK; |
| 3705 | int did_word = FALSE; |
| 3706 | int non_ascii = 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3707 | int flags; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3708 | int regionmask; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3709 | |
| 3710 | /* |
| 3711 | * Open the file. |
| 3712 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3713 | fd = mch_fopen((char *)fname, "r"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3714 | if (fd == NULL) |
| 3715 | { |
| 3716 | EMSG2(_(e_notopen), fname); |
| 3717 | return FAIL; |
| 3718 | } |
| 3719 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3720 | if (spin->si_verbose || p_verbose > 2) |
| 3721 | { |
| 3722 | if (!spin->si_verbose) |
| 3723 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 3724 | smsg((char_u *)_("Reading word file %s ..."), fname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3725 | out_flush(); |
| 3726 | if (!spin->si_verbose) |
| 3727 | verbose_leave(); |
| 3728 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3729 | |
| 3730 | /* |
| 3731 | * Read all the lines in the file one by one. |
| 3732 | */ |
| 3733 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 3734 | { |
| 3735 | line_breakcheck(); |
| 3736 | ++lnum; |
| 3737 | |
| 3738 | /* Skip comment lines. */ |
| 3739 | if (*rline == '#') |
| 3740 | continue; |
| 3741 | |
| 3742 | /* Remove CR, LF and white space from the end. */ |
| 3743 | l = STRLEN(rline); |
| 3744 | while (l > 0 && rline[l - 1] <= ' ') |
| 3745 | --l; |
| 3746 | if (l == 0) |
| 3747 | continue; /* empty or blank line */ |
| 3748 | rline[l] = NUL; |
| 3749 | |
| 3750 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 3751 | vim_free(pc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3752 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3753 | if (spin->si_conv.vc_type != CONV_NONE) |
| 3754 | { |
| 3755 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 3756 | if (pc == NULL) |
| 3757 | { |
| 3758 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 3759 | fname, lnum, rline); |
| 3760 | continue; |
| 3761 | } |
| 3762 | line = pc; |
| 3763 | } |
| 3764 | else |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3765 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3766 | { |
| 3767 | pc = NULL; |
| 3768 | line = rline; |
| 3769 | } |
| 3770 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3771 | flags = 0; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3772 | regionmask = spin->si_region; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3773 | |
| 3774 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3775 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3776 | ++line; |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3777 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3778 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3779 | { |
| 3780 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3781 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 3782 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3783 | else if (did_word) |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3784 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 3785 | fname, lnum, line - 1); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3786 | else |
| 3787 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3788 | #ifdef FEAT_MBYTE |
| 3789 | char_u *enc; |
| 3790 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3791 | /* Setup for conversion to 'encoding'. */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3792 | line += 10; |
| 3793 | enc = enc_canonize(line); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3794 | if (enc != NULL && !spin->si_ascii |
| 3795 | && convert_setup(&spin->si_conv, enc, |
| 3796 | p_enc) == FAIL) |
| 3797 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3798 | fname, line, p_enc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3799 | vim_free(enc); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3800 | #else |
| 3801 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 3802 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3803 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3804 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3805 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3806 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3807 | if (STRNCMP(line, "regions=", 8) == 0) |
| 3808 | { |
| 3809 | if (spin->si_region_count > 1) |
| 3810 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 3811 | fname, lnum, line); |
| 3812 | else |
| 3813 | { |
| 3814 | line += 8; |
| 3815 | if (STRLEN(line) > 16) |
| 3816 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 3817 | fname, lnum, line); |
| 3818 | else |
| 3819 | { |
| 3820 | spin->si_region_count = STRLEN(line) / 2; |
| 3821 | STRCPY(spin->si_region_name, line); |
| 3822 | } |
| 3823 | } |
| 3824 | continue; |
| 3825 | } |
| 3826 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3827 | if (*line == '=') |
| 3828 | { |
| 3829 | /* keep-case word */ |
| 3830 | flags |= WF_KEEPCAP; |
| 3831 | ++line; |
| 3832 | } |
| 3833 | |
| 3834 | if (*line == '!') |
| 3835 | { |
| 3836 | /* Bad, bad, wicked word. */ |
| 3837 | flags |= WF_BANNED; |
| 3838 | ++line; |
| 3839 | } |
| 3840 | else if (*line == '?') |
| 3841 | { |
| 3842 | /* Rare word. */ |
| 3843 | flags |= WF_RARE; |
| 3844 | ++line; |
| 3845 | } |
| 3846 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3847 | if (VIM_ISDIGIT(*line)) |
| 3848 | { |
| 3849 | /* region number(s) */ |
| 3850 | regionmask = 0; |
| 3851 | while (VIM_ISDIGIT(*line)) |
| 3852 | { |
| 3853 | l = *line - '0'; |
| 3854 | if (l > spin->si_region_count) |
| 3855 | { |
| 3856 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
| 3857 | fname, lnum, line); |
| 3858 | break; |
| 3859 | } |
| 3860 | regionmask |= 1 << (l - 1); |
| 3861 | ++line; |
| 3862 | } |
| 3863 | flags |= WF_REGION; |
| 3864 | } |
| 3865 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3866 | if (flags == 0) |
| 3867 | { |
| 3868 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3869 | fname, lnum, line); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3870 | continue; |
| 3871 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 3875 | if (spin->si_ascii && has_non_ascii(line)) |
| 3876 | { |
| 3877 | ++non_ascii; |
| 3878 | continue; |
| 3879 | } |
| 3880 | |
| 3881 | /* Normal word: store it. */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3882 | if (store_word(line, spin, flags, regionmask, NULL) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3883 | { |
| 3884 | retval = FAIL; |
| 3885 | break; |
| 3886 | } |
| 3887 | did_word = TRUE; |
| 3888 | } |
| 3889 | |
| 3890 | vim_free(pc); |
| 3891 | fclose(fd); |
| 3892 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3893 | if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2)) |
| 3894 | { |
| 3895 | if (p_verbose > 2) |
| 3896 | verbose_enter(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3897 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 3898 | non_ascii); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 3899 | if (p_verbose > 2) |
| 3900 | verbose_leave(); |
| 3901 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3902 | return retval; |
| 3903 | } |
| 3904 | |
| 3905 | /* |
| 3906 | * Get part of an sblock_T, "len" bytes long. |
| 3907 | * This avoids calling free() for every little struct we use. |
| 3908 | * The memory is cleared to all zeros. |
| 3909 | * Returns NULL when out of memory. |
| 3910 | */ |
| 3911 | static void * |
| 3912 | getroom(blp, len) |
| 3913 | sblock_T **blp; |
| 3914 | size_t len; /* length needed */ |
| 3915 | { |
| 3916 | char_u *p; |
| 3917 | sblock_T *bl = *blp; |
| 3918 | |
| 3919 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 3920 | { |
| 3921 | /* Allocate a block of memory. This is not freed until much later. */ |
| 3922 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 3923 | if (bl == NULL) |
| 3924 | return NULL; |
| 3925 | bl->sb_next = *blp; |
| 3926 | *blp = bl; |
| 3927 | bl->sb_used = 0; |
| 3928 | } |
| 3929 | |
| 3930 | p = bl->sb_data + bl->sb_used; |
| 3931 | bl->sb_used += len; |
| 3932 | |
| 3933 | return p; |
| 3934 | } |
| 3935 | |
| 3936 | /* |
| 3937 | * Make a copy of a string into memory allocated with getroom(). |
| 3938 | */ |
| 3939 | static char_u * |
| 3940 | getroom_save(blp, s) |
| 3941 | sblock_T **blp; |
| 3942 | char_u *s; |
| 3943 | { |
| 3944 | char_u *sc; |
| 3945 | |
| 3946 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 3947 | if (sc != NULL) |
| 3948 | STRCPY(sc, s); |
| 3949 | return sc; |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | /* |
| 3954 | * Free the list of allocated sblock_T. |
| 3955 | */ |
| 3956 | static void |
| 3957 | free_blocks(bl) |
| 3958 | sblock_T *bl; |
| 3959 | { |
| 3960 | sblock_T *next; |
| 3961 | |
| 3962 | while (bl != NULL) |
| 3963 | { |
| 3964 | next = bl->sb_next; |
| 3965 | vim_free(bl); |
| 3966 | bl = next; |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | /* |
| 3971 | * Allocate the root of a word tree. |
| 3972 | */ |
| 3973 | static wordnode_T * |
| 3974 | wordtree_alloc(blp) |
| 3975 | sblock_T **blp; |
| 3976 | { |
| 3977 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 3978 | } |
| 3979 | |
| 3980 | /* |
| 3981 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3982 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 3983 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3984 | * 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] | 3985 | * When "pfxlist" is not NULL store the word for each prefix ID. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3986 | */ |
| 3987 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3988 | store_word(word, spin, flags, region, pfxlist) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3989 | char_u *word; |
| 3990 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 3991 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 3992 | int region; /* supported region(s) */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3993 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 3994 | { |
| 3995 | int len = STRLEN(word); |
| 3996 | int ct = captype(word, word + len); |
| 3997 | char_u foldword[MAXWLEN]; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 3998 | int res = OK; |
| 3999 | char_u *p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4000 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4001 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4002 | for (p = pfxlist; res == OK; ++p) |
| 4003 | { |
| 4004 | res = tree_add_word(foldword, spin->si_foldroot, ct | flags, |
| 4005 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4006 | if (p == NULL || *p == NUL) |
| 4007 | break; |
| 4008 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4009 | ++spin->si_foldwcount; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4010 | |
| 4011 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4012 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4013 | for (p = pfxlist; res == OK; ++p) |
| 4014 | { |
| 4015 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 4016 | region, p == NULL ? 0 : *p, &spin->si_blocks); |
| 4017 | if (p == NULL || *p == NUL) |
| 4018 | break; |
| 4019 | } |
Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 4020 | ++spin->si_keepwcount; |
| 4021 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4022 | return res; |
| 4023 | } |
| 4024 | |
| 4025 | /* |
| 4026 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4027 | * When "flags" < 0 we are adding to the prefix tree where flags is used for |
| 4028 | * "rare" and "region" is the condition nr. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4029 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4030 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4031 | static int |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4032 | tree_add_word(word, root, flags, region, prefixID, blp) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4033 | char_u *word; |
| 4034 | wordnode_T *root; |
| 4035 | int flags; |
| 4036 | int region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4037 | int prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4038 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4039 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4040 | wordnode_T *node = root; |
| 4041 | wordnode_T *np; |
| 4042 | wordnode_T **prev = NULL; |
| 4043 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4044 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4045 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 4046 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4047 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4048 | /* Look for the sibling that has the same character. They are sorted |
| 4049 | * 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] | 4050 | * higher byte value. For zero bytes (end of word) the sorting is |
| 4051 | * done on flags and then on prefixID |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4052 | */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4053 | while (node != NULL |
| 4054 | && (node->wn_byte < word[i] |
| 4055 | || (node->wn_byte == NUL |
| 4056 | && (flags < 0 |
| 4057 | ? node->wn_prefixID < prefixID |
| 4058 | : node->wn_flags < (flags & 0xff) |
| 4059 | || (node->wn_flags == (flags & 0xff) |
| 4060 | && node->wn_prefixID < prefixID))))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4061 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4062 | prev = &node->wn_sibling; |
| 4063 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4064 | } |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4065 | if (node == NULL |
| 4066 | || node->wn_byte != word[i] |
| 4067 | || (word[i] == NUL |
| 4068 | && (flags < 0 |
| 4069 | || node->wn_flags != (flags & 0xff) |
| 4070 | || node->wn_prefixID != prefixID))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4071 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4072 | /* Allocate a new node. */ |
| 4073 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 4074 | if (np == NULL) |
| 4075 | return FAIL; |
| 4076 | np->wn_byte = word[i]; |
| 4077 | *prev = np; |
| 4078 | np->wn_sibling = node; |
| 4079 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4080 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4081 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4082 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4083 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4084 | node->wn_flags = flags; |
| 4085 | node->wn_region |= region; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4086 | node->wn_prefixID = prefixID; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4087 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4088 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4089 | prev = &node->wn_child; |
| 4090 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4097 | * Compress a tree: find tails that are identical and can be shared. |
| 4098 | */ |
| 4099 | static void |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4100 | wordtree_compress(root, spin) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4101 | wordnode_T *root; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4102 | spellinfo_T *spin; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4103 | { |
| 4104 | hashtab_T ht; |
| 4105 | int n; |
| 4106 | int tot = 0; |
| 4107 | |
| 4108 | if (root != NULL) |
| 4109 | { |
| 4110 | hash_init(&ht); |
| 4111 | n = node_compress(root, &ht, &tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4112 | if (spin->si_verbose || p_verbose > 2) |
| 4113 | { |
| 4114 | if (!spin->si_verbose) |
| 4115 | verbose_enter(); |
| 4116 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4117 | n, tot, (tot - n) * 100 / tot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4118 | if (p_verbose > 2) |
| 4119 | verbose_leave(); |
| 4120 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4121 | hash_clear(&ht); |
| 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | /* |
| 4126 | * Compress a node, its siblings and its children, depth first. |
| 4127 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4128 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4129 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4130 | node_compress(node, ht, tot) |
| 4131 | wordnode_T *node; |
| 4132 | hashtab_T *ht; |
| 4133 | int *tot; /* total count of nodes before compressing, |
| 4134 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4135 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4136 | wordnode_T *np; |
| 4137 | wordnode_T *tp; |
| 4138 | wordnode_T *child; |
| 4139 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4140 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4141 | int len = 0; |
| 4142 | unsigned nr, n; |
| 4143 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4144 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4145 | /* |
| 4146 | * Go through the list of siblings. Compress each child and then try |
| 4147 | * finding an identical child to replace it. |
| 4148 | * Note that with "child" we mean not just the node that is pointed to, |
| 4149 | * but the whole list of siblings, of which the node is the first. |
| 4150 | */ |
| 4151 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4152 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4153 | ++len; |
| 4154 | if ((child = np->wn_child) != NULL) |
| 4155 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4156 | /* Compress the child. This fills hashkey. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4157 | compressed += node_compress(child, ht, tot); |
| 4158 | |
| 4159 | /* Try to find an identical child. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4160 | hash = hash_hash(child->wn_u1.hashkey); |
| 4161 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4162 | tp = NULL; |
| 4163 | if (!HASHITEM_EMPTY(hi)) |
| 4164 | { |
| 4165 | /* There are children with an identical hash value. Now check |
| 4166 | * if there is one that is really identical. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4167 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4168 | if (node_equal(child, tp)) |
| 4169 | { |
| 4170 | /* Found one! Now use that child in place of the |
| 4171 | * current one. This means the current child is |
| 4172 | * dropped from the tree. */ |
| 4173 | np->wn_child = tp; |
| 4174 | ++compressed; |
| 4175 | break; |
| 4176 | } |
| 4177 | if (tp == NULL) |
| 4178 | { |
| 4179 | /* No other child with this hash value equals the child of |
| 4180 | * the node, add it to the linked list after the first |
| 4181 | * item. */ |
| 4182 | tp = HI2WN(hi); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4183 | child->wn_u2.next = tp->wn_u2.next; |
| 4184 | tp->wn_u2.next = child; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4185 | } |
| 4186 | } |
| 4187 | else |
| 4188 | /* No other child has this hash value, add it to the |
| 4189 | * hashtable. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4190 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4191 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4192 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4193 | *tot += len; |
| 4194 | |
| 4195 | /* |
| 4196 | * Make a hash key for the node and its siblings, so that we can quickly |
| 4197 | * find a lookalike node. This must be done after compressing the sibling |
| 4198 | * list, otherwise the hash key would become invalid by the compression. |
| 4199 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4200 | node->wn_u1.hashkey[0] = len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4201 | nr = 0; |
| 4202 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4203 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4204 | if (np->wn_byte == NUL) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4205 | /* end node: use wn_flags, wn_region and wn_prefixID */ |
| 4206 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4207 | else |
| 4208 | /* byte node: use the byte value and the child pointer */ |
| 4209 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 4210 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4211 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4212 | |
| 4213 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 4214 | n = nr & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4215 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4216 | n = (nr >> 8) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4217 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4218 | n = (nr >> 16) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4219 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4220 | n = (nr >> 24) & 0xff; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4221 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 4222 | node->wn_u1.hashkey[5] = NUL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4223 | |
| 4224 | return compressed; |
| 4225 | } |
| 4226 | |
| 4227 | /* |
| 4228 | * Return TRUE when two nodes have identical siblings and children. |
| 4229 | */ |
| 4230 | static int |
| 4231 | node_equal(n1, n2) |
| 4232 | wordnode_T *n1; |
| 4233 | wordnode_T *n2; |
| 4234 | { |
| 4235 | wordnode_T *p1; |
| 4236 | wordnode_T *p2; |
| 4237 | |
| 4238 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 4239 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 4240 | if (p1->wn_byte != p2->wn_byte |
| 4241 | || (p1->wn_byte == NUL |
| 4242 | ? (p1->wn_flags != p2->wn_flags |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4243 | || p1->wn_region != p2->wn_region |
| 4244 | || p1->wn_prefixID != p2->wn_prefixID) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4245 | : (p1->wn_child != p2->wn_child))) |
| 4246 | break; |
| 4247 | |
| 4248 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | /* |
| 4252 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 4253 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4254 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4255 | put_bytes(fd, nr, len) |
| 4256 | FILE *fd; |
| 4257 | long_u nr; |
| 4258 | int len; |
| 4259 | { |
| 4260 | int i; |
| 4261 | |
| 4262 | for (i = len - 1; i >= 0; --i) |
| 4263 | putc((int)(nr >> (i * 8)), fd); |
| 4264 | } |
| 4265 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4266 | static int |
| 4267 | #ifdef __BORLANDC__ |
| 4268 | _RTLENTRYF |
| 4269 | #endif |
| 4270 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 4271 | |
| 4272 | /* |
| 4273 | * Function given to qsort() to sort the REP items on "from" string. |
| 4274 | */ |
| 4275 | static int |
| 4276 | #ifdef __BORLANDC__ |
| 4277 | _RTLENTRYF |
| 4278 | #endif |
| 4279 | rep_compare(s1, s2) |
| 4280 | const void *s1; |
| 4281 | const void *s2; |
| 4282 | { |
| 4283 | fromto_T *p1 = (fromto_T *)s1; |
| 4284 | fromto_T *p2 = (fromto_T *)s2; |
| 4285 | |
| 4286 | return STRCMP(p1->ft_from, p2->ft_from); |
| 4287 | } |
| 4288 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4289 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4290 | * Write the Vim spell file "fname". |
| 4291 | */ |
| 4292 | static void |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4293 | write_vim_spell(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4294 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4295 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4296 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4297 | FILE *fd; |
| 4298 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4299 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4300 | wordnode_T *tree; |
| 4301 | int nodecount; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4302 | int i; |
| 4303 | int l; |
| 4304 | garray_T *gap; |
| 4305 | fromto_T *ftp; |
| 4306 | char_u *p; |
| 4307 | int rr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4308 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4309 | fd = mch_fopen((char *)fname, "w"); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4310 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4311 | { |
| 4312 | EMSG2(_(e_notopen), fname); |
| 4313 | return; |
| 4314 | } |
| 4315 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4316 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4317 | * <charflagslen> <charflags> |
| 4318 | * <fcharslen> <fchars> |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4319 | * <midwordlen> <midword> |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4320 | * <prefcondcnt> <prefcond> ... */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4321 | |
| 4322 | /* <fileID> */ |
| 4323 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 4324 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4325 | |
| 4326 | /* write the region names if there is more than one */ |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4327 | if (spin->si_region_count > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4328 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4329 | putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */ |
| 4330 | fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2), |
| 4331 | (size_t)1, fd); |
| 4332 | regionmask = (1 << spin->si_region_count) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4333 | } |
| 4334 | else |
| 4335 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4336 | putc(0, fd); |
| 4337 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4340 | /* |
| 4341 | * Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4342 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 4343 | * 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] | 4344 | * 'encoding'. |
| 4345 | * Also skip this for an .add.spl file, the main spell file must contain |
| 4346 | * the table (avoids that it conflicts). File is shorter too. |
| 4347 | */ |
| 4348 | if (spin->si_ascii || spin->si_add) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4349 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4350 | putc(0, fd); |
| 4351 | putc(0, fd); |
| 4352 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 4353 | } |
| 4354 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4355 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4356 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4357 | |
| 4358 | if (spin->si_midword == NULL) |
| 4359 | put_bytes(fd, 0L, 2); /* <midwordlen> */ |
| 4360 | else |
| 4361 | { |
| 4362 | i = STRLEN(spin->si_midword); |
| 4363 | put_bytes(fd, (long_u)i, 2); /* <midwordlen> */ |
| 4364 | fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */ |
| 4365 | } |
| 4366 | |
| 4367 | |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4368 | /* Write the prefix conditions. */ |
| 4369 | write_spell_prefcond(fd, &spin->si_prefcond); |
| 4370 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4371 | /* <SUGGEST> : <repcount> <rep> ... |
| 4372 | * <salflags> <salcount> <sal> ... |
| 4373 | * <maplen> <mapstr> */ |
| 4374 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4375 | /* Sort the REP items. */ |
| 4376 | qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len, |
| 4377 | sizeof(fromto_T), rep_compare); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4378 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4379 | for (round = 1; round <= 2; ++round) |
| 4380 | { |
| 4381 | if (round == 1) |
| 4382 | gap = &spin->si_rep; |
| 4383 | else |
| 4384 | { |
| 4385 | gap = &spin->si_sal; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4386 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4387 | i = 0; |
| 4388 | if (spin->si_followup) |
| 4389 | i |= SAL_F0LLOWUP; |
| 4390 | if (spin->si_collapse) |
| 4391 | i |= SAL_COLLAPSE; |
| 4392 | if (spin->si_rem_accents) |
| 4393 | i |= SAL_REM_ACCENTS; |
| 4394 | putc(i, fd); /* <salflags> */ |
| 4395 | } |
| 4396 | |
| 4397 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 4398 | for (i = 0; i < gap->ga_len; ++i) |
| 4399 | { |
| 4400 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 4401 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 4402 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 4403 | for (rr = 1; rr <= 2; ++rr) |
| 4404 | { |
| 4405 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| 4406 | l = STRLEN(p); |
| 4407 | putc(l, fd); |
| 4408 | fwrite(p, l, (size_t)1, fd); |
| 4409 | } |
| 4410 | } |
| 4411 | } |
| 4412 | |
| 4413 | put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */ |
| 4414 | if (spin->si_map.ga_len > 0) /* <mapstr> */ |
| 4415 | fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len, |
| 4416 | (size_t)1, fd); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4417 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4418 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4419 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4420 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4421 | spin->si_memtot = 0; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4422 | for (round = 1; round <= 3; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4423 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4424 | if (round == 1) |
| 4425 | tree = spin->si_foldroot; |
| 4426 | else if (round == 2) |
| 4427 | tree = spin->si_keeproot; |
| 4428 | else |
| 4429 | tree = spin->si_prefroot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4430 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4431 | /* Clear the index and wnode fields in the tree. */ |
| 4432 | clear_node(tree); |
| 4433 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4434 | /* Count the number of nodes. Needed to be able to allocate the |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4435 | * memory when reading the nodes. Also fills in index for shared |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4436 | * nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4437 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4438 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4439 | /* number of nodes in 4 bytes */ |
| 4440 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4441 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4442 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4443 | /* Write the nodes. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4444 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4447 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4451 | * Clear the index and wnode fields of "node", it siblings and its |
| 4452 | * children. This is needed because they are a union with other items to save |
| 4453 | * space. |
| 4454 | */ |
| 4455 | static void |
| 4456 | clear_node(node) |
| 4457 | wordnode_T *node; |
| 4458 | { |
| 4459 | wordnode_T *np; |
| 4460 | |
| 4461 | if (node != NULL) |
| 4462 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4463 | { |
| 4464 | np->wn_u1.index = 0; |
| 4465 | np->wn_u2.wnode = NULL; |
| 4466 | |
| 4467 | if (np->wn_byte != NUL) |
| 4468 | clear_node(np->wn_child); |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | |
| 4473 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4474 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4475 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4476 | * This first writes the list of possible bytes (siblings). Then for each |
| 4477 | * byte recursively write the children. |
| 4478 | * |
| 4479 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 4480 | * are made about the indexes (so that we don't have to write them in the |
| 4481 | * file). |
| 4482 | * |
| 4483 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4484 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4485 | static int |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4486 | put_node(fd, node, index, regionmask, prefixtree) |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4487 | FILE *fd; /* NULL when only counting */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4488 | wordnode_T *node; |
| 4489 | int index; |
| 4490 | int regionmask; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4491 | int prefixtree; /* TRUE for PREFIXTREE */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4492 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4493 | int newindex = index; |
| 4494 | int siblingcount = 0; |
| 4495 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4496 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4497 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4498 | /* If "node" is zero the tree is empty. */ |
| 4499 | if (node == NULL) |
| 4500 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4501 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4502 | /* Store the index where this node is written. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4503 | node->wn_u1.index = index; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4504 | |
| 4505 | /* Count the number of siblings. */ |
| 4506 | for (np = node; np != NULL; np = np->wn_sibling) |
| 4507 | ++siblingcount; |
| 4508 | |
| 4509 | /* Write the sibling count. */ |
| 4510 | if (fd != NULL) |
| 4511 | putc(siblingcount, fd); /* <siblingcount> */ |
| 4512 | |
| 4513 | /* Write each sibling byte and optionally extra info. */ |
| 4514 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4515 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4516 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4517 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4518 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4519 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4520 | /* For a NUL byte (end of word) write the flags etc. */ |
| 4521 | if (prefixtree) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4522 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4523 | /* In PREFIXTREE write the required prefixID and the |
| 4524 | * associated condition nr (stored in wn_region). */ |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4525 | if (np->wn_flags == (char_u)-2) |
| 4526 | putc(BY_FLAGS, fd); /* <byte> rare */ |
| 4527 | else |
| 4528 | putc(BY_NOFLAGS, fd); /* <byte> */ |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4529 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4530 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4531 | } |
| 4532 | else |
| 4533 | { |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4534 | /* For word trees we write the flag/region items. */ |
| 4535 | flags = np->wn_flags; |
| 4536 | if (regionmask != 0 && np->wn_region != regionmask) |
| 4537 | flags |= WF_REGION; |
| 4538 | if (np->wn_prefixID != 0) |
| 4539 | flags |= WF_PFX; |
| 4540 | if (flags == 0) |
| 4541 | { |
| 4542 | /* word without flags or region */ |
| 4543 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 4544 | } |
| 4545 | else |
| 4546 | { |
| 4547 | putc(BY_FLAGS, fd); /* <byte> */ |
| 4548 | putc(flags, fd); /* <flags> */ |
| 4549 | if (flags & WF_REGION) |
| 4550 | putc(np->wn_region, fd); /* <region> */ |
| 4551 | if (flags & WF_PFX) |
| 4552 | putc(np->wn_prefixID, fd); /* <prefixID> */ |
| 4553 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4554 | } |
| 4555 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 4556 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4557 | else |
| 4558 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4559 | if (np->wn_child->wn_u1.index != 0 |
| 4560 | && np->wn_child->wn_u2.wnode != node) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4561 | { |
| 4562 | /* The child is written elsewhere, write the reference. */ |
| 4563 | if (fd != NULL) |
| 4564 | { |
| 4565 | putc(BY_INDEX, fd); /* <byte> */ |
| 4566 | /* <nodeidx> */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4567 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4568 | } |
| 4569 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4570 | else if (np->wn_child->wn_u2.wnode == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4571 | /* We will write the child below and give it an index. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4572 | np->wn_child->wn_u2.wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4573 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4574 | if (fd != NULL) |
| 4575 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 4576 | { |
| 4577 | EMSG(_(e_write)); |
| 4578 | return 0; |
| 4579 | } |
| 4580 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4581 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4582 | |
| 4583 | /* Space used in the array when reading: one for each sibling and one for |
| 4584 | * the count. */ |
| 4585 | newindex += siblingcount + 1; |
| 4586 | |
| 4587 | /* Recursively dump the children of each sibling. */ |
| 4588 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4589 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 4590 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4591 | prefixtree); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4592 | |
| 4593 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | |
| 4597 | /* |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4598 | * ":mkspell [-ascii] outfile infile ..." |
| 4599 | * ":mkspell [-ascii] addfile" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4600 | */ |
| 4601 | void |
| 4602 | ex_mkspell(eap) |
| 4603 | exarg_T *eap; |
| 4604 | { |
| 4605 | int fcount; |
| 4606 | char_u **fnames; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4607 | char_u *arg = eap->arg; |
| 4608 | int ascii = FALSE; |
| 4609 | |
| 4610 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 4611 | { |
| 4612 | ascii = TRUE; |
| 4613 | arg = skipwhite(arg + 6); |
| 4614 | } |
| 4615 | |
| 4616 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 4617 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 4618 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4619 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4620 | FreeWild(fcount, fnames); |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * Create a Vim spell file from one or more word lists. |
| 4626 | * "fnames[0]" is the output file name. |
| 4627 | * "fnames[fcount - 1]" is the last input file name. |
| 4628 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 4629 | * and ".spl" is appended to make the output file name. |
| 4630 | */ |
| 4631 | static void |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4632 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4633 | int fcount; |
| 4634 | char_u **fnames; |
| 4635 | int ascii; /* -ascii argument given */ |
| 4636 | int overwrite; /* overwrite existing output file */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4637 | int added_word; /* invoked through "zg" */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4638 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4639 | char_u fname[MAXPATHL]; |
| 4640 | char_u wfname[MAXPATHL]; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4641 | char_u **innames; |
| 4642 | int incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4643 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4644 | int i; |
| 4645 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4646 | struct stat st; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 4647 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4648 | spellinfo_T spin; |
| 4649 | |
| 4650 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4651 | spin.si_verbose = !added_word; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4652 | spin.si_ascii = ascii; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4653 | spin.si_followup = TRUE; |
| 4654 | spin.si_rem_accents = TRUE; |
| 4655 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| 4656 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 4657 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4658 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4659 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4660 | /* default: fnames[0] is output file, following are input files */ |
| 4661 | innames = &fnames[1]; |
| 4662 | incount = fcount - 1; |
| 4663 | |
| 4664 | if (fcount >= 1) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4665 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4666 | len = STRLEN(fnames[0]); |
| 4667 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 4668 | { |
| 4669 | /* For ":mkspell path/en.latin1.add" output file is |
| 4670 | * "path/en.latin1.add.spl". */ |
| 4671 | innames = &fnames[0]; |
| 4672 | incount = 1; |
| 4673 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 4674 | } |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4675 | else if (fcount == 1) |
| 4676 | { |
| 4677 | /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */ |
| 4678 | innames = &fnames[0]; |
| 4679 | incount = 1; |
| 4680 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4681 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4682 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4683 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 4684 | { |
| 4685 | /* Name ends in ".spl", use as the file name. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4686 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4687 | } |
| 4688 | else |
| 4689 | /* Name should be language, make the file name from it. */ |
| 4690 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 4691 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 4692 | |
| 4693 | /* Check for .ascii.spl. */ |
| 4694 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 4695 | spin.si_ascii = TRUE; |
| 4696 | |
| 4697 | /* Check for .add.spl. */ |
| 4698 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 4699 | spin.si_add = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4702 | if (incount <= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4703 | EMSG(_(e_invarg)); /* need at least output and input names */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4704 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 4705 | EMSG(_("E751: Output file name must not have region name")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4706 | else if (incount > 8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4707 | EMSG(_("E754: Only up to 8 regions supported")); |
| 4708 | else |
| 4709 | { |
| 4710 | /* Check for overwriting before doing things that may take a lot of |
| 4711 | * time. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4712 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4713 | { |
| 4714 | EMSG(_(e_exists)); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4715 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4716 | } |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4717 | if (mch_isdir(wfname)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4718 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4719 | EMSG2(_(e_isadir2), wfname); |
| 4720 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | /* |
| 4724 | * Init the aff and dic pointers. |
| 4725 | * Get the region names if there are more than 2 arguments. |
| 4726 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4727 | for (i = 0; i < incount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4728 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4729 | afile[i] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4730 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4731 | if (incount > 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4732 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4733 | len = STRLEN(innames[i]); |
| 4734 | if (STRLEN(gettail(innames[i])) < 5 |
| 4735 | || innames[i][len - 3] != '_') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4736 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4737 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 4738 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4739 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4740 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 4741 | spin.si_region_name[i * 2 + 1] = |
| 4742 | TOLOWER_ASC(innames[i][len - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4743 | } |
| 4744 | } |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4745 | spin.si_region_count = incount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4746 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4747 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 4748 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4749 | spin.si_prefroot = wordtree_alloc(&spin.si_blocks); |
| 4750 | if (spin.si_foldroot == NULL |
| 4751 | || spin.si_keeproot == NULL |
| 4752 | || spin.si_prefroot == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4753 | { |
| 4754 | error = TRUE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4755 | return; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4758 | /* When not producing a .add.spl file clear the character table when |
| 4759 | * we encounter one in the .aff file. This means we dump the current |
| 4760 | * one in the .spl file if the .aff file doesn't define one. That's |
| 4761 | * better than guessing the contents, the table will match a |
| 4762 | * previously loaded spell file. */ |
| 4763 | if (!spin.si_add) |
| 4764 | spin.si_clear_chartab = TRUE; |
| 4765 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4766 | /* |
| 4767 | * Read all the .aff and .dic files. |
| 4768 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4769 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4770 | */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4771 | for (i = 0; i < incount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4772 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4773 | spin.si_conv.vc_type = CONV_NONE; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4774 | spin.si_region = 1 << i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4775 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4776 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4777 | if (mch_stat((char *)fname, &st) >= 0) |
| 4778 | { |
| 4779 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 4780 | * "SET" line. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4781 | afile[i] = spell_read_aff(fname, &spin); |
| 4782 | if (afile[i] == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4783 | error = TRUE; |
| 4784 | else |
| 4785 | { |
| 4786 | /* Read the .dic file and store the words in the trees. */ |
| 4787 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4788 | innames[i]); |
| 4789 | if (spell_read_dic(fname, &spin, afile[i]) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4790 | error = TRUE; |
| 4791 | } |
| 4792 | } |
| 4793 | else |
| 4794 | { |
| 4795 | /* No .aff file, try reading the file as a word list. Store |
| 4796 | * the words in the trees. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4797 | if (spell_read_wordfile(innames[i], &spin) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4798 | error = TRUE; |
| 4799 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4800 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4801 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4802 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4803 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4804 | #endif |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4807 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4808 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4809 | /* |
| 4810 | * Remove the dummy NUL from the start of the tree root. |
| 4811 | */ |
| 4812 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 4813 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4814 | spin.si_prefroot = spin.si_prefroot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4815 | |
| 4816 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4817 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4818 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4819 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4820 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4821 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4822 | verbose_enter(); |
| 4823 | MSG(_("Compressing word tree...")); |
| 4824 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4825 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4826 | verbose_leave(); |
| 4827 | } |
| 4828 | wordtree_compress(spin.si_foldroot, &spin); |
| 4829 | wordtree_compress(spin.si_keeproot, &spin); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4830 | wordtree_compress(spin.si_prefroot, &spin); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4833 | if (!error) |
| 4834 | { |
| 4835 | /* |
| 4836 | * Write the info in the spell file. |
| 4837 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4838 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4839 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4840 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4841 | verbose_enter(); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4842 | smsg((char_u *)_("Writing spell file %s ..."), wfname); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4843 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4844 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4845 | verbose_leave(); |
| 4846 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4847 | |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4848 | write_vim_spell(wfname, &spin); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4849 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4850 | if (!added_word || p_verbose > 2) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4851 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4852 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4853 | verbose_enter(); |
| 4854 | MSG(_("Done!")); |
| 4855 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 4856 | spin.si_memtot); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4857 | out_flush(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4858 | if (added_word) |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4859 | verbose_leave(); |
| 4860 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4861 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4862 | /* If the file is loaded need to reload it. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4863 | spell_reload_one(wfname, added_word); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | /* Free the allocated memory. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4867 | ga_clear(&spin.si_rep); |
| 4868 | ga_clear(&spin.si_sal); |
| 4869 | ga_clear(&spin.si_map); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4870 | ga_clear(&spin.si_prefcond); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4871 | vim_free(spin.si_midword); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4872 | |
| 4873 | /* Free the .aff file structures. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4874 | for (i = 0; i < incount; ++i) |
| 4875 | if (afile[i] != NULL) |
| 4876 | spell_free_aff(afile[i]); |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4877 | |
| 4878 | /* Free all the bits and pieces at once. */ |
| 4879 | free_blocks(spin.si_blocks); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4880 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4883 | |
| 4884 | /* |
| 4885 | * ":spellgood {word}" |
| 4886 | * ":spellwrong {word}" |
| 4887 | */ |
| 4888 | void |
| 4889 | ex_spell(eap) |
| 4890 | exarg_T *eap; |
| 4891 | { |
| 4892 | spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong); |
| 4893 | } |
| 4894 | |
| 4895 | /* |
| 4896 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 4897 | */ |
| 4898 | void |
| 4899 | spell_add_word(word, len, bad) |
| 4900 | char_u *word; |
| 4901 | int len; |
| 4902 | int bad; |
| 4903 | { |
| 4904 | FILE *fd; |
| 4905 | buf_T *buf; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4906 | int new_spf = FALSE; |
| 4907 | struct stat st; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4908 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4909 | /* If 'spellfile' isn't set figure out a good default value. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4910 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4911 | { |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4912 | init_spellfile(); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4913 | new_spf = TRUE; |
| 4914 | } |
| 4915 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4916 | if (*curbuf->b_p_spf == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4917 | EMSG(_("E764: 'spellfile' is not set")); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4918 | else |
| 4919 | { |
| 4920 | /* Check that the user isn't editing the .add file somewhere. */ |
| 4921 | buf = buflist_findname_exp(curbuf->b_p_spf); |
| 4922 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 4923 | buf = NULL; |
| 4924 | if (buf != NULL && bufIsChanged(buf)) |
| 4925 | EMSG(_(e_bufloaded)); |
| 4926 | else |
| 4927 | { |
| 4928 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4929 | if (fd == NULL && new_spf) |
| 4930 | { |
| 4931 | /* We just initialized the 'spellfile' option and can't open |
| 4932 | * the file. We may need to create the "spell" directory |
| 4933 | * first. We already checked the runtime directory is |
| 4934 | * writable in init_spellfile(). */ |
| 4935 | STRCPY(NameBuff, curbuf->b_p_spf); |
| 4936 | *gettail_sep(NameBuff) = NUL; |
| 4937 | if (mch_stat((char *)NameBuff, &st) < 0) |
| 4938 | { |
| 4939 | /* The directory doesn't exist. Try creating it and |
| 4940 | * opening the file again. */ |
| 4941 | vim_mkdir(NameBuff, 0755); |
| 4942 | fd = mch_fopen((char *)curbuf->b_p_spf, "a"); |
| 4943 | } |
| 4944 | } |
| 4945 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4946 | if (fd == NULL) |
| 4947 | EMSG2(_(e_notopen), curbuf->b_p_spf); |
| 4948 | else |
| 4949 | { |
| 4950 | if (bad) |
| 4951 | fprintf(fd, "/!%.*s\n", len, word); |
| 4952 | else |
| 4953 | fprintf(fd, "%.*s\n", len, word); |
| 4954 | fclose(fd); |
| 4955 | |
| 4956 | /* Update the .add.spl file. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4957 | mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4958 | |
| 4959 | /* If the .add file is edited somewhere, reload it. */ |
| 4960 | if (buf != NULL) |
| 4961 | buf_reload(buf); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4962 | |
| 4963 | redraw_all_later(NOT_VALID); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4964 | } |
| 4965 | } |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | /* |
| 4970 | * Initialize 'spellfile' for the current buffer. |
| 4971 | */ |
| 4972 | static void |
| 4973 | init_spellfile() |
| 4974 | { |
| 4975 | char_u buf[MAXPATHL]; |
| 4976 | int l; |
| 4977 | slang_T *sl; |
| 4978 | char_u *rtp; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4979 | char_u *lend; |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4980 | |
| 4981 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 4982 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4983 | /* Find the end of the language name. Exclude the region. */ |
| 4984 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 4985 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| 4986 | ; |
| 4987 | |
| 4988 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 4989 | * are allowed to write. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4990 | rtp = p_rtp; |
| 4991 | while (*rtp != NUL) |
| 4992 | { |
| 4993 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 4994 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 4995 | if (filewritable(buf) == 2) |
| 4996 | { |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4997 | /* Use the first language name from 'spelllang' and the |
| 4998 | * encoding used in the first loaded .spl file. */ |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4999 | sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang; |
| 5000 | l = STRLEN(buf); |
| 5001 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5002 | "/spell/%.*s.%s.add", |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5003 | (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl, |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5004 | strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL |
| 5005 | ? (char_u *)"ascii" : spell_enc()); |
| 5006 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 5007 | break; |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5012 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5013 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5014 | /* |
| 5015 | * Init the chartab used for spelling for ASCII. |
| 5016 | * EBCDIC is not supported! |
| 5017 | */ |
| 5018 | static void |
| 5019 | clear_spell_chartab(sp) |
| 5020 | spelltab_T *sp; |
| 5021 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5022 | int i; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5023 | |
| 5024 | /* Init everything to FALSE. */ |
| 5025 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 5026 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 5027 | for (i = 0; i < 256; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5028 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5029 | sp->st_fold[i] = i; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5030 | sp->st_upper[i] = i; |
| 5031 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5032 | |
| 5033 | /* We include digits. A word shouldn't start with a digit, but handling |
| 5034 | * that is done separately. */ |
| 5035 | for (i = '0'; i <= '9'; ++i) |
| 5036 | sp->st_isw[i] = TRUE; |
| 5037 | for (i = 'A'; i <= 'Z'; ++i) |
| 5038 | { |
| 5039 | sp->st_isw[i] = TRUE; |
| 5040 | sp->st_isu[i] = TRUE; |
| 5041 | sp->st_fold[i] = i + 0x20; |
| 5042 | } |
| 5043 | for (i = 'a'; i <= 'z'; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5044 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5045 | sp->st_isw[i] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5046 | sp->st_upper[i] = i - 0x20; |
| 5047 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | /* |
| 5051 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 5052 | * Called once while starting up and when 'encoding' changes. |
| 5053 | * The default is to use isalpha(), but the spell file should define the word |
| 5054 | * characters to make it possible that 'encoding' differs from the current |
| 5055 | * locale. |
| 5056 | */ |
| 5057 | void |
| 5058 | init_spell_chartab() |
| 5059 | { |
| 5060 | int i; |
| 5061 | |
| 5062 | did_set_spelltab = FALSE; |
| 5063 | clear_spell_chartab(&spelltab); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5064 | vim_memset(spell_ismw, FALSE, sizeof(spell_ismw)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5065 | #ifdef FEAT_MBYTE |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5066 | vim_free(spell_ismw_mb); |
| 5067 | spell_ismw_mb = NULL; |
| 5068 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5069 | if (enc_dbcs) |
| 5070 | { |
| 5071 | /* DBCS: assume double-wide characters are word characters. */ |
| 5072 | for (i = 128; i <= 255; ++i) |
| 5073 | if (MB_BYTE2LEN(i) == 2) |
| 5074 | spelltab.st_isw[i] = TRUE; |
| 5075 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5076 | else if (enc_utf8) |
| 5077 | { |
| 5078 | for (i = 128; i < 256; ++i) |
| 5079 | { |
| 5080 | spelltab.st_isu[i] = utf_isupper(i); |
| 5081 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 5082 | spelltab.st_fold[i] = utf_fold(i); |
| 5083 | spelltab.st_upper[i] = utf_toupper(i); |
| 5084 | } |
| 5085 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5086 | else |
| 5087 | #endif |
| 5088 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5089 | /* Rough guess: use locale-dependent library functions. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5090 | for (i = 128; i < 256; ++i) |
| 5091 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5092 | if (MB_ISUPPER(i)) |
| 5093 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5094 | spelltab.st_isw[i] = TRUE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5095 | spelltab.st_isu[i] = TRUE; |
| 5096 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 5097 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5098 | else if (MB_ISLOWER(i)) |
| 5099 | { |
| 5100 | spelltab.st_isw[i] = TRUE; |
| 5101 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 5102 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5107 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 5108 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 5109 | |
| 5110 | /* |
| 5111 | * Set the spell character tables from strings in the affix file. |
| 5112 | */ |
| 5113 | static int |
| 5114 | set_spell_chartab(fol, low, upp) |
| 5115 | char_u *fol; |
| 5116 | char_u *low; |
| 5117 | char_u *upp; |
| 5118 | { |
| 5119 | /* We build the new tables here first, so that we can compare with the |
| 5120 | * previous one. */ |
| 5121 | spelltab_T new_st; |
| 5122 | char_u *pf = fol, *pl = low, *pu = upp; |
| 5123 | int f, l, u; |
| 5124 | |
| 5125 | clear_spell_chartab(&new_st); |
| 5126 | |
| 5127 | while (*pf != NUL) |
| 5128 | { |
| 5129 | if (*pl == NUL || *pu == NUL) |
| 5130 | { |
| 5131 | EMSG(_(e_affform)); |
| 5132 | return FAIL; |
| 5133 | } |
| 5134 | #ifdef FEAT_MBYTE |
| 5135 | f = mb_ptr2char_adv(&pf); |
| 5136 | l = mb_ptr2char_adv(&pl); |
| 5137 | u = mb_ptr2char_adv(&pu); |
| 5138 | #else |
| 5139 | f = *pf++; |
| 5140 | l = *pl++; |
| 5141 | u = *pu++; |
| 5142 | #endif |
| 5143 | /* Every character that appears is a word character. */ |
| 5144 | if (f < 256) |
| 5145 | new_st.st_isw[f] = TRUE; |
| 5146 | if (l < 256) |
| 5147 | new_st.st_isw[l] = TRUE; |
| 5148 | if (u < 256) |
| 5149 | new_st.st_isw[u] = TRUE; |
| 5150 | |
| 5151 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 5152 | * case-folding */ |
| 5153 | if (l < 256 && l != f) |
| 5154 | { |
| 5155 | if (f >= 256) |
| 5156 | { |
| 5157 | EMSG(_(e_affrange)); |
| 5158 | return FAIL; |
| 5159 | } |
| 5160 | new_st.st_fold[l] = f; |
| 5161 | } |
| 5162 | |
| 5163 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5164 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 5165 | * "FOL" . */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5166 | if (u < 256 && u != f) |
| 5167 | { |
| 5168 | if (f >= 256) |
| 5169 | { |
| 5170 | EMSG(_(e_affrange)); |
| 5171 | return FAIL; |
| 5172 | } |
| 5173 | new_st.st_fold[u] = f; |
| 5174 | new_st.st_isu[u] = TRUE; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5175 | new_st.st_upper[f] = u; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | |
| 5179 | if (*pl != NUL || *pu != NUL) |
| 5180 | { |
| 5181 | EMSG(_(e_affform)); |
| 5182 | return FAIL; |
| 5183 | } |
| 5184 | |
| 5185 | return set_spell_finish(&new_st); |
| 5186 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5187 | |
| 5188 | /* |
| 5189 | * Set the spell character tables from strings in the .spl file. |
| 5190 | */ |
| 5191 | static int |
| 5192 | set_spell_charflags(flags, cnt, upp) |
| 5193 | char_u *flags; |
| 5194 | int cnt; |
| 5195 | char_u *upp; |
| 5196 | { |
| 5197 | /* We build the new tables here first, so that we can compare with the |
| 5198 | * previous one. */ |
| 5199 | spelltab_T new_st; |
| 5200 | int i; |
| 5201 | char_u *p = upp; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5202 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5203 | |
| 5204 | clear_spell_chartab(&new_st); |
| 5205 | |
| 5206 | for (i = 0; i < cnt; ++i) |
| 5207 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5208 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 5209 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5210 | |
| 5211 | if (*p == NUL) |
| 5212 | return FAIL; |
| 5213 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5214 | c = mb_ptr2char_adv(&p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5215 | #else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5216 | c = *p++; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5217 | #endif |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5218 | new_st.st_fold[i + 128] = c; |
| 5219 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 5220 | new_st.st_upper[c] = i + 128; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | return set_spell_finish(&new_st); |
| 5224 | } |
| 5225 | |
| 5226 | static int |
| 5227 | set_spell_finish(new_st) |
| 5228 | spelltab_T *new_st; |
| 5229 | { |
| 5230 | int i; |
| 5231 | |
| 5232 | if (did_set_spelltab) |
| 5233 | { |
| 5234 | /* check that it's the same table */ |
| 5235 | for (i = 0; i < 256; ++i) |
| 5236 | { |
| 5237 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 5238 | || spelltab.st_isu[i] != new_st->st_isu[i] |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5239 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 5240 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5241 | { |
| 5242 | EMSG(_("E763: Word characters differ between spell files")); |
| 5243 | return FAIL; |
| 5244 | } |
| 5245 | } |
| 5246 | } |
| 5247 | else |
| 5248 | { |
| 5249 | /* copy the new spelltab into the one being used */ |
| 5250 | spelltab = *new_st; |
| 5251 | did_set_spelltab = TRUE; |
| 5252 | } |
| 5253 | |
| 5254 | return OK; |
| 5255 | } |
| 5256 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5257 | /* |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5258 | * Return TRUE if "p" points to a word character. |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5259 | * 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] | 5260 | * 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] | 5261 | * 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] | 5262 | */ |
| 5263 | static int |
| 5264 | spell_iswordp(p) |
| 5265 | char_u *p; |
| 5266 | { |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5267 | #ifdef FEAT_MBYTE |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5268 | char_u *s; |
| 5269 | int l; |
| 5270 | int c; |
| 5271 | |
| 5272 | if (has_mbyte) |
| 5273 | { |
| 5274 | l = MB_BYTE2LEN(*p); |
| 5275 | s = p; |
| 5276 | if (l == 1) |
| 5277 | { |
| 5278 | /* be quick for ASCII */ |
| 5279 | if (spell_ismw[*p]) |
| 5280 | { |
| 5281 | s = p + 1; /* skip a mid-word character */ |
| 5282 | l = MB_BYTE2LEN(*s); |
| 5283 | } |
| 5284 | } |
| 5285 | else |
| 5286 | { |
| 5287 | c = mb_ptr2char(p); |
| 5288 | if (c < 256 ? spell_ismw[c] : (spell_ismw_mb != NULL |
| 5289 | && vim_strchr(spell_ismw_mb, c) != NULL)) |
| 5290 | { |
| 5291 | s = p + l; |
| 5292 | l = MB_BYTE2LEN(*s); |
| 5293 | } |
| 5294 | } |
| 5295 | |
| 5296 | if (l > 1) |
| 5297 | return mb_get_class(s) >= 2; |
| 5298 | return spelltab.st_isw[*s]; |
| 5299 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5300 | #endif |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5301 | |
| 5302 | return spelltab.st_isw[spell_ismw[*p] ? p[1] : p[0]]; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5305 | #ifdef FEAT_MBYTE |
| 5306 | /* |
| 5307 | * Return TRUE if "p" points to a word character. |
| 5308 | * Wide version of spell_iswordp(). |
| 5309 | */ |
| 5310 | static int |
| 5311 | spell_iswordp_w(p) |
| 5312 | int *p; |
| 5313 | { |
| 5314 | int *s; |
| 5315 | |
| 5316 | if (*p < 256 ? spell_ismw[*p] : (spell_ismw_mb != NULL |
| 5317 | && vim_strchr(spell_ismw_mb, *p) != NULL)) |
| 5318 | s = p + 1; |
| 5319 | else |
| 5320 | s = p; |
| 5321 | |
| 5322 | if (mb_char2len(*s) > 1) |
| 5323 | { |
| 5324 | if (enc_utf8) |
| 5325 | return utf_class(*s) >= 2; |
| 5326 | if (enc_dbcs) |
| 5327 | return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2; |
| 5328 | return 0; |
| 5329 | } |
| 5330 | return spelltab.st_isw[*s]; |
| 5331 | } |
| 5332 | #endif |
| 5333 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5334 | /* |
Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5335 | * Write the table with prefix conditions to the .spl file. |
| 5336 | */ |
| 5337 | static void |
| 5338 | write_spell_prefcond(fd, gap) |
| 5339 | FILE *fd; |
| 5340 | garray_T *gap; |
| 5341 | { |
| 5342 | int i; |
| 5343 | char_u *p; |
| 5344 | int len; |
| 5345 | |
| 5346 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 5347 | |
| 5348 | for (i = 0; i < gap->ga_len; ++i) |
| 5349 | { |
| 5350 | /* <prefcond> : <condlen> <condstr> */ |
| 5351 | p = ((char_u **)gap->ga_data)[i]; |
| 5352 | if (p == NULL) |
| 5353 | fputc(0, fd); |
| 5354 | else |
| 5355 | { |
| 5356 | len = STRLEN(p); |
| 5357 | fputc(len, fd); |
| 5358 | fwrite(p, (size_t)len, (size_t)1, fd); |
| 5359 | } |
| 5360 | } |
| 5361 | } |
| 5362 | |
| 5363 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5364 | * Write the current tables into the .spl file. |
| 5365 | * This makes sure the same characters are recognized as word characters when |
| 5366 | * generating an when using a spell file. |
| 5367 | */ |
| 5368 | static void |
| 5369 | write_spell_chartab(fd) |
| 5370 | FILE *fd; |
| 5371 | { |
| 5372 | char_u charbuf[256 * 4]; |
| 5373 | int len = 0; |
| 5374 | int flags; |
| 5375 | int i; |
| 5376 | |
| 5377 | fputc(128, fd); /* <charflagslen> */ |
| 5378 | for (i = 128; i < 256; ++i) |
| 5379 | { |
| 5380 | flags = 0; |
| 5381 | if (spelltab.st_isw[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5382 | flags |= CF_WORD; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5383 | if (spelltab.st_isu[i]) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5384 | flags |= CF_UPPER; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5385 | fputc(flags, fd); /* <charflags> */ |
| 5386 | |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5387 | #ifdef FEAT_MBYTE |
| 5388 | if (has_mbyte) |
| 5389 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 5390 | else |
| 5391 | #endif |
| 5392 | charbuf[len++] = spelltab.st_fold[i]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 5396 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 5397 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5398 | |
| 5399 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5400 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 5401 | * Uses the character definitions from the .spl file. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5402 | * When using a multi-byte 'encoding' the length may change! |
| 5403 | * Returns FAIL when something wrong. |
| 5404 | */ |
| 5405 | static int |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5406 | spell_casefold(str, len, buf, buflen) |
| 5407 | char_u *str; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5408 | int len; |
| 5409 | char_u *buf; |
| 5410 | int buflen; |
| 5411 | { |
| 5412 | int i; |
| 5413 | |
| 5414 | if (len >= buflen) |
| 5415 | { |
| 5416 | buf[0] = NUL; |
| 5417 | return FAIL; /* result will not fit */ |
| 5418 | } |
| 5419 | |
| 5420 | #ifdef FEAT_MBYTE |
| 5421 | if (has_mbyte) |
| 5422 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5423 | int outi = 0; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5424 | char_u *p; |
| 5425 | int c; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5426 | |
| 5427 | /* Fold one character at a time. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5428 | for (p = str; p < str + len; ) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5429 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5430 | if (outi + MB_MAXBYTES > buflen) |
| 5431 | { |
| 5432 | buf[outi] = NUL; |
| 5433 | return FAIL; |
| 5434 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5435 | c = mb_ptr2char_adv(&p); |
| 5436 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5437 | } |
| 5438 | buf[outi] = NUL; |
| 5439 | } |
| 5440 | else |
| 5441 | #endif |
| 5442 | { |
| 5443 | /* Be quick for non-multibyte encodings. */ |
| 5444 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5445 | buf[i] = spelltab.st_fold[str[i]]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5446 | buf[i] = NUL; |
| 5447 | } |
| 5448 | |
| 5449 | return OK; |
| 5450 | } |
| 5451 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5452 | #define SPS_BEST 1 |
| 5453 | #define SPS_FAST 2 |
| 5454 | #define SPS_DOUBLE 4 |
| 5455 | |
| 5456 | static int sps_flags = SPS_BEST; |
| 5457 | |
| 5458 | /* |
| 5459 | * Check the 'spellsuggest' option. Return FAIL if it's wrong. |
| 5460 | * Sets "sps_flags". |
| 5461 | */ |
| 5462 | int |
| 5463 | spell_check_sps() |
| 5464 | { |
| 5465 | char_u *p; |
| 5466 | char_u buf[MAXPATHL]; |
| 5467 | int f; |
| 5468 | |
| 5469 | sps_flags = 0; |
| 5470 | |
| 5471 | for (p = p_sps; *p != NUL; ) |
| 5472 | { |
| 5473 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 5474 | |
| 5475 | f = 0; |
| 5476 | if (STRCMP(buf, "best") == 0) |
| 5477 | f = SPS_BEST; |
| 5478 | else if (STRCMP(buf, "fast") == 0) |
| 5479 | f = SPS_FAST; |
| 5480 | else if (STRCMP(buf, "double") == 0) |
| 5481 | f = SPS_DOUBLE; |
| 5482 | else if (STRNCMP(buf, "expr:", 5) != 0 |
| 5483 | && STRNCMP(buf, "file:", 5) != 0) |
| 5484 | f = -1; |
| 5485 | |
| 5486 | if (f == -1 || (sps_flags != 0 && f != 0)) |
| 5487 | { |
| 5488 | sps_flags = SPS_BEST; |
| 5489 | return FAIL; |
| 5490 | } |
| 5491 | if (f != 0) |
| 5492 | sps_flags = f; |
| 5493 | } |
| 5494 | |
| 5495 | if (sps_flags == 0) |
| 5496 | sps_flags = SPS_BEST; |
| 5497 | |
| 5498 | return OK; |
| 5499 | } |
| 5500 | |
| 5501 | /* Remember what "z?" replaced. */ |
| 5502 | static char_u *repl_from = NULL; |
| 5503 | static char_u *repl_to = NULL; |
| 5504 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5505 | /* |
| 5506 | * "z?": Find badly spelled word under or after the cursor. |
| 5507 | * Give suggestions for the properly spelled word. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5508 | */ |
| 5509 | void |
| 5510 | spell_suggest() |
| 5511 | { |
| 5512 | char_u *line; |
| 5513 | pos_T prev_cursor = curwin->w_cursor; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5514 | char_u wcopy[MAXWLEN + 2]; |
| 5515 | char_u *p; |
| 5516 | int i; |
| 5517 | int c; |
| 5518 | suginfo_T sug; |
| 5519 | suggest_T *stp; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5520 | int mouse_used; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5521 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5522 | /* Find the start of the badly spelled word. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5523 | if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL |
| 5524 | || curwin->w_cursor.col > prev_cursor.col) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5525 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5526 | if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL) |
| 5527 | return; |
| 5528 | |
| 5529 | /* No bad word or it starts after the cursor: use the word under the |
| 5530 | * cursor. */ |
| 5531 | curwin->w_cursor = prev_cursor; |
| 5532 | line = ml_get_curline(); |
| 5533 | p = line + curwin->w_cursor.col; |
| 5534 | /* Backup to before start of word. */ |
| 5535 | while (p > line && SPELL_ISWORDP(p)) |
| 5536 | mb_ptr_back(line, p); |
| 5537 | /* Forward to start of word. */ |
| 5538 | while (!SPELL_ISWORDP(p)) |
| 5539 | mb_ptr_adv(p); |
| 5540 | |
| 5541 | if (!SPELL_ISWORDP(p)) /* No word found. */ |
| 5542 | { |
| 5543 | beep_flush(); |
| 5544 | return; |
| 5545 | } |
| 5546 | curwin->w_cursor.col = p - line; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5549 | /* Get the word and its length. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5550 | line = ml_get_curline(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5551 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5552 | /* Get the list of suggestions */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5553 | spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5554 | |
| 5555 | if (sug.su_ga.ga_len == 0) |
| 5556 | MSG(_("Sorry, no suggestions")); |
| 5557 | else |
| 5558 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5559 | vim_free(repl_from); |
| 5560 | repl_from = NULL; |
| 5561 | vim_free(repl_to); |
| 5562 | repl_to = NULL; |
| 5563 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5564 | /* List the suggestions. */ |
| 5565 | msg_start(); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5566 | lines_left = Rows; /* avoid more prompt */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5567 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 5568 | sug.su_badlen, sug.su_badptr); |
| 5569 | msg_puts(IObuff); |
| 5570 | msg_clr_eos(); |
| 5571 | msg_putchar('\n'); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5572 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5573 | msg_scroll = TRUE; |
| 5574 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 5575 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5576 | stp = &SUG(sug.su_ga, i); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5577 | |
| 5578 | /* The suggested word may replace only part of the bad word, add |
| 5579 | * the not replaced part. */ |
| 5580 | STRCPY(wcopy, stp->st_word); |
| 5581 | if (sug.su_badlen > stp->st_orglen) |
| 5582 | vim_strncpy(wcopy + STRLEN(wcopy), |
| 5583 | sug.su_badptr + stp->st_orglen, |
| 5584 | sug.su_badlen - stp->st_orglen); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5585 | vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy); |
| 5586 | msg_puts(IObuff); |
| 5587 | |
| 5588 | /* The word may replace more than "su_badlen". */ |
| 5589 | if (sug.su_badlen < stp->st_orglen) |
| 5590 | { |
| 5591 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 5592 | stp->st_orglen, sug.su_badptr); |
| 5593 | msg_puts(IObuff); |
| 5594 | } |
| 5595 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5596 | if (p_verbose > 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5597 | { |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5598 | /* Add the score. */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5599 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5600 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"), |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5601 | stp->st_salscore ? "s " : "", |
| 5602 | stp->st_score, stp->st_altscore); |
| 5603 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5604 | vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"), |
| 5605 | stp->st_score); |
| 5606 | msg_advance(30); |
| 5607 | msg_puts(IObuff); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5608 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5609 | msg_putchar('\n'); |
| 5610 | } |
| 5611 | |
| 5612 | /* Ask for choice. */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5613 | i = prompt_for_number(&mouse_used); |
| 5614 | if (mouse_used) |
| 5615 | i -= lines_left; |
| 5616 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5617 | 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] | 5618 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5619 | /* Save the from and to text for :spellrepall. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5620 | stp = &SUG(sug.su_ga, i - 1); |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5621 | repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); |
| 5622 | repl_to = vim_strsave(stp->st_word); |
| 5623 | |
| 5624 | /* Replace the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5625 | p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1); |
| 5626 | if (p != NULL) |
| 5627 | { |
| 5628 | c = sug.su_badptr - line; |
| 5629 | mch_memmove(p, line, c); |
| 5630 | STRCPY(p + c, stp->st_word); |
| 5631 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 5632 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 5633 | curwin->w_cursor.col = c; |
| 5634 | changed_bytes(curwin->w_cursor.lnum, c); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5635 | |
| 5636 | /* For redo we use a change-word command. */ |
| 5637 | ResetRedobuff(); |
| 5638 | AppendToRedobuff((char_u *)"ciw"); |
| 5639 | AppendToRedobuff(stp->st_word); |
| 5640 | AppendCharToRedobuff(ESC); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5641 | } |
| 5642 | } |
| 5643 | else |
| 5644 | curwin->w_cursor = prev_cursor; |
| 5645 | } |
| 5646 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5647 | spell_find_cleanup(&sug); |
| 5648 | } |
| 5649 | |
| 5650 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5651 | * ":spellrepall" |
| 5652 | */ |
| 5653 | /*ARGSUSED*/ |
| 5654 | void |
| 5655 | ex_spellrepall(eap) |
| 5656 | exarg_T *eap; |
| 5657 | { |
| 5658 | pos_T pos = curwin->w_cursor; |
| 5659 | char_u *frompat; |
| 5660 | int addlen; |
| 5661 | char_u *line; |
| 5662 | char_u *p; |
| 5663 | int didone = FALSE; |
| 5664 | int save_ws = p_ws; |
| 5665 | |
| 5666 | if (repl_from == NULL || repl_to == NULL) |
| 5667 | { |
| 5668 | EMSG(_("E752: No previous spell replacement")); |
| 5669 | return; |
| 5670 | } |
| 5671 | addlen = STRLEN(repl_to) - STRLEN(repl_from); |
| 5672 | |
| 5673 | frompat = alloc(STRLEN(repl_from) + 7); |
| 5674 | if (frompat == NULL) |
| 5675 | return; |
| 5676 | sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); |
| 5677 | p_ws = FALSE; |
| 5678 | |
| 5679 | curwin->w_cursor.lnum = 0; |
| 5680 | while (!got_int) |
| 5681 | { |
| 5682 | if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0 |
| 5683 | || u_save_cursor() == FAIL) |
| 5684 | break; |
| 5685 | |
| 5686 | /* Only replace when the right word isn't there yet. This happens |
| 5687 | * when changing "etc" to "etc.". */ |
| 5688 | line = ml_get_curline(); |
| 5689 | if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, |
| 5690 | repl_to, STRLEN(repl_to)) != 0) |
| 5691 | { |
| 5692 | p = alloc(STRLEN(line) + addlen + 1); |
| 5693 | if (p == NULL) |
| 5694 | break; |
| 5695 | mch_memmove(p, line, curwin->w_cursor.col); |
| 5696 | STRCPY(p + curwin->w_cursor.col, repl_to); |
| 5697 | STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); |
| 5698 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 5699 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 5700 | didone = TRUE; |
| 5701 | } |
| 5702 | curwin->w_cursor.col += STRLEN(repl_to); |
| 5703 | } |
| 5704 | |
| 5705 | p_ws = save_ws; |
| 5706 | curwin->w_cursor = pos; |
| 5707 | vim_free(frompat); |
| 5708 | |
| 5709 | if (!didone) |
| 5710 | EMSG2(_("E753: Not found: %s"), repl_from); |
| 5711 | } |
| 5712 | |
| 5713 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5714 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 5715 | * a list of allocated strings. |
| 5716 | */ |
| 5717 | void |
| 5718 | spell_suggest_list(gap, word, maxcount) |
| 5719 | garray_T *gap; |
| 5720 | char_u *word; |
| 5721 | int maxcount; /* maximum nr of suggestions */ |
| 5722 | { |
| 5723 | suginfo_T sug; |
| 5724 | int i; |
| 5725 | suggest_T *stp; |
| 5726 | char_u *wcopy; |
| 5727 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5728 | spell_find_suggest(word, &sug, maxcount, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5729 | |
| 5730 | /* Make room in "gap". */ |
| 5731 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| 5732 | if (ga_grow(gap, sug.su_ga.ga_len) == FAIL) |
| 5733 | return; |
| 5734 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5735 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5736 | { |
| 5737 | stp = &SUG(sug.su_ga, i); |
| 5738 | |
| 5739 | /* The suggested word may replace only part of "word", add the not |
| 5740 | * replaced part. */ |
| 5741 | wcopy = alloc(STRLEN(stp->st_word) |
| 5742 | + STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| 5743 | if (wcopy == NULL) |
| 5744 | break; |
| 5745 | STRCPY(wcopy, stp->st_word); |
| 5746 | STRCAT(wcopy, sug.su_badptr + stp->st_orglen); |
| 5747 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 5748 | } |
| 5749 | |
| 5750 | spell_find_cleanup(&sug); |
| 5751 | } |
| 5752 | |
| 5753 | /* |
| 5754 | * Find spell suggestions for the word at the start of "badptr". |
| 5755 | * Return the suggestions in "su->su_ga". |
| 5756 | * The maximum number of suggestions is "maxcount". |
| 5757 | * Note: does use info for the current window. |
| 5758 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 5759 | */ |
| 5760 | static void |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5761 | spell_find_suggest(badptr, su, maxcount, banbadword) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5762 | char_u *badptr; |
| 5763 | suginfo_T *su; |
| 5764 | int maxcount; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5765 | int banbadword; /* don't include badword in suggestions */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5766 | { |
| 5767 | int attr; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5768 | char_u buf[MAXPATHL]; |
| 5769 | char_u *p; |
| 5770 | int do_combine = FALSE; |
| 5771 | char_u *sps_copy; |
| 5772 | #ifdef FEAT_EVAL |
| 5773 | static int expr_busy = FALSE; |
| 5774 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5775 | |
| 5776 | /* |
| 5777 | * Set the info in "*su". |
| 5778 | */ |
| 5779 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 5780 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 5781 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 5782 | if (*badptr == NUL) |
| 5783 | return; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5784 | hash_init(&su->su_banned); |
| 5785 | |
| 5786 | su->su_badptr = badptr; |
| 5787 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr); |
| 5788 | su->su_maxcount = maxcount; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5789 | su->su_maxscore = SCORE_MAXINIT; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5790 | |
| 5791 | if (su->su_badlen >= MAXWLEN) |
| 5792 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 5793 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 5794 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 5795 | su->su_fbadword, MAXWLEN); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5796 | /* get caps flags for bad word */ |
| 5797 | su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5798 | |
| 5799 | /* Ban the bad word itself. It may appear in another region. */ |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 5800 | if (banbadword) |
| 5801 | add_banned(su, su->su_badword); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5802 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5803 | /* Make a copy of 'spellsuggest', because the expression may change it. */ |
| 5804 | sps_copy = vim_strsave(p_sps); |
| 5805 | if (sps_copy == NULL) |
| 5806 | return; |
| 5807 | |
| 5808 | /* Loop over the items in 'spellsuggest'. */ |
| 5809 | for (p = sps_copy; *p != NUL; ) |
| 5810 | { |
| 5811 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 5812 | |
| 5813 | if (STRNCMP(buf, "expr:", 5) == 0) |
| 5814 | { |
| 5815 | #ifdef FEAT_EVAL |
| 5816 | /* Evaluate an expression. Skip this when called recursively |
| 5817 | * (using spellsuggest() in the expression). */ |
| 5818 | if (!expr_busy) |
| 5819 | { |
| 5820 | expr_busy = TRUE; |
| 5821 | spell_suggest_expr(su, buf + 5); |
| 5822 | expr_busy = FALSE; |
| 5823 | } |
| 5824 | #endif |
| 5825 | } |
| 5826 | else if (STRNCMP(buf, "file:", 5) == 0) |
| 5827 | /* Use list of suggestions in a file. */ |
| 5828 | spell_suggest_file(su, buf + 5); |
| 5829 | else |
| 5830 | { |
| 5831 | /* Use internal method. */ |
| 5832 | spell_suggest_intern(su); |
| 5833 | if (sps_flags & SPS_DOUBLE) |
| 5834 | do_combine = TRUE; |
| 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | vim_free(sps_copy); |
| 5839 | |
| 5840 | if (do_combine) |
| 5841 | /* Combine the two list of suggestions. This must be done last, |
| 5842 | * because sorting changes the order again. */ |
| 5843 | score_combine(su); |
| 5844 | } |
| 5845 | |
| 5846 | #ifdef FEAT_EVAL |
| 5847 | /* |
| 5848 | * Find suggestions by evaluating expression "expr". |
| 5849 | */ |
| 5850 | static void |
| 5851 | spell_suggest_expr(su, expr) |
| 5852 | suginfo_T *su; |
| 5853 | char_u *expr; |
| 5854 | { |
| 5855 | list_T *list; |
| 5856 | listitem_T *li; |
| 5857 | int score; |
| 5858 | char_u *p; |
| 5859 | |
| 5860 | /* The work is split up in a few parts to avoid having to export |
| 5861 | * suginfo_T. |
| 5862 | * First evaluate the expression and get the resulting list. */ |
| 5863 | list = eval_spell_expr(su->su_badword, expr); |
| 5864 | if (list != NULL) |
| 5865 | { |
| 5866 | /* Loop over the items in the list. */ |
| 5867 | for (li = list->lv_first; li != NULL; li = li->li_next) |
| 5868 | if (li->li_tv.v_type == VAR_LIST) |
| 5869 | { |
| 5870 | /* Get the word and the score from the items. */ |
| 5871 | score = get_spellword(li->li_tv.vval.v_list, &p); |
| 5872 | if (score >= 0) |
| 5873 | add_suggestion(su, &su->su_ga, p, |
| 5874 | su->su_badlen, score, 0, TRUE); |
| 5875 | } |
| 5876 | list_unref(list); |
| 5877 | } |
| 5878 | |
| 5879 | /* Sort the suggestions and truncate at "maxcount". */ |
| 5880 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 5881 | } |
| 5882 | #endif |
| 5883 | |
| 5884 | /* |
| 5885 | * Find suggestions a file "fname". |
| 5886 | */ |
| 5887 | static void |
| 5888 | spell_suggest_file(su, fname) |
| 5889 | suginfo_T *su; |
| 5890 | char_u *fname; |
| 5891 | { |
| 5892 | FILE *fd; |
| 5893 | char_u line[MAXWLEN * 2]; |
| 5894 | char_u *p; |
| 5895 | int len; |
| 5896 | char_u cword[MAXWLEN]; |
| 5897 | |
| 5898 | /* Open the file. */ |
| 5899 | fd = mch_fopen((char *)fname, "r"); |
| 5900 | if (fd == NULL) |
| 5901 | { |
| 5902 | EMSG2(_(e_notopen), fname); |
| 5903 | return; |
| 5904 | } |
| 5905 | |
| 5906 | /* Read it line by line. */ |
| 5907 | while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) |
| 5908 | { |
| 5909 | line_breakcheck(); |
| 5910 | |
| 5911 | p = vim_strchr(line, '/'); |
| 5912 | if (p == NULL) |
| 5913 | continue; /* No Tab found, just skip the line. */ |
| 5914 | *p++ = NUL; |
| 5915 | if (STRICMP(su->su_badword, line) == 0) |
| 5916 | { |
| 5917 | /* Match! Isolate the good word, until CR or NL. */ |
| 5918 | for (len = 0; p[len] >= ' '; ++len) |
| 5919 | ; |
| 5920 | p[len] = NUL; |
| 5921 | |
| 5922 | /* If the suggestion doesn't have specific case duplicate the case |
| 5923 | * of the bad word. */ |
| 5924 | if (captype(p, NULL) == 0) |
| 5925 | { |
| 5926 | make_case_word(p, cword, su->su_badflags); |
| 5927 | p = cword; |
| 5928 | } |
| 5929 | |
| 5930 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| 5931 | SCORE_FILE, 0, TRUE); |
| 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | fclose(fd); |
| 5936 | |
| 5937 | /* Sort the suggestions and truncate at "maxcount". */ |
| 5938 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 5939 | } |
| 5940 | |
| 5941 | /* |
| 5942 | * Find suggestions for the internal method indicated by "sps_flags". |
| 5943 | */ |
| 5944 | static void |
| 5945 | spell_suggest_intern(su) |
| 5946 | suginfo_T *su; |
| 5947 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5948 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5949 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5950 | * |
| 5951 | * Set a maximum score to limit the combination of operations that is |
| 5952 | * tried. |
| 5953 | */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5954 | suggest_try_special(su); |
| 5955 | |
| 5956 | /* |
| 5957 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 5958 | * from the .aff file and inserting a space (split the word). |
| 5959 | */ |
| 5960 | suggest_try_change(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5961 | |
| 5962 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 5963 | if (sps_flags & SPS_DOUBLE) |
| 5964 | score_comp_sal(su); |
| 5965 | |
| 5966 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5967 | * 3. Try finding sound-a-like words. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5968 | * |
| 5969 | * Only do this when we don't have a lot of suggestions yet, because it's |
| 5970 | * very slow and often doesn't find new suggestions. |
| 5971 | */ |
| 5972 | if ((sps_flags & SPS_DOUBLE) |
| 5973 | || (!(sps_flags & SPS_FAST) |
| 5974 | && su->su_ga.ga_len < SUG_CLEAN_COUNT(su))) |
| 5975 | { |
| 5976 | /* Allow a higher score now. */ |
| 5977 | su->su_maxscore = SCORE_MAXMAX; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5978 | suggest_try_soundalike(su); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5979 | } |
| 5980 | |
| 5981 | /* When CTRL-C was hit while searching do show the results. */ |
| 5982 | ui_breakcheck(); |
| 5983 | if (got_int) |
| 5984 | { |
| 5985 | (void)vgetc(); |
| 5986 | got_int = FALSE; |
| 5987 | } |
| 5988 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5989 | if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5990 | { |
| 5991 | if (sps_flags & SPS_BEST) |
| 5992 | /* Adjust the word score for how it sounds like. */ |
| 5993 | rescore_suggestions(su); |
| 5994 | |
| 5995 | /* Sort the suggestions and truncate at "maxcount". */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 5996 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | /* |
| 6001 | * Free the info put in "*su" by spell_find_suggest(). |
| 6002 | */ |
| 6003 | static void |
| 6004 | spell_find_cleanup(su) |
| 6005 | suginfo_T *su; |
| 6006 | { |
| 6007 | int i; |
| 6008 | |
| 6009 | /* Free the suggestions. */ |
| 6010 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 6011 | vim_free(SUG(su->su_ga, i).st_word); |
| 6012 | ga_clear(&su->su_ga); |
| 6013 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 6014 | vim_free(SUG(su->su_sga, i).st_word); |
| 6015 | ga_clear(&su->su_sga); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6016 | |
| 6017 | /* Free the banned words. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6018 | free_banned(su); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6019 | } |
| 6020 | |
| 6021 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6022 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 6023 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 6024 | * The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6025 | */ |
| 6026 | static void |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6027 | onecap_copy(word, wcopy, upper) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6028 | char_u *word; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6029 | char_u *wcopy; |
| 6030 | int upper; /* TRUE: first letter made upper case */ |
| 6031 | { |
| 6032 | char_u *p; |
| 6033 | int c; |
| 6034 | int l; |
| 6035 | |
| 6036 | p = word; |
| 6037 | #ifdef FEAT_MBYTE |
| 6038 | if (has_mbyte) |
| 6039 | c = mb_ptr2char_adv(&p); |
| 6040 | else |
| 6041 | #endif |
| 6042 | c = *p++; |
| 6043 | if (upper) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6044 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6045 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6046 | c = SPELL_TOFOLD(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6047 | #ifdef FEAT_MBYTE |
| 6048 | if (has_mbyte) |
| 6049 | l = mb_char2bytes(c, wcopy); |
| 6050 | else |
| 6051 | #endif |
| 6052 | { |
| 6053 | l = 1; |
| 6054 | wcopy[0] = c; |
| 6055 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6056 | vim_strncpy(wcopy + l, p, MAXWLEN - l); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6060 | * Make a copy of "word" with all the letters upper cased into |
| 6061 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6062 | */ |
| 6063 | static void |
| 6064 | allcap_copy(word, wcopy) |
| 6065 | char_u *word; |
| 6066 | char_u *wcopy; |
| 6067 | { |
| 6068 | char_u *s; |
| 6069 | char_u *d; |
| 6070 | int c; |
| 6071 | |
| 6072 | d = wcopy; |
| 6073 | for (s = word; *s != NUL; ) |
| 6074 | { |
| 6075 | #ifdef FEAT_MBYTE |
| 6076 | if (has_mbyte) |
| 6077 | c = mb_ptr2char_adv(&s); |
| 6078 | else |
| 6079 | #endif |
| 6080 | c = *s++; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6081 | c = SPELL_TOUPPER(c); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6082 | |
| 6083 | #ifdef FEAT_MBYTE |
| 6084 | if (has_mbyte) |
| 6085 | { |
| 6086 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 6087 | break; |
| 6088 | d += mb_char2bytes(c, d); |
| 6089 | } |
| 6090 | else |
| 6091 | #endif |
| 6092 | { |
| 6093 | if (d - wcopy >= MAXWLEN - 1) |
| 6094 | break; |
| 6095 | *d++ = c; |
| 6096 | } |
| 6097 | } |
| 6098 | *d = NUL; |
| 6099 | } |
| 6100 | |
| 6101 | /* |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6102 | * Try finding suggestions by recognizing specific situations. |
| 6103 | */ |
| 6104 | static void |
| 6105 | suggest_try_special(su) |
| 6106 | suginfo_T *su; |
| 6107 | { |
| 6108 | char_u *p; |
| 6109 | int len; |
| 6110 | int c; |
| 6111 | char_u word[MAXWLEN]; |
| 6112 | |
| 6113 | /* |
| 6114 | * Recognize a word that is repeated: "the the". |
| 6115 | */ |
| 6116 | p = skiptowhite(su->su_fbadword); |
| 6117 | len = p - su->su_fbadword; |
| 6118 | p = skipwhite(p); |
| 6119 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 6120 | { |
| 6121 | /* Include badflags: if the badword is onecap or allcap |
| 6122 | * use that for the goodword too: "The the" -> "The". */ |
| 6123 | c = su->su_fbadword[len]; |
| 6124 | su->su_fbadword[len] = NUL; |
| 6125 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 6126 | su->su_fbadword[len] = c; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6127 | 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] | 6128 | } |
| 6129 | } |
| 6130 | |
| 6131 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6132 | * Try finding suggestions by adding/removing/swapping letters. |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6133 | * |
| 6134 | * This uses a state machine. At each node in the tree we try various |
| 6135 | * operations. When trying if an operation work "depth" is increased and the |
| 6136 | * stack[] is used to store info. This allows combinations, thus insert one |
| 6137 | * character, replace one and delete another. The number of changes is |
| 6138 | * limited by su->su_maxscore, checked in try_deeper(). |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6139 | */ |
| 6140 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6141 | suggest_try_change(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6142 | suginfo_T *su; |
| 6143 | { |
| 6144 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| 6145 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 6146 | trystate_T stack[MAXWLEN]; |
| 6147 | char_u preword[MAXWLEN * 3]; /* word found with proper case (appended |
| 6148 | * to for word split) */ |
| 6149 | char_u prewordlen = 0; /* length of word in "preword" */ |
| 6150 | int splitoff = 0; /* index in tword after last split */ |
| 6151 | trystate_T *sp; |
| 6152 | int newscore; |
| 6153 | langp_T *lp; |
| 6154 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6155 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6156 | int depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6157 | int c, c2, c3; |
| 6158 | int n = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6159 | int flags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6160 | garray_T *gap; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6161 | idx_T arridx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6162 | int len; |
| 6163 | char_u *p; |
| 6164 | fromto_T *ftp; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6165 | int fl = 0, tl; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6166 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6167 | |
| 6168 | /* 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] | 6169 | * to find matches (esp. REP items). Append some more text, changing |
| 6170 | * chars after the bad word may help. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6171 | STRCPY(fword, su->su_fbadword); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6172 | n = STRLEN(fword); |
| 6173 | p = su->su_badptr + su->su_badlen; |
| 6174 | (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6175 | |
| 6176 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 6177 | lp->lp_slang != NULL; ++lp) |
| 6178 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6179 | /* |
| 6180 | * Go through the whole case-fold tree, try changes at each node. |
| 6181 | * "tword[]" contains the word collected from nodes in the tree. |
| 6182 | * "fword[]" the word we are trying to match with (initially the bad |
| 6183 | * word). |
| 6184 | */ |
| 6185 | byts = lp->lp_slang->sl_fbyts; |
| 6186 | idxs = lp->lp_slang->sl_fidxs; |
| 6187 | |
| 6188 | depth = 0; |
| 6189 | stack[0].ts_state = STATE_START; |
| 6190 | stack[0].ts_score = 0; |
| 6191 | stack[0].ts_curi = 1; |
| 6192 | stack[0].ts_fidx = 0; |
| 6193 | stack[0].ts_fidxtry = 0; |
| 6194 | stack[0].ts_twordlen = 0; |
| 6195 | stack[0].ts_arridx = 0; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6196 | #ifdef FEAT_MBYTE |
| 6197 | stack[0].ts_tcharlen = 0; |
| 6198 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6199 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6200 | /* |
| 6201 | * Loop to find all suggestions. At each round we either: |
| 6202 | * - For the current state try one operation, advance "ts_curi", |
| 6203 | * increase "depth". |
| 6204 | * - When a state is done go to the next, set "ts_state". |
| 6205 | * - When all states are tried decrease "depth". |
| 6206 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6207 | while (depth >= 0 && !got_int) |
| 6208 | { |
| 6209 | sp = &stack[depth]; |
| 6210 | switch (sp->ts_state) |
| 6211 | { |
| 6212 | case STATE_START: |
| 6213 | /* |
| 6214 | * Start of node: Deal with NUL bytes, which means |
| 6215 | * tword[] may end here. |
| 6216 | */ |
| 6217 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 6218 | len = byts[arridx]; /* bytes in this node */ |
| 6219 | arridx += sp->ts_curi; /* index of current byte */ |
| 6220 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6221 | if (sp->ts_curi > len || byts[arridx] != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6222 | { |
| 6223 | /* Past bytes in node and/or past NUL bytes. */ |
| 6224 | sp->ts_state = STATE_ENDNUL; |
| 6225 | break; |
| 6226 | } |
| 6227 | |
| 6228 | /* |
| 6229 | * End of word in tree. |
| 6230 | */ |
| 6231 | ++sp->ts_curi; /* eat one NUL byte */ |
| 6232 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6233 | flags = (int)idxs[arridx]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6234 | |
| 6235 | /* |
| 6236 | * Form the word with proper case in preword. |
| 6237 | * If there is a word from a previous split, append. |
| 6238 | */ |
| 6239 | tword[sp->ts_twordlen] = NUL; |
| 6240 | if (flags & WF_KEEPCAP) |
| 6241 | /* Must find the word in the keep-case tree. */ |
| 6242 | find_keepcap_word(lp->lp_slang, tword + splitoff, |
| 6243 | preword + prewordlen); |
| 6244 | else |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6245 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6246 | /* Include badflags: if the badword is onecap or allcap |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6247 | * use that for the goodword too. But if the badword is |
| 6248 | * allcap and it's only one char long use onecap. */ |
| 6249 | c = su->su_badflags; |
| 6250 | if ((c & WF_ALLCAP) |
| 6251 | #ifdef FEAT_MBYTE |
| 6252 | && su->su_badlen == mb_ptr2len_check(su->su_badptr) |
| 6253 | #else |
| 6254 | && su->su_badlen == 1 |
| 6255 | #endif |
| 6256 | ) |
| 6257 | c = WF_ONECAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6258 | make_case_word(tword + splitoff, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6259 | preword + prewordlen, flags | c); |
| 6260 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6261 | |
| 6262 | /* Don't use a banned word. It may appear again as a good |
| 6263 | * word, thus remember it. */ |
| 6264 | if (flags & WF_BANNED) |
| 6265 | { |
| 6266 | add_banned(su, preword + prewordlen); |
| 6267 | break; |
| 6268 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 6269 | if (was_banned(su, preword + prewordlen) |
| 6270 | || was_banned(su, preword)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6271 | break; |
| 6272 | |
| 6273 | newscore = 0; |
| 6274 | if ((flags & WF_REGION) |
| 6275 | && (((unsigned)flags >> 8) & lp->lp_region) == 0) |
| 6276 | newscore += SCORE_REGION; |
| 6277 | if (flags & WF_RARE) |
| 6278 | newscore += SCORE_RARE; |
| 6279 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6280 | if (!spell_valid_case(su->su_badflags, |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6281 | captype(preword + prewordlen, NULL))) |
| 6282 | newscore += SCORE_ICASE; |
| 6283 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6284 | if ((fword[sp->ts_fidx] == NUL |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6285 | || !spell_iswordp(fword + sp->ts_fidx)) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6286 | && sp->ts_fidx >= sp->ts_fidxtry) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6287 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6288 | /* The badword also ends: add suggestions. Give a penalty |
| 6289 | * when changing non-word char to word char, e.g., "thes," |
| 6290 | * -> "these". */ |
| 6291 | p = fword + sp->ts_fidx; |
| 6292 | #ifdef FEAT_MBYTE |
| 6293 | if (has_mbyte) |
| 6294 | mb_ptr_back(fword, p); |
| 6295 | else |
| 6296 | #endif |
| 6297 | --p; |
| 6298 | if (!spell_iswordp(p)) |
| 6299 | { |
| 6300 | p = preword + STRLEN(preword); |
| 6301 | #ifdef FEAT_MBYTE |
| 6302 | if (has_mbyte) |
| 6303 | mb_ptr_back(preword, p); |
| 6304 | else |
| 6305 | #endif |
| 6306 | --p; |
| 6307 | if (spell_iswordp(p)) |
| 6308 | newscore += SCORE_NONWORD; |
| 6309 | } |
| 6310 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6311 | add_suggestion(su, &su->su_ga, preword, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6312 | sp->ts_fidx - repextra, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6313 | sp->ts_score + newscore, 0, FALSE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6314 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6315 | else if (sp->ts_fidx >= sp->ts_fidxtry |
| 6316 | #ifdef FEAT_MBYTE |
| 6317 | /* Don't split halfway a character. */ |
| 6318 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| 6319 | #endif |
| 6320 | ) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6321 | { |
| 6322 | /* The word in the tree ends but the badword |
| 6323 | * continues: try inserting a space and check that a valid |
| 6324 | * words starts at fword[sp->ts_fidx]. */ |
| 6325 | if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT)) |
| 6326 | { |
| 6327 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| 6328 | sp->ts_save_prewordlen = prewordlen; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6329 | sp->ts_save_badflags = su->su_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6330 | sp->ts_save_splitoff = splitoff; |
| 6331 | |
| 6332 | /* Append a space to preword. */ |
| 6333 | STRCAT(preword, " "); |
| 6334 | prewordlen = STRLEN(preword); |
| 6335 | splitoff = sp->ts_twordlen; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6336 | #ifdef FEAT_MBYTE |
| 6337 | if (has_mbyte) |
| 6338 | { |
| 6339 | int i = 0; |
| 6340 | |
| 6341 | /* Case-folding may change the number of bytes: |
| 6342 | * Count nr of chars in fword[sp->ts_fidx] and |
| 6343 | * advance that many chars in su->su_badptr. */ |
| 6344 | for (p = fword; p < fword + sp->ts_fidx; |
| 6345 | mb_ptr_adv(p)) |
| 6346 | ++i; |
| 6347 | for (p = su->su_badptr; i > 0; mb_ptr_adv(p)) |
| 6348 | --i; |
| 6349 | } |
| 6350 | else |
| 6351 | #endif |
| 6352 | p = su->su_badptr + sp->ts_fidx; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6353 | su->su_badflags = captype(p, su->su_badptr |
| 6354 | + su->su_badlen); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6355 | |
| 6356 | sp->ts_state = STATE_SPLITUNDO; |
| 6357 | ++depth; |
| 6358 | /* Restart at top of the tree. */ |
| 6359 | stack[depth].ts_arridx = 0; |
| 6360 | } |
| 6361 | } |
| 6362 | break; |
| 6363 | |
| 6364 | case STATE_SPLITUNDO: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6365 | /* Undo the changes done for word split. */ |
| 6366 | su->su_badflags = sp->ts_save_badflags; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6367 | splitoff = sp->ts_save_splitoff; |
| 6368 | prewordlen = sp->ts_save_prewordlen; |
| 6369 | |
| 6370 | /* Continue looking for NUL bytes. */ |
| 6371 | sp->ts_state = STATE_START; |
| 6372 | break; |
| 6373 | |
| 6374 | case STATE_ENDNUL: |
| 6375 | /* Past the NUL bytes in the node. */ |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6376 | if (fword[sp->ts_fidx] == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6377 | { |
| 6378 | /* The badword ends, can't use the bytes in this node. */ |
| 6379 | sp->ts_state = STATE_DEL; |
| 6380 | break; |
| 6381 | } |
| 6382 | sp->ts_state = STATE_PLAIN; |
| 6383 | /*FALLTHROUGH*/ |
| 6384 | |
| 6385 | case STATE_PLAIN: |
| 6386 | /* |
| 6387 | * Go over all possible bytes at this node, add each to |
| 6388 | * tword[] and use child node. "ts_curi" is the index. |
| 6389 | */ |
| 6390 | arridx = sp->ts_arridx; |
| 6391 | if (sp->ts_curi > byts[arridx]) |
| 6392 | { |
| 6393 | /* Done all bytes at this node, do next state. When still |
| 6394 | * at already changed bytes skip the other tricks. */ |
| 6395 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| 6396 | sp->ts_state = STATE_DEL; |
| 6397 | else |
| 6398 | sp->ts_state = STATE_FINAL; |
| 6399 | } |
| 6400 | else |
| 6401 | { |
| 6402 | arridx += sp->ts_curi++; |
| 6403 | c = byts[arridx]; |
| 6404 | |
| 6405 | /* Normal byte, go one level deeper. If it's not equal to |
| 6406 | * the byte in the bad word adjust the score. But don't |
| 6407 | * even try when the byte was already changed. */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6408 | if (c == fword[sp->ts_fidx] |
| 6409 | #ifdef FEAT_MBYTE |
| 6410 | || (sp->ts_tcharlen > 0 |
| 6411 | && sp->ts_isdiff != DIFF_NONE) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6412 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6413 | ) |
| 6414 | newscore = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6415 | else |
| 6416 | newscore = SCORE_SUBST; |
| 6417 | if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry) |
| 6418 | && try_deeper(su, stack, depth, newscore)) |
| 6419 | { |
| 6420 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6421 | sp = &stack[depth]; |
| 6422 | ++sp->ts_fidx; |
| 6423 | tword[sp->ts_twordlen++] = c; |
| 6424 | sp->ts_arridx = idxs[arridx]; |
| 6425 | #ifdef FEAT_MBYTE |
| 6426 | if (newscore == SCORE_SUBST) |
| 6427 | sp->ts_isdiff = DIFF_YES; |
| 6428 | if (has_mbyte) |
| 6429 | { |
| 6430 | /* Multi-byte characters are a bit complicated to |
| 6431 | * handle: They differ when any of the bytes |
| 6432 | * differ and then their length may also differ. */ |
| 6433 | if (sp->ts_tcharlen == 0) |
| 6434 | { |
| 6435 | /* First byte. */ |
| 6436 | sp->ts_tcharidx = 0; |
| 6437 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 6438 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 6439 | sp->ts_isdiff = (newscore != 0) |
| 6440 | ? DIFF_YES : DIFF_NONE; |
| 6441 | } |
| 6442 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 6443 | /* When inserting trail bytes don't advance in |
| 6444 | * the bad word. */ |
| 6445 | --sp->ts_fidx; |
| 6446 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 6447 | { |
| 6448 | /* Last byte of character. */ |
| 6449 | if (sp->ts_isdiff == DIFF_YES) |
| 6450 | { |
| 6451 | /* Correct ts_fidx for the byte length of |
| 6452 | * the character (we didn't check that |
| 6453 | * before). */ |
| 6454 | sp->ts_fidx = sp->ts_fcharstart |
| 6455 | + MB_BYTE2LEN( |
| 6456 | fword[sp->ts_fcharstart]); |
| 6457 | |
| 6458 | /* For a similar character adjust score |
| 6459 | * from SCORE_SUBST to SCORE_SIMILAR. */ |
| 6460 | if (lp->lp_slang->sl_has_map |
| 6461 | && similar_chars(lp->lp_slang, |
| 6462 | mb_ptr2char(tword |
| 6463 | + sp->ts_twordlen |
| 6464 | - sp->ts_tcharlen), |
| 6465 | mb_ptr2char(fword |
| 6466 | + sp->ts_fcharstart))) |
| 6467 | sp->ts_score -= |
| 6468 | SCORE_SUBST - SCORE_SIMILAR; |
| 6469 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6470 | else if (sp->ts_isdiff == DIFF_INSERT |
| 6471 | && sp->ts_twordlen > sp->ts_tcharlen) |
| 6472 | { |
| 6473 | /* If the previous character was the same, |
| 6474 | * thus doubling a character, give a bonus |
| 6475 | * to the score. */ |
| 6476 | p = tword + sp->ts_twordlen |
| 6477 | - sp->ts_tcharlen; |
| 6478 | c = mb_ptr2char(p); |
| 6479 | mb_ptr_back(tword, p); |
| 6480 | if (c == mb_ptr2char(p)) |
| 6481 | sp->ts_score -= SCORE_INS |
| 6482 | - SCORE_INSDUP; |
| 6483 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6484 | |
| 6485 | /* Starting a new char, reset the length. */ |
| 6486 | sp->ts_tcharlen = 0; |
| 6487 | } |
| 6488 | } |
| 6489 | else |
| 6490 | #endif |
| 6491 | { |
| 6492 | /* If we found a similar char adjust the score. |
| 6493 | * We do this after calling try_deeper() because |
| 6494 | * it's slow. */ |
| 6495 | if (newscore != 0 |
| 6496 | && lp->lp_slang->sl_has_map |
| 6497 | && similar_chars(lp->lp_slang, |
| 6498 | c, fword[sp->ts_fidx - 1])) |
| 6499 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| 6500 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6501 | } |
| 6502 | } |
| 6503 | break; |
| 6504 | |
| 6505 | case STATE_DEL: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6506 | #ifdef FEAT_MBYTE |
| 6507 | /* When past the first byte of a multi-byte char don't try |
| 6508 | * delete/insert/swap a character. */ |
| 6509 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 6510 | { |
| 6511 | sp->ts_state = STATE_FINAL; |
| 6512 | break; |
| 6513 | } |
| 6514 | #endif |
| 6515 | /* |
| 6516 | * Try skipping one character in the bad word (delete it). |
| 6517 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6518 | sp->ts_state = STATE_INS; |
| 6519 | sp->ts_curi = 1; |
| 6520 | if (fword[sp->ts_fidx] != NUL |
| 6521 | && try_deeper(su, stack, depth, SCORE_DEL)) |
| 6522 | { |
| 6523 | ++depth; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6524 | |
| 6525 | /* Advance over the character in fword[]. Give a bonus to |
| 6526 | * the score if the same character is following "nn" -> |
| 6527 | * "n". */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6528 | #ifdef FEAT_MBYTE |
| 6529 | if (has_mbyte) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6530 | { |
| 6531 | c = mb_ptr2char(fword + sp->ts_fidx); |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6532 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6533 | if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) |
| 6534 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 6535 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6536 | else |
| 6537 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6538 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6539 | ++stack[depth].ts_fidx; |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6540 | if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) |
| 6541 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 6542 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6543 | break; |
| 6544 | } |
| 6545 | /*FALLTHROUGH*/ |
| 6546 | |
| 6547 | case STATE_INS: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6548 | /* Insert one byte. Do this for each possible byte at this |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6549 | * node. */ |
| 6550 | n = sp->ts_arridx; |
| 6551 | if (sp->ts_curi > byts[n]) |
| 6552 | { |
| 6553 | /* Done all bytes at this node, do next state. */ |
| 6554 | sp->ts_state = STATE_SWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6555 | } |
| 6556 | else |
| 6557 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6558 | /* Do one more byte at this node. Skip NUL bytes. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6559 | n += sp->ts_curi++; |
| 6560 | c = byts[n]; |
| 6561 | if (c != 0 && try_deeper(su, stack, depth, SCORE_INS)) |
| 6562 | { |
| 6563 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6564 | sp = &stack[depth]; |
| 6565 | tword[sp->ts_twordlen++] = c; |
| 6566 | sp->ts_arridx = idxs[n]; |
| 6567 | #ifdef FEAT_MBYTE |
| 6568 | if (has_mbyte) |
| 6569 | { |
| 6570 | fl = MB_BYTE2LEN(c); |
| 6571 | if (fl > 1) |
| 6572 | { |
| 6573 | /* There are following bytes for the same |
| 6574 | * character. We must find all bytes before |
| 6575 | * trying delete/insert/swap/etc. */ |
| 6576 | sp->ts_tcharlen = fl; |
| 6577 | sp->ts_tcharidx = 1; |
| 6578 | sp->ts_isdiff = DIFF_INSERT; |
| 6579 | } |
| 6580 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6581 | else |
| 6582 | fl = 1; |
| 6583 | if (fl == 1) |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6584 | #endif |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6585 | { |
| 6586 | /* If the previous character was the same, thus |
| 6587 | * doubling a character, give a bonus to the |
| 6588 | * score. */ |
| 6589 | if (sp->ts_twordlen >= 2 |
| 6590 | && tword[sp->ts_twordlen - 2] == c) |
| 6591 | sp->ts_score -= SCORE_INS - SCORE_INSDUP; |
| 6592 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6593 | } |
| 6594 | } |
| 6595 | break; |
| 6596 | |
| 6597 | case STATE_SWAP: |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6598 | /* |
| 6599 | * Swap two bytes in the bad word: "12" -> "21". |
| 6600 | * We change "fword" here, it's changed back afterwards. |
| 6601 | */ |
| 6602 | p = fword + sp->ts_fidx; |
| 6603 | c = *p; |
| 6604 | if (c == NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6605 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6606 | /* End of word, can't swap or replace. */ |
| 6607 | sp->ts_state = STATE_FINAL; |
| 6608 | break; |
| 6609 | } |
| 6610 | #ifdef FEAT_MBYTE |
| 6611 | if (has_mbyte) |
| 6612 | { |
| 6613 | n = mb_ptr2len_check(p); |
| 6614 | c = mb_ptr2char(p); |
| 6615 | c2 = mb_ptr2char(p + n); |
| 6616 | } |
| 6617 | else |
| 6618 | #endif |
| 6619 | c2 = p[1]; |
| 6620 | if (c == c2) |
| 6621 | { |
| 6622 | /* Characters are identical, swap won't do anything. */ |
| 6623 | sp->ts_state = STATE_SWAP3; |
| 6624 | break; |
| 6625 | } |
| 6626 | if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP)) |
| 6627 | { |
| 6628 | sp->ts_state = STATE_UNSWAP; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6629 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6630 | #ifdef FEAT_MBYTE |
| 6631 | if (has_mbyte) |
| 6632 | { |
| 6633 | fl = mb_char2len(c2); |
| 6634 | mch_memmove(p, p + n, fl); |
| 6635 | mb_char2bytes(c, p + fl); |
| 6636 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 6637 | } |
| 6638 | else |
| 6639 | #endif |
| 6640 | { |
| 6641 | p[0] = c2; |
| 6642 | p[1] = c; |
| 6643 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| 6644 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6645 | } |
| 6646 | else |
| 6647 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 6648 | sp->ts_state = STATE_REP_INI; |
| 6649 | break; |
| 6650 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6651 | case STATE_UNSWAP: |
| 6652 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 6653 | p = fword + sp->ts_fidx; |
| 6654 | #ifdef FEAT_MBYTE |
| 6655 | if (has_mbyte) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6656 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6657 | n = MB_BYTE2LEN(*p); |
| 6658 | c = mb_ptr2char(p + n); |
| 6659 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 6660 | mb_char2bytes(c, p); |
| 6661 | } |
| 6662 | else |
| 6663 | #endif |
| 6664 | { |
| 6665 | c = *p; |
| 6666 | *p = p[1]; |
| 6667 | p[1] = c; |
| 6668 | } |
| 6669 | /*FALLTHROUGH*/ |
| 6670 | |
| 6671 | case STATE_SWAP3: |
| 6672 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 6673 | * "fword" here, it's changed back afterwards. */ |
| 6674 | p = fword + sp->ts_fidx; |
| 6675 | #ifdef FEAT_MBYTE |
| 6676 | if (has_mbyte) |
| 6677 | { |
| 6678 | n = mb_ptr2len_check(p); |
| 6679 | c = mb_ptr2char(p); |
| 6680 | fl = mb_ptr2len_check(p + n); |
| 6681 | c2 = mb_ptr2char(p + n); |
| 6682 | c3 = mb_ptr2char(p + n + fl); |
| 6683 | } |
| 6684 | else |
| 6685 | #endif |
| 6686 | { |
| 6687 | c = *p; |
| 6688 | c2 = p[1]; |
| 6689 | c3 = p[2]; |
| 6690 | } |
| 6691 | |
| 6692 | /* When characters are identical: "121" then SWAP3 result is |
| 6693 | * identical, ROT3L result is same as SWAP: "211", ROT3L |
| 6694 | * result is same as SWAP on next char: "112". Thus skip all |
| 6695 | * swapping. Also skip when c3 is NUL. */ |
| 6696 | if (c == c3 || c3 == NUL) |
| 6697 | { |
| 6698 | sp->ts_state = STATE_REP_INI; |
| 6699 | break; |
| 6700 | } |
| 6701 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6702 | { |
| 6703 | sp->ts_state = STATE_UNSWAP3; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6704 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6705 | #ifdef FEAT_MBYTE |
| 6706 | if (has_mbyte) |
| 6707 | { |
| 6708 | tl = mb_char2len(c3); |
| 6709 | mch_memmove(p, p + n + fl, tl); |
| 6710 | mb_char2bytes(c2, p + tl); |
| 6711 | mb_char2bytes(c, p + fl + tl); |
| 6712 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 6713 | } |
| 6714 | else |
| 6715 | #endif |
| 6716 | { |
| 6717 | p[0] = p[2]; |
| 6718 | p[2] = c; |
| 6719 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6720 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6721 | } |
| 6722 | else |
| 6723 | sp->ts_state = STATE_REP_INI; |
| 6724 | break; |
| 6725 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6726 | case STATE_UNSWAP3: |
| 6727 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 6728 | p = fword + sp->ts_fidx; |
| 6729 | #ifdef FEAT_MBYTE |
| 6730 | if (has_mbyte) |
| 6731 | { |
| 6732 | n = MB_BYTE2LEN(*p); |
| 6733 | c2 = mb_ptr2char(p + n); |
| 6734 | fl = MB_BYTE2LEN(p[n]); |
| 6735 | c = mb_ptr2char(p + n + fl); |
| 6736 | tl = MB_BYTE2LEN(p[n + fl]); |
| 6737 | mch_memmove(p + fl + tl, p, n); |
| 6738 | mb_char2bytes(c, p); |
| 6739 | mb_char2bytes(c2, p + tl); |
| 6740 | } |
| 6741 | else |
| 6742 | #endif |
| 6743 | { |
| 6744 | c = *p; |
| 6745 | *p = p[2]; |
| 6746 | p[2] = c; |
| 6747 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6748 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6749 | /* Rotate three characters left: "123" -> "231". We change |
| 6750 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6751 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6752 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6753 | sp->ts_state = STATE_UNROT3L; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6754 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6755 | p = fword + sp->ts_fidx; |
| 6756 | #ifdef FEAT_MBYTE |
| 6757 | if (has_mbyte) |
| 6758 | { |
| 6759 | n = mb_ptr2len_check(p); |
| 6760 | c = mb_ptr2char(p); |
| 6761 | fl = mb_ptr2len_check(p + n); |
| 6762 | fl += mb_ptr2len_check(p + n + fl); |
| 6763 | mch_memmove(p, p + n, fl); |
| 6764 | mb_char2bytes(c, p + fl); |
| 6765 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| 6766 | } |
| 6767 | else |
| 6768 | #endif |
| 6769 | { |
| 6770 | c = *p; |
| 6771 | *p = p[1]; |
| 6772 | p[1] = p[2]; |
| 6773 | p[2] = c; |
| 6774 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6775 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6776 | } |
| 6777 | else |
| 6778 | sp->ts_state = STATE_REP_INI; |
| 6779 | break; |
| 6780 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6781 | case STATE_UNROT3L: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6782 | /* Undo ROT3L: "231" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6783 | p = fword + sp->ts_fidx; |
| 6784 | #ifdef FEAT_MBYTE |
| 6785 | if (has_mbyte) |
| 6786 | { |
| 6787 | n = MB_BYTE2LEN(*p); |
| 6788 | n += MB_BYTE2LEN(p[n]); |
| 6789 | c = mb_ptr2char(p + n); |
| 6790 | tl = MB_BYTE2LEN(p[n]); |
| 6791 | mch_memmove(p + tl, p, n); |
| 6792 | mb_char2bytes(c, p); |
| 6793 | } |
| 6794 | else |
| 6795 | #endif |
| 6796 | { |
| 6797 | c = p[2]; |
| 6798 | p[2] = p[1]; |
| 6799 | p[1] = *p; |
| 6800 | *p = c; |
| 6801 | } |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6802 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6803 | /* Rotate three bytes right: "123" -> "312". We change |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6804 | * "fword" here, it's changed back afterwards. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6805 | if (try_deeper(su, stack, depth, SCORE_SWAP3)) |
| 6806 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6807 | sp->ts_state = STATE_UNROT3R; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6808 | ++depth; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6809 | p = fword + sp->ts_fidx; |
| 6810 | #ifdef FEAT_MBYTE |
| 6811 | if (has_mbyte) |
| 6812 | { |
| 6813 | n = mb_ptr2len_check(p); |
| 6814 | n += mb_ptr2len_check(p + n); |
| 6815 | c = mb_ptr2char(p + n); |
| 6816 | tl = mb_ptr2len_check(p + n); |
| 6817 | mch_memmove(p + tl, p, n); |
| 6818 | mb_char2bytes(c, p); |
| 6819 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| 6820 | } |
| 6821 | else |
| 6822 | #endif |
| 6823 | { |
| 6824 | c = p[2]; |
| 6825 | p[2] = p[1]; |
| 6826 | p[1] = *p; |
| 6827 | *p = c; |
| 6828 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 6829 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6830 | } |
| 6831 | else |
| 6832 | sp->ts_state = STATE_REP_INI; |
| 6833 | break; |
| 6834 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6835 | case STATE_UNROT3R: |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6836 | /* Undo ROT3R: "312" -> "123" */ |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6837 | p = fword + sp->ts_fidx; |
| 6838 | #ifdef FEAT_MBYTE |
| 6839 | if (has_mbyte) |
| 6840 | { |
| 6841 | c = mb_ptr2char(p); |
| 6842 | tl = MB_BYTE2LEN(*p); |
| 6843 | n = MB_BYTE2LEN(p[tl]); |
| 6844 | n += MB_BYTE2LEN(p[tl + n]); |
| 6845 | mch_memmove(p, p + tl, n); |
| 6846 | mb_char2bytes(c, p + n); |
| 6847 | } |
| 6848 | else |
| 6849 | #endif |
| 6850 | { |
| 6851 | c = *p; |
| 6852 | *p = p[1]; |
| 6853 | p[1] = p[2]; |
| 6854 | p[2] = c; |
| 6855 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6856 | /*FALLTHROUGH*/ |
| 6857 | |
| 6858 | case STATE_REP_INI: |
| 6859 | /* Check if matching with REP items from the .aff file would |
| 6860 | * work. Quickly skip if there are no REP items or the score |
| 6861 | * is going to be too high anyway. */ |
| 6862 | gap = &lp->lp_slang->sl_rep; |
| 6863 | if (gap->ga_len == 0 |
| 6864 | || sp->ts_score + SCORE_REP >= su->su_maxscore) |
| 6865 | { |
| 6866 | sp->ts_state = STATE_FINAL; |
| 6867 | break; |
| 6868 | } |
| 6869 | |
| 6870 | /* Use the first byte to quickly find the first entry that |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6871 | * may match. If the index is -1 there is none. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6872 | sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]]; |
| 6873 | if (sp->ts_curi < 0) |
| 6874 | { |
| 6875 | sp->ts_state = STATE_FINAL; |
| 6876 | break; |
| 6877 | } |
| 6878 | |
| 6879 | sp->ts_state = STATE_REP; |
| 6880 | /*FALLTHROUGH*/ |
| 6881 | |
| 6882 | case STATE_REP: |
| 6883 | /* Try matching with REP items from the .aff file. For each |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6884 | * match replace the characters and check if the resulting |
| 6885 | * word is valid. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6886 | p = fword + sp->ts_fidx; |
| 6887 | |
| 6888 | gap = &lp->lp_slang->sl_rep; |
| 6889 | while (sp->ts_curi < gap->ga_len) |
| 6890 | { |
| 6891 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 6892 | if (*ftp->ft_from != *p) |
| 6893 | { |
| 6894 | /* past possible matching entries */ |
| 6895 | sp->ts_curi = gap->ga_len; |
| 6896 | break; |
| 6897 | } |
| 6898 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 6899 | && try_deeper(su, stack, depth, SCORE_REP)) |
| 6900 | { |
| 6901 | /* Need to undo this afterwards. */ |
| 6902 | sp->ts_state = STATE_REP_UNDO; |
| 6903 | |
| 6904 | /* Change the "from" to the "to" string. */ |
| 6905 | ++depth; |
| 6906 | fl = STRLEN(ftp->ft_from); |
| 6907 | tl = STRLEN(ftp->ft_to); |
| 6908 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6909 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6910 | mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6911 | repextra += tl - fl; |
| 6912 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6913 | mch_memmove(p, ftp->ft_to, tl); |
| 6914 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6915 | #ifdef FEAT_MBYTE |
| 6916 | stack[depth].ts_tcharlen = 0; |
| 6917 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6918 | break; |
| 6919 | } |
| 6920 | } |
| 6921 | |
| 6922 | if (sp->ts_curi >= gap->ga_len) |
| 6923 | /* No (more) matches. */ |
| 6924 | sp->ts_state = STATE_FINAL; |
| 6925 | |
| 6926 | break; |
| 6927 | |
| 6928 | case STATE_REP_UNDO: |
| 6929 | /* Undo a REP replacement and continue with the next one. */ |
| 6930 | ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data |
| 6931 | + sp->ts_curi - 1; |
| 6932 | fl = STRLEN(ftp->ft_from); |
| 6933 | tl = STRLEN(ftp->ft_to); |
| 6934 | p = fword + sp->ts_fidx; |
| 6935 | if (fl != tl) |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6936 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6937 | mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1); |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6938 | repextra -= tl - fl; |
| 6939 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6940 | mch_memmove(p, ftp->ft_from, fl); |
| 6941 | sp->ts_state = STATE_REP; |
| 6942 | break; |
| 6943 | |
| 6944 | default: |
| 6945 | /* Did all possible states at this level, go up one level. */ |
| 6946 | --depth; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6947 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 6948 | /* Don't check for CTRL-C too often, it takes time. */ |
| 6949 | line_breakcheck(); |
| 6950 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6951 | } |
| 6952 | } |
| 6953 | } |
| 6954 | |
| 6955 | /* |
| 6956 | * Try going one level deeper in the tree. |
| 6957 | */ |
| 6958 | static int |
| 6959 | try_deeper(su, stack, depth, score_add) |
| 6960 | suginfo_T *su; |
| 6961 | trystate_T *stack; |
| 6962 | int depth; |
| 6963 | int score_add; |
| 6964 | { |
| 6965 | int newscore; |
| 6966 | |
| 6967 | /* Refuse to go deeper if the scrore is getting too big. */ |
| 6968 | newscore = stack[depth].ts_score + score_add; |
| 6969 | if (newscore >= su->su_maxscore) |
| 6970 | return FALSE; |
| 6971 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 6972 | stack[depth + 1] = stack[depth]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6973 | stack[depth + 1].ts_state = STATE_START; |
| 6974 | stack[depth + 1].ts_score = newscore; |
| 6975 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6976 | return TRUE; |
| 6977 | } |
| 6978 | |
| 6979 | /* |
| 6980 | * "fword" is a good word with case folded. Find the matching keep-case |
| 6981 | * words and put it in "kword". |
| 6982 | * Theoretically there could be several keep-case words that result in the |
| 6983 | * same case-folded word, but we only find one... |
| 6984 | */ |
| 6985 | static void |
| 6986 | find_keepcap_word(slang, fword, kword) |
| 6987 | slang_T *slang; |
| 6988 | char_u *fword; |
| 6989 | char_u *kword; |
| 6990 | { |
| 6991 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 6992 | int depth; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6993 | idx_T tryidx; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6994 | |
| 6995 | /* The following arrays are used at each depth in the tree. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6996 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6997 | int round[MAXWLEN]; |
| 6998 | int fwordidx[MAXWLEN]; |
| 6999 | int uwordidx[MAXWLEN]; |
| 7000 | int kwordlen[MAXWLEN]; |
| 7001 | |
| 7002 | int flen, ulen; |
| 7003 | int l; |
| 7004 | int len; |
| 7005 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7006 | idx_T lo, hi, m; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7007 | char_u *p; |
| 7008 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7009 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7010 | |
| 7011 | if (byts == NULL) |
| 7012 | { |
| 7013 | /* array is empty: "cannot happen" */ |
| 7014 | *kword = NUL; |
| 7015 | return; |
| 7016 | } |
| 7017 | |
| 7018 | /* Make an all-cap version of "fword". */ |
| 7019 | allcap_copy(fword, uword); |
| 7020 | |
| 7021 | /* |
| 7022 | * Each character needs to be tried both case-folded and upper-case. |
| 7023 | * All this gets very complicated if we keep in mind that changing case |
| 7024 | * may change the byte length of a multi-byte character... |
| 7025 | */ |
| 7026 | depth = 0; |
| 7027 | arridx[0] = 0; |
| 7028 | round[0] = 0; |
| 7029 | fwordidx[0] = 0; |
| 7030 | uwordidx[0] = 0; |
| 7031 | kwordlen[0] = 0; |
| 7032 | while (depth >= 0) |
| 7033 | { |
| 7034 | if (fword[fwordidx[depth]] == NUL) |
| 7035 | { |
| 7036 | /* We are at the end of "fword". If the tree allows a word to end |
| 7037 | * here we have found a match. */ |
| 7038 | if (byts[arridx[depth] + 1] == 0) |
| 7039 | { |
| 7040 | kword[kwordlen[depth]] = NUL; |
| 7041 | return; |
| 7042 | } |
| 7043 | |
| 7044 | /* kword is getting too long, continue one level up */ |
| 7045 | --depth; |
| 7046 | } |
| 7047 | else if (++round[depth] > 2) |
| 7048 | { |
| 7049 | /* tried both fold-case and upper-case character, continue one |
| 7050 | * level up */ |
| 7051 | --depth; |
| 7052 | } |
| 7053 | else |
| 7054 | { |
| 7055 | /* |
| 7056 | * round[depth] == 1: Try using the folded-case character. |
| 7057 | * round[depth] == 2: Try using the upper-case character. |
| 7058 | */ |
| 7059 | #ifdef FEAT_MBYTE |
| 7060 | if (has_mbyte) |
| 7061 | { |
| 7062 | flen = mb_ptr2len_check(fword + fwordidx[depth]); |
| 7063 | ulen = mb_ptr2len_check(uword + uwordidx[depth]); |
| 7064 | } |
| 7065 | else |
| 7066 | #endif |
| 7067 | ulen = flen = 1; |
| 7068 | if (round[depth] == 1) |
| 7069 | { |
| 7070 | p = fword + fwordidx[depth]; |
| 7071 | l = flen; |
| 7072 | } |
| 7073 | else |
| 7074 | { |
| 7075 | p = uword + uwordidx[depth]; |
| 7076 | l = ulen; |
| 7077 | } |
| 7078 | |
| 7079 | for (tryidx = arridx[depth]; l > 0; --l) |
| 7080 | { |
| 7081 | /* Perform a binary search in the list of accepted bytes. */ |
| 7082 | len = byts[tryidx++]; |
| 7083 | c = *p++; |
| 7084 | lo = tryidx; |
| 7085 | hi = tryidx + len - 1; |
| 7086 | while (lo < hi) |
| 7087 | { |
| 7088 | m = (lo + hi) / 2; |
| 7089 | if (byts[m] > c) |
| 7090 | hi = m - 1; |
| 7091 | else if (byts[m] < c) |
| 7092 | lo = m + 1; |
| 7093 | else |
| 7094 | { |
| 7095 | lo = hi = m; |
| 7096 | break; |
| 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | /* Stop if there is no matching byte. */ |
| 7101 | if (hi < lo || byts[lo] != c) |
| 7102 | break; |
| 7103 | |
| 7104 | /* Continue at the child (if there is one). */ |
| 7105 | tryidx = idxs[lo]; |
| 7106 | } |
| 7107 | |
| 7108 | if (l == 0) |
| 7109 | { |
| 7110 | /* |
| 7111 | * Found the matching char. Copy it to "kword" and go a |
| 7112 | * level deeper. |
| 7113 | */ |
| 7114 | if (round[depth] == 1) |
| 7115 | { |
| 7116 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 7117 | flen); |
| 7118 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 7119 | } |
| 7120 | else |
| 7121 | { |
| 7122 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 7123 | ulen); |
| 7124 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 7125 | } |
| 7126 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 7127 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 7128 | |
| 7129 | ++depth; |
| 7130 | arridx[depth] = tryidx; |
| 7131 | round[depth] = 0; |
| 7132 | } |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | /* Didn't find it: "cannot happen". */ |
| 7137 | *kword = NUL; |
| 7138 | } |
| 7139 | |
| 7140 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7141 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 7142 | * su->su_sga. |
| 7143 | */ |
| 7144 | static void |
| 7145 | score_comp_sal(su) |
| 7146 | suginfo_T *su; |
| 7147 | { |
| 7148 | langp_T *lp; |
| 7149 | char_u badsound[MAXWLEN]; |
| 7150 | int i; |
| 7151 | suggest_T *stp; |
| 7152 | suggest_T *sstp; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7153 | int score; |
| 7154 | |
| 7155 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 7156 | return; |
| 7157 | |
| 7158 | /* Use the sound-folding of the first language that supports it. */ |
| 7159 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7160 | lp->lp_slang != NULL; ++lp) |
| 7161 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7162 | { |
| 7163 | /* soundfold the bad word */ |
| 7164 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 7165 | |
| 7166 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7167 | { |
| 7168 | stp = &SUG(su->su_ga, i); |
| 7169 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7170 | /* Case-fold the suggested word, sound-fold it and compute the |
| 7171 | * sound-a-like score. */ |
| 7172 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7173 | if (score < SCORE_MAXMAX) |
| 7174 | { |
| 7175 | /* Add the suggestion. */ |
| 7176 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 7177 | sstp->st_word = vim_strsave(stp->st_word); |
| 7178 | if (sstp->st_word != NULL) |
| 7179 | { |
| 7180 | sstp->st_score = score; |
| 7181 | sstp->st_altscore = 0; |
| 7182 | sstp->st_orglen = stp->st_orglen; |
| 7183 | ++su->su_sga.ga_len; |
| 7184 | } |
| 7185 | } |
| 7186 | } |
| 7187 | break; |
| 7188 | } |
| 7189 | } |
| 7190 | |
| 7191 | /* |
| 7192 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 7193 | * They are intwined. |
| 7194 | */ |
| 7195 | static void |
| 7196 | score_combine(su) |
| 7197 | suginfo_T *su; |
| 7198 | { |
| 7199 | int i; |
| 7200 | int j; |
| 7201 | garray_T ga; |
| 7202 | garray_T *gap; |
| 7203 | langp_T *lp; |
| 7204 | suggest_T *stp; |
| 7205 | char_u *p; |
| 7206 | char_u badsound[MAXWLEN]; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7207 | int round; |
| 7208 | |
| 7209 | /* Add the alternate score to su_ga. */ |
| 7210 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7211 | lp->lp_slang != NULL; ++lp) |
| 7212 | { |
| 7213 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7214 | { |
| 7215 | /* soundfold the bad word */ |
| 7216 | spell_soundfold(lp->lp_slang, su->su_fbadword, badsound); |
| 7217 | |
| 7218 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7219 | { |
| 7220 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7221 | stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang, |
| 7222 | badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7223 | if (stp->st_altscore == SCORE_MAXMAX) |
| 7224 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 7225 | else |
| 7226 | stp->st_score = (stp->st_score * 3 |
| 7227 | + stp->st_altscore) / 4; |
| 7228 | stp->st_salscore = FALSE; |
| 7229 | } |
| 7230 | break; |
| 7231 | } |
| 7232 | } |
| 7233 | |
| 7234 | /* Add the alternate score to su_sga. */ |
| 7235 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 7236 | { |
| 7237 | stp = &SUG(su->su_sga, i); |
| 7238 | stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word); |
| 7239 | if (stp->st_score == SCORE_MAXMAX) |
| 7240 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 7241 | else |
| 7242 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 7243 | stp->st_salscore = TRUE; |
| 7244 | } |
| 7245 | |
| 7246 | /* Sort the suggestions and truncate at "maxcount" for both lists. */ |
| 7247 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 7248 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 7249 | |
| 7250 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 7251 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 7252 | return; |
| 7253 | |
| 7254 | stp = &SUG(ga, 0); |
| 7255 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 7256 | { |
| 7257 | /* round 1: get a suggestion from su_ga |
| 7258 | * round 2: get a suggestion from su_sga */ |
| 7259 | for (round = 1; round <= 2; ++round) |
| 7260 | { |
| 7261 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 7262 | if (i < gap->ga_len) |
| 7263 | { |
| 7264 | /* Don't add a word if it's already there. */ |
| 7265 | p = SUG(*gap, i).st_word; |
| 7266 | for (j = 0; j < ga.ga_len; ++j) |
| 7267 | if (STRCMP(stp[j].st_word, p) == 0) |
| 7268 | break; |
| 7269 | if (j == ga.ga_len) |
| 7270 | stp[ga.ga_len++] = SUG(*gap, i); |
| 7271 | else |
| 7272 | vim_free(p); |
| 7273 | } |
| 7274 | } |
| 7275 | } |
| 7276 | |
| 7277 | ga_clear(&su->su_ga); |
| 7278 | ga_clear(&su->su_sga); |
| 7279 | |
| 7280 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 7281 | if (ga.ga_len > su->su_maxcount) |
| 7282 | { |
| 7283 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 7284 | vim_free(stp[i].st_word); |
| 7285 | ga.ga_len = su->su_maxcount; |
| 7286 | } |
| 7287 | |
| 7288 | su->su_ga = ga; |
| 7289 | } |
| 7290 | |
| 7291 | /* |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7292 | * For the goodword in "stp" compute the soundalike score compared to the |
| 7293 | * badword. |
| 7294 | */ |
| 7295 | static int |
| 7296 | stp_sal_score(stp, su, slang, badsound) |
| 7297 | suggest_T *stp; |
| 7298 | suginfo_T *su; |
| 7299 | slang_T *slang; |
| 7300 | char_u *badsound; /* sound-folded badword */ |
| 7301 | { |
| 7302 | char_u *p; |
| 7303 | char_u badsound2[MAXWLEN]; |
| 7304 | char_u fword[MAXWLEN]; |
| 7305 | char_u goodsound[MAXWLEN]; |
| 7306 | |
| 7307 | if (stp->st_orglen <= su->su_badlen) |
| 7308 | p = badsound; |
| 7309 | else |
| 7310 | { |
| 7311 | /* soundfold the bad word with more characters following */ |
| 7312 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 7313 | |
| 7314 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 7315 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 7316 | * removing the space. Don't do it when the good word also contains a |
| 7317 | * space. */ |
| 7318 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 7319 | && *skiptowhite(stp->st_word) == NUL) |
| 7320 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| 7321 | mch_memmove(p, p + 1, STRLEN(p)); |
| 7322 | |
| 7323 | spell_soundfold(slang, fword, badsound2); |
| 7324 | p = badsound2; |
| 7325 | } |
| 7326 | |
| 7327 | /* Case-fold the word, sound-fold the word and compute the score for the |
| 7328 | * difference. */ |
| 7329 | (void)spell_casefold(stp->st_word, STRLEN(stp->st_word), fword, MAXWLEN); |
| 7330 | spell_soundfold(slang, fword, goodsound); |
| 7331 | |
| 7332 | return soundalike_score(goodsound, p); |
| 7333 | } |
| 7334 | |
| 7335 | /* |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7336 | * Find suggestions by comparing the word in a sound-a-like form. |
| 7337 | */ |
| 7338 | static void |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7339 | suggest_try_soundalike(su) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7340 | suginfo_T *su; |
| 7341 | { |
| 7342 | char_u salword[MAXWLEN]; |
| 7343 | char_u tword[MAXWLEN]; |
| 7344 | char_u tfword[MAXWLEN]; |
| 7345 | char_u tsalword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7346 | idx_T arridx[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7347 | int curi[MAXWLEN]; |
| 7348 | langp_T *lp; |
| 7349 | char_u *byts; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7350 | idx_T *idxs; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7351 | int depth; |
| 7352 | int c; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7353 | idx_T n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7354 | int round; |
| 7355 | int flags; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7356 | int sound_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7357 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7358 | /* Do this for all languages that support sound folding. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7359 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7360 | lp->lp_slang != NULL; ++lp) |
| 7361 | { |
| 7362 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7363 | { |
| 7364 | /* soundfold the bad word */ |
| 7365 | spell_soundfold(lp->lp_slang, su->su_fbadword, salword); |
| 7366 | |
| 7367 | /* |
| 7368 | * Go through the whole tree, soundfold each word and compare. |
| 7369 | * round 1: use the case-folded tree. |
| 7370 | * round 2: use the keep-case tree. |
| 7371 | */ |
| 7372 | for (round = 1; round <= 2; ++round) |
| 7373 | { |
| 7374 | if (round == 1) |
| 7375 | { |
| 7376 | byts = lp->lp_slang->sl_fbyts; |
| 7377 | idxs = lp->lp_slang->sl_fidxs; |
| 7378 | } |
| 7379 | else |
| 7380 | { |
| 7381 | byts = lp->lp_slang->sl_kbyts; |
| 7382 | idxs = lp->lp_slang->sl_kidxs; |
| 7383 | } |
| 7384 | |
| 7385 | depth = 0; |
| 7386 | arridx[0] = 0; |
| 7387 | curi[0] = 1; |
| 7388 | while (depth >= 0 && !got_int) |
| 7389 | { |
| 7390 | if (curi[depth] > byts[arridx[depth]]) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7391 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7392 | /* Done all bytes at this node, go up one level. */ |
| 7393 | --depth; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7394 | line_breakcheck(); |
| 7395 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7396 | else |
| 7397 | { |
| 7398 | /* Do one more byte at this node. */ |
| 7399 | n = arridx[depth] + curi[depth]; |
| 7400 | ++curi[depth]; |
| 7401 | c = byts[n]; |
| 7402 | if (c == 0) |
| 7403 | { |
| 7404 | /* End of word, deal with the word. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7405 | flags = (int)idxs[n]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7406 | if (round == 2 || (flags & WF_KEEPCAP) == 0) |
| 7407 | { |
| 7408 | tword[depth] = NUL; |
| 7409 | if (round == 1) |
| 7410 | spell_soundfold(lp->lp_slang, |
| 7411 | tword, tsalword); |
| 7412 | else |
| 7413 | { |
| 7414 | /* In keep-case tree need to case-fold the |
| 7415 | * word. */ |
| 7416 | (void)spell_casefold(tword, depth, |
| 7417 | tfword, MAXWLEN); |
| 7418 | spell_soundfold(lp->lp_slang, |
| 7419 | tfword, tsalword); |
| 7420 | } |
| 7421 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7422 | /* Compute the edit distance between the |
| 7423 | * sound-a-like words. */ |
| 7424 | sound_score = soundalike_score(salword, |
| 7425 | tsalword); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7426 | if (sound_score < SCORE_MAXMAX) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7427 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7428 | char_u cword[MAXWLEN]; |
| 7429 | char_u *p; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7430 | int score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7431 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7432 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7433 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7434 | /* Need to fix case according to |
| 7435 | * "flags". */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7436 | make_case_word(tword, cword, flags); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7437 | p = cword; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7438 | } |
| 7439 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7440 | p = tword; |
| 7441 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7442 | if (sps_flags & SPS_DOUBLE) |
| 7443 | add_suggestion(su, &su->su_sga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7444 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7445 | sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7446 | else |
| 7447 | { |
| 7448 | /* Compute the score. */ |
| 7449 | score = spell_edit_score( |
| 7450 | su->su_badword, p); |
| 7451 | if (sps_flags & SPS_BEST) |
| 7452 | /* give a bonus for the good word |
| 7453 | * sounding the same as the bad |
| 7454 | * word */ |
| 7455 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7456 | su->su_badlen, |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7457 | RESCORE(score, sound_score), |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7458 | sound_score, TRUE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7459 | else |
| 7460 | add_suggestion(su, &su->su_ga, p, |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7461 | su->su_badlen, |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7462 | score + sound_score, 0, FALSE); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7463 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7464 | } |
| 7465 | } |
| 7466 | |
| 7467 | /* Skip over other NUL bytes. */ |
| 7468 | while (byts[n + 1] == 0) |
| 7469 | { |
| 7470 | ++n; |
| 7471 | ++curi[depth]; |
| 7472 | } |
| 7473 | } |
| 7474 | else |
| 7475 | { |
| 7476 | /* Normal char, go one level deeper. */ |
| 7477 | tword[depth++] = c; |
| 7478 | arridx[depth] = idxs[n]; |
| 7479 | curi[depth] = 1; |
| 7480 | } |
| 7481 | } |
| 7482 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7483 | } |
| 7484 | } |
| 7485 | } |
| 7486 | } |
| 7487 | |
| 7488 | /* |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7489 | * Copy "fword" to "cword", fixing case according to "flags". |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7490 | */ |
| 7491 | static void |
| 7492 | make_case_word(fword, cword, flags) |
| 7493 | char_u *fword; |
| 7494 | char_u *cword; |
| 7495 | int flags; |
| 7496 | { |
| 7497 | if (flags & WF_ALLCAP) |
| 7498 | /* Make it all upper-case */ |
| 7499 | allcap_copy(fword, cword); |
| 7500 | else if (flags & WF_ONECAP) |
| 7501 | /* Make the first letter upper-case */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7502 | onecap_copy(fword, cword, TRUE); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7503 | else |
| 7504 | /* Use goodword as-is. */ |
| 7505 | STRCPY(cword, fword); |
| 7506 | } |
| 7507 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7508 | /* |
| 7509 | * Use map string "map" for languages "lp". |
| 7510 | */ |
| 7511 | static void |
| 7512 | set_map_str(lp, map) |
| 7513 | slang_T *lp; |
| 7514 | char_u *map; |
| 7515 | { |
| 7516 | char_u *p; |
| 7517 | int headc = 0; |
| 7518 | int c; |
| 7519 | int i; |
| 7520 | |
| 7521 | if (*map == NUL) |
| 7522 | { |
| 7523 | lp->sl_has_map = FALSE; |
| 7524 | return; |
| 7525 | } |
| 7526 | lp->sl_has_map = TRUE; |
| 7527 | |
| 7528 | /* Init the array and hash table empty. */ |
| 7529 | for (i = 0; i < 256; ++i) |
| 7530 | lp->sl_map_array[i] = 0; |
| 7531 | #ifdef FEAT_MBYTE |
| 7532 | hash_init(&lp->sl_map_hash); |
| 7533 | #endif |
| 7534 | |
| 7535 | /* |
| 7536 | * The similar characters are stored separated with slashes: |
| 7537 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 7538 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 7539 | */ |
| 7540 | for (p = map; *p != NUL; ) |
| 7541 | { |
| 7542 | #ifdef FEAT_MBYTE |
| 7543 | c = mb_ptr2char_adv(&p); |
| 7544 | #else |
| 7545 | c = *p++; |
| 7546 | #endif |
| 7547 | if (c == '/') |
| 7548 | headc = 0; |
| 7549 | else |
| 7550 | { |
| 7551 | if (headc == 0) |
| 7552 | headc = c; |
| 7553 | |
| 7554 | #ifdef FEAT_MBYTE |
| 7555 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 7556 | * the hash table. Each entry is the char, a NUL the headchar and |
| 7557 | * a NUL. */ |
| 7558 | if (c >= 256) |
| 7559 | { |
| 7560 | int cl = mb_char2len(c); |
| 7561 | int headcl = mb_char2len(headc); |
| 7562 | char_u *b; |
| 7563 | hash_T hash; |
| 7564 | hashitem_T *hi; |
| 7565 | |
| 7566 | b = alloc((unsigned)(cl + headcl + 2)); |
| 7567 | if (b == NULL) |
| 7568 | return; |
| 7569 | mb_char2bytes(c, b); |
| 7570 | b[cl] = NUL; |
| 7571 | mb_char2bytes(headc, b + cl + 1); |
| 7572 | b[cl + 1 + headcl] = NUL; |
| 7573 | hash = hash_hash(b); |
| 7574 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 7575 | if (HASHITEM_EMPTY(hi)) |
| 7576 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 7577 | else |
| 7578 | { |
| 7579 | /* This should have been checked when generating the .spl |
| 7580 | * file. */ |
| 7581 | EMSG(_("E999: duplicate char in MAP entry")); |
| 7582 | vim_free(b); |
| 7583 | } |
| 7584 | } |
| 7585 | else |
| 7586 | #endif |
| 7587 | lp->sl_map_array[c] = headc; |
| 7588 | } |
| 7589 | } |
| 7590 | } |
| 7591 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7592 | /* |
| 7593 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 7594 | * lines in the .aff file. |
| 7595 | */ |
| 7596 | static int |
| 7597 | similar_chars(slang, c1, c2) |
| 7598 | slang_T *slang; |
| 7599 | int c1; |
| 7600 | int c2; |
| 7601 | { |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7602 | int m1, m2; |
| 7603 | #ifdef FEAT_MBYTE |
| 7604 | char_u buf[MB_MAXBYTES]; |
| 7605 | hashitem_T *hi; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7606 | |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7607 | if (c1 >= 256) |
| 7608 | { |
| 7609 | buf[mb_char2bytes(c1, buf)] = 0; |
| 7610 | hi = hash_find(&slang->sl_map_hash, buf); |
| 7611 | if (HASHITEM_EMPTY(hi)) |
| 7612 | m1 = 0; |
| 7613 | else |
| 7614 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 7615 | } |
| 7616 | else |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7617 | #endif |
Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 7618 | m1 = slang->sl_map_array[c1]; |
| 7619 | if (m1 == 0) |
| 7620 | return FALSE; |
| 7621 | |
| 7622 | |
| 7623 | #ifdef FEAT_MBYTE |
| 7624 | if (c2 >= 256) |
| 7625 | { |
| 7626 | buf[mb_char2bytes(c2, buf)] = 0; |
| 7627 | hi = hash_find(&slang->sl_map_hash, buf); |
| 7628 | if (HASHITEM_EMPTY(hi)) |
| 7629 | m2 = 0; |
| 7630 | else |
| 7631 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 7632 | } |
| 7633 | else |
| 7634 | #endif |
| 7635 | m2 = slang->sl_map_array[c2]; |
| 7636 | |
| 7637 | return m1 == m2; |
| 7638 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7639 | |
| 7640 | /* |
| 7641 | * Add a suggestion to the list of suggestions. |
| 7642 | * Do not add a duplicate suggestion or suggestions with a bad score. |
| 7643 | * When "use_score" is not zero it's used, otherwise the score is computed |
| 7644 | * with spell_edit_score(). |
| 7645 | */ |
| 7646 | static void |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7647 | add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7648 | suginfo_T *su; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7649 | garray_T *gap; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7650 | char_u *goodword; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7651 | int badlen; /* length of bad word used */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7652 | int score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7653 | int altscore; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7654 | int had_bonus; /* value for st_had_bonus */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7655 | { |
| 7656 | suggest_T *stp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7657 | int i; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7658 | char_u *p = NULL; |
| 7659 | int c = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7660 | |
| 7661 | /* Check that the word wasn't banned. */ |
| 7662 | if (was_banned(su, goodword)) |
| 7663 | return; |
| 7664 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7665 | /* If past "su_badlen" and the rest is identical stop at "su_badlen". |
| 7666 | * Remove the common part from "goodword". */ |
| 7667 | i = badlen - su->su_badlen; |
| 7668 | if (i > 0) |
| 7669 | { |
| 7670 | /* This assumes there was no case folding or it didn't change the |
| 7671 | * length... */ |
| 7672 | p = goodword + STRLEN(goodword) - i; |
| 7673 | if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0) |
| 7674 | { |
| 7675 | badlen = su->su_badlen; |
| 7676 | c = *p; |
| 7677 | *p = NUL; |
| 7678 | } |
| 7679 | else |
| 7680 | p = NULL; |
| 7681 | } |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 7682 | else if (i < 0) |
| 7683 | { |
| 7684 | /* When replacing part of the word check that we actually change |
| 7685 | * something. For "the the" a suggestion can be replacing the first |
| 7686 | * "the" with itself, since "the" wasn't banned. */ |
| 7687 | if (badlen == STRLEN(goodword) |
| 7688 | && STRNCMP(su->su_badword, goodword, badlen) == 0) |
| 7689 | return; |
| 7690 | } |
| 7691 | |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7692 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7693 | if (score <= su->su_maxscore) |
| 7694 | { |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7695 | /* Check if the word is already there. Also check the length that is |
| 7696 | * being replaced "thes," -> "these" is a different suggestion from |
| 7697 | * "thes" -> "these". */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7698 | stp = &SUG(*gap, 0); |
| 7699 | for (i = gap->ga_len - 1; i >= 0; --i) |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7700 | if (STRCMP(stp[i].st_word, goodword) == 0 |
| 7701 | && stp[i].st_orglen == badlen) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7702 | { |
| 7703 | /* Found it. Remember the lowest score. */ |
| 7704 | if (stp[i].st_score > score) |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7705 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7706 | stp[i].st_score = score; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7707 | stp[i].st_had_bonus = had_bonus; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7708 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7709 | break; |
| 7710 | } |
| 7711 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7712 | if (i < 0 && ga_grow(gap, 1) == OK) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7713 | { |
| 7714 | /* Add a suggestion. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7715 | stp = &SUG(*gap, gap->ga_len); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7716 | stp->st_word = vim_strsave(goodword); |
| 7717 | if (stp->st_word != NULL) |
| 7718 | { |
| 7719 | stp->st_score = score; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7720 | stp->st_altscore = altscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7721 | stp->st_had_bonus = had_bonus; |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7722 | stp->st_orglen = badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7723 | ++gap->ga_len; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7724 | |
| 7725 | /* If we have too many suggestions now, sort the list and keep |
| 7726 | * the best suggestions. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7727 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 7728 | su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, |
| 7729 | SUG_CLEAN_COUNT(su)); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7730 | } |
| 7731 | } |
| 7732 | } |
Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7733 | |
| 7734 | if (p != NULL) |
| 7735 | *p = c; /* restore "goodword" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7736 | } |
| 7737 | |
| 7738 | /* |
| 7739 | * Add a word to be banned. |
| 7740 | */ |
| 7741 | static void |
| 7742 | add_banned(su, word) |
| 7743 | suginfo_T *su; |
| 7744 | char_u *word; |
| 7745 | { |
| 7746 | char_u *s = vim_strsave(word); |
| 7747 | hash_T hash; |
| 7748 | hashitem_T *hi; |
| 7749 | |
| 7750 | if (s != NULL) |
| 7751 | { |
| 7752 | hash = hash_hash(s); |
| 7753 | hi = hash_lookup(&su->su_banned, s, hash); |
| 7754 | if (HASHITEM_EMPTY(hi)) |
| 7755 | hash_add_item(&su->su_banned, hi, s, hash); |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 7756 | else |
| 7757 | vim_free(s); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7758 | } |
| 7759 | } |
| 7760 | |
| 7761 | /* |
| 7762 | * Return TRUE if a word appears in the list of banned words. |
| 7763 | */ |
| 7764 | static int |
| 7765 | was_banned(su, word) |
| 7766 | suginfo_T *su; |
| 7767 | char_u *word; |
| 7768 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7769 | hashitem_T *hi = hash_find(&su->su_banned, word); |
| 7770 | |
| 7771 | return !HASHITEM_EMPTY(hi); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7772 | } |
| 7773 | |
| 7774 | /* |
| 7775 | * Free the banned words in "su". |
| 7776 | */ |
| 7777 | static void |
| 7778 | free_banned(su) |
| 7779 | suginfo_T *su; |
| 7780 | { |
| 7781 | int todo; |
| 7782 | hashitem_T *hi; |
| 7783 | |
| 7784 | todo = su->su_banned.ht_used; |
| 7785 | for (hi = su->su_banned.ht_array; todo > 0; ++hi) |
| 7786 | { |
| 7787 | if (!HASHITEM_EMPTY(hi)) |
| 7788 | { |
| 7789 | vim_free(hi->hi_key); |
| 7790 | --todo; |
| 7791 | } |
| 7792 | } |
| 7793 | hash_clear(&su->su_banned); |
| 7794 | } |
| 7795 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7796 | /* |
| 7797 | * Recompute the score if sound-folding is possible. This is slow, |
| 7798 | * thus only done for the final results. |
| 7799 | */ |
| 7800 | static void |
| 7801 | rescore_suggestions(su) |
| 7802 | suginfo_T *su; |
| 7803 | { |
| 7804 | langp_T *lp; |
| 7805 | suggest_T *stp; |
| 7806 | char_u sal_badword[MAXWLEN]; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7807 | int i; |
| 7808 | |
| 7809 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7810 | lp->lp_slang != NULL; ++lp) |
| 7811 | { |
| 7812 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7813 | { |
| 7814 | /* soundfold the bad word */ |
| 7815 | spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword); |
| 7816 | |
| 7817 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 7818 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7819 | stp = &SUG(su->su_ga, i); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7820 | if (!stp->st_had_bonus) |
| 7821 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 7822 | stp->st_altscore = stp_sal_score(stp, su, |
| 7823 | lp->lp_slang, sal_badword); |
| 7824 | if (stp->st_altscore == SCORE_MAXMAX) |
| 7825 | stp->st_altscore = SCORE_BIG; |
| 7826 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7827 | } |
| 7828 | } |
| 7829 | break; |
| 7830 | } |
| 7831 | } |
| 7832 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7833 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7834 | static int |
| 7835 | #ifdef __BORLANDC__ |
| 7836 | _RTLENTRYF |
| 7837 | #endif |
| 7838 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 7839 | |
| 7840 | /* |
| 7841 | * Function given to qsort() to sort the suggestions on st_score. |
| 7842 | */ |
| 7843 | static int |
| 7844 | #ifdef __BORLANDC__ |
| 7845 | _RTLENTRYF |
| 7846 | #endif |
| 7847 | sug_compare(s1, s2) |
| 7848 | const void *s1; |
| 7849 | const void *s2; |
| 7850 | { |
| 7851 | suggest_T *p1 = (suggest_T *)s1; |
| 7852 | suggest_T *p2 = (suggest_T *)s2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7853 | int n = p1->st_score - p2->st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7854 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7855 | if (n == 0) |
| 7856 | return p1->st_altscore - p2->st_altscore; |
| 7857 | return n; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7858 | } |
| 7859 | |
| 7860 | /* |
| 7861 | * Cleanup the suggestions: |
| 7862 | * - Sort on score. |
| 7863 | * - Remove words that won't be displayed. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7864 | * Returns the maximum score in the list or "maxscore" unmodified. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7865 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7866 | static int |
| 7867 | cleanup_suggestions(gap, maxscore, keep) |
| 7868 | garray_T *gap; |
| 7869 | int maxscore; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7870 | int keep; /* nr of suggestions to keep */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7871 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7872 | suggest_T *stp = &SUG(*gap, 0); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7873 | int i; |
| 7874 | |
| 7875 | /* Sort the list. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7876 | 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] | 7877 | |
| 7878 | /* Truncate the list to the number of suggestions that will be displayed. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7879 | if (gap->ga_len > keep) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7880 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7881 | for (i = keep; i < gap->ga_len; ++i) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7882 | vim_free(stp[i].st_word); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7883 | gap->ga_len = keep; |
| 7884 | return stp[keep - 1].st_score; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7885 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7886 | return maxscore; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7887 | } |
| 7888 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 7889 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 7890 | /* |
| 7891 | * Soundfold a string, for soundfold(). |
| 7892 | * Result is in allocated memory, NULL for an error. |
| 7893 | */ |
| 7894 | char_u * |
| 7895 | eval_soundfold(word) |
| 7896 | char_u *word; |
| 7897 | { |
| 7898 | langp_T *lp; |
| 7899 | char_u fword[MAXWLEN]; |
| 7900 | char_u sound[MAXWLEN]; |
| 7901 | |
| 7902 | if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) |
| 7903 | /* Use the sound-folding of the first language that supports it. */ |
| 7904 | for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0); |
| 7905 | lp->lp_slang != NULL; ++lp) |
| 7906 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 7907 | { |
| 7908 | /* word most be case-folded first. */ |
| 7909 | (void)spell_casefold(word, STRLEN(word), fword, MAXWLEN); |
| 7910 | |
| 7911 | /* soundfold the word */ |
| 7912 | spell_soundfold(lp->lp_slang, fword, sound); |
| 7913 | return vim_strsave(sound); |
| 7914 | } |
| 7915 | |
| 7916 | /* No language with sound folding, return word as-is. */ |
| 7917 | return vim_strsave(word); |
| 7918 | } |
| 7919 | #endif |
| 7920 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7921 | /* |
| 7922 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 7923 | */ |
| 7924 | static void |
| 7925 | spell_soundfold(slang, inword, res) |
| 7926 | slang_T *slang; |
| 7927 | char_u *inword; |
| 7928 | char_u *res; |
| 7929 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7930 | salitem_T *smp; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7931 | char_u word[MAXWLEN]; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7932 | char_u *s; |
| 7933 | char_u *t; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7934 | char_u *pf; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7935 | int i, j, z; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7936 | int reslen; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7937 | int n, k = 0; |
| 7938 | int z0; |
| 7939 | int k0; |
| 7940 | int n0; |
| 7941 | int c; |
| 7942 | int pri; |
| 7943 | int p0 = -333; |
| 7944 | int c0; |
| 7945 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 7946 | #ifdef FEAT_MBYTE |
| 7947 | if (has_mbyte) |
| 7948 | { |
| 7949 | /* Call the multi-byte version of this. */ |
| 7950 | spell_soundfold_w(slang, inword, res); |
| 7951 | return; |
| 7952 | } |
| 7953 | #endif |
| 7954 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7955 | /* Remove accents, if wanted. We actually remove all non-word characters. |
| 7956 | * But keep white space. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7957 | if (slang->sl_rem_accents) |
| 7958 | { |
| 7959 | t = word; |
| 7960 | for (s = inword; *s != NUL; ) |
| 7961 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7962 | if (vim_iswhite(*s)) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7963 | { |
| 7964 | *t++ = ' '; |
| 7965 | s = skipwhite(s); |
| 7966 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7967 | else |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7968 | { |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7969 | if (spell_iswordp(s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7970 | *t++ = *s; |
| 7971 | ++s; |
| 7972 | } |
| 7973 | } |
| 7974 | *t = NUL; |
| 7975 | } |
| 7976 | else |
| 7977 | STRCPY(word, inword); |
| 7978 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7979 | smp = (salitem_T *)slang->sl_sal.ga_data; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7980 | |
| 7981 | /* |
| 7982 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7983 | * Changed to keep spaces. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7984 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7985 | i = reslen = z = 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7986 | while ((c = word[i]) != NUL) |
| 7987 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7988 | /* Start with the first rule that has the character in the word. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7989 | n = slang->sl_sal_first[c]; |
| 7990 | z0 = 0; |
| 7991 | |
| 7992 | if (n >= 0) |
| 7993 | { |
| 7994 | /* check all rules for the same letter */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7995 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7996 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 7997 | /* Quickly skip entries that don't match the word. Most |
| 7998 | * entries are less then three chars, optimize for that. */ |
| 7999 | k = smp[n].sm_leadlen; |
| 8000 | if (k > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8001 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8002 | if (word[i + 1] != s[1]) |
| 8003 | continue; |
| 8004 | if (k > 2) |
| 8005 | { |
| 8006 | for (j = 2; j < k; ++j) |
| 8007 | if (word[i + j] != s[j]) |
| 8008 | break; |
| 8009 | if (j < k) |
| 8010 | continue; |
| 8011 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8012 | } |
| 8013 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8014 | if ((pf = smp[n].sm_oneoff) != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8015 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8016 | /* Check for match with one of the chars in "sm_oneoff". */ |
| 8017 | while (*pf != NUL && *pf != word[i + k]) |
| 8018 | ++pf; |
| 8019 | if (*pf == NUL) |
| 8020 | continue; |
| 8021 | ++k; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8022 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8023 | s = smp[n].sm_rules; |
| 8024 | pri = 5; /* default priority */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8025 | |
| 8026 | p0 = *s; |
| 8027 | k0 = k; |
| 8028 | while (*s == '-' && k > 1) |
| 8029 | { |
| 8030 | k--; |
| 8031 | s++; |
| 8032 | } |
| 8033 | if (*s == '<') |
| 8034 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8035 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8036 | { |
| 8037 | /* determine priority */ |
| 8038 | pri = *s - '0'; |
| 8039 | s++; |
| 8040 | } |
| 8041 | if (*s == '^' && *(s + 1) == '^') |
| 8042 | s++; |
| 8043 | |
| 8044 | if (*s == NUL |
| 8045 | || (*s == '^' |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8046 | && (i == 0 || !(word[i - 1] == ' ' |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 8047 | || spell_iswordp(word + i - 1))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8048 | && (*(s + 1) != '$' |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 8049 | || (!spell_iswordp(word + i + k0)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8050 | || (*s == '$' && i > 0 |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 8051 | && spell_iswordp(word + i - 1) |
| 8052 | && (!spell_iswordp(word + i + k0)))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8053 | { |
| 8054 | /* search for followup rules, if: */ |
| 8055 | /* followup and k > 1 and NO '-' in searchstring */ |
| 8056 | c0 = word[i + k - 1]; |
| 8057 | n0 = slang->sl_sal_first[c0]; |
| 8058 | |
| 8059 | if (slang->sl_followup && k > 1 && n0 >= 0 |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8060 | && p0 != '-' && word[i + k] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8061 | { |
| 8062 | /* test follow-up rule for "word[i + k]" */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8063 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8064 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8065 | /* Quickly skip entries that don't match the word. |
| 8066 | * */ |
| 8067 | k0 = smp[n0].sm_leadlen; |
| 8068 | if (k0 > 1) |
| 8069 | { |
| 8070 | if (word[i + k] != s[1]) |
| 8071 | continue; |
| 8072 | if (k0 > 2) |
| 8073 | { |
| 8074 | pf = word + i + k + 1; |
| 8075 | for (j = 2; j < k0; ++j) |
| 8076 | if (*pf++ != s[j]) |
| 8077 | break; |
| 8078 | if (j < k0) |
| 8079 | continue; |
| 8080 | } |
| 8081 | } |
| 8082 | k0 += k - 1; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8083 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8084 | if ((pf = smp[n0].sm_oneoff) != NULL) |
| 8085 | { |
| 8086 | /* Check for match with one of the chars in |
| 8087 | * "sm_oneoff". */ |
| 8088 | while (*pf != NUL && *pf != word[i + k0]) |
| 8089 | ++pf; |
| 8090 | if (*pf == NUL) |
| 8091 | continue; |
| 8092 | ++k0; |
| 8093 | } |
| 8094 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8095 | p0 = 5; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8096 | s = smp[n0].sm_rules; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8097 | while (*s == '-') |
| 8098 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8099 | /* "k0" gets NOT reduced because |
| 8100 | * "if (k0 == k)" */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8101 | s++; |
| 8102 | } |
| 8103 | if (*s == '<') |
| 8104 | s++; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8105 | if (VIM_ISDIGIT(*s)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8106 | { |
| 8107 | p0 = *s - '0'; |
| 8108 | s++; |
| 8109 | } |
| 8110 | |
| 8111 | if (*s == NUL |
| 8112 | /* *s == '^' cuts */ |
| 8113 | || (*s == '$' |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 8114 | && !spell_iswordp(word + i + k0))) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8115 | { |
| 8116 | if (k0 == k) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8117 | /* this is just a piece of the string */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8118 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8119 | |
| 8120 | if (p0 < pri) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8121 | /* priority too low */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8122 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8123 | /* rule fits; stop search */ |
| 8124 | break; |
| 8125 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8126 | } |
| 8127 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8128 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8129 | continue; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8130 | } |
| 8131 | |
| 8132 | /* replace string */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8133 | s = smp[n].sm_to; |
| 8134 | pf = smp[n].sm_rules; |
| 8135 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8136 | if (p0 == 1 && z == 0) |
| 8137 | { |
| 8138 | /* rule with '<' is used */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8139 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 8140 | || res[reslen - 1] == *s)) |
| 8141 | reslen--; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8142 | z0 = 1; |
| 8143 | z = 1; |
| 8144 | k0 = 0; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8145 | while (*s != NUL && word[i + k0] != NUL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8146 | { |
| 8147 | word[i + k0] = *s; |
| 8148 | k0++; |
| 8149 | s++; |
| 8150 | } |
| 8151 | if (k > k0) |
| 8152 | mch_memmove(word + i + k0, word + i + k, |
| 8153 | STRLEN(word + i + k) + 1); |
| 8154 | |
| 8155 | /* new "actual letter" */ |
| 8156 | c = word[i]; |
| 8157 | } |
| 8158 | else |
| 8159 | { |
| 8160 | /* no '<' rule used */ |
| 8161 | i += k - 1; |
| 8162 | z = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8163 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8164 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8165 | if (reslen == 0 || res[reslen - 1] != *s) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8166 | res[reslen++] = *s; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8167 | s++; |
| 8168 | } |
| 8169 | /* new "actual letter" */ |
| 8170 | c = *s; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8171 | if (strstr((char *)pf, "^^") != NULL) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8172 | { |
| 8173 | if (c != NUL) |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8174 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8175 | mch_memmove(word, word + i + 1, |
| 8176 | STRLEN(word + i + 1) + 1); |
| 8177 | i = 0; |
| 8178 | z0 = 1; |
| 8179 | } |
| 8180 | } |
| 8181 | break; |
| 8182 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8183 | } |
| 8184 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8185 | else if (vim_iswhite(c)) |
| 8186 | { |
| 8187 | c = ' '; |
| 8188 | k = 1; |
| 8189 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8190 | |
| 8191 | if (z0 == 0) |
| 8192 | { |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8193 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 8194 | && (!slang->sl_collapse || reslen == 0 |
| 8195 | || res[reslen - 1] != c)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8196 | /* condense only double letters */ |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8197 | res[reslen++] = c; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8198 | |
| 8199 | i++; |
| 8200 | z = 0; |
| 8201 | k = 0; |
| 8202 | } |
| 8203 | } |
| 8204 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8205 | res[reslen] = NUL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8208 | #ifdef FEAT_MBYTE |
| 8209 | /* |
| 8210 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 8211 | * Multi-byte version of spell_soundfold(). |
| 8212 | */ |
| 8213 | static void |
| 8214 | spell_soundfold_w(slang, inword, res) |
| 8215 | slang_T *slang; |
| 8216 | char_u *inword; |
| 8217 | char_u *res; |
| 8218 | { |
| 8219 | salitem_T *smp; |
| 8220 | int word[MAXWLEN]; |
| 8221 | int wres[MAXWLEN]; |
| 8222 | int l; |
| 8223 | char_u *s; |
| 8224 | int *ws; |
| 8225 | char_u *t; |
| 8226 | int *pf; |
| 8227 | int i, j, z; |
| 8228 | int reslen; |
| 8229 | int n, k = 0; |
| 8230 | int z0; |
| 8231 | int k0; |
| 8232 | int n0; |
| 8233 | int c; |
| 8234 | int pri; |
| 8235 | int p0 = -333; |
| 8236 | int c0; |
| 8237 | int did_white = FALSE; |
| 8238 | |
| 8239 | /* |
| 8240 | * Convert the multi-byte string to a wide-character string. |
| 8241 | * Remove accents, if wanted. We actually remove all non-word characters. |
| 8242 | * But keep white space. |
| 8243 | */ |
| 8244 | n = 0; |
| 8245 | for (s = inword; *s != NUL; ) |
| 8246 | { |
| 8247 | t = s; |
| 8248 | c = mb_ptr2char_adv(&s); |
| 8249 | if (slang->sl_rem_accents) |
| 8250 | { |
| 8251 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 8252 | { |
| 8253 | if (did_white) |
| 8254 | continue; |
| 8255 | c = ' '; |
| 8256 | did_white = TRUE; |
| 8257 | } |
| 8258 | else |
| 8259 | { |
| 8260 | did_white = FALSE; |
| 8261 | if (!spell_iswordp(t)) |
| 8262 | continue; |
| 8263 | } |
| 8264 | } |
| 8265 | word[n++] = c; |
| 8266 | } |
| 8267 | word[n] = NUL; |
| 8268 | |
| 8269 | smp = (salitem_T *)slang->sl_sal.ga_data; |
| 8270 | |
| 8271 | /* |
| 8272 | * This comes from Aspell phonet.cpp. |
| 8273 | * Converted from C++ to C. Added support for multi-byte chars. |
| 8274 | * Changed to keep spaces. |
| 8275 | */ |
| 8276 | i = reslen = z = 0; |
| 8277 | while ((c = word[i]) != NUL) |
| 8278 | { |
| 8279 | /* Start with the first rule that has the character in the word. */ |
| 8280 | n = slang->sl_sal_first[c & 0xff]; |
| 8281 | z0 = 0; |
| 8282 | |
| 8283 | if (n >= 0) |
| 8284 | { |
| 8285 | /* check all rules for the same letter */ |
| 8286 | for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n) |
| 8287 | { |
| 8288 | /* Quickly skip entries that don't match the word. Most |
| 8289 | * entries are less then three chars, optimize for that. */ |
| 8290 | k = smp[n].sm_leadlen; |
| 8291 | if (k > 1) |
| 8292 | { |
| 8293 | if (word[i + 1] != ws[1]) |
| 8294 | continue; |
| 8295 | if (k > 2) |
| 8296 | { |
| 8297 | for (j = 2; j < k; ++j) |
| 8298 | if (word[i + j] != ws[j]) |
| 8299 | break; |
| 8300 | if (j < k) |
| 8301 | continue; |
| 8302 | } |
| 8303 | } |
| 8304 | |
| 8305 | if ((pf = smp[n].sm_oneoff_w) != NULL) |
| 8306 | { |
| 8307 | /* Check for match with one of the chars in "sm_oneoff". */ |
| 8308 | while (*pf != NUL && *pf != word[i + k]) |
| 8309 | ++pf; |
| 8310 | if (*pf == NUL) |
| 8311 | continue; |
| 8312 | ++k; |
| 8313 | } |
| 8314 | s = smp[n].sm_rules; |
| 8315 | pri = 5; /* default priority */ |
| 8316 | |
| 8317 | p0 = *s; |
| 8318 | k0 = k; |
| 8319 | while (*s == '-' && k > 1) |
| 8320 | { |
| 8321 | k--; |
| 8322 | s++; |
| 8323 | } |
| 8324 | if (*s == '<') |
| 8325 | s++; |
| 8326 | if (VIM_ISDIGIT(*s)) |
| 8327 | { |
| 8328 | /* determine priority */ |
| 8329 | pri = *s - '0'; |
| 8330 | s++; |
| 8331 | } |
| 8332 | if (*s == '^' && *(s + 1) == '^') |
| 8333 | s++; |
| 8334 | |
| 8335 | if (*s == NUL |
| 8336 | || (*s == '^' |
| 8337 | && (i == 0 || !(word[i - 1] == ' ' |
| 8338 | || spell_iswordp_w(word + i - 1))) |
| 8339 | && (*(s + 1) != '$' |
| 8340 | || (!spell_iswordp_w(word + i + k0)))) |
| 8341 | || (*s == '$' && i > 0 |
| 8342 | && spell_iswordp_w(word + i - 1) |
| 8343 | && (!spell_iswordp_w(word + i + k0)))) |
| 8344 | { |
| 8345 | /* search for followup rules, if: */ |
| 8346 | /* followup and k > 1 and NO '-' in searchstring */ |
| 8347 | c0 = word[i + k - 1]; |
| 8348 | n0 = slang->sl_sal_first[c0 & 0xff]; |
| 8349 | |
| 8350 | if (slang->sl_followup && k > 1 && n0 >= 0 |
| 8351 | && p0 != '-' && word[i + k] != NUL) |
| 8352 | { |
| 8353 | /* test follow-up rule for "word[i + k]" */ |
| 8354 | for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
| 8355 | == (c0 & 0xff); ++n0) |
| 8356 | { |
| 8357 | /* Quickly skip entries that don't match the word. |
| 8358 | * */ |
| 8359 | k0 = smp[n0].sm_leadlen; |
| 8360 | if (k0 > 1) |
| 8361 | { |
| 8362 | if (word[i + k] != ws[1]) |
| 8363 | continue; |
| 8364 | if (k0 > 2) |
| 8365 | { |
| 8366 | pf = word + i + k + 1; |
| 8367 | for (j = 2; j < k0; ++j) |
| 8368 | if (*pf++ != ws[j]) |
| 8369 | break; |
| 8370 | if (j < k0) |
| 8371 | continue; |
| 8372 | } |
| 8373 | } |
| 8374 | k0 += k - 1; |
| 8375 | |
| 8376 | if ((pf = smp[n0].sm_oneoff_w) != NULL) |
| 8377 | { |
| 8378 | /* Check for match with one of the chars in |
| 8379 | * "sm_oneoff". */ |
| 8380 | while (*pf != NUL && *pf != word[i + k0]) |
| 8381 | ++pf; |
| 8382 | if (*pf == NUL) |
| 8383 | continue; |
| 8384 | ++k0; |
| 8385 | } |
| 8386 | |
| 8387 | p0 = 5; |
| 8388 | s = smp[n0].sm_rules; |
| 8389 | while (*s == '-') |
| 8390 | { |
| 8391 | /* "k0" gets NOT reduced because |
| 8392 | * "if (k0 == k)" */ |
| 8393 | s++; |
| 8394 | } |
| 8395 | if (*s == '<') |
| 8396 | s++; |
| 8397 | if (VIM_ISDIGIT(*s)) |
| 8398 | { |
| 8399 | p0 = *s - '0'; |
| 8400 | s++; |
| 8401 | } |
| 8402 | |
| 8403 | if (*s == NUL |
| 8404 | /* *s == '^' cuts */ |
| 8405 | || (*s == '$' |
| 8406 | && !spell_iswordp_w(word + i + k0))) |
| 8407 | { |
| 8408 | if (k0 == k) |
| 8409 | /* this is just a piece of the string */ |
| 8410 | continue; |
| 8411 | |
| 8412 | if (p0 < pri) |
| 8413 | /* priority too low */ |
| 8414 | continue; |
| 8415 | /* rule fits; stop search */ |
| 8416 | break; |
| 8417 | } |
| 8418 | } |
| 8419 | |
| 8420 | if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) |
| 8421 | == (c0 & 0xff)) |
| 8422 | continue; |
| 8423 | } |
| 8424 | |
| 8425 | /* replace string */ |
| 8426 | ws = smp[n].sm_to_w; |
| 8427 | s = smp[n].sm_rules; |
| 8428 | p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; |
| 8429 | if (p0 == 1 && z == 0) |
| 8430 | { |
| 8431 | /* rule with '<' is used */ |
| 8432 | if (reslen > 0 && *ws != NUL && (wres[reslen - 1] == c |
| 8433 | || wres[reslen - 1] == *ws)) |
| 8434 | reslen--; |
| 8435 | z0 = 1; |
| 8436 | z = 1; |
| 8437 | k0 = 0; |
| 8438 | while (*ws != NUL && word[i + k0] != NUL) |
| 8439 | { |
| 8440 | word[i + k0] = *ws; |
| 8441 | k0++; |
| 8442 | ws++; |
| 8443 | } |
| 8444 | if (k > k0) |
| 8445 | mch_memmove(word + i + k0, word + i + k, |
| 8446 | sizeof(int) * (STRLEN(word + i + k) + 1)); |
| 8447 | |
| 8448 | /* new "actual letter" */ |
| 8449 | c = word[i]; |
| 8450 | } |
| 8451 | else |
| 8452 | { |
| 8453 | /* no '<' rule used */ |
| 8454 | i += k - 1; |
| 8455 | z = 0; |
| 8456 | while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN) |
| 8457 | { |
| 8458 | if (reslen == 0 || wres[reslen - 1] != *ws) |
| 8459 | wres[reslen++] = *ws; |
| 8460 | ws++; |
| 8461 | } |
| 8462 | /* new "actual letter" */ |
| 8463 | c = *ws; |
| 8464 | if (strstr((char *)s, "^^") != NULL) |
| 8465 | { |
| 8466 | if (c != NUL) |
| 8467 | wres[reslen++] = c; |
| 8468 | mch_memmove(word, word + i + 1, |
| 8469 | sizeof(int) * (STRLEN(word + i + 1) + 1)); |
| 8470 | i = 0; |
| 8471 | z0 = 1; |
| 8472 | } |
| 8473 | } |
| 8474 | break; |
| 8475 | } |
| 8476 | } |
| 8477 | } |
| 8478 | else if (vim_iswhite(c)) |
| 8479 | { |
| 8480 | c = ' '; |
| 8481 | k = 1; |
| 8482 | } |
| 8483 | |
| 8484 | if (z0 == 0) |
| 8485 | { |
| 8486 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 8487 | && (!slang->sl_collapse || reslen == 0 |
| 8488 | || wres[reslen - 1] != c)) |
| 8489 | /* condense only double letters */ |
| 8490 | wres[reslen++] = c; |
| 8491 | |
| 8492 | i++; |
| 8493 | z = 0; |
| 8494 | k = 0; |
| 8495 | } |
| 8496 | } |
| 8497 | |
| 8498 | /* Convert wide characters in "wres" to a multi-byte string in "res". */ |
| 8499 | l = 0; |
| 8500 | for (n = 0; n < reslen; ++n) |
| 8501 | { |
| 8502 | l += mb_char2bytes(wres[n], res + l); |
| 8503 | if (l + MB_MAXBYTES > MAXWLEN) |
| 8504 | break; |
| 8505 | } |
| 8506 | res[l] = NUL; |
| 8507 | } |
| 8508 | #endif |
| 8509 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8510 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8511 | * Compute a score for two sound-a-like words. |
| 8512 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 8513 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 8514 | * avoiding checks that will not be possible. |
| 8515 | */ |
| 8516 | static int |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8517 | soundalike_score(goodstart, badstart) |
| 8518 | char_u *goodstart; /* sound-folded good word */ |
| 8519 | char_u *badstart; /* sound-folded bad word */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8520 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8521 | char_u *goodsound = goodstart; |
| 8522 | char_u *badsound = badstart; |
| 8523 | int goodlen; |
| 8524 | int badlen; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8525 | int n; |
| 8526 | char_u *pl, *ps; |
| 8527 | char_u *pl2, *ps2; |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8528 | int score = 0; |
| 8529 | |
| 8530 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 8531 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 8532 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 8533 | { |
| 8534 | score = SCORE_DEL / 2; |
| 8535 | if (*badsound == '*') |
| 8536 | ++badsound; |
| 8537 | else |
| 8538 | ++goodsound; |
| 8539 | } |
| 8540 | |
| 8541 | goodlen = STRLEN(goodsound); |
| 8542 | badlen = STRLEN(badsound); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8543 | |
| 8544 | /* Return quickly if the lenghts are too different to be fixed by two |
| 8545 | * changes. */ |
| 8546 | n = goodlen - badlen; |
| 8547 | if (n < -2 || n > 2) |
| 8548 | return SCORE_MAXMAX; |
| 8549 | |
| 8550 | if (n > 0) |
| 8551 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8552 | pl = goodsound; /* goodsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8553 | ps = badsound; |
| 8554 | } |
| 8555 | else |
| 8556 | { |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8557 | pl = badsound; /* badsound is longest */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8558 | ps = goodsound; |
| 8559 | } |
| 8560 | |
| 8561 | /* Skip over the identical part. */ |
| 8562 | while (*pl == *ps && *pl != NUL) |
| 8563 | { |
| 8564 | ++pl; |
| 8565 | ++ps; |
| 8566 | } |
| 8567 | |
| 8568 | switch (n) |
| 8569 | { |
| 8570 | case -2: |
| 8571 | case 2: |
| 8572 | /* |
| 8573 | * Must delete two characters from "pl". |
| 8574 | */ |
| 8575 | ++pl; /* first delete */ |
| 8576 | while (*pl == *ps) |
| 8577 | { |
| 8578 | ++pl; |
| 8579 | ++ps; |
| 8580 | } |
| 8581 | /* strings must be equal after second delete */ |
| 8582 | if (STRCMP(pl + 1, ps) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8583 | return score + SCORE_DEL * 2; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8584 | |
| 8585 | /* Failed to compare. */ |
| 8586 | break; |
| 8587 | |
| 8588 | case -1: |
| 8589 | case 1: |
| 8590 | /* |
| 8591 | * Minimal one delete from "pl" required. |
| 8592 | */ |
| 8593 | |
| 8594 | /* 1: delete */ |
| 8595 | pl2 = pl + 1; |
| 8596 | ps2 = ps; |
| 8597 | while (*pl2 == *ps2) |
| 8598 | { |
| 8599 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8600 | return score + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8601 | ++pl2; |
| 8602 | ++ps2; |
| 8603 | } |
| 8604 | |
| 8605 | /* 2: delete then swap, then rest must be equal */ |
| 8606 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 8607 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8608 | return score + SCORE_DEL + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8609 | |
| 8610 | /* 3: delete then substitute, then the rest must be equal */ |
| 8611 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8612 | return score + SCORE_DEL + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8613 | |
| 8614 | /* 4: first swap then delete */ |
| 8615 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 8616 | { |
| 8617 | pl2 = pl + 2; /* swap, skip two chars */ |
| 8618 | ps2 = ps + 2; |
| 8619 | while (*pl2 == *ps2) |
| 8620 | { |
| 8621 | ++pl2; |
| 8622 | ++ps2; |
| 8623 | } |
| 8624 | /* delete a char and then strings must be equal */ |
| 8625 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8626 | return score + SCORE_SWAP + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8627 | } |
| 8628 | |
| 8629 | /* 5: first substitute then delete */ |
| 8630 | pl2 = pl + 1; /* substitute, skip one char */ |
| 8631 | ps2 = ps + 1; |
| 8632 | while (*pl2 == *ps2) |
| 8633 | { |
| 8634 | ++pl2; |
| 8635 | ++ps2; |
| 8636 | } |
| 8637 | /* delete a char and then strings must be equal */ |
| 8638 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8639 | return score + SCORE_SUBST + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8640 | |
| 8641 | /* Failed to compare. */ |
| 8642 | break; |
| 8643 | |
| 8644 | case 0: |
| 8645 | /* |
| 8646 | * Lenghts are equal, thus changes must result in same length: An |
| 8647 | * insert is only possible in combination with a delete. |
| 8648 | * 1: check if for identical strings |
| 8649 | */ |
| 8650 | if (*pl == NUL) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8651 | return score; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8652 | |
| 8653 | /* 2: swap */ |
| 8654 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 8655 | { |
| 8656 | pl2 = pl + 2; /* swap, skip two chars */ |
| 8657 | ps2 = ps + 2; |
| 8658 | while (*pl2 == *ps2) |
| 8659 | { |
| 8660 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8661 | return score + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8662 | ++pl2; |
| 8663 | ++ps2; |
| 8664 | } |
| 8665 | /* 3: swap and swap again */ |
| 8666 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 8667 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8668 | return score + SCORE_SWAP + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8669 | |
| 8670 | /* 4: swap and substitute */ |
| 8671 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8672 | return score + SCORE_SWAP + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | /* 5: substitute */ |
| 8676 | pl2 = pl + 1; |
| 8677 | ps2 = ps + 1; |
| 8678 | while (*pl2 == *ps2) |
| 8679 | { |
| 8680 | if (*pl2 == NUL) /* reached the end */ |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8681 | return score + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8682 | ++pl2; |
| 8683 | ++ps2; |
| 8684 | } |
| 8685 | |
| 8686 | /* 6: substitute and swap */ |
| 8687 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 8688 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8689 | return score + SCORE_SUBST + SCORE_SWAP; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8690 | |
| 8691 | /* 7: substitute and substitute */ |
| 8692 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8693 | return score + SCORE_SUBST + SCORE_SUBST; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8694 | |
| 8695 | /* 8: insert then delete */ |
| 8696 | pl2 = pl; |
| 8697 | ps2 = ps + 1; |
| 8698 | while (*pl2 == *ps2) |
| 8699 | { |
| 8700 | ++pl2; |
| 8701 | ++ps2; |
| 8702 | } |
| 8703 | if (STRCMP(pl2 + 1, ps2) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8704 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8705 | |
| 8706 | /* 9: delete then insert */ |
| 8707 | pl2 = pl + 1; |
| 8708 | ps2 = ps; |
| 8709 | while (*pl2 == *ps2) |
| 8710 | { |
| 8711 | ++pl2; |
| 8712 | ++ps2; |
| 8713 | } |
| 8714 | if (STRCMP(pl2, ps2 + 1) == 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8715 | return score + SCORE_INS + SCORE_DEL; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8716 | |
| 8717 | /* Failed to compare. */ |
| 8718 | break; |
| 8719 | } |
| 8720 | |
| 8721 | return SCORE_MAXMAX; |
| 8722 | } |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8723 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8724 | /* |
| 8725 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8726 | * deletes/inserts/substitutes/swaps are required the lower the score. |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8727 | * |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8728 | * The algorithm comes from Aspell editdist.cpp, edit_distance(). |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8729 | * It has been converted from C++ to C and modified to support multi-byte |
| 8730 | * characters. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8731 | */ |
| 8732 | static int |
| 8733 | spell_edit_score(badword, goodword) |
| 8734 | char_u *badword; |
| 8735 | char_u *goodword; |
| 8736 | { |
| 8737 | int *cnt; |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 8738 | int badlen, goodlen; /* lenghts including NUL */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8739 | int j, i; |
| 8740 | int t; |
| 8741 | int bc, gc; |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8742 | int pbc, pgc; |
| 8743 | #ifdef FEAT_MBYTE |
| 8744 | char_u *p; |
| 8745 | int wbadword[MAXWLEN]; |
| 8746 | int wgoodword[MAXWLEN]; |
| 8747 | |
| 8748 | if (has_mbyte) |
| 8749 | { |
| 8750 | /* Get the characters from the multi-byte strings and put them in an |
| 8751 | * int array for easy access. */ |
| 8752 | for (p = badword, badlen = 0; *p != NUL; ) |
| 8753 | wbadword[badlen++] = mb_ptr2char_adv(&p); |
| 8754 | ++badlen; |
| 8755 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| 8756 | wgoodword[goodlen++] = mb_ptr2char_adv(&p); |
| 8757 | ++goodlen; |
| 8758 | } |
| 8759 | else |
| 8760 | #endif |
| 8761 | { |
| 8762 | badlen = STRLEN(badword) + 1; |
| 8763 | goodlen = STRLEN(goodword) + 1; |
| 8764 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8765 | |
| 8766 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 8767 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8768 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 8769 | TRUE); |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8770 | if (cnt == NULL) |
| 8771 | return 0; /* out of memory */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8772 | |
| 8773 | CNT(0, 0) = 0; |
| 8774 | for (j = 1; j <= goodlen; ++j) |
| 8775 | CNT(0, j) = CNT(0, j - 1) + SCORE_DEL; |
| 8776 | |
| 8777 | for (i = 1; i <= badlen; ++i) |
| 8778 | { |
| 8779 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS; |
| 8780 | for (j = 1; j <= goodlen; ++j) |
| 8781 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8782 | #ifdef FEAT_MBYTE |
| 8783 | if (has_mbyte) |
| 8784 | { |
| 8785 | bc = wbadword[i - 1]; |
| 8786 | gc = wgoodword[j - 1]; |
| 8787 | } |
| 8788 | else |
| 8789 | #endif |
| 8790 | { |
| 8791 | bc = badword[i - 1]; |
| 8792 | gc = goodword[j - 1]; |
| 8793 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8794 | if (bc == gc) |
| 8795 | CNT(i, j) = CNT(i - 1, j - 1); |
| 8796 | else |
| 8797 | { |
| 8798 | /* Use a better score when there is only a case difference. */ |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8799 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8800 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 8801 | else |
| 8802 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 8803 | |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8804 | if (i > 1 && j > 1) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8805 | { |
Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 8806 | #ifdef FEAT_MBYTE |
| 8807 | if (has_mbyte) |
| 8808 | { |
| 8809 | pbc = wbadword[i - 2]; |
| 8810 | pgc = wgoodword[j - 2]; |
| 8811 | } |
| 8812 | else |
| 8813 | #endif |
| 8814 | { |
| 8815 | pbc = badword[i - 2]; |
| 8816 | pgc = goodword[j - 2]; |
| 8817 | } |
| 8818 | if (bc == pgc && pbc == gc) |
| 8819 | { |
| 8820 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 8821 | if (t < CNT(i, j)) |
| 8822 | CNT(i, j) = t; |
| 8823 | } |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8824 | } |
| 8825 | t = SCORE_DEL + CNT(i - 1, j); |
| 8826 | if (t < CNT(i, j)) |
| 8827 | CNT(i, j) = t; |
| 8828 | t = SCORE_INS + CNT(i, j - 1); |
| 8829 | if (t < CNT(i, j)) |
| 8830 | CNT(i, j) = t; |
| 8831 | } |
| 8832 | } |
| 8833 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 8834 | |
| 8835 | i = CNT(badlen - 1, goodlen - 1); |
| 8836 | vim_free(cnt); |
| 8837 | return i; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8838 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 8839 | |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8840 | /* |
| 8841 | * ":spelldump" |
| 8842 | */ |
| 8843 | /*ARGSUSED*/ |
| 8844 | void |
| 8845 | ex_spelldump(eap) |
| 8846 | exarg_T *eap; |
| 8847 | { |
| 8848 | buf_T *buf = curbuf; |
| 8849 | langp_T *lp; |
| 8850 | slang_T *slang; |
| 8851 | idx_T arridx[MAXWLEN]; |
| 8852 | int curi[MAXWLEN]; |
| 8853 | char_u word[MAXWLEN]; |
| 8854 | int c; |
| 8855 | char_u *byts; |
| 8856 | idx_T *idxs; |
| 8857 | linenr_T lnum = 0; |
| 8858 | int round; |
| 8859 | int depth; |
| 8860 | int n; |
| 8861 | int flags; |
| 8862 | |
| 8863 | if (no_spell_checking()) |
| 8864 | return; |
| 8865 | |
| 8866 | /* Create a new empty buffer by splitting the window. */ |
| 8867 | do_cmdline_cmd((char_u *)"new"); |
| 8868 | if (!bufempty() || !buf_valid(buf)) |
| 8869 | return; |
| 8870 | |
| 8871 | for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp) |
| 8872 | { |
| 8873 | slang = lp->lp_slang; |
| 8874 | |
| 8875 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 8876 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 8877 | |
| 8878 | /* round 1: case-folded tree |
| 8879 | * round 2: keep-case tree */ |
| 8880 | for (round = 1; round <= 2; ++round) |
| 8881 | { |
| 8882 | if (round == 1) |
| 8883 | { |
| 8884 | byts = slang->sl_fbyts; |
| 8885 | idxs = slang->sl_fidxs; |
| 8886 | } |
| 8887 | else |
| 8888 | { |
| 8889 | byts = slang->sl_kbyts; |
| 8890 | idxs = slang->sl_kidxs; |
| 8891 | } |
| 8892 | if (byts == NULL) |
| 8893 | continue; /* array is empty */ |
| 8894 | |
| 8895 | depth = 0; |
| 8896 | arridx[0] = 0; |
| 8897 | curi[0] = 1; |
| 8898 | while (depth >= 0 && !got_int) |
| 8899 | { |
| 8900 | if (curi[depth] > byts[arridx[depth]]) |
| 8901 | { |
| 8902 | /* Done all bytes at this node, go up one level. */ |
| 8903 | --depth; |
| 8904 | line_breakcheck(); |
| 8905 | } |
| 8906 | else |
| 8907 | { |
| 8908 | /* Do one more byte at this node. */ |
| 8909 | n = arridx[depth] + curi[depth]; |
| 8910 | ++curi[depth]; |
| 8911 | c = byts[n]; |
| 8912 | if (c == 0) |
| 8913 | { |
| 8914 | /* End of word, deal with the word. |
| 8915 | * Don't use keep-case words in the fold-case tree, |
| 8916 | * they will appear in the keep-case tree. |
| 8917 | * Only use the word when the region matches. */ |
| 8918 | flags = (int)idxs[n]; |
| 8919 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
| 8920 | && ((flags & WF_REGION) == 0 |
| 8921 | || (((unsigned)flags >> 8) |
| 8922 | & lp->lp_region) != 0)) |
| 8923 | { |
| 8924 | word[depth] = NUL; |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8925 | |
| 8926 | /* Dump the basic word if there is no prefix or |
| 8927 | * when it's the first one. */ |
| 8928 | c = (unsigned)flags >> 16; |
| 8929 | if (c == 0 || curi[depth] == 2) |
| 8930 | dump_word(word, round, flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8931 | |
| 8932 | /* Apply the prefix, if there is one. */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 8933 | if (c != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 8934 | lnum = apply_prefixes(slang, word, round, |
| 8935 | flags, lnum); |
| 8936 | } |
| 8937 | } |
| 8938 | else |
| 8939 | { |
| 8940 | /* Normal char, go one level deeper. */ |
| 8941 | word[depth++] = c; |
| 8942 | arridx[depth] = idxs[n]; |
| 8943 | curi[depth] = 1; |
| 8944 | } |
| 8945 | } |
| 8946 | } |
| 8947 | } |
| 8948 | } |
| 8949 | |
| 8950 | /* Delete the empty line that we started with. */ |
| 8951 | if (curbuf->b_ml.ml_line_count > 1) |
| 8952 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 8953 | |
| 8954 | redraw_later(NOT_VALID); |
| 8955 | } |
| 8956 | |
| 8957 | /* |
| 8958 | * Dump one word: apply case modifications and append a line to the buffer. |
| 8959 | */ |
| 8960 | static void |
| 8961 | dump_word(word, round, flags, lnum) |
| 8962 | char_u *word; |
| 8963 | int round; |
| 8964 | int flags; |
| 8965 | linenr_T lnum; |
| 8966 | { |
| 8967 | int keepcap = FALSE; |
| 8968 | char_u *p; |
| 8969 | char_u cword[MAXWLEN]; |
| 8970 | char_u badword[MAXWLEN + 3]; |
| 8971 | |
| 8972 | if (round == 1 && (flags & WF_CAPMASK) != 0) |
| 8973 | { |
| 8974 | /* Need to fix case according to "flags". */ |
| 8975 | make_case_word(word, cword, flags); |
| 8976 | p = cword; |
| 8977 | } |
| 8978 | else |
| 8979 | { |
| 8980 | p = word; |
| 8981 | if (round == 2 && (captype(word, NULL) & WF_KEEPCAP) == 0) |
| 8982 | keepcap = TRUE; |
| 8983 | } |
| 8984 | |
| 8985 | /* Bad word is preceded by "/!" and some other |
| 8986 | * flags. */ |
| 8987 | if ((flags & (WF_BANNED | WF_RARE)) || keepcap) |
| 8988 | { |
| 8989 | STRCPY(badword, "/"); |
| 8990 | if (keepcap) |
| 8991 | STRCAT(badword, "="); |
| 8992 | if (flags & WF_BANNED) |
| 8993 | STRCAT(badword, "!"); |
| 8994 | else if (flags & WF_RARE) |
| 8995 | STRCAT(badword, "?"); |
| 8996 | STRCAT(badword, p); |
| 8997 | p = badword; |
| 8998 | } |
| 8999 | |
| 9000 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 9001 | } |
| 9002 | |
| 9003 | /* |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 9004 | * For ":spelldump": Find matching prefixes for "word". Prepend each to |
| 9005 | * "word" and append a line to the buffer. |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9006 | * Return the updated line number. |
| 9007 | */ |
| 9008 | static linenr_T |
| 9009 | apply_prefixes(slang, word, round, flags, startlnum) |
| 9010 | slang_T *slang; |
| 9011 | char_u *word; /* case-folded word */ |
| 9012 | int round; |
| 9013 | int flags; /* flags with prefix ID */ |
| 9014 | linenr_T startlnum; |
| 9015 | { |
| 9016 | idx_T arridx[MAXWLEN]; |
| 9017 | int curi[MAXWLEN]; |
| 9018 | char_u prefix[MAXWLEN]; |
| 9019 | int c; |
| 9020 | char_u *byts; |
| 9021 | idx_T *idxs; |
| 9022 | linenr_T lnum = startlnum; |
| 9023 | int depth; |
| 9024 | int n; |
| 9025 | int len; |
| 9026 | int prefid = (unsigned)flags >> 16; |
| 9027 | int i; |
| 9028 | |
| 9029 | byts = slang->sl_pbyts; |
| 9030 | idxs = slang->sl_pidxs; |
| 9031 | if (byts != NULL) /* array not is empty */ |
| 9032 | { |
| 9033 | /* |
| 9034 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
| 9035 | * When at the end of a prefix check that it supports "prefid". |
| 9036 | */ |
| 9037 | depth = 0; |
| 9038 | arridx[0] = 0; |
| 9039 | curi[0] = 1; |
| 9040 | while (depth >= 0 && !got_int) |
| 9041 | { |
| 9042 | len = arridx[depth]; |
| 9043 | if (curi[depth] > byts[len]) |
| 9044 | { |
| 9045 | /* Done all bytes at this node, go up one level. */ |
| 9046 | --depth; |
| 9047 | line_breakcheck(); |
| 9048 | } |
| 9049 | else |
| 9050 | { |
| 9051 | /* Do one more byte at this node. */ |
| 9052 | n = len + curi[depth]; |
| 9053 | ++curi[depth]; |
| 9054 | c = byts[n]; |
| 9055 | if (c == 0) |
| 9056 | { |
| 9057 | /* End of prefix, find out how many IDs there are. */ |
| 9058 | for (i = 1; i < len; ++i) |
| 9059 | if (byts[n + i] != 0) |
| 9060 | break; |
| 9061 | curi[depth] += i - 1; |
| 9062 | |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9063 | i = valid_word_prefix(i, n, prefid, word, slang); |
| 9064 | if (i != 0) |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9065 | { |
| 9066 | vim_strncpy(prefix + depth, word, MAXWLEN - depth); |
Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9067 | dump_word(prefix, round, |
| 9068 | (i & WF_RAREPFX) ? (flags | WF_RARE) |
| 9069 | : flags, lnum++); |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9070 | } |
| 9071 | } |
| 9072 | else |
| 9073 | { |
| 9074 | /* Normal char, go one level deeper. */ |
| 9075 | prefix[depth++] = c; |
| 9076 | arridx[depth] = idxs[n]; |
| 9077 | curi[depth] = 1; |
| 9078 | } |
| 9079 | } |
| 9080 | } |
| 9081 | } |
| 9082 | |
| 9083 | return lnum; |
| 9084 | } |
| 9085 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9086 | #endif /* FEAT_SYN_HL */ |