| 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 | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 38 | * There is one additional tree for when not all prefixes are applied when |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 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 | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 46 | * LZ trie ideas: |
| 47 | * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf |
| 48 | * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 49 | * |
| 50 | * Matching involves checking the caps type: Onecap ALLCAP KeepCap. |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 51 | * |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 52 | * Why doesn't Vim use aspell/ispell/myspell/etc.? |
| 53 | * See ":help develop-spell". |
| 54 | */ |
| 55 | |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 56 | /* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word. |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 57 | * Only use it for small word lists! */ |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 58 | #if 0 |
| 59 | # define SPELL_PRINTTREE |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
| Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 62 | /* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a |
| 63 | * specific word. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 64 | #if 0 |
| 65 | # define DEBUG_TRIEWALK |
| 66 | #endif |
| 67 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 68 | /* |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 69 | * Use this to adjust the score after finding suggestions, based on the |
| 70 | * suggested word sounding like the bad word. This is much faster than doing |
| 71 | * it for every possible suggestion. |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 72 | * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@" |
| 73 | * vs "ht") and goes down in the list. |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 74 | * Used when 'spellsuggest' is set to "best". |
| 75 | */ |
| 76 | #define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4) |
| 77 | |
| 78 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 79 | * Do the opposite: based on a maximum end score and a known sound score, |
| Bram Moolenaar | 6949d1d | 2008-08-25 02:14:05 +0000 | [diff] [blame] | 80 | * compute the maximum word score that can be used. |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 81 | */ |
| 82 | #define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3) |
| 83 | |
| 84 | /* |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 85 | * Vim spell file format: <HEADER> |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 86 | * <SECTIONS> |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 87 | * <LWORDTREE> |
| 88 | * <KWORDTREE> |
| 89 | * <PREFIXTREE> |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 90 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 91 | * <HEADER>: <fileID> <versionnr> |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 92 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 93 | * <fileID> 8 bytes "VIMspell" |
| 94 | * <versionnr> 1 byte VIMSPELLVERSION |
| 95 | * |
| 96 | * |
| 97 | * Sections make it possible to add information to the .spl file without |
| 98 | * making it incompatible with previous versions. There are two kinds of |
| 99 | * sections: |
| 100 | * 1. Not essential for correct spell checking. E.g. for making suggestions. |
| 101 | * These are skipped when not supported. |
| 102 | * 2. Optional information, but essential for spell checking when present. |
| 103 | * E.g. conditions for affixes. When this section is present but not |
| 104 | * supported an error message is given. |
| 105 | * |
| 106 | * <SECTIONS>: <section> ... <sectionend> |
| 107 | * |
| 108 | * <section>: <sectionID> <sectionflags> <sectionlen> (section contents) |
| 109 | * |
| 110 | * <sectionID> 1 byte number from 0 to 254 identifying the section |
| 111 | * |
| 112 | * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct |
| 113 | * spell checking |
| 114 | * |
| 115 | * <sectionlen> 4 bytes length of section contents, MSB first |
| 116 | * |
| 117 | * <sectionend> 1 byte SN_END |
| 118 | * |
| 119 | * |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 120 | * sectionID == SN_INFO: <infotext> |
| 121 | * <infotext> N bytes free format text with spell file info (version, |
| 122 | * website, etc) |
| 123 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 124 | * sectionID == SN_REGION: <regionname> ... |
| 125 | * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 126 | * First <regionname> is region 1. |
| 127 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 128 | * sectionID == SN_CHARFLAGS: <charflagslen> <charflags> |
| 129 | * <folcharslen> <folchars> |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 130 | * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128). |
| 131 | * <charflags> N bytes List of flags (first one is for character 128): |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 132 | * 0x01 word character CF_WORD |
| 133 | * 0x02 upper-case character CF_UPPER |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 134 | * <folcharslen> 2 bytes Number of bytes in <folchars>. |
| 135 | * <folchars> N bytes Folded characters, first one is for character 128. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 136 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 137 | * sectionID == SN_MIDWORD: <midword> |
| 138 | * <midword> N bytes Characters that are word characters only when used |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 139 | * in the middle of a word. |
| 140 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 141 | * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ... |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 142 | * <prefcondcnt> 2 bytes Number of <prefcond> items following. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 143 | * <prefcond> : <condlen> <condstr> |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 144 | * <condlen> 1 byte Length of <condstr>. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 145 | * <condstr> N bytes Condition for the prefix. |
| 146 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 147 | * sectionID == SN_REP: <repcount> <rep> ... |
| 148 | * <repcount> 2 bytes number of <rep> items, MSB first. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 149 | * <rep> : <repfromlen> <repfrom> <reptolen> <repto> |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 150 | * <repfromlen> 1 byte length of <repfrom> |
| 151 | * <repfrom> N bytes "from" part of replacement |
| 152 | * <reptolen> 1 byte length of <repto> |
| 153 | * <repto> N bytes "to" part of replacement |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 154 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 155 | * sectionID == SN_REPSAL: <repcount> <rep> ... |
| 156 | * just like SN_REP but for soundfolded words |
| 157 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 158 | * sectionID == SN_SAL: <salflags> <salcount> <sal> ... |
| 159 | * <salflags> 1 byte flags for soundsalike conversion: |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 160 | * SAL_F0LLOWUP |
| 161 | * SAL_COLLAPSE |
| 162 | * SAL_REM_ACCENTS |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 163 | * <salcount> 2 bytes number of <sal> items following |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 164 | * <sal> : <salfromlen> <salfrom> <saltolen> <salto> |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 165 | * <salfromlen> 1 byte length of <salfrom> |
| 166 | * <salfrom> N bytes "from" part of soundsalike |
| 167 | * <saltolen> 1 byte length of <salto> |
| 168 | * <salto> N bytes "to" part of soundsalike |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 169 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 170 | * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto> |
| 171 | * <sofofromlen> 2 bytes length of <sofofrom> |
| 172 | * <sofofrom> N bytes "from" part of soundfold |
| 173 | * <sofotolen> 2 bytes length of <sofoto> |
| 174 | * <sofoto> N bytes "to" part of soundfold |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 175 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 176 | * sectionID == SN_SUGFILE: <timestamp> |
| 177 | * <timestamp> 8 bytes time in seconds that must match with .sug file |
| 178 | * |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 179 | * sectionID == SN_NOSPLITSUGS: nothing |
| 180 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 181 | * sectionID == SN_WORDS: <word> ... |
| 182 | * <word> N bytes NUL terminated common word |
| 183 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 184 | * sectionID == SN_MAP: <mapstr> |
| 185 | * <mapstr> N bytes String with sequences of similar characters, |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 186 | * separated by slashes. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 187 | * |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 188 | * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions> |
| 189 | * <comppatcount> <comppattern> ... <compflags> |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 190 | * <compmax> 1 byte Maximum nr of words in compound word. |
| 191 | * <compminlen> 1 byte Minimal word length for compounding. |
| 192 | * <compsylmax> 1 byte Maximum nr of syllables in compound word. |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 193 | * <compoptions> 2 bytes COMP_ flags. |
| 194 | * <comppatcount> 2 bytes number of <comppattern> following |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 195 | * <compflags> N bytes Flags from COMPOUNDRULE items, separated by |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 196 | * slashes. |
| 197 | * |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 198 | * <comppattern>: <comppatlen> <comppattext> |
| 199 | * <comppatlen> 1 byte length of <comppattext> |
| 200 | * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN |
| 201 | * |
| 202 | * sectionID == SN_NOBREAK: (empty, its presence is what matters) |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 203 | * |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 204 | * sectionID == SN_SYLLABLE: <syllable> |
| 205 | * <syllable> N bytes String from SYLLABLE item. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 206 | * |
| 207 | * <LWORDTREE>: <wordtree> |
| 208 | * |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 209 | * <KWORDTREE>: <wordtree> |
| 210 | * |
| 211 | * <PREFIXTREE>: <wordtree> |
| 212 | * |
| 213 | * |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 214 | * <wordtree>: <nodecount> <nodedata> ... |
| 215 | * |
| 216 | * <nodecount> 4 bytes Number of nodes following. MSB first. |
| 217 | * |
| 218 | * <nodedata>: <siblingcount> <sibling> ... |
| 219 | * |
| 220 | * <siblingcount> 1 byte Number of siblings in this node. The siblings |
| 221 | * follow in sorted order. |
| 222 | * |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 223 | * <sibling>: <byte> [ <nodeidx> <xbyte> |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 224 | * | <flags> [<flags2>] [<region>] [<affixID>] |
| 225 | * | [<pflags>] <affixID> <prefcondnr> ] |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 226 | * |
| 227 | * <byte> 1 byte Byte value of the sibling. Special cases: |
| 228 | * BY_NOFLAGS: End of word without flags and for all |
| 229 | * regions. |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 230 | * For PREFIXTREE <affixID> and |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 231 | * <prefcondnr> follow. |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 232 | * BY_FLAGS: End of word, <flags> follow. |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 233 | * For PREFIXTREE <pflags>, <affixID> |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 234 | * and <prefcondnr> follow. |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 235 | * BY_FLAGS2: End of word, <flags> and <flags2> |
| 236 | * follow. Not used in PREFIXTREE. |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 237 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 238 | * and <xbyte> follow. |
| 239 | * |
| 240 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 241 | * |
| 242 | * <xbyte> 1 byte byte value of the sibling. |
| 243 | * |
| 244 | * <flags> 1 byte bitmask of: |
| 245 | * WF_ALLCAP word must have only capitals |
| 246 | * WF_ONECAP first char of word must be capital |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 247 | * WF_KEEPCAP keep-case word |
| 248 | * WF_FIXCAP keep-case word, all caps not allowed |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 249 | * WF_RARE rare word |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 250 | * WF_BANNED bad word |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 251 | * WF_REGION <region> follows |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 252 | * WF_AFX <affixID> follows |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 253 | * |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 254 | * <flags2> 1 byte Bitmask of: |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 255 | * WF_HAS_AFF >> 8 word includes affix |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 256 | * WF_NEEDCOMP >> 8 word only valid in compound |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 257 | * WF_NOSUGGEST >> 8 word not used for suggestions |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 258 | * WF_COMPROOT >> 8 word already a compound |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 259 | * WF_NOCOMPBEF >> 8 no compounding before this word |
| 260 | * WF_NOCOMPAFT >> 8 no compounding after this word |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 261 | * |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 262 | * <pflags> 1 byte bitmask of: |
| 263 | * WFP_RARE rare prefix |
| 264 | * WFP_NC non-combining prefix |
| 265 | * WFP_UP letter after prefix made upper case |
| 266 | * |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 267 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 268 | * omitted it's valid in all regions. |
| 269 | * Lowest bit is for region 1. |
| 270 | * |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 271 | * <affixID> 1 byte ID of affix that can be used with this word. In |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 272 | * PREFIXTREE used for the required prefix ID. |
| 273 | * |
| 274 | * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list |
| 275 | * from HEADER. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 276 | * |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 277 | * All text characters are in 'encoding', but stored as single bytes. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 278 | */ |
| 279 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 280 | /* |
| 281 | * Vim .sug file format: <SUGHEADER> |
| 282 | * <SUGWORDTREE> |
| 283 | * <SUGTABLE> |
| 284 | * |
| 285 | * <SUGHEADER>: <fileID> <versionnr> <timestamp> |
| 286 | * |
| 287 | * <fileID> 6 bytes "VIMsug" |
| 288 | * <versionnr> 1 byte VIMSUGVERSION |
| 289 | * <timestamp> 8 bytes timestamp that must match with .spl file |
| 290 | * |
| 291 | * |
| 292 | * <SUGWORDTREE>: <wordtree> (see above, no flags or region used) |
| 293 | * |
| 294 | * |
| 295 | * <SUGTABLE>: <sugwcount> <sugline> ... |
| 296 | * |
| 297 | * <sugwcount> 4 bytes number of <sugline> following |
| 298 | * |
| 299 | * <sugline>: <sugnr> ... NUL |
| 300 | * |
| 301 | * <sugnr>: X bytes word number that results in this soundfolded word, |
| 302 | * stored as an offset to the previous number in as |
| 303 | * few bytes as possible, see offset2bytes()) |
| 304 | */ |
| 305 | |
| Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 306 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 307 | # include "vimio.h" /* for lseek(), must be before vim.h */ |
| Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 308 | #endif |
| 309 | |
| 310 | #include "vim.h" |
| 311 | |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 312 | #if defined(FEAT_SPELL) || defined(PROTO) |
| Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 313 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 314 | #ifndef UNIX /* it's in os_unix.h for Unix */ |
| 315 | # include <time.h> /* for time_t */ |
| 316 | #endif |
| 317 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 318 | #define MAXWLEN 250 /* Assume max. word len is this many bytes. |
| 319 | Some places assume a word length fits in a |
| 320 | byte, thus it can't be above 255. */ |
| Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 321 | |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 322 | /* Type used for indexes in the word tree need to be at least 4 bytes. If int |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 323 | * is 8 bytes we could use something smaller, but what? */ |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 324 | #if SIZEOF_INT > 3 |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 325 | typedef int idx_T; |
| 326 | #else |
| 327 | typedef long idx_T; |
| 328 | #endif |
| 329 | |
| 330 | /* Flags used for a word. Only the lowest byte can be used, the region byte |
| 331 | * comes above it. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 332 | #define WF_REGION 0x01 /* region byte follows */ |
| 333 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 334 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 335 | #define WF_RARE 0x08 /* rare word */ |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 336 | #define WF_BANNED 0x10 /* bad word */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 337 | #define WF_AFX 0x20 /* affix ID follows */ |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 338 | #define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 339 | #define WF_KEEPCAP 0x80 /* keep-case word */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 340 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 341 | /* for <flags2>, shifted up one byte to be used in wn_flags */ |
| 342 | #define WF_HAS_AFF 0x0100 /* word includes affix */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 343 | #define WF_NEEDCOMP 0x0200 /* word only valid in compound */ |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 344 | #define WF_NOSUGGEST 0x0400 /* word not to be suggested */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 345 | #define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */ |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 346 | #define WF_NOCOMPBEF 0x1000 /* no compounding before this word */ |
| 347 | #define WF_NOCOMPAFT 0x2000 /* no compounding after this word */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 348 | |
| Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 349 | /* only used for su_badflags */ |
| 350 | #define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */ |
| 351 | |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 352 | #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 353 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 354 | /* flags for <pflags> */ |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 355 | #define WFP_RARE 0x01 /* rare prefix */ |
| 356 | #define WFP_NC 0x02 /* prefix is not combining */ |
| 357 | #define WFP_UP 0x04 /* to-upper prefix */ |
| 358 | #define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */ |
| 359 | #define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 360 | |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 361 | /* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one |
| 362 | * byte) and prefcondnr (two bytes). */ |
| 363 | #define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */ |
| 364 | #define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */ |
| 365 | #define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */ |
| 366 | #define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with |
| 367 | * COMPOUNDPERMITFLAG */ |
| 368 | #define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with |
| 369 | * COMPOUNDFORBIDFLAG */ |
| 370 | |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 371 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 372 | /* flags for <compoptions> */ |
| 373 | #define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */ |
| 374 | #define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */ |
| 375 | #define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */ |
| 376 | #define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */ |
| 377 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 378 | /* Special byte values for <byte>. Some are only used in the tree for |
| 379 | * postponed prefixes, some only in the other trees. This is a bit messy... */ |
| 380 | #define BY_NOFLAGS 0 /* end of word without flags or region; for |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 381 | * postponed prefix: no <pflags> */ |
| 382 | #define BY_INDEX 1 /* child is shared, index follows */ |
| 383 | #define BY_FLAGS 2 /* end of word, <flags> byte follows; for |
| 384 | * postponed prefix: <pflags> follows */ |
| 385 | #define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes |
| 386 | * follow; never used in prefix tree */ |
| 387 | #define BY_SPECIAL BY_FLAGS2 /* highest special byte value */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 388 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 389 | /* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep, |
| 390 | * si_repsal, sl_rep, and si_sal. Not for sl_sal! |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 391 | * One replacement: from "ft_from" to "ft_to". */ |
| 392 | typedef struct fromto_S |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 393 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 394 | char_u *ft_from; |
| 395 | char_u *ft_to; |
| 396 | } fromto_T; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 397 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 398 | /* Info from "SAL" entries in ".aff" file used in sl_sal. |
| 399 | * The info is split for quick processing by spell_soundfold(). |
| 400 | * Note that "sm_oneof" and "sm_rules" point into sm_lead. */ |
| 401 | typedef struct salitem_S |
| 402 | { |
| 403 | char_u *sm_lead; /* leading letters */ |
| 404 | int sm_leadlen; /* length of "sm_lead" */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 405 | char_u *sm_oneof; /* letters from () or NULL */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 406 | char_u *sm_rules; /* rules like ^, $, priority */ |
| 407 | char_u *sm_to; /* replacement. */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 408 | #ifdef FEAT_MBYTE |
| 409 | int *sm_lead_w; /* wide character copy of "sm_lead" */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 410 | int *sm_oneof_w; /* wide character copy of "sm_oneof" */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 411 | int *sm_to_w; /* wide character copy of "sm_to" */ |
| 412 | #endif |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 413 | } salitem_T; |
| 414 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 415 | #ifdef FEAT_MBYTE |
| 416 | typedef int salfirst_T; |
| 417 | #else |
| 418 | typedef short salfirst_T; |
| 419 | #endif |
| 420 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 421 | /* Values for SP_*ERROR are negative, positive values are used by |
| 422 | * read_cnt_string(). */ |
| 423 | #define SP_TRUNCERROR -1 /* spell file truncated error */ |
| 424 | #define SP_FORMERROR -2 /* format error in spell file */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 425 | #define SP_OTHERERROR -3 /* other error while reading spell file */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 426 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 427 | /* |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 428 | * Structure used to store words and other info for one language, loaded from |
| 429 | * a .spl file. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 430 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 431 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 432 | * |
| 433 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 434 | * the number of possible bytes, sorted on byte value: |
| 435 | * <len> <byte1> <byte2> ... |
| 436 | * The "idxs" array stores the index of the child node corresponding to the |
| 437 | * byte in "byts". |
| 438 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 439 | * the flags, region mask and affixID for the word. There may be several |
| 440 | * zeros in sequence for alternative flag/region/affixID combinations. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 441 | */ |
| 442 | typedef struct slang_S slang_T; |
| 443 | struct slang_S |
| 444 | { |
| 445 | slang_T *sl_next; /* next language */ |
| 446 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 447 | char_u *sl_fname; /* name of .spl file */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 448 | int sl_add; /* TRUE if it's a .add file. */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 449 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 450 | char_u *sl_fbyts; /* case-folded word bytes */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 451 | idx_T *sl_fidxs; /* case-folded word indexes */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 452 | char_u *sl_kbyts; /* keep-case word bytes */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 453 | idx_T *sl_kidxs; /* keep-case word indexes */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 454 | char_u *sl_pbyts; /* prefix tree word bytes */ |
| 455 | idx_T *sl_pidxs; /* prefix tree word indexes */ |
| 456 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 457 | char_u *sl_info; /* infotext string or NULL */ |
| 458 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 459 | 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] | 460 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 461 | char_u *sl_midword; /* MIDWORD string or NULL */ |
| 462 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 463 | hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */ |
| 464 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 465 | int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */ |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 466 | int sl_compminlen; /* COMPOUNDMIN (default: 0) */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 467 | int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 468 | int sl_compoptions; /* COMP_* flags */ |
| 469 | garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */ |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 470 | regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 471 | * (NULL when no compounding) */ |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 472 | char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 473 | char_u *sl_compstartflags; /* flags for first compound word */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 474 | char_u *sl_compallflags; /* all flags for compound words */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 475 | char_u sl_nobreak; /* When TRUE: no spaces between words */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 476 | char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */ |
| 477 | garray_T sl_syl_items; /* syllable items */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 478 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 479 | int sl_prefixcnt; /* number of items in "sl_prefprog" */ |
| 480 | regprog_T **sl_prefprog; /* table with regprogs for prefixes */ |
| 481 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 482 | garray_T sl_rep; /* list of fromto_T entries from REP lines */ |
| 483 | short sl_rep_first[256]; /* indexes where byte first appears, -1 if |
| 484 | there is none */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 485 | garray_T sl_sal; /* list of salitem_T entries from SAL lines */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 486 | salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 487 | there is none */ |
| 488 | int sl_followup; /* SAL followup */ |
| 489 | int sl_collapse; /* SAL collapse_result */ |
| 490 | int sl_rem_accents; /* SAL remove_accents */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 491 | int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items: |
| 492 | * "sl_sal_first" maps chars, when has_mbyte |
| 493 | * "sl_sal" is a list of wide char lists. */ |
| 494 | garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */ |
| 495 | short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */ |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 496 | int sl_nosplitsugs; /* don't suggest splitting a word */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 497 | |
| 498 | /* Info from the .sug file. Loaded on demand. */ |
| 499 | time_t sl_sugtime; /* timestamp for .sug file */ |
| 500 | char_u *sl_sbyts; /* soundfolded word bytes */ |
| 501 | idx_T *sl_sidxs; /* soundfolded word indexes */ |
| 502 | buf_T *sl_sugbuf; /* buffer with word number table */ |
| 503 | int sl_sugloaded; /* TRUE when .sug file was loaded or failed to |
| 504 | load */ |
| 505 | |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 506 | int sl_has_map; /* TRUE if there is a MAP line */ |
| 507 | #ifdef FEAT_MBYTE |
| 508 | hashtab_T sl_map_hash; /* MAP for multi-byte chars */ |
| 509 | int sl_map_array[256]; /* MAP for first 256 chars */ |
| 510 | #else |
| 511 | char_u sl_map_array[256]; /* MAP for first 256 chars */ |
| 512 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 513 | hashtab_T sl_sounddone; /* table with soundfolded words that have |
| 514 | handled, see add_sound_suggest() */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 517 | /* First language that is loaded, start of the linked list of loaded |
| 518 | * languages. */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 519 | static slang_T *first_lang = NULL; |
| 520 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 521 | /* Flags used in .spl file for soundsalike flags. */ |
| 522 | #define SAL_F0LLOWUP 1 |
| 523 | #define SAL_COLLAPSE 2 |
| 524 | #define SAL_REM_ACCENTS 4 |
| 525 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 526 | /* |
| 527 | * Structure used in "b_langp", filled from 'spelllang'. |
| 528 | */ |
| 529 | typedef struct langp_S |
| 530 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 531 | slang_T *lp_slang; /* info for this language */ |
| 532 | slang_T *lp_sallang; /* language used for sound folding or NULL */ |
| 533 | slang_T *lp_replang; /* language used for REP items or NULL */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 534 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 535 | } langp_T; |
| 536 | |
| 537 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 538 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 539 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 540 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 541 | #define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */ |
| 542 | #define VIMSPELLMAGICL 8 |
| 543 | #define VIMSPELLVERSION 50 |
| 544 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 545 | #define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */ |
| 546 | #define VIMSUGMAGICL 6 |
| 547 | #define VIMSUGVERSION 1 |
| 548 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 549 | /* Section IDs. Only renumber them when VIMSPELLVERSION changes! */ |
| 550 | #define SN_REGION 0 /* <regionname> section */ |
| 551 | #define SN_CHARFLAGS 1 /* charflags section */ |
| 552 | #define SN_MIDWORD 2 /* <midword> section */ |
| 553 | #define SN_PREFCOND 3 /* <prefcond> section */ |
| 554 | #define SN_REP 4 /* REP items section */ |
| 555 | #define SN_SAL 5 /* SAL items section */ |
| 556 | #define SN_SOFO 6 /* soundfolding section */ |
| 557 | #define SN_MAP 7 /* MAP items section */ |
| 558 | #define SN_COMPOUND 8 /* compound words section */ |
| 559 | #define SN_SYLLABLE 9 /* syllable section */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 560 | #define SN_NOBREAK 10 /* NOBREAK section */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 561 | #define SN_SUGFILE 11 /* timestamp for .sug file */ |
| 562 | #define SN_REPSAL 12 /* REPSAL items section */ |
| 563 | #define SN_WORDS 13 /* common words */ |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 564 | #define SN_NOSPLITSUGS 14 /* don't split word for suggestions */ |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 565 | #define SN_INFO 15 /* info section */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 566 | #define SN_END 255 /* end of sections */ |
| 567 | |
| 568 | #define SNF_REQUIRED 1 /* <sectionflags>: required section */ |
| 569 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 570 | /* Result values. Lower number is accepted over higher one. */ |
| 571 | #define SP_BANNED -1 |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 572 | #define SP_OK 0 |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 573 | #define SP_RARE 1 |
| 574 | #define SP_LOCAL 2 |
| 575 | #define SP_BAD 3 |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 576 | |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 577 | /* file used for "zG" and "zW" */ |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 578 | static char_u *int_wordlist = NULL; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 579 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 580 | typedef struct wordcount_S |
| 581 | { |
| 582 | short_u wc_count; /* nr of times word was seen */ |
| 583 | char_u wc_word[1]; /* word, actually longer */ |
| 584 | } wordcount_T; |
| 585 | |
| 586 | static wordcount_T dumwc; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 587 | #define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 588 | #define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF)) |
| 589 | #define MAXWORDCOUNT 0xffff |
| 590 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 591 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 592 | * Information used when looking for suggestions. |
| 593 | */ |
| 594 | typedef struct suginfo_S |
| 595 | { |
| 596 | garray_T su_ga; /* suggestions, contains "suggest_T" */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 597 | int su_maxcount; /* max. number of suggestions displayed */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 598 | int su_maxscore; /* maximum score for adding to su_ga */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 599 | int su_sfmaxscore; /* idem, for when doing soundfold words */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 600 | garray_T su_sga; /* like su_ga, sound-folded scoring */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 601 | char_u *su_badptr; /* start of bad word in line */ |
| 602 | int su_badlen; /* length of detected bad word in line */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 603 | int su_badflags; /* caps flags for bad word */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 604 | char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
| 605 | char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 606 | char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 607 | hashtab_T su_banned; /* table with banned words */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 608 | slang_T *su_sallang; /* default language for sound folding */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 609 | } suginfo_T; |
| 610 | |
| 611 | /* One word suggestion. Used in "si_ga". */ |
| 612 | typedef struct suggest_S |
| 613 | { |
| 614 | char_u *st_word; /* suggested word, allocated string */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 615 | int st_wordlen; /* STRLEN(st_word) */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 616 | int st_orglen; /* length of replaced text */ |
| 617 | int st_score; /* lower is better */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 618 | int st_altscore; /* used when st_score compares equal */ |
| 619 | int st_salscore; /* st_score is for soundalike */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 620 | int st_had_bonus; /* bonus already included in score */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 621 | slang_T *st_slang; /* language used for sound folding */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 622 | } suggest_T; |
| 623 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 624 | #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 625 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 626 | /* TRUE if a word appears in the list of banned words. */ |
| 627 | #define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word))) |
| 628 | |
| Bram Moolenaar | 6949d1d | 2008-08-25 02:14:05 +0000 | [diff] [blame] | 629 | /* Number of suggestions kept when cleaning up. We need to keep more than |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 630 | * what is displayed, because when rescore_suggestions() is called the score |
| 631 | * may change and wrong suggestions may be removed later. */ |
| 632 | #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 633 | |
| 634 | /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots |
| 635 | * of suggestions that are not going to be displayed. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 636 | #define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 637 | |
| 638 | /* score for various changes */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 639 | #define SCORE_SPLIT 149 /* split bad word */ |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 640 | #define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 641 | #define SCORE_ICASE 52 /* slightly different case */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 642 | #define SCORE_REGION 200 /* word is for different region */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 643 | #define SCORE_RARE 180 /* rare word */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 644 | #define SCORE_SWAP 75 /* swap two characters */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 645 | #define SCORE_SWAP3 110 /* swap two characters in three */ |
| Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 646 | #define SCORE_REP 65 /* REP replacement */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 647 | #define SCORE_SUBST 93 /* substitute a character */ |
| 648 | #define SCORE_SIMILAR 33 /* substitute a similar character */ |
| Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 649 | #define SCORE_SUBCOMP 33 /* substitute a composing character */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 650 | #define SCORE_DEL 94 /* delete a character */ |
| Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 651 | #define SCORE_DELDUP 66 /* delete a duplicated character */ |
| Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 652 | #define SCORE_DELCOMP 28 /* delete a composing character */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 653 | #define SCORE_INS 96 /* insert a character */ |
| Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 654 | #define SCORE_INSDUP 67 /* insert a duplicate character */ |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 655 | #define SCORE_INSCOMP 30 /* insert a composing character */ |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 656 | #define SCORE_NONWORD 103 /* change non-word to word char */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 657 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 658 | #define SCORE_FILE 30 /* suggestion from a file */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 659 | #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
| 660 | * 350 allows for about three changes. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 661 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 662 | #define SCORE_COMMON1 30 /* subtracted for words seen before */ |
| 663 | #define SCORE_COMMON2 40 /* subtracted for words often seen */ |
| 664 | #define SCORE_COMMON3 50 /* subtracted for words very often seen */ |
| 665 | #define SCORE_THRES2 10 /* word count threshold for COMMON2 */ |
| 666 | #define SCORE_THRES3 100 /* word count threshold for COMMON3 */ |
| 667 | |
| 668 | /* When trying changed soundfold words it becomes slow when trying more than |
| 669 | * two changes. With less then two changes it's slightly faster but we miss a |
| 670 | * few good suggestions. In rare cases we need to try three of four changes. |
| 671 | */ |
| 672 | #define SCORE_SFMAX1 200 /* maximum score for first try */ |
| 673 | #define SCORE_SFMAX2 300 /* maximum score for second try */ |
| 674 | #define SCORE_SFMAX3 400 /* maximum score for third try */ |
| 675 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 676 | #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 677 | #define SCORE_MAXMAX 999999 /* accept any score */ |
| 678 | #define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */ |
| 679 | |
| 680 | /* for spell_edit_score_limit() we need to know the minimum value of |
| 681 | * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */ |
| 682 | #define SCORE_EDIT_MIN SCORE_SIMILAR |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 683 | |
| 684 | /* |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 685 | * Structure to store info for word matching. |
| 686 | */ |
| 687 | typedef struct matchinf_S |
| 688 | { |
| 689 | langp_T *mi_lp; /* info for language and region */ |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 690 | |
| 691 | /* pointers to original text to be checked */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 692 | char_u *mi_word; /* start of word being checked */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 693 | char_u *mi_end; /* end of matching word so far */ |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 694 | char_u *mi_fend; /* next char to be added to mi_fword */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 695 | char_u *mi_cend; /* char after what was used for |
| 696 | mi_capflags */ |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 697 | |
| 698 | /* case-folded text */ |
| 699 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 700 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 701 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 702 | /* for when checking word after a prefix */ |
| 703 | int mi_prefarridx; /* index in sl_pidxs with list of |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 704 | affixID/condition */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 705 | int mi_prefcnt; /* number of entries at mi_prefarridx */ |
| 706 | int mi_prefixlen; /* byte length of prefix */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 707 | #ifdef FEAT_MBYTE |
| 708 | int mi_cprefixlen; /* byte length of prefix in original |
| 709 | case */ |
| 710 | #else |
| 711 | # define mi_cprefixlen mi_prefixlen /* it's the same value */ |
| 712 | #endif |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 713 | |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 714 | /* for when checking a compound word */ |
| 715 | int mi_compoff; /* start of following word offset */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 716 | char_u mi_compflags[MAXWLEN]; /* flags for compound words used */ |
| 717 | int mi_complen; /* nr of compound words used */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 718 | int mi_compextra; /* nr of COMPOUNDROOT words */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 719 | |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 720 | /* others */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 721 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 722 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 723 | buf_T *mi_buf; /* buffer being checked */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 724 | |
| 725 | /* for NOBREAK */ |
| 726 | int mi_result2; /* "mi_resul" without following word */ |
| 727 | char_u *mi_end2; /* "mi_end" without following word */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 728 | } matchinf_T; |
| 729 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 730 | /* |
| 731 | * The tables used for recognizing word characters according to spelling. |
| 732 | * These are only used for the first 256 characters of 'encoding'. |
| 733 | */ |
| 734 | typedef struct spelltab_S |
| 735 | { |
| 736 | char_u st_isw[256]; /* flags: is word char */ |
| 737 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 738 | char_u st_fold[256]; /* chars: folded case */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 739 | char_u st_upper[256]; /* chars: upper case */ |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 740 | } spelltab_T; |
| 741 | |
| 742 | static spelltab_T spelltab; |
| 743 | static int did_set_spelltab; |
| 744 | |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 745 | #define CF_WORD 0x01 |
| 746 | #define CF_UPPER 0x02 |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 747 | |
| 748 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 749 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 750 | static int spell_iswordp __ARGS((char_u *p, buf_T *buf)); |
| 751 | static int spell_iswordp_nmw __ARGS((char_u *p)); |
| 752 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 7a91a4a | 2008-04-09 13:49:57 +0000 | [diff] [blame] | 753 | static int spell_mb_isword_class __ARGS((int cl)); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 754 | static int spell_iswordp_w __ARGS((int *p, buf_T *buf)); |
| 755 | #endif |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 756 | static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap)); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 757 | |
| 758 | /* |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 759 | * 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] | 760 | */ |
| 761 | typedef enum |
| 762 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 763 | STATE_START = 0, /* At start of node check for NUL bytes (goodword |
| 764 | * ends); if badword ends there is a match, otherwise |
| 765 | * try splitting word. */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 766 | STATE_NOPREFIX, /* try without prefix */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 767 | STATE_SPLITUNDO, /* Undo splitting. */ |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 768 | STATE_ENDNUL, /* Past NUL bytes at start of the node. */ |
| 769 | STATE_PLAIN, /* Use each byte of the node. */ |
| 770 | STATE_DEL, /* Delete a byte from the bad word. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 771 | STATE_INS_PREP, /* Prepare for inserting bytes. */ |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 772 | STATE_INS, /* Insert a byte in the bad word. */ |
| 773 | STATE_SWAP, /* Swap two bytes. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 774 | STATE_UNSWAP, /* Undo swap two characters. */ |
| 775 | STATE_SWAP3, /* Swap two characters over three. */ |
| 776 | STATE_UNSWAP3, /* Undo Swap two characters over three. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 777 | STATE_UNROT3L, /* Undo rotate three characters left */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 778 | STATE_UNROT3R, /* Undo rotate three characters right */ |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 779 | STATE_REP_INI, /* Prepare for using REP items. */ |
| 780 | STATE_REP, /* Use matching REP items from the .aff file. */ |
| 781 | STATE_REP_UNDO, /* Undo a REP item replacement. */ |
| 782 | STATE_FINAL /* End of this node. */ |
| 783 | } state_T; |
| 784 | |
| 785 | /* |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 786 | * Struct to keep the state at each level in suggest_try_change(). |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 787 | */ |
| 788 | typedef struct trystate_S |
| 789 | { |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 790 | state_T ts_state; /* state at this level, STATE_ */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 791 | int ts_score; /* score */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 792 | idx_T ts_arridx; /* index in tree array, start of node */ |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 793 | short ts_curi; /* index in list of child nodes */ |
| 794 | char_u ts_fidx; /* index in fword[], case-folded bad word */ |
| 795 | char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */ |
| 796 | char_u ts_twordlen; /* valid length of tword[] */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 797 | char_u ts_prefixdepth; /* stack depth for end of prefix or |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 798 | * PFD_PREFIXTREE or PFD_NOPREFIX */ |
| 799 | char_u ts_flags; /* TSF_ flags */ |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 800 | #ifdef FEAT_MBYTE |
| 801 | char_u ts_tcharlen; /* number of bytes in tword character */ |
| 802 | char_u ts_tcharidx; /* current byte index in tword character */ |
| 803 | char_u ts_isdiff; /* DIFF_ values */ |
| 804 | char_u ts_fcharstart; /* index in fword where badword char started */ |
| 805 | #endif |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 806 | char_u ts_prewordlen; /* length of word in "preword[]" */ |
| 807 | char_u ts_splitoff; /* index in "tword" after last split */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 808 | char_u ts_splitfidx; /* "ts_fidx" at word split */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 809 | char_u ts_complen; /* nr of compound words used */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 810 | char_u ts_compsplit; /* index for "compflags" where word was spit */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 811 | char_u ts_save_badflags; /* su_badflags saved here */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 812 | char_u ts_delidx; /* index in fword for char that was deleted, |
| 813 | valid when "ts_flags" has TSF_DIDDEL */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 814 | } trystate_T; |
| 815 | |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 816 | /* values for ts_isdiff */ |
| 817 | #define DIFF_NONE 0 /* no different byte (yet) */ |
| 818 | #define DIFF_YES 1 /* different byte found */ |
| 819 | #define DIFF_INSERT 2 /* inserting character */ |
| 820 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 821 | /* values for ts_flags */ |
| 822 | #define TSF_PREFIXOK 1 /* already checked that prefix is OK */ |
| 823 | #define TSF_DIDSPLIT 2 /* tried split at this point */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 824 | #define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 825 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 826 | /* special values ts_prefixdepth */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 827 | #define PFD_NOPREFIX 0xff /* not using prefixes */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 828 | #define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 829 | #define PFD_NOTSPECIAL 0xfd /* highest value that's not special */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 830 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 831 | /* mode values for find_word */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 832 | #define FIND_FOLDWORD 0 /* find word case-folded */ |
| 833 | #define FIND_KEEPWORD 1 /* find keep-case word */ |
| 834 | #define FIND_PREFIX 2 /* find word after prefix */ |
| 835 | #define FIND_COMPOUND 3 /* find case-folded compound word */ |
| 836 | #define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 837 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 838 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 839 | static void slang_free __ARGS((slang_T *lp)); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 840 | static void slang_clear __ARGS((slang_T *lp)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 841 | static void slang_clear_sug __ARGS((slang_T *lp)); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 842 | static void find_word __ARGS((matchinf_T *mip, int mode)); |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 843 | static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 844 | static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags)); |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 845 | static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag)); |
| 846 | static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags)); |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 847 | static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req)); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 848 | static void find_prefix __ARGS((matchinf_T *mip, int mode)); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 849 | static int fold_more __ARGS((matchinf_T *mip)); |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 850 | static int spell_valid_case __ARGS((int wordflags, int treeflags)); |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 851 | static int no_spell_checking __ARGS((win_T *wp)); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 852 | static void spell_load_lang __ARGS((char_u *lang)); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 853 | static char_u *spell_enc __ARGS((void)); |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 854 | static void int_wordlist_spl __ARGS((char_u *fname)); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 855 | static void spell_load_cb __ARGS((char_u *fname, void *cookie)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 856 | static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent)); |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 857 | static int get2c __ARGS((FILE *fd)); |
| 858 | static int get3c __ARGS((FILE *fd)); |
| 859 | static int get4c __ARGS((FILE *fd)); |
| 860 | static time_t get8c __ARGS((FILE *fd)); |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 861 | static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 862 | static char_u *read_string __ARGS((FILE *fd, int cnt)); |
| 863 | static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len)); |
| 864 | static int read_charflags_section __ARGS((FILE *fd)); |
| 865 | static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 866 | static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 867 | static int read_sal_section __ARGS((FILE *fd, slang_T *slang)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 868 | static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len)); |
| 869 | static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count)); |
| 870 | static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 871 | static int read_sofo_section __ARGS((FILE *fd, slang_T *slang)); |
| 872 | static int read_compound __ARGS((FILE *fd, slang_T *slang, int len)); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 873 | static int byte_in_str __ARGS((char_u *str, int byte)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 874 | static int init_syl_tab __ARGS((slang_T *slang)); |
| 875 | static int count_syllables __ARGS((slang_T *slang, char_u *word)); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 876 | static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to)); |
| 877 | static void set_sal_first __ARGS((slang_T *lp)); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 878 | #ifdef FEAT_MBYTE |
| 879 | static int *mb_str2wide __ARGS((char_u *s)); |
| 880 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 881 | static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt)); |
| 882 | static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr)); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 883 | static void clear_midword __ARGS((buf_T *buf)); |
| 884 | static void use_midword __ARGS((slang_T *lp, buf_T *buf)); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 885 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 886 | static int captype __ARGS((char_u *word, char_u *end)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 887 | static int badword_captype __ARGS((char_u *word, char_u *end)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 888 | static void spell_reload_one __ARGS((char_u *fname, int added_word)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 889 | static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp)); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 890 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 891 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 892 | static int check_need_cap __ARGS((linenr_T lnum, colnr_T col)); |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 893 | static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive)); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 894 | #ifdef FEAT_EVAL |
| 895 | static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr)); |
| 896 | #endif |
| 897 | static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 898 | static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive)); |
| 899 | static void suggest_load_files __ARGS((void)); |
| 900 | static void tree_count_words __ARGS((char_u *byts, idx_T *idxs)); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 901 | static void spell_find_cleanup __ARGS((suginfo_T *su)); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 902 | 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] | 903 | static void allcap_copy __ARGS((char_u *word, char_u *wcopy)); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 904 | static void suggest_try_special __ARGS((suginfo_T *su)); |
| 905 | static void suggest_try_change __ARGS((suginfo_T *su)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 906 | static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold)); |
| 907 | static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add)); |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 908 | #ifdef FEAT_MBYTE |
| 909 | static int nofold_len __ARGS((char_u *fword, int flen, char_u *word)); |
| 910 | #endif |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 911 | 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] | 912 | static void score_comp_sal __ARGS((suginfo_T *su)); |
| 913 | static void score_combine __ARGS((suginfo_T *su)); |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 914 | static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 915 | static void suggest_try_soundalike_prep __ARGS((void)); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 916 | static void suggest_try_soundalike __ARGS((suginfo_T *su)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 917 | static void suggest_try_soundalike_finish __ARGS((void)); |
| 918 | static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp)); |
| 919 | static int soundfold_find __ARGS((slang_T *slang, char_u *word)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 920 | 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] | 921 | static void set_map_str __ARGS((slang_T *lp, char_u *map)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 922 | static int similar_chars __ARGS((slang_T *slang, int c1, int c2)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 923 | static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf)); |
| 924 | static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 925 | static void add_banned __ARGS((suginfo_T *su, char_u *word)); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 926 | static void rescore_suggestions __ARGS((suginfo_T *su)); |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 927 | static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp)); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 928 | static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep)); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 929 | static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res)); |
| 930 | static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| 931 | static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 932 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 933 | static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res)); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 934 | #endif |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 935 | static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 936 | static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword)); |
| 937 | static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit)); |
| 938 | #ifdef FEAT_MBYTE |
| 939 | static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit)); |
| 940 | #endif |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 941 | static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum)); |
| 942 | static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 943 | static buf_T *open_spellbuf __ARGS((void)); |
| 944 | static void close_spellbuf __ARGS((buf_T *buf)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 945 | |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 946 | /* |
| 947 | * Use our own character-case definitions, because the current locale may |
| 948 | * differ from what the .spl file uses. |
| 949 | * These must not be called with negative number! |
| 950 | */ |
| 951 | #ifndef FEAT_MBYTE |
| 952 | /* Non-multi-byte implementation. */ |
| 953 | # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c)) |
| 954 | # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c)) |
| 955 | # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE) |
| 956 | #else |
| Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 957 | # if defined(HAVE_WCHAR_H) |
| 958 | # include <wchar.h> /* for towupper() and towlower() */ |
| 959 | # endif |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 960 | /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do |
| 961 | * that for ASCII, because we don't want to use 'casemap' here. Otherwise use |
| 962 | * the "w" library function for characters above 255 if available. */ |
| 963 | # ifdef HAVE_TOWLOWER |
| 964 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 965 | : (c) < 256 ? spelltab.st_fold[c] : towlower(c)) |
| 966 | # else |
| 967 | # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \ |
| 968 | : (c) < 256 ? spelltab.st_fold[c] : (c)) |
| 969 | # endif |
| 970 | |
| 971 | # ifdef HAVE_TOWUPPER |
| 972 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 973 | : (c) < 256 ? spelltab.st_upper[c] : towupper(c)) |
| 974 | # else |
| 975 | # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \ |
| 976 | : (c) < 256 ? spelltab.st_upper[c] : (c)) |
| 977 | # endif |
| 978 | |
| 979 | # ifdef HAVE_ISWUPPER |
| 980 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| 981 | : (c) < 256 ? spelltab.st_isu[c] : iswupper(c)) |
| 982 | # else |
| 983 | # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 984 | : (c) < 256 ? spelltab.st_isu[c] : (FALSE)) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 985 | # endif |
| 986 | #endif |
| 987 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 988 | |
| 989 | static char *e_format = N_("E759: Format error in spell file"); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 990 | static char *e_spell_trunc = N_("E758: Truncated spell file"); |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 991 | static char *e_afftrailing = N_("Trailing text in %s line %d: %s"); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 992 | static char *e_affname = N_("Affix name too long in %s line %d: %s"); |
| 993 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 994 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 995 | static char *msg_compressing = N_("Compressing word tree..."); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 996 | |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 997 | /* Remember what "z?" replaced. */ |
| 998 | static char_u *repl_from = NULL; |
| 999 | static char_u *repl_to = NULL; |
| 1000 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1001 | /* |
| 1002 | * Main spell-checking function. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1003 | * "ptr" points to a character that could be the start of a word. |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1004 | * "*attrp" is set to the highlight index for a badly spelled word. For a |
| 1005 | * non-word or when it's OK it remains unchanged. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1006 | * This must only be called when 'spelllang' is not empty. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1007 | * |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1008 | * "capcol" is used to check for a Capitalised word after the end of a |
| 1009 | * sentence. If it's zero then perform the check. Return the column where to |
| 1010 | * check next, or -1 when no sentence end was found. If it's NULL then don't |
| 1011 | * worry. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1012 | * |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1013 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 1014 | * caller can skip over the word. |
| 1015 | */ |
| 1016 | int |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1017 | spell_check(wp, ptr, attrp, capcol, docount) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1018 | win_T *wp; /* current window */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1019 | char_u *ptr; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1020 | hlf_T *attrp; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1021 | int *capcol; /* column to check for Capital */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1022 | int docount; /* count good words */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1023 | { |
| 1024 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 1025 | be passed to functions quickly. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1026 | int nrlen = 0; /* found a number first */ |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1027 | int c; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1028 | int wrongcaplen = 0; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1029 | int lpi; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1030 | int count_word = docount; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1031 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1032 | /* A word never starts at a space or a control character. Return quickly |
| 1033 | * then, skipping over the character. */ |
| 1034 | if (*ptr <= ' ') |
| 1035 | return 1; |
| Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 1036 | |
| 1037 | /* Return here when loading language files failed. */ |
| 1038 | if (wp->w_buffer->b_langp.ga_len == 0) |
| 1039 | return 1; |
| 1040 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1041 | vim_memset(&mi, 0, sizeof(matchinf_T)); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1042 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1043 | /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
| Bram Moolenaar | 43abc52 | 2005-12-10 20:15:02 +0000 | [diff] [blame] | 1044 | * 0X99FF. But always do check spelling to find "3GPP" and "11 |
| 1045 | * julifeest". */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1046 | if (*ptr >= '0' && *ptr <= '9') |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1047 | { |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 1048 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
| 1049 | mi.mi_end = skiphex(ptr + 2); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1050 | else |
| 1051 | mi.mi_end = skipdigits(ptr); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1052 | nrlen = (int)(mi.mi_end - ptr); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1053 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1054 | |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1055 | /* 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] | 1056 | mi.mi_word = ptr; |
| Bram Moolenaar | 43abc52 | 2005-12-10 20:15:02 +0000 | [diff] [blame] | 1057 | mi.mi_fend = ptr; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1058 | if (spell_iswordp(mi.mi_fend, wp->w_buffer)) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1059 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1060 | do |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1061 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1062 | mb_ptr_adv(mi.mi_fend); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1063 | } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer)); |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL) |
| 1066 | { |
| 1067 | /* Check word starting with capital letter. */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1068 | c = PTR2CHAR(ptr); |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1069 | if (!SPELL_ISUPPER(c)) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1070 | wrongcaplen = (int)(mi.mi_fend - ptr); |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1071 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1072 | } |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1073 | if (capcol != NULL) |
| 1074 | *capcol = -1; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1075 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1076 | /* We always use the characters up to the next non-word character, |
| 1077 | * also for bad words. */ |
| 1078 | mi.mi_end = mi.mi_fend; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1079 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1080 | /* Check caps type later. */ |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1081 | mi.mi_buf = wp->w_buffer; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1082 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1083 | /* case-fold the word with one non-word character, so that we can check |
| 1084 | * for the word end. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1085 | if (*mi.mi_fend != NUL) |
| 1086 | mb_ptr_adv(mi.mi_fend); |
| 1087 | |
| 1088 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 1089 | MAXWLEN + 1); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1090 | mi.mi_fwordlen = (int)STRLEN(mi.mi_fword); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1091 | |
| 1092 | /* The word is bad unless we recognize it. */ |
| 1093 | mi.mi_result = SP_BAD; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1094 | mi.mi_result2 = SP_BAD; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1095 | |
| 1096 | /* |
| 1097 | * Loop over the languages specified in 'spelllang'. |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1098 | * We check them all, because a word may be matched longer in another |
| 1099 | * language. |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1100 | */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1101 | for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1102 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1103 | mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi); |
| 1104 | |
| 1105 | /* If reloading fails the language is still in the list but everything |
| 1106 | * has been cleared. */ |
| 1107 | if (mi.mi_lp->lp_slang->sl_fidxs == NULL) |
| 1108 | continue; |
| 1109 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1110 | /* Check for a matching word in case-folded words. */ |
| 1111 | find_word(&mi, FIND_FOLDWORD); |
| 1112 | |
| 1113 | /* Check for a matching word in keep-case words. */ |
| 1114 | find_word(&mi, FIND_KEEPWORD); |
| 1115 | |
| 1116 | /* Check for matching prefixes. */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1117 | find_prefix(&mi, FIND_FOLDWORD); |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1118 | |
| 1119 | /* For a NOBREAK language, may want to use a word without a following |
| 1120 | * word as a backup. */ |
| 1121 | if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD |
| 1122 | && mi.mi_result2 != SP_BAD) |
| 1123 | { |
| 1124 | mi.mi_result = mi.mi_result2; |
| 1125 | mi.mi_end = mi.mi_end2; |
| 1126 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* Count the word in the first language where it's found to be OK. */ |
| 1129 | if (count_word && mi.mi_result == SP_OK) |
| 1130 | { |
| 1131 | count_common_word(mi.mi_lp->lp_slang, ptr, |
| 1132 | (int)(mi.mi_end - ptr), 1); |
| 1133 | count_word = FALSE; |
| 1134 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | if (mi.mi_result != SP_OK) |
| 1138 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1139 | /* If we found a number skip over it. Allows for "42nd". Do flag |
| 1140 | * rare and local words, e.g., "3GPP". */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1141 | if (nrlen > 0) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1142 | { |
| 1143 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| 1144 | return nrlen; |
| 1145 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* When we are at a non-word character there is no error, just |
| 1148 | * skip over the character (try looking for a word after it). */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1149 | else if (!spell_iswordp_nmw(ptr)) |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1150 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 1151 | if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL) |
| 1152 | { |
| 1153 | regmatch_T regmatch; |
| 1154 | |
| 1155 | /* Check for end of sentence. */ |
| 1156 | regmatch.regprog = wp->w_buffer->b_cap_prog; |
| 1157 | regmatch.rm_ic = FALSE; |
| 1158 | if (vim_regexec(®match, ptr, 0)) |
| 1159 | *capcol = (int)(regmatch.endp[0] - ptr); |
| 1160 | } |
| 1161 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1162 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1163 | if (has_mbyte) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1164 | return (*mb_ptr2len)(ptr); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1165 | #endif |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1166 | return 1; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1167 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1168 | else if (mi.mi_end == ptr) |
| 1169 | /* Always include at least one character. Required for when there |
| 1170 | * is a mixup in "midword". */ |
| 1171 | mb_ptr_adv(mi.mi_end); |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1172 | else if (mi.mi_result == SP_BAD |
| 1173 | && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak) |
| 1174 | { |
| 1175 | char_u *p, *fp; |
| 1176 | int save_result = mi.mi_result; |
| 1177 | |
| 1178 | /* First language in 'spelllang' is NOBREAK. Find first position |
| 1179 | * at which any word would be valid. */ |
| 1180 | mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1181 | if (mi.mi_lp->lp_slang->sl_fidxs != NULL) |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1182 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1183 | p = mi.mi_word; |
| 1184 | fp = mi.mi_fword; |
| 1185 | for (;;) |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1186 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1187 | mb_ptr_adv(p); |
| 1188 | mb_ptr_adv(fp); |
| 1189 | if (p >= mi.mi_end) |
| 1190 | break; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1191 | mi.mi_compoff = (int)(fp - mi.mi_fword); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1192 | find_word(&mi, FIND_COMPOUND); |
| 1193 | if (mi.mi_result != SP_BAD) |
| 1194 | { |
| 1195 | mi.mi_end = p; |
| 1196 | break; |
| 1197 | } |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1198 | } |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1199 | mi.mi_result = save_result; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1200 | } |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1201 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1204 | *attrp = HLF_SPB; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1205 | else if (mi.mi_result == SP_RARE) |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1206 | *attrp = HLF_SPR; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1207 | else |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1208 | *attrp = HLF_SPL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1211 | if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE)) |
| 1212 | { |
| 1213 | /* Report SpellCap only when the word isn't badly spelled. */ |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1214 | *attrp = HLF_SPC; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1215 | return wrongcaplen; |
| 1216 | } |
| 1217 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1218 | return (int)(mi.mi_end - ptr); |
| 1219 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1220 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1221 | /* |
| 1222 | * Check if the word at "mip->mi_word" is in the tree. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1223 | * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
| 1224 | * When "mode" is FIND_KEEPWORD check in keep-case word tree. |
| 1225 | * When "mode" is FIND_PREFIX check for word after prefix in fold-case word |
| 1226 | * tree. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1227 | * |
| 1228 | * For a match mip->mi_result is updated. |
| 1229 | */ |
| 1230 | static void |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1231 | find_word(mip, mode) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1232 | matchinf_T *mip; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1233 | int mode; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1234 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1235 | idx_T arridx = 0; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1236 | int endlen[MAXWLEN]; /* length at possible word endings */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1237 | idx_T endidx[MAXWLEN]; /* possible word endings */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1238 | int endidxcnt = 0; |
| 1239 | int len; |
| 1240 | int wlen = 0; |
| 1241 | int flen; |
| 1242 | int c; |
| 1243 | char_u *ptr; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1244 | idx_T lo, hi, m; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1245 | #ifdef FEAT_MBYTE |
| 1246 | char_u *s; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1247 | #endif |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 1248 | char_u *p; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1249 | int res = SP_BAD; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1250 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1251 | unsigned flags; |
| 1252 | char_u *byts; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1253 | idx_T *idxs; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1254 | int word_ends; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1255 | int prefix_found; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1256 | int nobreak_result; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1257 | |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1258 | if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1259 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1260 | /* Check for word with matching case in keep-case tree. */ |
| 1261 | ptr = mip->mi_word; |
| 1262 | flen = 9999; /* no case folding, always enough bytes */ |
| 1263 | byts = slang->sl_kbyts; |
| 1264 | idxs = slang->sl_kidxs; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (mode == FIND_KEEPCOMPOUND) |
| 1267 | /* Skip over the previously found word(s). */ |
| 1268 | wlen += mip->mi_compoff; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1269 | } |
| 1270 | else |
| 1271 | { |
| 1272 | /* Check for case-folded in case-folded tree. */ |
| 1273 | ptr = mip->mi_fword; |
| 1274 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 1275 | byts = slang->sl_fbyts; |
| 1276 | idxs = slang->sl_fidxs; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1277 | |
| 1278 | if (mode == FIND_PREFIX) |
| 1279 | { |
| 1280 | /* Skip over the prefix. */ |
| 1281 | wlen = mip->mi_prefixlen; |
| 1282 | flen -= mip->mi_prefixlen; |
| 1283 | } |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1284 | else if (mode == FIND_COMPOUND) |
| 1285 | { |
| 1286 | /* Skip over the previously found word(s). */ |
| 1287 | wlen = mip->mi_compoff; |
| 1288 | flen -= mip->mi_compoff; |
| 1289 | } |
| 1290 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1293 | if (byts == NULL) |
| 1294 | return; /* array is empty */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1295 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1296 | /* |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1297 | * Repeat advancing in the tree until: |
| 1298 | * - there is a byte that doesn't match, |
| 1299 | * - we reach the end of the tree, |
| 1300 | * - or we reach the end of the line. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1301 | */ |
| 1302 | for (;;) |
| 1303 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1304 | if (flen <= 0 && *mip->mi_fend != NUL) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1305 | flen = fold_more(mip); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1306 | |
| 1307 | len = byts[arridx++]; |
| 1308 | |
| 1309 | /* If the first possible byte is a zero the word could end here. |
| 1310 | * Remember this index, we first check for the longest word. */ |
| 1311 | if (byts[arridx] == 0) |
| 1312 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1313 | if (endidxcnt == MAXWLEN) |
| 1314 | { |
| 1315 | /* Must be a corrupted spell file. */ |
| 1316 | EMSG(_(e_format)); |
| 1317 | return; |
| 1318 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1319 | endlen[endidxcnt] = wlen; |
| 1320 | endidx[endidxcnt++] = arridx++; |
| 1321 | --len; |
| 1322 | |
| 1323 | /* Skip over the zeros, there can be several flag/region |
| 1324 | * combinations. */ |
| 1325 | while (len > 0 && byts[arridx] == 0) |
| 1326 | { |
| 1327 | ++arridx; |
| 1328 | --len; |
| 1329 | } |
| 1330 | if (len == 0) |
| 1331 | break; /* no children, word must end here */ |
| 1332 | } |
| 1333 | |
| 1334 | /* Stop looking at end of the line. */ |
| 1335 | if (ptr[wlen] == NUL) |
| 1336 | break; |
| 1337 | |
| 1338 | /* Perform a binary search in the list of accepted bytes. */ |
| 1339 | c = ptr[wlen]; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1340 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 1341 | c = ' '; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1342 | lo = arridx; |
| 1343 | hi = arridx + len - 1; |
| 1344 | while (lo < hi) |
| 1345 | { |
| 1346 | m = (lo + hi) / 2; |
| 1347 | if (byts[m] > c) |
| 1348 | hi = m - 1; |
| 1349 | else if (byts[m] < c) |
| 1350 | lo = m + 1; |
| 1351 | else |
| 1352 | { |
| 1353 | lo = hi = m; |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* Stop if there is no matching byte. */ |
| 1359 | if (hi < lo || byts[lo] != c) |
| 1360 | break; |
| 1361 | |
| 1362 | /* Continue at the child (if there is one). */ |
| 1363 | arridx = idxs[lo]; |
| 1364 | ++wlen; |
| 1365 | --flen; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1366 | |
| 1367 | /* One space in the good word may stand for several spaces in the |
| 1368 | * checked word. */ |
| 1369 | if (c == ' ') |
| 1370 | { |
| 1371 | for (;;) |
| 1372 | { |
| 1373 | if (flen <= 0 && *mip->mi_fend != NUL) |
| 1374 | flen = fold_more(mip); |
| 1375 | if (ptr[wlen] != ' ' && ptr[wlen] != TAB) |
| 1376 | break; |
| 1377 | ++wlen; |
| 1378 | --flen; |
| 1379 | } |
| 1380 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Verify that one of the possible endings is valid. Try the longest |
| 1385 | * first. |
| 1386 | */ |
| 1387 | while (endidxcnt > 0) |
| 1388 | { |
| 1389 | --endidxcnt; |
| 1390 | arridx = endidx[endidxcnt]; |
| 1391 | wlen = endlen[endidxcnt]; |
| 1392 | |
| 1393 | #ifdef FEAT_MBYTE |
| 1394 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 1395 | continue; /* not at first byte of character */ |
| 1396 | #endif |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 1397 | if (spell_iswordp(ptr + wlen, mip->mi_buf)) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1398 | { |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1399 | if (slang->sl_compprog == NULL && !slang->sl_nobreak) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1400 | continue; /* next char is a word character */ |
| 1401 | word_ends = FALSE; |
| 1402 | } |
| 1403 | else |
| 1404 | word_ends = TRUE; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1405 | /* The prefix flag is before compound flags. Once a valid prefix flag |
| 1406 | * has been found we try compound flags. */ |
| 1407 | prefix_found = FALSE; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1408 | |
| 1409 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1410 | if (mode != FIND_KEEPWORD && has_mbyte) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1411 | { |
| 1412 | /* Compute byte length in original word, length may change |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1413 | * when folding case. This can be slow, take a shortcut when the |
| 1414 | * case-folded word is equal to the keep-case word. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1415 | p = mip->mi_word; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1416 | if (STRNCMP(ptr, p, wlen) != 0) |
| 1417 | { |
| 1418 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 1419 | mb_ptr_adv(p); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1420 | wlen = (int)(p - mip->mi_word); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1421 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1422 | } |
| 1423 | #endif |
| 1424 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1425 | /* Check flags and region. For FIND_PREFIX check the condition and |
| 1426 | * prefix ID. |
| 1427 | * Repeat this if there are more flags/region alternatives until there |
| 1428 | * is a match. */ |
| 1429 | res = SP_BAD; |
| 1430 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; |
| 1431 | --len, ++arridx) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1432 | { |
| 1433 | flags = idxs[arridx]; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 1434 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1435 | /* For the fold-case tree check that the case of the checked word |
| 1436 | * matches with what the word in the tree requires. |
| 1437 | * For keep-case tree the case is always right. For prefixes we |
| 1438 | * don't bother to check. */ |
| 1439 | if (mode == FIND_FOLDWORD) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1440 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1441 | if (mip->mi_cend != mip->mi_word + wlen) |
| 1442 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1443 | /* mi_capflags was set for a different word length, need |
| 1444 | * to do it again. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1445 | mip->mi_cend = mip->mi_word + wlen; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1446 | mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 1449 | if (mip->mi_capflags == WF_KEEPCAP |
| 1450 | || !spell_valid_case(mip->mi_capflags, flags)) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1451 | continue; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1454 | /* When mode is FIND_PREFIX the word must support the prefix: |
| 1455 | * 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] | 1456 | * mip->mi_prefarridx that find_prefix() filled. */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1457 | else if (mode == FIND_PREFIX && !prefix_found) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1458 | { |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1459 | c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1460 | flags, |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1461 | mip->mi_word + mip->mi_cprefixlen, slang, |
| 1462 | FALSE); |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1463 | if (c == 0) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1464 | continue; |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1465 | |
| 1466 | /* Use the WF_RARE flag for a rare prefix. */ |
| 1467 | if (c & WF_RAREPFX) |
| 1468 | flags |= WF_RARE; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1469 | prefix_found = TRUE; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1472 | if (slang->sl_nobreak) |
| 1473 | { |
| 1474 | if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND) |
| 1475 | && (flags & WF_BANNED) == 0) |
| 1476 | { |
| 1477 | /* NOBREAK: found a valid following word. That's all we |
| 1478 | * need to know, so return. */ |
| 1479 | mip->mi_result = SP_OK; |
| 1480 | break; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND |
| 1485 | || !word_ends)) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1486 | { |
| Bram Moolenaar | 2113a1d | 2006-09-11 19:38:08 +0000 | [diff] [blame] | 1487 | /* If there is no compound flag or the word is shorter than |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1488 | * COMPOUNDMIN reject it quickly. |
| 1489 | * Makes you wonder why someone puts a compound flag on a word |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1490 | * that's too short... Myspell compatibility requires this |
| 1491 | * anyway. */ |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 1492 | if (((unsigned)flags >> 24) == 0 |
| 1493 | || wlen - mip->mi_compoff < slang->sl_compminlen) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1494 | continue; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1495 | #ifdef FEAT_MBYTE |
| 1496 | /* For multi-byte chars check character length against |
| 1497 | * COMPOUNDMIN. */ |
| 1498 | if (has_mbyte |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1499 | && slang->sl_compminlen > 0 |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1500 | && mb_charlen_len(mip->mi_word + mip->mi_compoff, |
| 1501 | wlen - mip->mi_compoff) < slang->sl_compminlen) |
| 1502 | continue; |
| 1503 | #endif |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1504 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 1505 | /* Limit the number of compound words to COMPOUNDWORDMAX if no |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 1506 | * maximum for syllables is specified. */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 1507 | if (!word_ends && mip->mi_complen + mip->mi_compextra + 2 |
| 1508 | > slang->sl_compmax |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 1509 | && slang->sl_compsylmax == MAXWLEN) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1510 | continue; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1511 | |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1512 | /* Don't allow compounding on a side where an affix was added, |
| 1513 | * unless COMPOUNDPERMITFLAG was used. */ |
| 1514 | if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF)) |
| 1515 | continue; |
| 1516 | if (!word_ends && (flags & WF_NOCOMPAFT)) |
| 1517 | continue; |
| 1518 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1519 | /* Quickly check if compounding is possible with this flag. */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 1520 | if (!byte_in_str(mip->mi_complen == 0 |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1521 | ? slang->sl_compstartflags |
| 1522 | : slang->sl_compallflags, |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 1523 | ((unsigned)flags >> 24))) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1524 | continue; |
| 1525 | |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 1526 | /* If there is a match with a CHECKCOMPOUNDPATTERN rule |
| 1527 | * discard the compound word. */ |
| 1528 | if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat)) |
| 1529 | continue; |
| 1530 | |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 1531 | if (mode == FIND_COMPOUND) |
| 1532 | { |
| 1533 | int capflags; |
| 1534 | |
| 1535 | /* Need to check the caps type of the appended compound |
| 1536 | * word. */ |
| 1537 | #ifdef FEAT_MBYTE |
| 1538 | if (has_mbyte && STRNCMP(ptr, mip->mi_word, |
| 1539 | mip->mi_compoff) != 0) |
| 1540 | { |
| 1541 | /* case folding may have changed the length */ |
| 1542 | p = mip->mi_word; |
| 1543 | for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s)) |
| 1544 | mb_ptr_adv(p); |
| 1545 | } |
| 1546 | else |
| 1547 | #endif |
| 1548 | p = mip->mi_word + mip->mi_compoff; |
| 1549 | capflags = captype(p, mip->mi_word + wlen); |
| 1550 | if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP |
| 1551 | && (flags & WF_FIXCAP) != 0)) |
| 1552 | continue; |
| 1553 | |
| 1554 | if (capflags != WF_ALLCAP) |
| 1555 | { |
| 1556 | /* When the character before the word is a word |
| 1557 | * character we do not accept a Onecap word. We do |
| 1558 | * accept a no-caps word, even when the dictionary |
| 1559 | * word specifies ONECAP. */ |
| 1560 | mb_ptr_back(mip->mi_word, p); |
| 1561 | if (spell_iswordp_nmw(p) |
| 1562 | ? capflags == WF_ONECAP |
| 1563 | : (flags & WF_ONECAP) != 0 |
| 1564 | && capflags != WF_ONECAP) |
| 1565 | continue; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1569 | /* If the word ends the sequence of compound flags of the |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1570 | * words must match with one of the COMPOUNDRULE items and |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1571 | * the number of syllables must not be too large. */ |
| 1572 | mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24); |
| 1573 | mip->mi_compflags[mip->mi_complen + 1] = NUL; |
| 1574 | if (word_ends) |
| 1575 | { |
| 1576 | char_u fword[MAXWLEN]; |
| 1577 | |
| 1578 | if (slang->sl_compsylmax < MAXWLEN) |
| 1579 | { |
| 1580 | /* "fword" is only needed for checking syllables. */ |
| 1581 | if (ptr == mip->mi_word) |
| 1582 | (void)spell_casefold(ptr, wlen, fword, MAXWLEN); |
| 1583 | else |
| 1584 | vim_strncpy(fword, ptr, endlen[endidxcnt]); |
| 1585 | } |
| 1586 | if (!can_compound(slang, fword, mip->mi_compflags)) |
| 1587 | continue; |
| 1588 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 1589 | else if (slang->sl_comprules != NULL |
| 1590 | && !match_compoundrule(slang, mip->mi_compflags)) |
| 1591 | /* The compound flags collected so far do not match any |
| 1592 | * COMPOUNDRULE, discard the compounded word. */ |
| 1593 | continue; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 1596 | /* Check NEEDCOMPOUND: can't use word without compounding. */ |
| 1597 | else if (flags & WF_NEEDCOMP) |
| 1598 | continue; |
| 1599 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1600 | nobreak_result = SP_OK; |
| 1601 | |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1602 | if (!word_ends) |
| 1603 | { |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1604 | int save_result = mip->mi_result; |
| 1605 | char_u *save_end = mip->mi_end; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1606 | langp_T *save_lp = mip->mi_lp; |
| 1607 | int lpi; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1608 | |
| 1609 | /* Check that a valid word follows. If there is one and we |
| 1610 | * are compounding, it will set "mi_result", thus we are |
| 1611 | * always finished here. For NOBREAK we only check that a |
| 1612 | * valid word follows. |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1613 | * Recursive! */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1614 | if (slang->sl_nobreak) |
| 1615 | mip->mi_result = SP_BAD; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1616 | |
| 1617 | /* Find following word in case-folded tree. */ |
| 1618 | mip->mi_compoff = endlen[endidxcnt]; |
| 1619 | #ifdef FEAT_MBYTE |
| 1620 | if (has_mbyte && mode == FIND_KEEPWORD) |
| 1621 | { |
| 1622 | /* Compute byte length in case-folded word from "wlen": |
| 1623 | * byte length in keep-case word. Length may change when |
| 1624 | * folding case. This can be slow, take a shortcut when |
| 1625 | * the case-folded word is equal to the keep-case word. */ |
| 1626 | p = mip->mi_fword; |
| 1627 | if (STRNCMP(ptr, p, wlen) != 0) |
| 1628 | { |
| 1629 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 1630 | mb_ptr_adv(p); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1631 | mip->mi_compoff = (int)(p - mip->mi_fword); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | #endif |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1635 | c = mip->mi_compoff; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1636 | ++mip->mi_complen; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 1637 | if (flags & WF_COMPROOT) |
| 1638 | ++mip->mi_compextra; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1639 | |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1640 | /* For NOBREAK we need to try all NOBREAK languages, at least |
| 1641 | * to find the ".add" file(s). */ |
| 1642 | for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1643 | { |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1644 | if (slang->sl_nobreak) |
| 1645 | { |
| 1646 | mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi); |
| 1647 | if (mip->mi_lp->lp_slang->sl_fidxs == NULL |
| 1648 | || !mip->mi_lp->lp_slang->sl_nobreak) |
| 1649 | continue; |
| 1650 | } |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1651 | |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1652 | find_word(mip, FIND_COMPOUND); |
| 1653 | |
| 1654 | /* When NOBREAK any word that matches is OK. Otherwise we |
| 1655 | * need to find the longest match, thus try with keep-case |
| 1656 | * and prefix too. */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1657 | if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
| 1658 | { |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1659 | /* Find following word in keep-case tree. */ |
| 1660 | mip->mi_compoff = wlen; |
| 1661 | find_word(mip, FIND_KEEPCOMPOUND); |
| 1662 | |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1663 | #if 0 /* Disabled, a prefix must not appear halfway a compound word, |
| 1664 | unless the COMPOUNDPERMITFLAG is used and then it can't be a |
| 1665 | postponed prefix. */ |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1666 | if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
| 1667 | { |
| 1668 | /* Check for following word with prefix. */ |
| 1669 | mip->mi_compoff = c; |
| 1670 | find_prefix(mip, FIND_COMPOUND); |
| 1671 | } |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1672 | #endif |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1673 | } |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1674 | |
| 1675 | if (!slang->sl_nobreak) |
| 1676 | break; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1677 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1678 | --mip->mi_complen; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 1679 | if (flags & WF_COMPROOT) |
| 1680 | --mip->mi_compextra; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 1681 | mip->mi_lp = save_lp; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1682 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1683 | if (slang->sl_nobreak) |
| 1684 | { |
| 1685 | nobreak_result = mip->mi_result; |
| 1686 | mip->mi_result = save_result; |
| 1687 | mip->mi_end = save_end; |
| 1688 | } |
| 1689 | else |
| 1690 | { |
| 1691 | if (mip->mi_result == SP_OK) |
| 1692 | break; |
| 1693 | continue; |
| 1694 | } |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1697 | if (flags & WF_BANNED) |
| 1698 | res = SP_BANNED; |
| 1699 | else if (flags & WF_REGION) |
| 1700 | { |
| 1701 | /* Check region. */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1702 | if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1703 | res = SP_OK; |
| 1704 | else |
| 1705 | res = SP_LOCAL; |
| 1706 | } |
| 1707 | else if (flags & WF_RARE) |
| 1708 | res = SP_RARE; |
| 1709 | else |
| 1710 | res = SP_OK; |
| 1711 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1712 | /* Always use the longest match and the best result. For NOBREAK |
| 1713 | * we separately keep the longest match without a following good |
| 1714 | * word as a fall-back. */ |
| 1715 | if (nobreak_result == SP_BAD) |
| 1716 | { |
| 1717 | if (mip->mi_result2 > res) |
| 1718 | { |
| 1719 | mip->mi_result2 = res; |
| 1720 | mip->mi_end2 = mip->mi_word + wlen; |
| 1721 | } |
| 1722 | else if (mip->mi_result2 == res |
| 1723 | && mip->mi_end2 < mip->mi_word + wlen) |
| 1724 | mip->mi_end2 = mip->mi_word + wlen; |
| 1725 | } |
| 1726 | else if (mip->mi_result > res) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1727 | { |
| 1728 | mip->mi_result = res; |
| 1729 | mip->mi_end = mip->mi_word + wlen; |
| 1730 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1731 | 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] | 1732 | mip->mi_end = mip->mi_word + wlen; |
| 1733 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1734 | if (mip->mi_result == SP_OK) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1735 | break; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 1738 | if (mip->mi_result == SP_OK) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1739 | break; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1740 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 1743 | /* |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 1744 | * Return TRUE if there is a match between the word ptr[wlen] and |
| 1745 | * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another |
| 1746 | * word. |
| 1747 | * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the |
| 1748 | * end of ptr[wlen] and the second part matches after it. |
| 1749 | */ |
| 1750 | static int |
| 1751 | match_checkcompoundpattern(ptr, wlen, gap) |
| 1752 | char_u *ptr; |
| 1753 | int wlen; |
| 1754 | garray_T *gap; /* &sl_comppat */ |
| 1755 | { |
| 1756 | int i; |
| 1757 | char_u *p; |
| 1758 | int len; |
| 1759 | |
| 1760 | for (i = 0; i + 1 < gap->ga_len; i += 2) |
| 1761 | { |
| 1762 | p = ((char_u **)gap->ga_data)[i + 1]; |
| 1763 | if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0) |
| 1764 | { |
| 1765 | /* Second part matches at start of following compound word, now |
| 1766 | * check if first part matches at end of previous word. */ |
| 1767 | p = ((char_u **)gap->ga_data)[i]; |
| Bram Moolenaar | 19c9c76 | 2008-12-09 21:34:39 +0000 | [diff] [blame^] | 1768 | len = (int)STRLEN(p); |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 1769 | if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) |
| 1770 | return TRUE; |
| 1771 | } |
| 1772 | } |
| 1773 | return FALSE; |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 1777 | * Return TRUE if "flags" is a valid sequence of compound flags and "word" |
| 1778 | * does not have too many syllables. |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 1779 | */ |
| 1780 | static int |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1781 | can_compound(slang, word, flags) |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 1782 | slang_T *slang; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1783 | char_u *word; |
| 1784 | char_u *flags; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 1785 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1786 | regmatch_T regmatch; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 1787 | #ifdef FEAT_MBYTE |
| 1788 | char_u uflags[MAXWLEN * 2]; |
| 1789 | int i; |
| 1790 | #endif |
| 1791 | char_u *p; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1792 | |
| 1793 | if (slang->sl_compprog == NULL) |
| 1794 | return FALSE; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 1795 | #ifdef FEAT_MBYTE |
| 1796 | if (enc_utf8) |
| 1797 | { |
| 1798 | /* Need to convert the single byte flags to utf8 characters. */ |
| 1799 | p = uflags; |
| 1800 | for (i = 0; flags[i] != NUL; ++i) |
| 1801 | p += mb_char2bytes(flags[i], p); |
| 1802 | *p = NUL; |
| 1803 | p = uflags; |
| 1804 | } |
| 1805 | else |
| 1806 | #endif |
| 1807 | p = flags; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1808 | regmatch.regprog = slang->sl_compprog; |
| 1809 | regmatch.rm_ic = FALSE; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 1810 | if (!vim_regexec(®match, p, 0)) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1811 | return FALSE; |
| 1812 | |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 1813 | /* Count the number of syllables. This may be slow, do it last. If there |
| 1814 | * are too many syllables AND the number of compound words is above |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 1815 | * COMPOUNDWORDMAX then compounding is not allowed. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1816 | if (slang->sl_compsylmax < MAXWLEN |
| 1817 | && count_syllables(slang, word) > slang->sl_compsylmax) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 1818 | return (int)STRLEN(flags) < slang->sl_compmax; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 1819 | return TRUE; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 1823 | * Return TRUE when the sequence of flags in "compflags" plus "flag" can |
| 1824 | * possibly form a valid compounded word. This also checks the COMPOUNDRULE |
| 1825 | * lines if they don't contain wildcards. |
| 1826 | */ |
| 1827 | static int |
| 1828 | can_be_compound(sp, slang, compflags, flag) |
| 1829 | trystate_T *sp; |
| 1830 | slang_T *slang; |
| 1831 | char_u *compflags; |
| 1832 | int flag; |
| 1833 | { |
| 1834 | /* If the flag doesn't appear in sl_compstartflags or sl_compallflags |
| 1835 | * then it can't possibly compound. */ |
| 1836 | if (!byte_in_str(sp->ts_complen == sp->ts_compsplit |
| 1837 | ? slang->sl_compstartflags : slang->sl_compallflags, flag)) |
| 1838 | return FALSE; |
| 1839 | |
| 1840 | /* If there are no wildcards, we can check if the flags collected so far |
| 1841 | * possibly can form a match with COMPOUNDRULE patterns. This only |
| 1842 | * makes sense when we have two or more words. */ |
| 1843 | if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit) |
| 1844 | { |
| 1845 | int v; |
| 1846 | |
| 1847 | compflags[sp->ts_complen] = flag; |
| 1848 | compflags[sp->ts_complen + 1] = NUL; |
| 1849 | v = match_compoundrule(slang, compflags + sp->ts_compsplit); |
| 1850 | compflags[sp->ts_complen] = NUL; |
| 1851 | return v; |
| 1852 | } |
| 1853 | |
| 1854 | return TRUE; |
| 1855 | } |
| 1856 | |
| 1857 | |
| 1858 | /* |
| 1859 | * Return TRUE if the compound flags in compflags[] match the start of any |
| 1860 | * compound rule. This is used to stop trying a compound if the flags |
| 1861 | * collected so far can't possibly match any compound rule. |
| 1862 | * Caller must check that slang->sl_comprules is not NULL. |
| 1863 | */ |
| 1864 | static int |
| 1865 | match_compoundrule(slang, compflags) |
| 1866 | slang_T *slang; |
| 1867 | char_u *compflags; |
| 1868 | { |
| 1869 | char_u *p; |
| 1870 | int i; |
| 1871 | int c; |
| 1872 | |
| 1873 | /* loop over all the COMPOUNDRULE entries */ |
| 1874 | for (p = slang->sl_comprules; *p != NUL; ++p) |
| 1875 | { |
| 1876 | /* loop over the flags in the compound word we have made, match |
| 1877 | * them against the current rule entry */ |
| 1878 | for (i = 0; ; ++i) |
| 1879 | { |
| 1880 | c = compflags[i]; |
| 1881 | if (c == NUL) |
| 1882 | /* found a rule that matches for the flags we have so far */ |
| 1883 | return TRUE; |
| 1884 | if (*p == '/' || *p == NUL) |
| 1885 | break; /* end of rule, it's too short */ |
| 1886 | if (*p == '[') |
| 1887 | { |
| 1888 | int match = FALSE; |
| 1889 | |
| 1890 | /* compare against all the flags in [] */ |
| 1891 | ++p; |
| 1892 | while (*p != ']' && *p != NUL) |
| 1893 | if (*p++ == c) |
| 1894 | match = TRUE; |
| 1895 | if (!match) |
| 1896 | break; /* none matches */ |
| 1897 | } |
| 1898 | else if (*p != c) |
| 1899 | break; /* flag of word doesn't match flag in pattern */ |
| 1900 | ++p; |
| 1901 | } |
| 1902 | |
| 1903 | /* Skip to the next "/", where the next pattern starts. */ |
| 1904 | p = vim_strchr(p, '/'); |
| 1905 | if (p == NULL) |
| 1906 | break; |
| 1907 | } |
| 1908 | |
| 1909 | /* Checked all the rules and none of them match the flags, so there |
| 1910 | * can't possibly be a compound starting with these flags. */ |
| 1911 | return FALSE; |
| 1912 | } |
| 1913 | |
| 1914 | /* |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1915 | * Return non-zero if the prefix indicated by "arridx" matches with the prefix |
| 1916 | * ID in "flags" for the word "word". |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1917 | * 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] | 1918 | */ |
| 1919 | static int |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1920 | valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1921 | int totprefcnt; /* nr of prefix IDs */ |
| 1922 | int arridx; /* idx in sl_pidxs[] */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1923 | int flags; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1924 | char_u *word; |
| 1925 | slang_T *slang; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1926 | int cond_req; /* only use prefixes with a condition */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1927 | { |
| 1928 | int prefcnt; |
| 1929 | int pidx; |
| 1930 | regprog_T *rp; |
| 1931 | regmatch_T regmatch; |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1932 | int prefid; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1933 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1934 | prefid = (unsigned)flags >> 24; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1935 | for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
| 1936 | { |
| 1937 | pidx = slang->sl_pidxs[arridx + prefcnt]; |
| 1938 | |
| 1939 | /* Check the prefix ID. */ |
| 1940 | if (prefid != (pidx & 0xff)) |
| 1941 | continue; |
| 1942 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 1943 | /* Check if the prefix doesn't combine and the word already has a |
| 1944 | * suffix. */ |
| 1945 | if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) |
| 1946 | continue; |
| 1947 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1948 | /* Check the condition, if there is one. The condition index is |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1949 | * stored in the two bytes above the prefix ID byte. */ |
| 1950 | rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1951 | if (rp != NULL) |
| 1952 | { |
| 1953 | regmatch.regprog = rp; |
| 1954 | regmatch.rm_ic = FALSE; |
| 1955 | if (!vim_regexec(®match, word, 0)) |
| 1956 | continue; |
| 1957 | } |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1958 | else if (cond_req) |
| 1959 | continue; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1960 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 1961 | /* It's a match! Return the WF_ flags. */ |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1962 | return pidx; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1963 | } |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 1964 | return 0; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | /* |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1968 | * Check if the word at "mip->mi_word" has a matching prefix. |
| 1969 | * If it does, then check the following word. |
| 1970 | * |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1971 | * If "mode" is "FIND_COMPOUND" then do the same after another word, find a |
| 1972 | * prefix in a compound word. |
| 1973 | * |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1974 | * For a match mip->mi_result is updated. |
| 1975 | */ |
| 1976 | static void |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1977 | find_prefix(mip, mode) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1978 | matchinf_T *mip; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 1979 | int mode; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1980 | { |
| 1981 | idx_T arridx = 0; |
| 1982 | int len; |
| 1983 | int wlen = 0; |
| 1984 | int flen; |
| 1985 | int c; |
| 1986 | char_u *ptr; |
| 1987 | idx_T lo, hi, m; |
| 1988 | slang_T *slang = mip->mi_lp->lp_slang; |
| 1989 | char_u *byts; |
| 1990 | idx_T *idxs; |
| 1991 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 1992 | byts = slang->sl_pbyts; |
| 1993 | if (byts == NULL) |
| 1994 | return; /* array is empty */ |
| 1995 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 1996 | /* We use the case-folded word here, since prefixes are always |
| 1997 | * case-folded. */ |
| 1998 | ptr = mip->mi_fword; |
| 1999 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 2000 | if (mode == FIND_COMPOUND) |
| 2001 | { |
| 2002 | /* Skip over the previously found word(s). */ |
| 2003 | ptr += mip->mi_compoff; |
| 2004 | flen -= mip->mi_compoff; |
| 2005 | } |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2006 | idxs = slang->sl_pidxs; |
| 2007 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2008 | /* |
| 2009 | * Repeat advancing in the tree until: |
| 2010 | * - there is a byte that doesn't match, |
| 2011 | * - we reach the end of the tree, |
| 2012 | * - or we reach the end of the line. |
| 2013 | */ |
| 2014 | for (;;) |
| 2015 | { |
| 2016 | if (flen == 0 && *mip->mi_fend != NUL) |
| 2017 | flen = fold_more(mip); |
| 2018 | |
| 2019 | len = byts[arridx++]; |
| 2020 | |
| 2021 | /* If the first possible byte is a zero the prefix could end here. |
| 2022 | * Check if the following word matches and supports the prefix. */ |
| 2023 | if (byts[arridx] == 0) |
| 2024 | { |
| 2025 | /* There can be several prefixes with different conditions. We |
| 2026 | * try them all, since we don't know which one will give the |
| 2027 | * longest match. The word is the same each time, pass the list |
| 2028 | * of possible prefixes to find_word(). */ |
| 2029 | mip->mi_prefarridx = arridx; |
| 2030 | mip->mi_prefcnt = len; |
| 2031 | while (len > 0 && byts[arridx] == 0) |
| 2032 | { |
| 2033 | ++arridx; |
| 2034 | --len; |
| 2035 | } |
| 2036 | mip->mi_prefcnt -= len; |
| 2037 | |
| 2038 | /* Find the word that comes after the prefix. */ |
| 2039 | mip->mi_prefixlen = wlen; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 2040 | if (mode == FIND_COMPOUND) |
| 2041 | /* Skip over the previously found word(s). */ |
| 2042 | mip->mi_prefixlen += mip->mi_compoff; |
| 2043 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 2044 | #ifdef FEAT_MBYTE |
| 2045 | if (has_mbyte) |
| 2046 | { |
| 2047 | /* Case-folded length may differ from original length. */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 2048 | mip->mi_cprefixlen = nofold_len(mip->mi_fword, |
| 2049 | mip->mi_prefixlen, mip->mi_word); |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 2050 | } |
| 2051 | else |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 2052 | mip->mi_cprefixlen = mip->mi_prefixlen; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 2053 | #endif |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2054 | find_word(mip, FIND_PREFIX); |
| 2055 | |
| 2056 | |
| 2057 | if (len == 0) |
| 2058 | break; /* no children, word must end here */ |
| 2059 | } |
| 2060 | |
| 2061 | /* Stop looking at end of the line. */ |
| 2062 | if (ptr[wlen] == NUL) |
| 2063 | break; |
| 2064 | |
| 2065 | /* Perform a binary search in the list of accepted bytes. */ |
| 2066 | c = ptr[wlen]; |
| 2067 | lo = arridx; |
| 2068 | hi = arridx + len - 1; |
| 2069 | while (lo < hi) |
| 2070 | { |
| 2071 | m = (lo + hi) / 2; |
| 2072 | if (byts[m] > c) |
| 2073 | hi = m - 1; |
| 2074 | else if (byts[m] < c) |
| 2075 | lo = m + 1; |
| 2076 | else |
| 2077 | { |
| 2078 | lo = hi = m; |
| 2079 | break; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /* Stop if there is no matching byte. */ |
| 2084 | if (hi < lo || byts[lo] != c) |
| 2085 | break; |
| 2086 | |
| 2087 | /* Continue at the child (if there is one). */ |
| 2088 | arridx = idxs[lo]; |
| 2089 | ++wlen; |
| 2090 | --flen; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * Need to fold at least one more character. Do until next non-word character |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 2096 | * for efficiency. Include the non-word character too. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2097 | * Return the length of the folded chars in bytes. |
| 2098 | */ |
| 2099 | static int |
| 2100 | fold_more(mip) |
| 2101 | matchinf_T *mip; |
| 2102 | { |
| 2103 | int flen; |
| 2104 | char_u *p; |
| 2105 | |
| 2106 | p = mip->mi_fend; |
| 2107 | do |
| 2108 | { |
| 2109 | mb_ptr_adv(mip->mi_fend); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2110 | } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf)); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2111 | |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 2112 | /* Include the non-word character so that we can check for the word end. */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2113 | if (*mip->mi_fend != NUL) |
| 2114 | mb_ptr_adv(mip->mi_fend); |
| 2115 | |
| 2116 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
| 2117 | mip->mi_fword + mip->mi_fwordlen, |
| 2118 | MAXWLEN - mip->mi_fwordlen); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2119 | flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2120 | mip->mi_fwordlen += flen; |
| 2121 | return flen; |
| 2122 | } |
| 2123 | |
| 2124 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2125 | * Check case flags for a word. Return TRUE if the word has the requested |
| 2126 | * case. |
| 2127 | */ |
| 2128 | static int |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2129 | spell_valid_case(wordflags, treeflags) |
| 2130 | int wordflags; /* flags for the checked word. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2131 | int treeflags; /* flags for the word in the spell tree */ |
| 2132 | { |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2133 | return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2134 | || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2135 | && ((treeflags & WF_ONECAP) == 0 |
| 2136 | || (wordflags & WF_ONECAP) != 0))); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2139 | /* |
| 2140 | * Return TRUE if spell checking is not enabled. |
| 2141 | */ |
| 2142 | static int |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2143 | no_spell_checking(wp) |
| 2144 | win_T *wp; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2145 | { |
| Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 2146 | if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL |
| 2147 | || wp->w_buffer->b_langp.ga_len == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 2148 | { |
| 2149 | EMSG(_("E756: Spell checking is not enabled")); |
| 2150 | return TRUE; |
| 2151 | } |
| 2152 | return FALSE; |
| 2153 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2154 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2155 | /* |
| 2156 | * Move to next spell error. |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2157 | * "curline" is FALSE for "[s", "]s", "[S" and "]S". |
| 2158 | * "curline" is TRUE to find word under/after cursor in the same line. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2159 | * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move |
| 2160 | * to after badly spelled word before the cursor. |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2161 | * Return 0 if not found, length of the badly spelled word otherwise. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2162 | */ |
| 2163 | int |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2164 | spell_move_to(wp, dir, allwords, curline, attrp) |
| 2165 | win_T *wp; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2166 | int dir; /* FORWARD or BACKWARD */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2167 | int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2168 | int curline; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2169 | hlf_T *attrp; /* return: attributes of bad word or NULL |
| 2170 | (only when "dir" is FORWARD) */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2171 | { |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2172 | linenr_T lnum; |
| 2173 | pos_T found_pos; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2174 | int found_len = 0; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2175 | char_u *line; |
| 2176 | char_u *p; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2177 | char_u *endp; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2178 | hlf_T attr; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2179 | int len; |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 2180 | # ifdef FEAT_SYN_HL |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2181 | int has_syntax = syntax_present(wp->w_buffer); |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 2182 | # endif |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2183 | int col; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2184 | int can_spell; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2185 | char_u *buf = NULL; |
| 2186 | int buflen = 0; |
| 2187 | int skip = 0; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2188 | int capcol = -1; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2189 | int found_one = FALSE; |
| 2190 | int wrapped = FALSE; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2191 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2192 | if (no_spell_checking(wp)) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2193 | return 0; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2194 | |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2195 | /* |
| 2196 | * Start looking for bad word at the start of the line, because we can't |
| Bram Moolenaar | 86ca6e3 | 2006-03-29 21:06:37 +0000 | [diff] [blame] | 2197 | * start halfway a word, we don't know where it starts or ends. |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2198 | * |
| 2199 | * When searching backwards, we continue in the line to find the last |
| 2200 | * bad word (in the cursor line: before the cursor). |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2201 | * |
| 2202 | * We concatenate the start of the next line, so that wrapped words work |
| 2203 | * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards |
| 2204 | * though... |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2205 | */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2206 | lnum = wp->w_cursor.lnum; |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 2207 | clearpos(&found_pos); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2208 | |
| 2209 | while (!got_int) |
| 2210 | { |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2211 | line = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2212 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2213 | len = (int)STRLEN(line); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2214 | if (buflen < len + MAXWLEN + 2) |
| 2215 | { |
| 2216 | vim_free(buf); |
| 2217 | buflen = len + MAXWLEN + 2; |
| 2218 | buf = alloc(buflen); |
| 2219 | if (buf == NULL) |
| 2220 | break; |
| 2221 | } |
| 2222 | |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2223 | /* In first line check first word for Capital. */ |
| 2224 | if (lnum == 1) |
| 2225 | capcol = 0; |
| 2226 | |
| 2227 | /* For checking first word with a capital skip white space. */ |
| 2228 | if (capcol == 0) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2229 | capcol = (int)(skipwhite(line) - line); |
| 2230 | else if (curline && wp == curwin) |
| 2231 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2232 | /* For spellbadword(): check if first word needs a capital. */ |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2233 | col = (int)(skipwhite(line) - line); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2234 | if (check_need_cap(lnum, col)) |
| 2235 | capcol = col; |
| 2236 | |
| 2237 | /* Need to get the line again, may have looked at the previous |
| 2238 | * one. */ |
| 2239 | line = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2240 | } |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2241 | |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2242 | /* Copy the line into "buf" and append the start of the next line if |
| 2243 | * possible. */ |
| 2244 | STRCPY(buf, line); |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2245 | if (lnum < wp->w_buffer->b_ml.ml_line_count) |
| Bram Moolenaar | 5dd95a1 | 2006-05-13 12:09:24 +0000 | [diff] [blame] | 2246 | spell_cat_line(buf + STRLEN(buf), |
| 2247 | ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2248 | |
| 2249 | p = buf + skip; |
| 2250 | endp = buf + len; |
| 2251 | while (p < endp) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2252 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2253 | /* When searching backward don't search after the cursor. Unless |
| 2254 | * we wrapped around the end of the buffer. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2255 | if (dir == BACKWARD |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2256 | && lnum == wp->w_cursor.lnum |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2257 | && !wrapped |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2258 | && (colnr_T)(p - buf) >= wp->w_cursor.col) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | |
| 2261 | /* start of word */ |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2262 | attr = HLF_COUNT; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2263 | len = spell_check(wp, p, &attr, &capcol, FALSE); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2264 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2265 | if (attr != HLF_COUNT) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2266 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2267 | /* We found a bad word. Check the attribute. */ |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2268 | if (allwords || attr == HLF_SPB) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2269 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2270 | /* When searching forward only accept a bad word after |
| 2271 | * the cursor. */ |
| 2272 | if (dir == BACKWARD |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2273 | || lnum != wp->w_cursor.lnum |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2274 | || (lnum == wp->w_cursor.lnum |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2275 | && (wrapped |
| 2276 | || (colnr_T)(curline ? p - buf + len |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2277 | : p - buf) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2278 | > wp->w_cursor.col))) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2279 | { |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 2280 | # ifdef FEAT_SYN_HL |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2281 | if (has_syntax) |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2282 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2283 | col = (int)(p - buf); |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2284 | (void)syn_get_id(wp, lnum, (colnr_T)col, |
| Bram Moolenaar | 56cefaf | 2008-01-12 15:47:10 +0000 | [diff] [blame] | 2285 | FALSE, &can_spell, FALSE); |
| Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 2286 | if (!can_spell) |
| 2287 | attr = HLF_COUNT; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2288 | } |
| 2289 | else |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 2290 | #endif |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2291 | can_spell = TRUE; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2292 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2293 | if (can_spell) |
| 2294 | { |
| Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 2295 | found_one = TRUE; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2296 | found_pos.lnum = lnum; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2297 | found_pos.col = (int)(p - buf); |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2298 | #ifdef FEAT_VIRTUALEDIT |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2299 | found_pos.coladd = 0; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2300 | #endif |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2301 | if (dir == FORWARD) |
| 2302 | { |
| 2303 | /* No need to search further. */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2304 | wp->w_cursor = found_pos; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2305 | vim_free(buf); |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2306 | if (attrp != NULL) |
| 2307 | *attrp = attr; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2308 | return len; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2309 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2310 | else if (curline) |
| 2311 | /* Insert mode completion: put cursor after |
| 2312 | * the bad word. */ |
| 2313 | found_pos.col += len; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2314 | found_len = len; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2315 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2316 | } |
| Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 2317 | else |
| 2318 | found_one = TRUE; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2319 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2322 | /* advance to character after the word */ |
| 2323 | p += len; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2324 | capcol -= len; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2327 | if (dir == BACKWARD && found_pos.lnum != 0) |
| 2328 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2329 | /* Use the last match in the line (before the cursor). */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 2330 | wp->w_cursor = found_pos; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2331 | vim_free(buf); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2332 | return found_len; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2335 | if (curline) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2336 | break; /* only check cursor line */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2337 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2338 | /* Advance to next line. */ |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2339 | if (dir == BACKWARD) |
| 2340 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2341 | /* If we are back at the starting line and searched it again there |
| 2342 | * is no match, give up. */ |
| 2343 | if (lnum == wp->w_cursor.lnum && wrapped) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2344 | break; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2345 | |
| 2346 | if (lnum > 1) |
| 2347 | --lnum; |
| 2348 | else if (!p_ws) |
| 2349 | break; /* at first line and 'nowrapscan' */ |
| 2350 | else |
| 2351 | { |
| 2352 | /* Wrap around to the end of the buffer. May search the |
| 2353 | * starting line again and accept the last match. */ |
| 2354 | lnum = wp->w_buffer->b_ml.ml_line_count; |
| 2355 | wrapped = TRUE; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 2356 | if (!shortmess(SHM_SEARCH)) |
| 2357 | give_warning((char_u *)_(top_bot_msg), TRUE); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2358 | } |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2359 | capcol = -1; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2360 | } |
| 2361 | else |
| 2362 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2363 | if (lnum < wp->w_buffer->b_ml.ml_line_count) |
| 2364 | ++lnum; |
| 2365 | else if (!p_ws) |
| 2366 | break; /* at first line and 'nowrapscan' */ |
| 2367 | else |
| 2368 | { |
| 2369 | /* Wrap around to the start of the buffer. May search the |
| 2370 | * starting line again and accept the first match. */ |
| 2371 | lnum = 1; |
| 2372 | wrapped = TRUE; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 2373 | if (!shortmess(SHM_SEARCH)) |
| 2374 | give_warning((char_u *)_(bot_top_msg), TRUE); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | /* If we are back at the starting line and there is no match then |
| 2378 | * give up. */ |
| 2379 | if (lnum == wp->w_cursor.lnum && !found_one) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2380 | break; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2381 | |
| 2382 | /* Skip the characters at the start of the next line that were |
| 2383 | * included in a match crossing line boundaries. */ |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2384 | if (attr == HLF_COUNT) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2385 | skip = (int)(p - endp); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2386 | else |
| 2387 | skip = 0; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2388 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2389 | /* Capcol skips over the inserted space. */ |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2390 | --capcol; |
| 2391 | |
| 2392 | /* But after empty line check first word in next line */ |
| 2393 | if (*skipwhite(line) == NUL) |
| 2394 | capcol = 0; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2395 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2396 | |
| 2397 | line_breakcheck(); |
| 2398 | } |
| 2399 | |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2400 | vim_free(buf); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2401 | return 0; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * For spell checking: concatenate the start of the following line "line" into |
| 2406 | * "buf", blanking-out special characters. Copy less then "maxlen" bytes. |
| Bram Moolenaar | 6a5d2ac | 2008-04-01 15:14:36 +0000 | [diff] [blame] | 2407 | * Keep the blanks at the start of the next line, this is used in win_line() |
| 2408 | * to skip those bytes if the word was OK. |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2409 | */ |
| 2410 | void |
| 2411 | spell_cat_line(buf, line, maxlen) |
| 2412 | char_u *buf; |
| 2413 | char_u *line; |
| 2414 | int maxlen; |
| 2415 | { |
| 2416 | char_u *p; |
| 2417 | int n; |
| 2418 | |
| 2419 | p = skipwhite(line); |
| 2420 | while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) |
| 2421 | p = skipwhite(p + 1); |
| 2422 | |
| 2423 | if (*p != NUL) |
| 2424 | { |
| Bram Moolenaar | 6a5d2ac | 2008-04-01 15:14:36 +0000 | [diff] [blame] | 2425 | /* Only worth concatenating if there is something else than spaces to |
| 2426 | * concatenate. */ |
| 2427 | n = (int)(p - line) + 1; |
| 2428 | if (n < maxlen - 1) |
| 2429 | { |
| 2430 | vim_memset(buf, ' ', n); |
| 2431 | vim_strncpy(buf + n, p, maxlen - 1 - n); |
| 2432 | } |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 2433 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 2436 | /* |
| 2437 | * Structure used for the cookie argument of do_in_runtimepath(). |
| 2438 | */ |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2439 | typedef struct spelload_S |
| 2440 | { |
| 2441 | char_u sl_lang[MAXWLEN + 1]; /* language name */ |
| 2442 | slang_T *sl_slang; /* resulting slang_T struct */ |
| 2443 | int sl_nobreak; /* NOBREAK language found */ |
| 2444 | } spelload_T; |
| 2445 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2446 | /* |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2447 | * Load word list(s) for "lang" from Vim spell file(s). |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2448 | * "lang" must be the language without the region: e.g., "en". |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2449 | */ |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2450 | static void |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2451 | spell_load_lang(lang) |
| 2452 | char_u *lang; |
| 2453 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2454 | char_u fname_enc[85]; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2455 | int r; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2456 | spelload_T sl; |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2457 | #ifdef FEAT_AUTOCMD |
| 2458 | int round; |
| 2459 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2460 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2461 | /* 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] | 2462 | * It's truncated when an error is detected. */ |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2463 | STRCPY(sl.sl_lang, lang); |
| 2464 | sl.sl_slang = NULL; |
| 2465 | sl.sl_nobreak = FALSE; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2466 | |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2467 | #ifdef FEAT_AUTOCMD |
| 2468 | /* We may retry when no spell file is found for the language, an |
| 2469 | * autocommand may load it then. */ |
| 2470 | for (round = 1; round <= 2; ++round) |
| 2471 | #endif |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2472 | { |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2473 | /* |
| 2474 | * Find the first spell file for "lang" in 'runtimepath' and load it. |
| 2475 | */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2476 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2477 | "spell/%s.%s.spl", lang, spell_enc()); |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2478 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl); |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2479 | |
| 2480 | if (r == FAIL && *sl.sl_lang != NUL) |
| 2481 | { |
| 2482 | /* Try loading the ASCII version. */ |
| 2483 | vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, |
| 2484 | "spell/%s.ascii.spl", lang); |
| 2485 | r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl); |
| 2486 | |
| 2487 | #ifdef FEAT_AUTOCMD |
| 2488 | if (r == FAIL && *sl.sl_lang != NUL && round == 1 |
| 2489 | && apply_autocmds(EVENT_SPELLFILEMISSING, lang, |
| 2490 | curbuf->b_fname, FALSE, curbuf)) |
| 2491 | continue; |
| 2492 | break; |
| 2493 | #endif |
| 2494 | } |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2495 | #ifdef FEAT_AUTOCMD |
| 2496 | break; |
| 2497 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2500 | if (r == FAIL) |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2501 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2502 | smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""), |
| 2503 | lang, spell_enc(), lang); |
| Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 2504 | } |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2505 | else if (sl.sl_slang != NULL) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2506 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 2507 | /* At least one file was loaded, now load ALL the additions. */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2508 | STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2509 | do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | /* |
| 2514 | * Return the encoding used for spell checking: Use 'encoding', except that we |
| 2515 | * use "latin1" for "latin9". And limit to 60 characters (just in case). |
| 2516 | */ |
| 2517 | static char_u * |
| 2518 | spell_enc() |
| 2519 | { |
| 2520 | |
| 2521 | #ifdef FEAT_MBYTE |
| 2522 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 2523 | return p_enc; |
| 2524 | #endif |
| 2525 | return (char_u *)"latin1"; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | /* |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 2529 | * Get the name of the .spl file for the internal wordlist into |
| 2530 | * "fname[MAXPATHL]". |
| 2531 | */ |
| 2532 | static void |
| 2533 | int_wordlist_spl(fname) |
| 2534 | char_u *fname; |
| 2535 | { |
| 2536 | vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl", |
| 2537 | int_wordlist, spell_enc()); |
| 2538 | } |
| 2539 | |
| 2540 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2541 | * Allocate a new slang_T for language "lang". "lang" can be NULL. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2542 | * Caller must fill "sl_next". |
| 2543 | */ |
| 2544 | static slang_T * |
| 2545 | slang_alloc(lang) |
| 2546 | char_u *lang; |
| 2547 | { |
| 2548 | slang_T *lp; |
| 2549 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2550 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2551 | if (lp != NULL) |
| 2552 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2553 | if (lang != NULL) |
| 2554 | lp->sl_name = vim_strsave(lang); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2555 | ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2556 | ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2557 | lp->sl_compmax = MAXWLEN; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2558 | lp->sl_compsylmax = MAXWLEN; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2559 | hash_init(&lp->sl_wordcount); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2560 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2561 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2562 | return lp; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Free the contents of an slang_T and the structure itself. |
| 2567 | */ |
| 2568 | static void |
| 2569 | slang_free(lp) |
| 2570 | slang_T *lp; |
| 2571 | { |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2572 | vim_free(lp->sl_name); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2573 | vim_free(lp->sl_fname); |
| 2574 | slang_clear(lp); |
| 2575 | vim_free(lp); |
| 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * Clear an slang_T so that the file can be reloaded. |
| 2580 | */ |
| 2581 | static void |
| 2582 | slang_clear(lp) |
| 2583 | slang_T *lp; |
| 2584 | { |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2585 | garray_T *gap; |
| 2586 | fromto_T *ftp; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2587 | salitem_T *smp; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2588 | int i; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2589 | int round; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2590 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2591 | vim_free(lp->sl_fbyts); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2592 | lp->sl_fbyts = NULL; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2593 | vim_free(lp->sl_kbyts); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2594 | lp->sl_kbyts = NULL; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2595 | vim_free(lp->sl_pbyts); |
| 2596 | lp->sl_pbyts = NULL; |
| 2597 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2598 | vim_free(lp->sl_fidxs); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2599 | lp->sl_fidxs = NULL; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2600 | vim_free(lp->sl_kidxs); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2601 | lp->sl_kidxs = NULL; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2602 | vim_free(lp->sl_pidxs); |
| 2603 | lp->sl_pidxs = NULL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2604 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2605 | for (round = 1; round <= 2; ++round) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2606 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2607 | gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal; |
| 2608 | while (gap->ga_len > 0) |
| 2609 | { |
| 2610 | ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; |
| 2611 | vim_free(ftp->ft_from); |
| 2612 | vim_free(ftp->ft_to); |
| 2613 | } |
| 2614 | ga_clear(gap); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2615 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2616 | |
| 2617 | gap = &lp->sl_sal; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2618 | if (lp->sl_sofo) |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2619 | { |
| 2620 | /* "ga_len" is set to 1 without adding an item for latin1 */ |
| 2621 | if (gap->ga_data != NULL) |
| 2622 | /* SOFOFROM and SOFOTO items: free lists of wide characters. */ |
| 2623 | for (i = 0; i < gap->ga_len; ++i) |
| 2624 | vim_free(((int **)gap->ga_data)[i]); |
| 2625 | } |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2626 | else |
| 2627 | /* SAL items: free salitem_T items */ |
| 2628 | while (gap->ga_len > 0) |
| 2629 | { |
| 2630 | smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; |
| 2631 | vim_free(smp->sm_lead); |
| 2632 | /* Don't free sm_oneof and sm_rules, they point into sm_lead. */ |
| 2633 | vim_free(smp->sm_to); |
| 2634 | #ifdef FEAT_MBYTE |
| 2635 | vim_free(smp->sm_lead_w); |
| 2636 | vim_free(smp->sm_oneof_w); |
| 2637 | vim_free(smp->sm_to_w); |
| 2638 | #endif |
| 2639 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2640 | ga_clear(gap); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2641 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2642 | for (i = 0; i < lp->sl_prefixcnt; ++i) |
| 2643 | vim_free(lp->sl_prefprog[i]); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2644 | lp->sl_prefixcnt = 0; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2645 | vim_free(lp->sl_prefprog); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2646 | lp->sl_prefprog = NULL; |
| 2647 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2648 | vim_free(lp->sl_info); |
| 2649 | lp->sl_info = NULL; |
| 2650 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2651 | vim_free(lp->sl_midword); |
| 2652 | lp->sl_midword = NULL; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2653 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2654 | vim_free(lp->sl_compprog); |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 2655 | vim_free(lp->sl_comprules); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2656 | vim_free(lp->sl_compstartflags); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 2657 | vim_free(lp->sl_compallflags); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2658 | lp->sl_compprog = NULL; |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 2659 | lp->sl_comprules = NULL; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2660 | lp->sl_compstartflags = NULL; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 2661 | lp->sl_compallflags = NULL; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2662 | |
| 2663 | vim_free(lp->sl_syllable); |
| 2664 | lp->sl_syllable = NULL; |
| 2665 | ga_clear(&lp->sl_syl_items); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2666 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 2667 | ga_clear_strings(&lp->sl_comppat); |
| 2668 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2669 | hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF); |
| 2670 | hash_init(&lp->sl_wordcount); |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 2671 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2672 | #ifdef FEAT_MBYTE |
| 2673 | hash_clear_all(&lp->sl_map_hash, 0); |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 2674 | #endif |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2675 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2676 | /* Clear info from .sug file. */ |
| 2677 | slang_clear_sug(lp); |
| 2678 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2679 | lp->sl_compmax = MAXWLEN; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2680 | lp->sl_compminlen = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2681 | lp->sl_compsylmax = MAXWLEN; |
| 2682 | lp->sl_regions[0] = NUL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2686 | * Clear the info from the .sug file in "lp". |
| 2687 | */ |
| 2688 | static void |
| 2689 | slang_clear_sug(lp) |
| 2690 | slang_T *lp; |
| 2691 | { |
| 2692 | vim_free(lp->sl_sbyts); |
| 2693 | lp->sl_sbyts = NULL; |
| 2694 | vim_free(lp->sl_sidxs); |
| 2695 | lp->sl_sidxs = NULL; |
| 2696 | close_spellbuf(lp->sl_sugbuf); |
| 2697 | lp->sl_sugbuf = NULL; |
| 2698 | lp->sl_sugloaded = FALSE; |
| 2699 | lp->sl_sugtime = 0; |
| 2700 | } |
| 2701 | |
| 2702 | /* |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2703 | * Load one spell file and store the info into a slang_T. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2704 | * Invoked through do_in_runtimepath(). |
| 2705 | */ |
| 2706 | static void |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2707 | spell_load_cb(fname, cookie) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2708 | char_u *fname; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2709 | void *cookie; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2710 | { |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 2711 | spelload_T *slp = (spelload_T *)cookie; |
| 2712 | slang_T *slang; |
| 2713 | |
| 2714 | slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE); |
| 2715 | if (slang != NULL) |
| 2716 | { |
| 2717 | /* When a previously loaded file has NOBREAK also use it for the |
| 2718 | * ".add" files. */ |
| 2719 | if (slp->sl_nobreak && slang->sl_add) |
| 2720 | slang->sl_nobreak = TRUE; |
| 2721 | else if (slang->sl_nobreak) |
| 2722 | slp->sl_nobreak = TRUE; |
| 2723 | |
| 2724 | slp->sl_slang = slang; |
| 2725 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | /* |
| 2729 | * Load one spell file and store the info into a slang_T. |
| 2730 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2731 | * This is invoked in three ways: |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2732 | * - From spell_load_cb() to load a spell file for the first time. "lang" is |
| 2733 | * the language name, "old_lp" is NULL. Will allocate an slang_T. |
| 2734 | * - To reload a spell file that was changed. "lang" is NULL and "old_lp" |
| 2735 | * points to the existing slang_T. |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2736 | * - Just after writing a .spl file; it's read back to produce the .sug file. |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 2737 | * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T. |
| 2738 | * |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2739 | * 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] | 2740 | */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2741 | static slang_T * |
| 2742 | spell_load_file(fname, lang, old_lp, silent) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2743 | char_u *fname; |
| 2744 | char_u *lang; |
| 2745 | slang_T *old_lp; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2746 | int silent; /* no error if file doesn't exist */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2747 | { |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2748 | FILE *fd; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2749 | char_u buf[VIMSPELLMAGICL]; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2750 | char_u *p; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2751 | int i; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2752 | int n; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2753 | int len; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2754 | char_u *save_sourcing_name = sourcing_name; |
| 2755 | linenr_T save_sourcing_lnum = sourcing_lnum; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2756 | slang_T *lp = NULL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2757 | int c = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2758 | int res; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2759 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2760 | fd = mch_fopen((char *)fname, "r"); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2761 | if (fd == NULL) |
| 2762 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2763 | if (!silent) |
| 2764 | EMSG2(_(e_notopen), fname); |
| 2765 | else if (p_verbose > 2) |
| 2766 | { |
| 2767 | verbose_enter(); |
| 2768 | smsg((char_u *)e_notopen, fname); |
| 2769 | verbose_leave(); |
| 2770 | } |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2771 | goto endFAIL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2772 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2773 | if (p_verbose > 2) |
| 2774 | { |
| 2775 | verbose_enter(); |
| 2776 | smsg((char_u *)_("Reading spell file \"%s\""), fname); |
| 2777 | verbose_leave(); |
| 2778 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2779 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2780 | if (old_lp == NULL) |
| 2781 | { |
| 2782 | lp = slang_alloc(lang); |
| 2783 | if (lp == NULL) |
| 2784 | goto endFAIL; |
| 2785 | |
| 2786 | /* Remember the file name, used to reload the file when it's updated. */ |
| 2787 | lp->sl_fname = vim_strsave(fname); |
| 2788 | if (lp->sl_fname == NULL) |
| 2789 | goto endFAIL; |
| 2790 | |
| 2791 | /* Check for .add.spl. */ |
| 2792 | lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL; |
| 2793 | } |
| 2794 | else |
| 2795 | lp = old_lp; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2796 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2797 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 2798 | sourcing_name = fname; |
| 2799 | sourcing_lnum = 0; |
| 2800 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2801 | /* |
| 2802 | * <HEADER>: <fileID> |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2803 | */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2804 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 2805 | buf[i] = getc(fd); /* <fileID> */ |
| 2806 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 2807 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2808 | EMSG(_("E757: This does not look like a spell file")); |
| 2809 | goto endFAIL; |
| 2810 | } |
| 2811 | c = getc(fd); /* <versionnr> */ |
| 2812 | if (c < VIMSPELLVERSION) |
| 2813 | { |
| 2814 | EMSG(_("E771: Old spell file, needs to be updated")); |
| 2815 | goto endFAIL; |
| 2816 | } |
| 2817 | else if (c > VIMSPELLVERSION) |
| 2818 | { |
| 2819 | EMSG(_("E772: Spell file is for newer version of Vim")); |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2820 | goto endFAIL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2823 | |
| 2824 | /* |
| 2825 | * <SECTIONS>: <section> ... <sectionend> |
| 2826 | * <section>: <sectionID> <sectionflags> <sectionlen> (section contents) |
| 2827 | */ |
| 2828 | for (;;) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2829 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2830 | n = getc(fd); /* <sectionID> or <sectionend> */ |
| 2831 | if (n == SN_END) |
| 2832 | break; |
| 2833 | c = getc(fd); /* <sectionflags> */ |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 2834 | len = get4c(fd); /* <sectionlen> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2835 | if (len < 0) |
| 2836 | goto truncerr; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2837 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2838 | res = 0; |
| 2839 | switch (n) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2840 | { |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2841 | case SN_INFO: |
| 2842 | lp->sl_info = read_string(fd, len); /* <infotext> */ |
| 2843 | if (lp->sl_info == NULL) |
| 2844 | goto endFAIL; |
| 2845 | break; |
| 2846 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2847 | case SN_REGION: |
| 2848 | res = read_region_section(fd, lp, len); |
| 2849 | break; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2850 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2851 | case SN_CHARFLAGS: |
| 2852 | res = read_charflags_section(fd); |
| 2853 | break; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2854 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2855 | case SN_MIDWORD: |
| 2856 | lp->sl_midword = read_string(fd, len); /* <midword> */ |
| 2857 | if (lp->sl_midword == NULL) |
| 2858 | goto endFAIL; |
| 2859 | break; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2860 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2861 | case SN_PREFCOND: |
| 2862 | res = read_prefcond_section(fd, lp); |
| 2863 | break; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2864 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2865 | case SN_REP: |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2866 | res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first); |
| 2867 | break; |
| 2868 | |
| 2869 | case SN_REPSAL: |
| 2870 | res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2871 | break; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2872 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2873 | case SN_SAL: |
| 2874 | res = read_sal_section(fd, lp); |
| 2875 | break; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2876 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2877 | case SN_SOFO: |
| 2878 | res = read_sofo_section(fd, lp); |
| 2879 | break; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 2880 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2881 | case SN_MAP: |
| 2882 | p = read_string(fd, len); /* <mapstr> */ |
| 2883 | if (p == NULL) |
| 2884 | goto endFAIL; |
| 2885 | set_map_str(lp, p); |
| 2886 | vim_free(p); |
| 2887 | break; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2888 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2889 | case SN_WORDS: |
| 2890 | res = read_words_section(fd, lp, len); |
| 2891 | break; |
| 2892 | |
| 2893 | case SN_SUGFILE: |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 2894 | lp->sl_sugtime = get8c(fd); /* <timestamp> */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2895 | break; |
| 2896 | |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 2897 | case SN_NOSPLITSUGS: |
| 2898 | lp->sl_nosplitsugs = TRUE; /* <timestamp> */ |
| 2899 | break; |
| 2900 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2901 | case SN_COMPOUND: |
| 2902 | res = read_compound(fd, lp, len); |
| 2903 | break; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2904 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 2905 | case SN_NOBREAK: |
| 2906 | lp->sl_nobreak = TRUE; |
| 2907 | break; |
| 2908 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2909 | case SN_SYLLABLE: |
| 2910 | lp->sl_syllable = read_string(fd, len); /* <syllable> */ |
| 2911 | if (lp->sl_syllable == NULL) |
| 2912 | goto endFAIL; |
| 2913 | if (init_syl_tab(lp) == FAIL) |
| 2914 | goto endFAIL; |
| 2915 | break; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2916 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2917 | default: |
| 2918 | /* Unsupported section. When it's required give an error |
| 2919 | * message. When it's not required skip the contents. */ |
| 2920 | if (c & SNF_REQUIRED) |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2921 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2922 | EMSG(_("E770: Unsupported section in spell file")); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 2923 | goto endFAIL; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2924 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2925 | while (--len >= 0) |
| 2926 | if (getc(fd) < 0) |
| 2927 | goto truncerr; |
| 2928 | break; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 2929 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2930 | someerror: |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2931 | if (res == SP_FORMERROR) |
| 2932 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2933 | EMSG(_(e_format)); |
| 2934 | goto endFAIL; |
| 2935 | } |
| 2936 | if (res == SP_TRUNCERROR) |
| 2937 | { |
| 2938 | truncerr: |
| 2939 | EMSG(_(e_spell_trunc)); |
| 2940 | goto endFAIL; |
| 2941 | } |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 2942 | if (res == SP_OTHERERROR) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 2943 | goto endFAIL; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 2944 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2945 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2946 | /* <LWORDTREE> */ |
| 2947 | res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0); |
| 2948 | if (res != 0) |
| 2949 | goto someerror; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2950 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2951 | /* <KWORDTREE> */ |
| 2952 | res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0); |
| 2953 | if (res != 0) |
| 2954 | goto someerror; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2955 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 2956 | /* <PREFIXTREE> */ |
| 2957 | res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE, |
| 2958 | lp->sl_prefixcnt); |
| 2959 | if (res != 0) |
| 2960 | goto someerror; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2961 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2962 | /* For a new file link it in the list of spell files. */ |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 2963 | if (old_lp == NULL && lang != NULL) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2964 | { |
| 2965 | lp->sl_next = first_lang; |
| 2966 | first_lang = lp; |
| 2967 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2968 | |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2969 | goto endOK; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2970 | |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2971 | endFAIL: |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 2972 | if (lang != NULL) |
| 2973 | /* truncating the name signals the error to spell_load_lang() */ |
| 2974 | *lang = NUL; |
| 2975 | if (lp != NULL && old_lp == NULL) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2976 | slang_free(lp); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 2977 | lp = NULL; |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2978 | |
| 2979 | endOK: |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2980 | if (fd != NULL) |
| 2981 | fclose(fd); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2982 | sourcing_name = save_sourcing_name; |
| 2983 | sourcing_lnum = save_sourcing_lnum; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2984 | |
| 2985 | return lp; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 2988 | /* |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 2989 | * Read 2 bytes from "fd" and turn them into an int, MSB first. |
| 2990 | */ |
| 2991 | static int |
| 2992 | get2c(fd) |
| 2993 | FILE *fd; |
| 2994 | { |
| 2995 | long n; |
| 2996 | |
| 2997 | n = getc(fd); |
| 2998 | n = (n << 8) + getc(fd); |
| 2999 | return n; |
| 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | * Read 3 bytes from "fd" and turn them into an int, MSB first. |
| 3004 | */ |
| 3005 | static int |
| 3006 | get3c(fd) |
| 3007 | FILE *fd; |
| 3008 | { |
| 3009 | long n; |
| 3010 | |
| 3011 | n = getc(fd); |
| 3012 | n = (n << 8) + getc(fd); |
| 3013 | n = (n << 8) + getc(fd); |
| 3014 | return n; |
| 3015 | } |
| 3016 | |
| 3017 | /* |
| 3018 | * Read 4 bytes from "fd" and turn them into an int, MSB first. |
| 3019 | */ |
| 3020 | static int |
| 3021 | get4c(fd) |
| 3022 | FILE *fd; |
| 3023 | { |
| 3024 | long n; |
| 3025 | |
| 3026 | n = getc(fd); |
| 3027 | n = (n << 8) + getc(fd); |
| 3028 | n = (n << 8) + getc(fd); |
| 3029 | n = (n << 8) + getc(fd); |
| 3030 | return n; |
| 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * Read 8 bytes from "fd" and turn them into a time_t, MSB first. |
| 3035 | */ |
| 3036 | static time_t |
| 3037 | get8c(fd) |
| 3038 | FILE *fd; |
| 3039 | { |
| 3040 | time_t n = 0; |
| 3041 | int i; |
| 3042 | |
| 3043 | for (i = 0; i < 8; ++i) |
| 3044 | n = (n << 8) + getc(fd); |
| 3045 | return n; |
| 3046 | } |
| 3047 | |
| 3048 | /* |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3049 | * Read a length field from "fd" in "cnt_bytes" bytes. |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3050 | * Allocate memory, read the string into it and add a NUL at the end. |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3051 | * Returns NULL when the count is zero. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3052 | * Sets "*cntp" to SP_*ERROR when there is an error, length of the result |
| 3053 | * otherwise. |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3054 | */ |
| 3055 | static char_u * |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 3056 | read_cnt_string(fd, cnt_bytes, cntp) |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3057 | FILE *fd; |
| 3058 | int cnt_bytes; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 3059 | int *cntp; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3060 | { |
| 3061 | int cnt = 0; |
| 3062 | int i; |
| 3063 | char_u *str; |
| 3064 | |
| 3065 | /* read the length bytes, MSB first */ |
| 3066 | for (i = 0; i < cnt_bytes; ++i) |
| 3067 | cnt = (cnt << 8) + getc(fd); |
| 3068 | if (cnt < 0) |
| 3069 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3070 | *cntp = SP_TRUNCERROR; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3071 | return NULL; |
| 3072 | } |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 3073 | *cntp = cnt; |
| 3074 | if (cnt == 0) |
| 3075 | return NULL; /* nothing to read, return NULL */ |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3076 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3077 | str = read_string(fd, cnt); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3078 | if (str == NULL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3079 | *cntp = SP_OTHERERROR; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 3080 | return str; |
| 3081 | } |
| 3082 | |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3083 | /* |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3084 | * Read a string of length "cnt" from "fd" into allocated memory. |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3085 | * Returns NULL when out of memory or unable to read that many bytes. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3086 | */ |
| 3087 | static char_u * |
| 3088 | read_string(fd, cnt) |
| 3089 | FILE *fd; |
| 3090 | int cnt; |
| 3091 | { |
| 3092 | char_u *str; |
| 3093 | int i; |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3094 | int c; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3095 | |
| 3096 | /* allocate memory */ |
| 3097 | str = alloc((unsigned)cnt + 1); |
| 3098 | if (str != NULL) |
| 3099 | { |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3100 | /* Read the string. Quit when running into the EOF. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3101 | for (i = 0; i < cnt; ++i) |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3102 | { |
| 3103 | c = getc(fd); |
| 3104 | if (c == EOF) |
| 3105 | { |
| 3106 | vim_free(str); |
| 3107 | return NULL; |
| 3108 | } |
| 3109 | str[i] = c; |
| 3110 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3111 | str[i] = NUL; |
| 3112 | } |
| 3113 | return str; |
| 3114 | } |
| 3115 | |
| 3116 | /* |
| 3117 | * Read SN_REGION: <regionname> ... |
| 3118 | * Return SP_*ERROR flags. |
| 3119 | */ |
| 3120 | static int |
| 3121 | read_region_section(fd, lp, len) |
| 3122 | FILE *fd; |
| 3123 | slang_T *lp; |
| 3124 | int len; |
| 3125 | { |
| 3126 | int i; |
| 3127 | |
| 3128 | if (len > 16) |
| 3129 | return SP_FORMERROR; |
| 3130 | for (i = 0; i < len; ++i) |
| 3131 | lp->sl_regions[i] = getc(fd); /* <regionname> */ |
| 3132 | lp->sl_regions[len] = NUL; |
| 3133 | return 0; |
| 3134 | } |
| 3135 | |
| 3136 | /* |
| 3137 | * Read SN_CHARFLAGS section: <charflagslen> <charflags> |
| 3138 | * <folcharslen> <folchars> |
| 3139 | * Return SP_*ERROR flags. |
| 3140 | */ |
| 3141 | static int |
| 3142 | read_charflags_section(fd) |
| 3143 | FILE *fd; |
| 3144 | { |
| 3145 | char_u *flags; |
| 3146 | char_u *fol; |
| 3147 | int flagslen, follen; |
| 3148 | |
| 3149 | /* <charflagslen> <charflags> */ |
| 3150 | flags = read_cnt_string(fd, 1, &flagslen); |
| 3151 | if (flagslen < 0) |
| 3152 | return flagslen; |
| 3153 | |
| 3154 | /* <folcharslen> <folchars> */ |
| 3155 | fol = read_cnt_string(fd, 2, &follen); |
| 3156 | if (follen < 0) |
| 3157 | { |
| 3158 | vim_free(flags); |
| 3159 | return follen; |
| 3160 | } |
| 3161 | |
| 3162 | /* Set the word-char flags and fill SPELL_ISUPPER() table. */ |
| 3163 | if (flags != NULL && fol != NULL) |
| 3164 | set_spell_charflags(flags, flagslen, fol); |
| 3165 | |
| 3166 | vim_free(flags); |
| 3167 | vim_free(fol); |
| 3168 | |
| 3169 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 3170 | if ((flags == NULL) != (fol == NULL)) |
| 3171 | return SP_FORMERROR; |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
| 3175 | /* |
| 3176 | * Read SN_PREFCOND section. |
| 3177 | * Return SP_*ERROR flags. |
| 3178 | */ |
| 3179 | static int |
| 3180 | read_prefcond_section(fd, lp) |
| 3181 | FILE *fd; |
| 3182 | slang_T *lp; |
| 3183 | { |
| 3184 | int cnt; |
| 3185 | int i; |
| 3186 | int n; |
| 3187 | char_u *p; |
| 3188 | char_u buf[MAXWLEN + 1]; |
| 3189 | |
| 3190 | /* <prefcondcnt> <prefcond> ... */ |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 3191 | cnt = get2c(fd); /* <prefcondcnt> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3192 | if (cnt <= 0) |
| 3193 | return SP_FORMERROR; |
| 3194 | |
| 3195 | lp->sl_prefprog = (regprog_T **)alloc_clear( |
| 3196 | (unsigned)sizeof(regprog_T *) * cnt); |
| 3197 | if (lp->sl_prefprog == NULL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3198 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3199 | lp->sl_prefixcnt = cnt; |
| 3200 | |
| 3201 | for (i = 0; i < cnt; ++i) |
| 3202 | { |
| 3203 | /* <prefcond> : <condlen> <condstr> */ |
| 3204 | n = getc(fd); /* <condlen> */ |
| 3205 | if (n < 0 || n >= MAXWLEN) |
| 3206 | return SP_FORMERROR; |
| 3207 | |
| 3208 | /* When <condlen> is zero we have an empty condition. Otherwise |
| 3209 | * compile the regexp program used to check for the condition. */ |
| 3210 | if (n > 0) |
| 3211 | { |
| 3212 | buf[0] = '^'; /* always match at one position only */ |
| 3213 | p = buf + 1; |
| 3214 | while (n-- > 0) |
| 3215 | *p++ = getc(fd); /* <condstr> */ |
| 3216 | *p = NUL; |
| 3217 | lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 3218 | } |
| 3219 | } |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
| 3223 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3224 | * Read REP or REPSAL items section from "fd": <repcount> <rep> ... |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3225 | * Return SP_*ERROR flags. |
| 3226 | */ |
| 3227 | static int |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3228 | read_rep_section(fd, gap, first) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3229 | FILE *fd; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3230 | garray_T *gap; |
| 3231 | short *first; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3232 | { |
| 3233 | int cnt; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3234 | fromto_T *ftp; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3235 | int i; |
| 3236 | |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 3237 | cnt = get2c(fd); /* <repcount> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3238 | if (cnt < 0) |
| 3239 | return SP_TRUNCERROR; |
| 3240 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3241 | if (ga_grow(gap, cnt) == FAIL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3242 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3243 | |
| 3244 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 3245 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 3246 | { |
| 3247 | ftp = &((fromto_T *)gap->ga_data)[gap->ga_len]; |
| 3248 | ftp->ft_from = read_cnt_string(fd, 1, &i); |
| 3249 | if (i < 0) |
| 3250 | return i; |
| 3251 | if (i == 0) |
| 3252 | return SP_FORMERROR; |
| 3253 | ftp->ft_to = read_cnt_string(fd, 1, &i); |
| 3254 | if (i <= 0) |
| 3255 | { |
| 3256 | vim_free(ftp->ft_from); |
| 3257 | if (i < 0) |
| 3258 | return i; |
| 3259 | return SP_FORMERROR; |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | /* Fill the first-index table. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3264 | for (i = 0; i < 256; ++i) |
| 3265 | first[i] = -1; |
| 3266 | for (i = 0; i < gap->ga_len; ++i) |
| 3267 | { |
| 3268 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 3269 | if (first[*ftp->ft_from] == -1) |
| 3270 | first[*ftp->ft_from] = i; |
| 3271 | } |
| 3272 | return 0; |
| 3273 | } |
| 3274 | |
| 3275 | /* |
| 3276 | * Read SN_SAL section: <salflags> <salcount> <sal> ... |
| 3277 | * Return SP_*ERROR flags. |
| 3278 | */ |
| 3279 | static int |
| 3280 | read_sal_section(fd, slang) |
| 3281 | FILE *fd; |
| 3282 | slang_T *slang; |
| 3283 | { |
| 3284 | int i; |
| 3285 | int cnt; |
| 3286 | garray_T *gap; |
| 3287 | salitem_T *smp; |
| 3288 | int ccnt; |
| 3289 | char_u *p; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3290 | int c = NUL; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3291 | |
| 3292 | slang->sl_sofo = FALSE; |
| 3293 | |
| 3294 | i = getc(fd); /* <salflags> */ |
| 3295 | if (i & SAL_F0LLOWUP) |
| 3296 | slang->sl_followup = TRUE; |
| 3297 | if (i & SAL_COLLAPSE) |
| 3298 | slang->sl_collapse = TRUE; |
| 3299 | if (i & SAL_REM_ACCENTS) |
| 3300 | slang->sl_rem_accents = TRUE; |
| 3301 | |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 3302 | cnt = get2c(fd); /* <salcount> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3303 | if (cnt < 0) |
| 3304 | return SP_TRUNCERROR; |
| 3305 | |
| 3306 | gap = &slang->sl_sal; |
| 3307 | ga_init2(gap, sizeof(salitem_T), 10); |
| Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3308 | if (ga_grow(gap, cnt + 1) == FAIL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3309 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3310 | |
| 3311 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 3312 | for (; gap->ga_len < cnt; ++gap->ga_len) |
| 3313 | { |
| 3314 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 3315 | ccnt = getc(fd); /* <salfromlen> */ |
| 3316 | if (ccnt < 0) |
| 3317 | return SP_TRUNCERROR; |
| 3318 | if ((p = alloc(ccnt + 2)) == NULL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3319 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3320 | smp->sm_lead = p; |
| 3321 | |
| 3322 | /* Read up to the first special char into sm_lead. */ |
| 3323 | for (i = 0; i < ccnt; ++i) |
| 3324 | { |
| 3325 | c = getc(fd); /* <salfrom> */ |
| 3326 | if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL) |
| 3327 | break; |
| 3328 | *p++ = c; |
| 3329 | } |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3330 | smp->sm_leadlen = (int)(p - smp->sm_lead); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3331 | *p++ = NUL; |
| 3332 | |
| 3333 | /* Put (abc) chars in sm_oneof, if any. */ |
| 3334 | if (c == '(') |
| 3335 | { |
| 3336 | smp->sm_oneof = p; |
| 3337 | for (++i; i < ccnt; ++i) |
| 3338 | { |
| 3339 | c = getc(fd); /* <salfrom> */ |
| 3340 | if (c == ')') |
| 3341 | break; |
| 3342 | *p++ = c; |
| 3343 | } |
| 3344 | *p++ = NUL; |
| 3345 | if (++i < ccnt) |
| 3346 | c = getc(fd); |
| 3347 | } |
| 3348 | else |
| 3349 | smp->sm_oneof = NULL; |
| 3350 | |
| 3351 | /* Any following chars go in sm_rules. */ |
| 3352 | smp->sm_rules = p; |
| 3353 | if (i < ccnt) |
| 3354 | /* store the char we got while checking for end of sm_lead */ |
| 3355 | *p++ = c; |
| 3356 | for (++i; i < ccnt; ++i) |
| 3357 | *p++ = getc(fd); /* <salfrom> */ |
| 3358 | *p++ = NUL; |
| 3359 | |
| 3360 | /* <saltolen> <salto> */ |
| 3361 | smp->sm_to = read_cnt_string(fd, 1, &ccnt); |
| 3362 | if (ccnt < 0) |
| 3363 | { |
| 3364 | vim_free(smp->sm_lead); |
| 3365 | return ccnt; |
| 3366 | } |
| 3367 | |
| 3368 | #ifdef FEAT_MBYTE |
| 3369 | if (has_mbyte) |
| 3370 | { |
| 3371 | /* convert the multi-byte strings to wide char strings */ |
| 3372 | smp->sm_lead_w = mb_str2wide(smp->sm_lead); |
| 3373 | smp->sm_leadlen = mb_charlen(smp->sm_lead); |
| 3374 | if (smp->sm_oneof == NULL) |
| 3375 | smp->sm_oneof_w = NULL; |
| 3376 | else |
| 3377 | smp->sm_oneof_w = mb_str2wide(smp->sm_oneof); |
| 3378 | if (smp->sm_to == NULL) |
| 3379 | smp->sm_to_w = NULL; |
| 3380 | else |
| 3381 | smp->sm_to_w = mb_str2wide(smp->sm_to); |
| 3382 | if (smp->sm_lead_w == NULL |
| 3383 | || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL) |
| 3384 | || (smp->sm_to_w == NULL && smp->sm_to != NULL)) |
| 3385 | { |
| 3386 | vim_free(smp->sm_lead); |
| 3387 | vim_free(smp->sm_to); |
| 3388 | vim_free(smp->sm_lead_w); |
| 3389 | vim_free(smp->sm_oneof_w); |
| 3390 | vim_free(smp->sm_to_w); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3391 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | #endif |
| 3395 | } |
| 3396 | |
| Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3397 | if (gap->ga_len > 0) |
| 3398 | { |
| 3399 | /* Add one extra entry to mark the end with an empty sm_lead. Avoids |
| 3400 | * that we need to check the index every time. */ |
| 3401 | smp = &((salitem_T *)gap->ga_data)[gap->ga_len]; |
| 3402 | if ((p = alloc(1)) == NULL) |
| 3403 | return SP_OTHERERROR; |
| 3404 | p[0] = NUL; |
| 3405 | smp->sm_lead = p; |
| 3406 | smp->sm_leadlen = 0; |
| 3407 | smp->sm_oneof = NULL; |
| 3408 | smp->sm_rules = p; |
| 3409 | smp->sm_to = NULL; |
| 3410 | #ifdef FEAT_MBYTE |
| 3411 | if (has_mbyte) |
| 3412 | { |
| 3413 | smp->sm_lead_w = mb_str2wide(smp->sm_lead); |
| 3414 | smp->sm_leadlen = 0; |
| 3415 | smp->sm_oneof_w = NULL; |
| 3416 | smp->sm_to_w = NULL; |
| 3417 | } |
| 3418 | #endif |
| 3419 | ++gap->ga_len; |
| 3420 | } |
| 3421 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3422 | /* Fill the first-index table. */ |
| 3423 | set_sal_first(slang); |
| 3424 | |
| 3425 | return 0; |
| 3426 | } |
| 3427 | |
| 3428 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3429 | * Read SN_WORDS: <word> ... |
| 3430 | * Return SP_*ERROR flags. |
| 3431 | */ |
| 3432 | static int |
| 3433 | read_words_section(fd, lp, len) |
| 3434 | FILE *fd; |
| 3435 | slang_T *lp; |
| 3436 | int len; |
| 3437 | { |
| 3438 | int done = 0; |
| 3439 | int i; |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3440 | int c; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3441 | char_u word[MAXWLEN]; |
| 3442 | |
| 3443 | while (done < len) |
| 3444 | { |
| 3445 | /* Read one word at a time. */ |
| 3446 | for (i = 0; ; ++i) |
| 3447 | { |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3448 | c = getc(fd); |
| 3449 | if (c == EOF) |
| 3450 | return SP_TRUNCERROR; |
| 3451 | word[i] = c; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3452 | if (word[i] == NUL) |
| 3453 | break; |
| 3454 | if (i == MAXWLEN - 1) |
| 3455 | return SP_FORMERROR; |
| 3456 | } |
| 3457 | |
| 3458 | /* Init the count to 10. */ |
| 3459 | count_common_word(lp, word, -1, 10); |
| 3460 | done += i + 1; |
| 3461 | } |
| 3462 | return 0; |
| 3463 | } |
| 3464 | |
| 3465 | /* |
| 3466 | * Add a word to the hashtable of common words. |
| 3467 | * If it's already there then the counter is increased. |
| 3468 | */ |
| 3469 | static void |
| 3470 | count_common_word(lp, word, len, count) |
| 3471 | slang_T *lp; |
| 3472 | char_u *word; |
| 3473 | int len; /* word length, -1 for upto NUL */ |
| 3474 | int count; /* 1 to count once, 10 to init */ |
| 3475 | { |
| 3476 | hash_T hash; |
| 3477 | hashitem_T *hi; |
| 3478 | wordcount_T *wc; |
| 3479 | char_u buf[MAXWLEN]; |
| 3480 | char_u *p; |
| 3481 | |
| 3482 | if (len == -1) |
| 3483 | p = word; |
| 3484 | else |
| 3485 | { |
| 3486 | vim_strncpy(buf, word, len); |
| 3487 | p = buf; |
| 3488 | } |
| 3489 | |
| 3490 | hash = hash_hash(p); |
| 3491 | hi = hash_lookup(&lp->sl_wordcount, p, hash); |
| 3492 | if (HASHITEM_EMPTY(hi)) |
| 3493 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3494 | wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p))); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 3495 | if (wc == NULL) |
| 3496 | return; |
| 3497 | STRCPY(wc->wc_word, p); |
| 3498 | wc->wc_count = count; |
| 3499 | hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash); |
| 3500 | } |
| 3501 | else |
| 3502 | { |
| 3503 | wc = HI2WC(hi); |
| 3504 | if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */ |
| 3505 | wc->wc_count = MAXWORDCOUNT; |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | /* |
| 3510 | * Adjust the score of common words. |
| 3511 | */ |
| 3512 | static int |
| 3513 | score_wordcount_adj(slang, score, word, split) |
| 3514 | slang_T *slang; |
| 3515 | int score; |
| 3516 | char_u *word; |
| 3517 | int split; /* word was split, less bonus */ |
| 3518 | { |
| 3519 | hashitem_T *hi; |
| 3520 | wordcount_T *wc; |
| 3521 | int bonus; |
| 3522 | int newscore; |
| 3523 | |
| 3524 | hi = hash_find(&slang->sl_wordcount, word); |
| 3525 | if (!HASHITEM_EMPTY(hi)) |
| 3526 | { |
| 3527 | wc = HI2WC(hi); |
| 3528 | if (wc->wc_count < SCORE_THRES2) |
| 3529 | bonus = SCORE_COMMON1; |
| 3530 | else if (wc->wc_count < SCORE_THRES3) |
| 3531 | bonus = SCORE_COMMON2; |
| 3532 | else |
| 3533 | bonus = SCORE_COMMON3; |
| 3534 | if (split) |
| 3535 | newscore = score - bonus / 2; |
| 3536 | else |
| 3537 | newscore = score - bonus; |
| 3538 | if (newscore < 0) |
| 3539 | return 0; |
| 3540 | return newscore; |
| 3541 | } |
| 3542 | return score; |
| 3543 | } |
| 3544 | |
| 3545 | /* |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3546 | * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto> |
| 3547 | * Return SP_*ERROR flags. |
| 3548 | */ |
| 3549 | static int |
| 3550 | read_sofo_section(fd, slang) |
| 3551 | FILE *fd; |
| 3552 | slang_T *slang; |
| 3553 | { |
| 3554 | int cnt; |
| 3555 | char_u *from, *to; |
| 3556 | int res; |
| 3557 | |
| 3558 | slang->sl_sofo = TRUE; |
| 3559 | |
| 3560 | /* <sofofromlen> <sofofrom> */ |
| 3561 | from = read_cnt_string(fd, 2, &cnt); |
| 3562 | if (cnt < 0) |
| 3563 | return cnt; |
| 3564 | |
| 3565 | /* <sofotolen> <sofoto> */ |
| 3566 | to = read_cnt_string(fd, 2, &cnt); |
| 3567 | if (cnt < 0) |
| 3568 | { |
| 3569 | vim_free(from); |
| 3570 | return cnt; |
| 3571 | } |
| 3572 | |
| 3573 | /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */ |
| 3574 | if (from != NULL && to != NULL) |
| 3575 | res = set_sofo(slang, from, to); |
| 3576 | else if (from != NULL || to != NULL) |
| 3577 | res = SP_FORMERROR; /* only one of two strings is an error */ |
| 3578 | else |
| 3579 | res = 0; |
| 3580 | |
| 3581 | vim_free(from); |
| 3582 | vim_free(to); |
| 3583 | return res; |
| 3584 | } |
| 3585 | |
| 3586 | /* |
| 3587 | * Read the compound section from the .spl file: |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 3588 | * <compmax> <compminlen> <compsylmax> <compoptions> <compflags> |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3589 | * Returns SP_*ERROR flags. |
| 3590 | */ |
| 3591 | static int |
| 3592 | read_compound(fd, slang, len) |
| 3593 | FILE *fd; |
| 3594 | slang_T *slang; |
| 3595 | int len; |
| 3596 | { |
| 3597 | int todo = len; |
| 3598 | int c; |
| 3599 | int atstart; |
| 3600 | char_u *pat; |
| 3601 | char_u *pp; |
| 3602 | char_u *cp; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3603 | char_u *ap; |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 3604 | char_u *crp; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 3605 | int cnt; |
| 3606 | garray_T *gap; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3607 | |
| 3608 | if (todo < 2) |
| 3609 | return SP_FORMERROR; /* need at least two bytes */ |
| 3610 | |
| 3611 | --todo; |
| 3612 | c = getc(fd); /* <compmax> */ |
| 3613 | if (c < 2) |
| 3614 | c = MAXWLEN; |
| 3615 | slang->sl_compmax = c; |
| 3616 | |
| 3617 | --todo; |
| 3618 | c = getc(fd); /* <compminlen> */ |
| 3619 | if (c < 1) |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 3620 | c = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3621 | slang->sl_compminlen = c; |
| 3622 | |
| 3623 | --todo; |
| 3624 | c = getc(fd); /* <compsylmax> */ |
| 3625 | if (c < 1) |
| 3626 | c = MAXWLEN; |
| 3627 | slang->sl_compsylmax = c; |
| 3628 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 3629 | c = getc(fd); /* <compoptions> */ |
| 3630 | if (c != 0) |
| 3631 | ungetc(c, fd); /* be backwards compatible with Vim 7.0b */ |
| 3632 | else |
| 3633 | { |
| 3634 | --todo; |
| 3635 | c = getc(fd); /* only use the lower byte for now */ |
| 3636 | --todo; |
| 3637 | slang->sl_compoptions = c; |
| 3638 | |
| 3639 | gap = &slang->sl_comppat; |
| 3640 | c = get2c(fd); /* <comppatcount> */ |
| 3641 | todo -= 2; |
| 3642 | ga_init2(gap, sizeof(char_u *), c); |
| 3643 | if (ga_grow(gap, c) == OK) |
| 3644 | while (--c >= 0) |
| 3645 | { |
| 3646 | ((char_u **)(gap->ga_data))[gap->ga_len++] = |
| 3647 | read_cnt_string(fd, 1, &cnt); |
| 3648 | /* <comppatlen> <comppattext> */ |
| 3649 | if (cnt < 0) |
| 3650 | return cnt; |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 3651 | todo -= cnt + 1; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 3652 | } |
| 3653 | } |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 3654 | if (todo < 0) |
| 3655 | return SP_FORMERROR; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 3656 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3657 | /* Turn the COMPOUNDRULE items into a regexp pattern: |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3658 | * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$". |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3659 | * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes. |
| 3660 | * Conversion to utf-8 may double the size. */ |
| 3661 | c = todo * 2 + 7; |
| 3662 | #ifdef FEAT_MBYTE |
| 3663 | if (enc_utf8) |
| 3664 | c += todo * 2; |
| 3665 | #endif |
| 3666 | pat = alloc((unsigned)c); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3667 | if (pat == NULL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3668 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3669 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3670 | /* We also need a list of all flags that can appear at the start and one |
| 3671 | * for all flags. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3672 | cp = alloc(todo + 1); |
| 3673 | if (cp == NULL) |
| 3674 | { |
| 3675 | vim_free(pat); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3676 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3677 | } |
| 3678 | slang->sl_compstartflags = cp; |
| 3679 | *cp = NUL; |
| 3680 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3681 | ap = alloc(todo + 1); |
| 3682 | if (ap == NULL) |
| 3683 | { |
| 3684 | vim_free(pat); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3685 | return SP_OTHERERROR; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3686 | } |
| 3687 | slang->sl_compallflags = ap; |
| 3688 | *ap = NUL; |
| 3689 | |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 3690 | /* And a list of all patterns in their original form, for checking whether |
| 3691 | * compounding may work in match_compoundrule(). This is freed when we |
| 3692 | * encounter a wildcard, the check doesn't work then. */ |
| 3693 | crp = alloc(todo + 1); |
| 3694 | slang->sl_comprules = crp; |
| 3695 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3696 | pp = pat; |
| 3697 | *pp++ = '^'; |
| 3698 | *pp++ = '\\'; |
| 3699 | *pp++ = '('; |
| 3700 | |
| 3701 | atstart = 1; |
| 3702 | while (todo-- > 0) |
| 3703 | { |
| 3704 | c = getc(fd); /* <compflags> */ |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 3705 | if (c == EOF) |
| 3706 | { |
| 3707 | vim_free(pat); |
| 3708 | return SP_TRUNCERROR; |
| 3709 | } |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3710 | |
| 3711 | /* Add all flags to "sl_compallflags". */ |
| 3712 | if (vim_strchr((char_u *)"+*[]/", c) == NULL |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3713 | && !byte_in_str(slang->sl_compallflags, c)) |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 3714 | { |
| 3715 | *ap++ = c; |
| 3716 | *ap = NUL; |
| 3717 | } |
| 3718 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3719 | if (atstart != 0) |
| 3720 | { |
| 3721 | /* At start of item: copy flags to "sl_compstartflags". For a |
| 3722 | * [abc] item set "atstart" to 2 and copy up to the ']'. */ |
| 3723 | if (c == '[') |
| 3724 | atstart = 2; |
| 3725 | else if (c == ']') |
| 3726 | atstart = 0; |
| 3727 | else |
| 3728 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3729 | if (!byte_in_str(slang->sl_compstartflags, c)) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3730 | { |
| 3731 | *cp++ = c; |
| 3732 | *cp = NUL; |
| 3733 | } |
| 3734 | if (atstart == 1) |
| 3735 | atstart = 0; |
| 3736 | } |
| 3737 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 3738 | |
| 3739 | /* Copy flag to "sl_comprules", unless we run into a wildcard. */ |
| 3740 | if (crp != NULL) |
| 3741 | { |
| 3742 | if (c == '+' || c == '*') |
| 3743 | { |
| 3744 | vim_free(slang->sl_comprules); |
| 3745 | slang->sl_comprules = NULL; |
| 3746 | crp = NULL; |
| 3747 | } |
| 3748 | else |
| 3749 | *crp++ = c; |
| 3750 | } |
| 3751 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3752 | if (c == '/') /* slash separates two items */ |
| 3753 | { |
| 3754 | *pp++ = '\\'; |
| 3755 | *pp++ = '|'; |
| 3756 | atstart = 1; |
| 3757 | } |
| 3758 | else /* normal char, "[abc]" and '*' are copied as-is */ |
| 3759 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 3760 | if (c == '+' || c == '~') |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3761 | *pp++ = '\\'; /* "a+" becomes "a\+" */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3762 | #ifdef FEAT_MBYTE |
| 3763 | if (enc_utf8) |
| 3764 | pp += mb_char2bytes(c, pp); |
| 3765 | else |
| 3766 | #endif |
| 3767 | *pp++ = c; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | |
| 3771 | *pp++ = '\\'; |
| 3772 | *pp++ = ')'; |
| 3773 | *pp++ = '$'; |
| 3774 | *pp = NUL; |
| 3775 | |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 3776 | if (crp != NULL) |
| 3777 | *crp = NUL; |
| 3778 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3779 | slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT); |
| 3780 | vim_free(pat); |
| 3781 | if (slang->sl_compprog == NULL) |
| 3782 | return SP_FORMERROR; |
| 3783 | |
| 3784 | return 0; |
| 3785 | } |
| 3786 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3787 | /* |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 3788 | * Return TRUE if byte "n" appears in "str". |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3789 | * Like strchr() but independent of locale. |
| 3790 | */ |
| 3791 | static int |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 3792 | byte_in_str(str, n) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3793 | char_u *str; |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 3794 | int n; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3795 | { |
| 3796 | char_u *p; |
| 3797 | |
| 3798 | for (p = str; *p != NUL; ++p) |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 3799 | if (*p == n) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3800 | return TRUE; |
| 3801 | return FALSE; |
| 3802 | } |
| 3803 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3804 | #define SY_MAXLEN 30 |
| 3805 | typedef struct syl_item_S |
| 3806 | { |
| 3807 | char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */ |
| 3808 | int sy_len; |
| 3809 | } syl_item_T; |
| 3810 | |
| 3811 | /* |
| 3812 | * Truncate "slang->sl_syllable" at the first slash and put the following items |
| 3813 | * in "slang->sl_syl_items". |
| 3814 | */ |
| 3815 | static int |
| 3816 | init_syl_tab(slang) |
| 3817 | slang_T *slang; |
| 3818 | { |
| 3819 | char_u *p; |
| 3820 | char_u *s; |
| 3821 | int l; |
| 3822 | syl_item_T *syl; |
| 3823 | |
| 3824 | ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4); |
| 3825 | p = vim_strchr(slang->sl_syllable, '/'); |
| 3826 | while (p != NULL) |
| 3827 | { |
| 3828 | *p++ = NUL; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3829 | if (*p == NUL) /* trailing slash */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3830 | break; |
| 3831 | s = p; |
| 3832 | p = vim_strchr(p, '/'); |
| 3833 | if (p == NULL) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3834 | l = (int)STRLEN(s); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3835 | else |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3836 | l = (int)(p - s); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3837 | if (l >= SY_MAXLEN) |
| 3838 | return SP_FORMERROR; |
| 3839 | if (ga_grow(&slang->sl_syl_items, 1) == FAIL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3840 | return SP_OTHERERROR; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3841 | syl = ((syl_item_T *)slang->sl_syl_items.ga_data) |
| 3842 | + slang->sl_syl_items.ga_len++; |
| 3843 | vim_strncpy(syl->sy_chars, s, l); |
| 3844 | syl->sy_len = l; |
| 3845 | } |
| 3846 | return OK; |
| 3847 | } |
| 3848 | |
| 3849 | /* |
| 3850 | * Count the number of syllables in "word". |
| 3851 | * When "word" contains spaces the syllables after the last space are counted. |
| 3852 | * Returns zero if syllables are not defines. |
| 3853 | */ |
| 3854 | static int |
| 3855 | count_syllables(slang, word) |
| 3856 | slang_T *slang; |
| 3857 | char_u *word; |
| 3858 | { |
| 3859 | int cnt = 0; |
| 3860 | int skip = FALSE; |
| 3861 | char_u *p; |
| 3862 | int len; |
| 3863 | int i; |
| 3864 | syl_item_T *syl; |
| 3865 | int c; |
| 3866 | |
| 3867 | if (slang->sl_syllable == NULL) |
| 3868 | return 0; |
| 3869 | |
| 3870 | for (p = word; *p != NUL; p += len) |
| 3871 | { |
| 3872 | /* When running into a space reset counter. */ |
| 3873 | if (*p == ' ') |
| 3874 | { |
| 3875 | len = 1; |
| 3876 | cnt = 0; |
| 3877 | continue; |
| 3878 | } |
| 3879 | |
| 3880 | /* Find longest match of syllable items. */ |
| 3881 | len = 0; |
| 3882 | for (i = 0; i < slang->sl_syl_items.ga_len; ++i) |
| 3883 | { |
| 3884 | syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i; |
| 3885 | if (syl->sy_len > len |
| 3886 | && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0) |
| 3887 | len = syl->sy_len; |
| 3888 | } |
| 3889 | if (len != 0) /* found a match, count syllable */ |
| 3890 | { |
| 3891 | ++cnt; |
| 3892 | skip = FALSE; |
| 3893 | } |
| 3894 | else |
| 3895 | { |
| 3896 | /* No recognized syllable item, at least a syllable char then? */ |
| 3897 | #ifdef FEAT_MBYTE |
| 3898 | c = mb_ptr2char(p); |
| 3899 | len = (*mb_ptr2len)(p); |
| 3900 | #else |
| 3901 | c = *p; |
| 3902 | len = 1; |
| 3903 | #endif |
| 3904 | if (vim_strchr(slang->sl_syllable, c) == NULL) |
| 3905 | skip = FALSE; /* No, search for next syllable */ |
| 3906 | else if (!skip) |
| 3907 | { |
| 3908 | ++cnt; /* Yes, count it */ |
| 3909 | skip = TRUE; /* don't count following syllable chars */ |
| 3910 | } |
| 3911 | } |
| 3912 | } |
| 3913 | return cnt; |
| 3914 | } |
| 3915 | |
| 3916 | /* |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3917 | * Set the SOFOFROM and SOFOTO items in language "lp". |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3918 | * Returns SP_*ERROR flags when there is something wrong. |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3919 | */ |
| 3920 | static int |
| 3921 | set_sofo(lp, from, to) |
| 3922 | slang_T *lp; |
| 3923 | char_u *from; |
| 3924 | char_u *to; |
| 3925 | { |
| 3926 | int i; |
| 3927 | |
| 3928 | #ifdef FEAT_MBYTE |
| 3929 | garray_T *gap; |
| 3930 | char_u *s; |
| 3931 | char_u *p; |
| 3932 | int c; |
| 3933 | int *inp; |
| 3934 | |
| 3935 | if (has_mbyte) |
| 3936 | { |
| 3937 | /* Use "sl_sal" as an array with 256 pointers to a list of wide |
| 3938 | * characters. The index is the low byte of the character. |
| 3939 | * The list contains from-to pairs with a terminating NUL. |
| 3940 | * sl_sal_first[] is used for latin1 "from" characters. */ |
| 3941 | gap = &lp->sl_sal; |
| 3942 | ga_init2(gap, sizeof(int *), 1); |
| 3943 | if (ga_grow(gap, 256) == FAIL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3944 | return SP_OTHERERROR; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3945 | vim_memset(gap->ga_data, 0, sizeof(int *) * 256); |
| 3946 | gap->ga_len = 256; |
| 3947 | |
| 3948 | /* First count the number of items for each list. Temporarily use |
| 3949 | * sl_sal_first[] for this. */ |
| 3950 | for (p = from, s = to; *p != NUL && *s != NUL; ) |
| 3951 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3952 | c = mb_cptr2char_adv(&p); |
| 3953 | mb_cptr_adv(s); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3954 | if (c >= 256) |
| 3955 | ++lp->sl_sal_first[c & 0xff]; |
| 3956 | } |
| 3957 | if (*p != NUL || *s != NUL) /* lengths differ */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3958 | return SP_FORMERROR; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3959 | |
| 3960 | /* Allocate the lists. */ |
| 3961 | for (i = 0; i < 256; ++i) |
| 3962 | if (lp->sl_sal_first[i] > 0) |
| 3963 | { |
| 3964 | p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1)); |
| 3965 | if (p == NULL) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 3966 | return SP_OTHERERROR; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3967 | ((int **)gap->ga_data)[i] = (int *)p; |
| 3968 | *(int *)p = 0; |
| 3969 | } |
| 3970 | |
| 3971 | /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal |
| 3972 | * list. */ |
| 3973 | vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256); |
| 3974 | for (p = from, s = to; *p != NUL && *s != NUL; ) |
| 3975 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3976 | c = mb_cptr2char_adv(&p); |
| 3977 | i = mb_cptr2char_adv(&s); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 3978 | if (c >= 256) |
| 3979 | { |
| 3980 | /* Append the from-to chars at the end of the list with |
| 3981 | * the low byte. */ |
| 3982 | inp = ((int **)gap->ga_data)[c & 0xff]; |
| 3983 | while (*inp != 0) |
| 3984 | ++inp; |
| 3985 | *inp++ = c; /* from char */ |
| 3986 | *inp++ = i; /* to char */ |
| 3987 | *inp++ = NUL; /* NUL at the end */ |
| 3988 | } |
| 3989 | else |
| 3990 | /* mapping byte to char is done in sl_sal_first[] */ |
| 3991 | lp->sl_sal_first[c] = i; |
| 3992 | } |
| 3993 | } |
| 3994 | else |
| 3995 | #endif |
| 3996 | { |
| 3997 | /* mapping bytes to bytes is done in sl_sal_first[] */ |
| 3998 | if (STRLEN(from) != STRLEN(to)) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 3999 | return SP_FORMERROR; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4000 | |
| 4001 | for (i = 0; to[i] != NUL; ++i) |
| 4002 | lp->sl_sal_first[from[i]] = to[i]; |
| 4003 | lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */ |
| 4004 | } |
| 4005 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 4006 | return 0; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | /* |
| 4010 | * Fill the first-index table for "lp". |
| 4011 | */ |
| 4012 | static void |
| 4013 | set_sal_first(lp) |
| 4014 | slang_T *lp; |
| 4015 | { |
| 4016 | salfirst_T *sfirst; |
| 4017 | int i; |
| 4018 | salitem_T *smp; |
| 4019 | int c; |
| 4020 | garray_T *gap = &lp->sl_sal; |
| 4021 | |
| 4022 | sfirst = lp->sl_sal_first; |
| 4023 | for (i = 0; i < 256; ++i) |
| 4024 | sfirst[i] = -1; |
| 4025 | smp = (salitem_T *)gap->ga_data; |
| 4026 | for (i = 0; i < gap->ga_len; ++i) |
| 4027 | { |
| 4028 | #ifdef FEAT_MBYTE |
| 4029 | if (has_mbyte) |
| 4030 | /* Use the lowest byte of the first character. For latin1 it's |
| 4031 | * the character, for other encodings it should differ for most |
| 4032 | * characters. */ |
| 4033 | c = *smp[i].sm_lead_w & 0xff; |
| 4034 | else |
| 4035 | #endif |
| 4036 | c = *smp[i].sm_lead; |
| 4037 | if (sfirst[c] == -1) |
| 4038 | { |
| 4039 | sfirst[c] = i; |
| 4040 | #ifdef FEAT_MBYTE |
| 4041 | if (has_mbyte) |
| 4042 | { |
| 4043 | int n; |
| 4044 | |
| 4045 | /* Make sure all entries with this byte are following each |
| 4046 | * other. Move the ones that are in the wrong position. Do |
| 4047 | * keep the same ordering! */ |
| 4048 | while (i + 1 < gap->ga_len |
| 4049 | && (*smp[i + 1].sm_lead_w & 0xff) == c) |
| 4050 | /* Skip over entry with same index byte. */ |
| 4051 | ++i; |
| 4052 | |
| 4053 | for (n = 1; i + n < gap->ga_len; ++n) |
| 4054 | if ((*smp[i + n].sm_lead_w & 0xff) == c) |
| 4055 | { |
| 4056 | salitem_T tsal; |
| 4057 | |
| 4058 | /* Move entry with same index byte after the entries |
| 4059 | * we already found. */ |
| 4060 | ++i; |
| 4061 | --n; |
| 4062 | tsal = smp[i + n]; |
| 4063 | mch_memmove(smp + i + 1, smp + i, |
| 4064 | sizeof(salitem_T) * n); |
| 4065 | smp[i] = tsal; |
| 4066 | } |
| 4067 | } |
| 4068 | #endif |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4072 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 4073 | #ifdef FEAT_MBYTE |
| 4074 | /* |
| 4075 | * Turn a multi-byte string into a wide character string. |
| 4076 | * Return it in allocated memory (NULL for out-of-memory) |
| 4077 | */ |
| 4078 | static int * |
| 4079 | mb_str2wide(s) |
| 4080 | char_u *s; |
| 4081 | { |
| 4082 | int *res; |
| 4083 | char_u *p; |
| 4084 | int i = 0; |
| 4085 | |
| 4086 | res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1)); |
| 4087 | if (res != NULL) |
| 4088 | { |
| 4089 | for (p = s; *p != NUL; ) |
| 4090 | res[i++] = mb_ptr2char_adv(&p); |
| 4091 | res[i] = NUL; |
| 4092 | } |
| 4093 | return res; |
| 4094 | } |
| 4095 | #endif |
| 4096 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4097 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4098 | * Read a tree from the .spl or .sug file. |
| 4099 | * Allocates the memory and stores pointers in "bytsp" and "idxsp". |
| 4100 | * This is skipped when the tree has zero length. |
| 4101 | * Returns zero when OK, SP_ value for an error. |
| 4102 | */ |
| 4103 | static int |
| 4104 | spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt) |
| 4105 | FILE *fd; |
| 4106 | char_u **bytsp; |
| 4107 | idx_T **idxsp; |
| 4108 | int prefixtree; /* TRUE for the prefix tree */ |
| 4109 | int prefixcnt; /* when "prefixtree" is TRUE: prefix count */ |
| 4110 | { |
| 4111 | int len; |
| 4112 | int idx; |
| 4113 | char_u *bp; |
| 4114 | idx_T *ip; |
| 4115 | |
| 4116 | /* The tree size was computed when writing the file, so that we can |
| 4117 | * allocate it as one long block. <nodecount> */ |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 4118 | len = get4c(fd); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4119 | if (len < 0) |
| 4120 | return SP_TRUNCERROR; |
| 4121 | if (len > 0) |
| 4122 | { |
| 4123 | /* Allocate the byte array. */ |
| 4124 | bp = lalloc((long_u)len, TRUE); |
| 4125 | if (bp == NULL) |
| 4126 | return SP_OTHERERROR; |
| 4127 | *bytsp = bp; |
| 4128 | |
| 4129 | /* Allocate the index array. */ |
| 4130 | ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 4131 | if (ip == NULL) |
| 4132 | return SP_OTHERERROR; |
| 4133 | *idxsp = ip; |
| 4134 | |
| 4135 | /* Recursively read the tree and store it in the array. */ |
| 4136 | idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt); |
| 4137 | if (idx < 0) |
| 4138 | return idx; |
| 4139 | } |
| 4140 | return 0; |
| 4141 | } |
| 4142 | |
| 4143 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4144 | * Read one row of siblings from the spell file and store it in the byte array |
| 4145 | * "byts" and index array "idxs". Recursively read the children. |
| 4146 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4147 | * NOTE: The code here must match put_node()! |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4148 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4149 | * Returns the index (>= 0) following the siblings. |
| 4150 | * Returns SP_TRUNCERROR if the file is shorter than expected. |
| 4151 | * Returns SP_FORMERROR if there is a format error. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4152 | */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4153 | static idx_T |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4154 | read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4155 | FILE *fd; |
| 4156 | char_u *byts; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4157 | idx_T *idxs; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4158 | int maxidx; /* size of arrays */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4159 | idx_T startidx; /* current index in "byts" and "idxs" */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4160 | int prefixtree; /* TRUE for reading PREFIXTREE */ |
| 4161 | int maxprefcondnr; /* maximum for <prefcondnr> */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4162 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4163 | int len; |
| 4164 | int i; |
| 4165 | int n; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4166 | idx_T idx = startidx; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4167 | int c; |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4168 | int c2; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4169 | #define SHARED_MASK 0x8000000 |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4170 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4171 | len = getc(fd); /* <siblingcount> */ |
| 4172 | if (len <= 0) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4173 | return SP_TRUNCERROR; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4174 | |
| 4175 | if (startidx + len >= maxidx) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4176 | return SP_FORMERROR; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4177 | byts[idx++] = len; |
| 4178 | |
| 4179 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 4180 | for (i = 1; i <= len; ++i) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4181 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4182 | c = getc(fd); /* <byte> */ |
| 4183 | if (c < 0) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4184 | return SP_TRUNCERROR; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4185 | if (c <= BY_SPECIAL) |
| 4186 | { |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4187 | if (c == BY_NOFLAGS && !prefixtree) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4188 | { |
| 4189 | /* No flags, all regions. */ |
| 4190 | idxs[idx] = 0; |
| 4191 | c = 0; |
| 4192 | } |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4193 | else if (c != BY_INDEX) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4194 | { |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4195 | if (prefixtree) |
| 4196 | { |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4197 | /* Read the optional pflags byte, the prefix ID and the |
| 4198 | * condition nr. In idxs[] store the prefix ID in the low |
| 4199 | * byte, the condition index shifted up 8 bits, the flags |
| 4200 | * shifted up 24 bits. */ |
| 4201 | if (c == BY_FLAGS) |
| 4202 | c = getc(fd) << 24; /* <pflags> */ |
| 4203 | else |
| 4204 | c = 0; |
| 4205 | |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 4206 | c |= getc(fd); /* <affixID> */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4207 | |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 4208 | n = get2c(fd); /* <prefcondnr> */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4209 | if (n >= maxprefcondnr) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4210 | return SP_FORMERROR; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4211 | c |= (n << 8); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4212 | } |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4213 | else /* c must be BY_FLAGS or BY_FLAGS2 */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4214 | { |
| 4215 | /* Read flags and optional region and prefix ID. In |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4216 | * idxs[] the flags go in the low two bytes, region above |
| 4217 | * that and prefix ID above the region. */ |
| 4218 | c2 = c; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4219 | c = getc(fd); /* <flags> */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4220 | if (c2 == BY_FLAGS2) |
| 4221 | c = (getc(fd) << 8) + c; /* <flags2> */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4222 | if (c & WF_REGION) |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 4223 | c = (getc(fd) << 16) + c; /* <region> */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 4224 | if (c & WF_AFX) |
| 4225 | c = (getc(fd) << 24) + c; /* <affixID> */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4226 | } |
| 4227 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4228 | idxs[idx] = c; |
| 4229 | c = 0; |
| 4230 | } |
| 4231 | else /* c == BY_INDEX */ |
| 4232 | { |
| 4233 | /* <nodeidx> */ |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 4234 | n = get3c(fd); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4235 | if (n < 0 || n >= maxidx) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4236 | return SP_FORMERROR; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4237 | idxs[idx] = n + SHARED_MASK; |
| 4238 | c = getc(fd); /* <xbyte> */ |
| 4239 | } |
| 4240 | } |
| 4241 | byts[idx++] = c; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4244 | /* Recursively read the children for non-shared siblings. |
| 4245 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 4246 | * remove SHARED_MASK) */ |
| 4247 | for (i = 1; i <= len; ++i) |
| 4248 | if (byts[startidx + i] != 0) |
| 4249 | { |
| 4250 | if (idxs[startidx + i] & SHARED_MASK) |
| 4251 | idxs[startidx + i] &= ~SHARED_MASK; |
| 4252 | else |
| 4253 | { |
| 4254 | idxs[startidx + i] = idx; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4255 | idx = read_tree_node(fd, byts, idxs, maxidx, idx, |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 4256 | prefixtree, maxprefcondnr); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4257 | if (idx < 0) |
| 4258 | break; |
| 4259 | } |
| 4260 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4261 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4262 | return idx; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | /* |
| 4266 | * Parse 'spelllang' and set buf->b_langp accordingly. |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4267 | * Returns NULL if it's OK, an error message otherwise. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4268 | */ |
| 4269 | char_u * |
| 4270 | did_set_spelllang(buf) |
| 4271 | buf_T *buf; |
| 4272 | { |
| 4273 | garray_T ga; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4274 | char_u *splp; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4275 | char_u *region; |
| Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 4276 | char_u region_cp[3]; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4277 | int filename; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4278 | int region_mask; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4279 | slang_T *slang; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4280 | int c; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4281 | char_u lang[MAXWLEN + 1]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4282 | char_u spf_name[MAXPATHL]; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4283 | int len; |
| 4284 | char_u *p; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4285 | int round; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4286 | char_u *spf; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4287 | char_u *use_region = NULL; |
| 4288 | int dont_use_region = FALSE; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 4289 | int nobreak = FALSE; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4290 | int i, j; |
| 4291 | langp_T *lp, *lp2; |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4292 | static int recursive = FALSE; |
| 4293 | char_u *ret_msg = NULL; |
| 4294 | char_u *spl_copy; |
| 4295 | |
| 4296 | /* We don't want to do this recursively. May happen when a language is |
| 4297 | * not available and the SpellFileMissing autocommand opens a new buffer |
| 4298 | * in which 'spell' is set. */ |
| 4299 | if (recursive) |
| 4300 | return NULL; |
| 4301 | recursive = TRUE; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4302 | |
| 4303 | ga_init2(&ga, sizeof(langp_T), 2); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4304 | clear_midword(buf); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4305 | |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4306 | /* Make a copy of 'spellang', the SpellFileMissing autocommands may change |
| 4307 | * it under our fingers. */ |
| 4308 | spl_copy = vim_strsave(buf->b_p_spl); |
| 4309 | if (spl_copy == NULL) |
| 4310 | goto theend; |
| 4311 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4312 | /* loop over comma separated language names. */ |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4313 | for (splp = spl_copy; *splp != NUL; ) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4314 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4315 | /* Get one language name. */ |
| 4316 | copy_option_part(&splp, lang, MAXWLEN, ","); |
| 4317 | |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4318 | region = NULL; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4319 | len = (int)STRLEN(lang); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4320 | |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4321 | /* If the name ends in ".spl" use it as the name of the spell file. |
| 4322 | * If there is a region name let "region" point to it and remove it |
| 4323 | * from the name. */ |
| 4324 | if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) |
| 4325 | { |
| 4326 | filename = TRUE; |
| 4327 | |
| Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 4328 | /* Locate a region and remove it from the file name. */ |
| 4329 | p = vim_strchr(gettail(lang), '_'); |
| 4330 | if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2]) |
| 4331 | && !ASCII_ISALPHA(p[3])) |
| 4332 | { |
| 4333 | vim_strncpy(region_cp, p + 1, 2); |
| 4334 | mch_memmove(p, p + 3, len - (p - lang) - 2); |
| 4335 | len -= 3; |
| 4336 | region = region_cp; |
| 4337 | } |
| 4338 | else |
| 4339 | dont_use_region = TRUE; |
| 4340 | |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4341 | /* Check if we loaded this language before. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4342 | for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
| 4343 | if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME) |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4344 | break; |
| 4345 | } |
| 4346 | else |
| 4347 | { |
| 4348 | filename = FALSE; |
| 4349 | if (len > 3 && lang[len - 3] == '_') |
| 4350 | { |
| 4351 | region = lang + len - 2; |
| 4352 | len -= 3; |
| 4353 | lang[len] = NUL; |
| 4354 | } |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4355 | else |
| 4356 | dont_use_region = TRUE; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4357 | |
| 4358 | /* Check if we loaded this language before. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4359 | for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
| 4360 | if (STRICMP(lang, slang->sl_name) == 0) |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4361 | break; |
| 4362 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4363 | |
| Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 4364 | if (region != NULL) |
| 4365 | { |
| 4366 | /* If the region differs from what was used before then don't |
| 4367 | * use it for 'spellfile'. */ |
| 4368 | if (use_region != NULL && STRCMP(region, use_region) != 0) |
| 4369 | dont_use_region = TRUE; |
| 4370 | use_region = region; |
| 4371 | } |
| 4372 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4373 | /* If not found try loading the language now. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4374 | if (slang == NULL) |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4375 | { |
| 4376 | if (filename) |
| 4377 | (void)spell_load_file(lang, lang, NULL, FALSE); |
| 4378 | else |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4379 | { |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4380 | spell_load_lang(lang); |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4381 | #ifdef FEAT_AUTOCMD |
| 4382 | /* SpellFileMissing autocommands may do anything, including |
| 4383 | * destroying the buffer we are using... */ |
| 4384 | if (!buf_valid(buf)) |
| 4385 | { |
| 4386 | ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer"; |
| 4387 | goto theend; |
| 4388 | } |
| 4389 | #endif |
| 4390 | } |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4391 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4392 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4393 | /* |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4394 | * Loop over the languages, there can be several files for "lang". |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4395 | */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4396 | for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
| 4397 | if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME |
| 4398 | : STRICMP(lang, slang->sl_name) == 0) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4399 | { |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4400 | region_mask = REGION_ALL; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4401 | if (!filename && region != NULL) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4402 | { |
| 4403 | /* find region in sl_regions */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4404 | c = find_region(slang->sl_regions, region); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4405 | if (c == REGION_ALL) |
| 4406 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4407 | if (slang->sl_add) |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4408 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4409 | if (*slang->sl_regions != NUL) |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4410 | /* This addition file is for other regions. */ |
| 4411 | region_mask = 0; |
| 4412 | } |
| 4413 | else |
| 4414 | /* This is probably an error. Give a warning and |
| 4415 | * accept the words anyway. */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4416 | smsg((char_u *) |
| 4417 | _("Warning: region %s not supported"), |
| 4418 | region); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4419 | } |
| 4420 | else |
| 4421 | region_mask = 1 << c; |
| 4422 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4423 | |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4424 | if (region_mask != 0) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4425 | { |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4426 | if (ga_grow(&ga, 1) == FAIL) |
| 4427 | { |
| 4428 | ga_clear(&ga); |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4429 | ret_msg = e_outofmem; |
| 4430 | goto theend; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4431 | } |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4432 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4433 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 4434 | ++ga.ga_len; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4435 | use_midword(slang, buf); |
| 4436 | if (slang->sl_nobreak) |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 4437 | nobreak = TRUE; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4438 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4439 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4442 | /* round 0: load int_wordlist, if possible. |
| 4443 | * round 1: load first name in 'spellfile'. |
| 4444 | * round 2: load second name in 'spellfile. |
| 4445 | * etc. */ |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4446 | spf = buf->b_p_spf; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4447 | for (round = 0; round == 0 || *spf != NUL; ++round) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4448 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4449 | if (round == 0) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4450 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4451 | /* Internal wordlist, if there is one. */ |
| 4452 | if (int_wordlist == NULL) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4453 | continue; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4454 | int_wordlist_spl(spf_name); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4455 | } |
| 4456 | else |
| 4457 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4458 | /* One entry in 'spellfile'. */ |
| 4459 | copy_option_part(&spf, spf_name, MAXPATHL - 5, ","); |
| 4460 | STRCAT(spf_name, ".spl"); |
| 4461 | |
| 4462 | /* If it was already found above then skip it. */ |
| 4463 | for (c = 0; c < ga.ga_len; ++c) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 4464 | { |
| 4465 | p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname; |
| 4466 | if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME) |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4467 | break; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 4468 | } |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4469 | if (c < ga.ga_len) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4470 | continue; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4473 | /* Check if it was loaded already. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4474 | for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
| 4475 | if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4476 | break; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4477 | if (slang == NULL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4478 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 4479 | /* Not loaded, try loading it now. The language name includes the |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4480 | * region name, the region is ignored otherwise. for int_wordlist |
| 4481 | * use an arbitrary name. */ |
| 4482 | if (round == 0) |
| 4483 | STRCPY(lang, "internal wordlist"); |
| 4484 | else |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4485 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4486 | vim_strncpy(lang, gettail(spf_name), MAXWLEN); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4487 | p = vim_strchr(lang, '.'); |
| 4488 | if (p != NULL) |
| 4489 | *p = NUL; /* truncate at ".encoding.add" */ |
| 4490 | } |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4491 | slang = spell_load_file(spf_name, lang, NULL, TRUE); |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 4492 | |
| 4493 | /* If one of the languages has NOBREAK we assume the addition |
| 4494 | * files also have this. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4495 | if (slang != NULL && nobreak) |
| 4496 | slang->sl_nobreak = TRUE; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4497 | } |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4498 | if (slang != NULL && ga_grow(&ga, 1) == OK) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4499 | { |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4500 | region_mask = REGION_ALL; |
| 4501 | if (use_region != NULL && !dont_use_region) |
| 4502 | { |
| 4503 | /* find region in sl_regions */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4504 | c = find_region(slang->sl_regions, use_region); |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4505 | if (c != REGION_ALL) |
| 4506 | region_mask = 1 << c; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4507 | else if (*slang->sl_regions != NUL) |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4508 | /* This spell file is for other regions. */ |
| 4509 | region_mask = 0; |
| 4510 | } |
| 4511 | |
| 4512 | if (region_mask != 0) |
| 4513 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4514 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
| 4515 | LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL; |
| 4516 | LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4517 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 4518 | ++ga.ga_len; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4519 | use_midword(slang, buf); |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4520 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4524 | /* Everything is fine, store the new b_langp value. */ |
| 4525 | ga_clear(&buf->b_langp); |
| 4526 | buf->b_langp = ga; |
| 4527 | |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4528 | /* For each language figure out what language to use for sound folding and |
| 4529 | * REP items. If the language doesn't support it itself use another one |
| 4530 | * with the same name. E.g. for "en-math" use "en". */ |
| 4531 | for (i = 0; i < ga.ga_len; ++i) |
| 4532 | { |
| 4533 | lp = LANGP_ENTRY(ga, i); |
| 4534 | |
| 4535 | /* sound folding */ |
| 4536 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 4537 | /* language does sound folding itself */ |
| 4538 | lp->lp_sallang = lp->lp_slang; |
| 4539 | else |
| 4540 | /* find first similar language that does sound folding */ |
| 4541 | for (j = 0; j < ga.ga_len; ++j) |
| 4542 | { |
| 4543 | lp2 = LANGP_ENTRY(ga, j); |
| 4544 | if (lp2->lp_slang->sl_sal.ga_len > 0 |
| 4545 | && STRNCMP(lp->lp_slang->sl_name, |
| 4546 | lp2->lp_slang->sl_name, 2) == 0) |
| 4547 | { |
| 4548 | lp->lp_sallang = lp2->lp_slang; |
| 4549 | break; |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | /* REP items */ |
| 4554 | if (lp->lp_slang->sl_rep.ga_len > 0) |
| 4555 | /* language has REP items itself */ |
| 4556 | lp->lp_replang = lp->lp_slang; |
| 4557 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4558 | /* find first similar language that has REP items */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4559 | for (j = 0; j < ga.ga_len; ++j) |
| 4560 | { |
| 4561 | lp2 = LANGP_ENTRY(ga, j); |
| 4562 | if (lp2->lp_slang->sl_rep.ga_len > 0 |
| 4563 | && STRNCMP(lp->lp_slang->sl_name, |
| 4564 | lp2->lp_slang->sl_name, 2) == 0) |
| 4565 | { |
| 4566 | lp->lp_replang = lp2->lp_slang; |
| 4567 | break; |
| 4568 | } |
| 4569 | } |
| 4570 | } |
| 4571 | |
| Bram Moolenaar | 706cdeb | 2007-05-06 21:55:31 +0000 | [diff] [blame] | 4572 | theend: |
| 4573 | vim_free(spl_copy); |
| 4574 | recursive = FALSE; |
| 4575 | return ret_msg; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | /* |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4579 | * Clear the midword characters for buffer "buf". |
| 4580 | */ |
| 4581 | static void |
| 4582 | clear_midword(buf) |
| 4583 | buf_T *buf; |
| 4584 | { |
| 4585 | vim_memset(buf->b_spell_ismw, 0, 256); |
| 4586 | #ifdef FEAT_MBYTE |
| 4587 | vim_free(buf->b_spell_ismw_mb); |
| 4588 | buf->b_spell_ismw_mb = NULL; |
| 4589 | #endif |
| 4590 | } |
| 4591 | |
| 4592 | /* |
| 4593 | * Use the "sl_midword" field of language "lp" for buffer "buf". |
| 4594 | * They add up to any currently used midword characters. |
| 4595 | */ |
| 4596 | static void |
| 4597 | use_midword(lp, buf) |
| 4598 | slang_T *lp; |
| 4599 | buf_T *buf; |
| 4600 | { |
| 4601 | char_u *p; |
| 4602 | |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 4603 | if (lp->sl_midword == NULL) /* there aren't any */ |
| 4604 | return; |
| 4605 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4606 | for (p = lp->sl_midword; *p != NUL; ) |
| 4607 | #ifdef FEAT_MBYTE |
| 4608 | if (has_mbyte) |
| 4609 | { |
| 4610 | int c, l, n; |
| 4611 | char_u *bp; |
| 4612 | |
| 4613 | c = mb_ptr2char(p); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4614 | l = (*mb_ptr2len)(p); |
| 4615 | if (c < 256 && l <= 2) |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4616 | buf->b_spell_ismw[c] = TRUE; |
| 4617 | else if (buf->b_spell_ismw_mb == NULL) |
| 4618 | /* First multi-byte char in "b_spell_ismw_mb". */ |
| 4619 | buf->b_spell_ismw_mb = vim_strnsave(p, l); |
| 4620 | else |
| 4621 | { |
| 4622 | /* Append multi-byte chars to "b_spell_ismw_mb". */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4623 | n = (int)STRLEN(buf->b_spell_ismw_mb); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4624 | bp = vim_strnsave(buf->b_spell_ismw_mb, n + l); |
| 4625 | if (bp != NULL) |
| 4626 | { |
| 4627 | vim_free(buf->b_spell_ismw_mb); |
| 4628 | buf->b_spell_ismw_mb = bp; |
| 4629 | vim_strncpy(bp + n, p, l); |
| 4630 | } |
| 4631 | } |
| 4632 | p += l; |
| 4633 | } |
| 4634 | else |
| 4635 | #endif |
| 4636 | buf->b_spell_ismw[*p++] = TRUE; |
| 4637 | } |
| 4638 | |
| 4639 | /* |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4640 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 4641 | * Each region is simply stored as the two characters of it's name. |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4642 | * Returns the index if found (first is 0), REGION_ALL if not found. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4643 | */ |
| 4644 | static int |
| 4645 | find_region(rp, region) |
| 4646 | char_u *rp; |
| 4647 | char_u *region; |
| 4648 | { |
| 4649 | int i; |
| 4650 | |
| 4651 | for (i = 0; ; i += 2) |
| 4652 | { |
| 4653 | if (rp[i] == NUL) |
| 4654 | return REGION_ALL; |
| 4655 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 4656 | break; |
| 4657 | } |
| 4658 | return i / 2; |
| 4659 | } |
| 4660 | |
| 4661 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4662 | * Return case type of word: |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4663 | * w word 0 |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4664 | * Word WF_ONECAP |
| 4665 | * W WORD WF_ALLCAP |
| 4666 | * WoRd wOrd WF_KEEPCAP |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4667 | */ |
| 4668 | static int |
| 4669 | captype(word, end) |
| 4670 | char_u *word; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4671 | char_u *end; /* When NULL use up to NUL byte. */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4672 | { |
| 4673 | char_u *p; |
| 4674 | int c; |
| 4675 | int firstcap; |
| 4676 | int allcap; |
| 4677 | int past_second = FALSE; /* past second word char */ |
| 4678 | |
| 4679 | /* find first letter */ |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4680 | for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4681 | if (end == NULL ? *p == NUL : p >= end) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4682 | return 0; /* only non-word characters, illegal word */ |
| 4683 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4684 | if (has_mbyte) |
| 4685 | c = mb_ptr2char_adv(&p); |
| 4686 | else |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4687 | #endif |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4688 | c = *p++; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4689 | firstcap = allcap = SPELL_ISUPPER(c); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4690 | |
| 4691 | /* |
| 4692 | * Need to check all letters to find a word with mixed upper/lower. |
| 4693 | * But a word with an upper char only at start is a ONECAP. |
| 4694 | */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4695 | for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p)) |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 4696 | if (spell_iswordp_nmw(p)) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4697 | { |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4698 | c = PTR2CHAR(p); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 4699 | if (!SPELL_ISUPPER(c)) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4700 | { |
| 4701 | /* UUl -> KEEPCAP */ |
| 4702 | if (past_second && allcap) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4703 | return WF_KEEPCAP; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4704 | allcap = FALSE; |
| 4705 | } |
| 4706 | else if (!allcap) |
| 4707 | /* UlU -> KEEPCAP */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4708 | return WF_KEEPCAP; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4709 | past_second = TRUE; |
| 4710 | } |
| 4711 | |
| 4712 | if (allcap) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4713 | return WF_ALLCAP; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4714 | if (firstcap) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4715 | return WF_ONECAP; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4716 | return 0; |
| 4717 | } |
| 4718 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4719 | /* |
| 4720 | * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a |
| 4721 | * capital. So that make_case_word() can turn WOrd into Word. |
| 4722 | * Add ALLCAP for "WOrD". |
| 4723 | */ |
| 4724 | static int |
| 4725 | badword_captype(word, end) |
| 4726 | char_u *word; |
| 4727 | char_u *end; |
| 4728 | { |
| 4729 | int flags = captype(word, end); |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 4730 | int c; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4731 | int l, u; |
| 4732 | int first; |
| 4733 | char_u *p; |
| 4734 | |
| 4735 | if (flags & WF_KEEPCAP) |
| 4736 | { |
| 4737 | /* Count the number of UPPER and lower case letters. */ |
| 4738 | l = u = 0; |
| 4739 | first = FALSE; |
| 4740 | for (p = word; p < end; mb_ptr_adv(p)) |
| 4741 | { |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 4742 | c = PTR2CHAR(p); |
| 4743 | if (SPELL_ISUPPER(c)) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4744 | { |
| 4745 | ++u; |
| 4746 | if (p == word) |
| 4747 | first = TRUE; |
| 4748 | } |
| 4749 | else |
| 4750 | ++l; |
| 4751 | } |
| 4752 | |
| 4753 | /* If there are more UPPER than lower case letters suggest an |
| 4754 | * ALLCAP word. Otherwise, if the first letter is UPPER then |
| 4755 | * suggest ONECAP. Exception: "ALl" most likely should be "All", |
| 4756 | * require three upper case letters. */ |
| 4757 | if (u > l && u > 2) |
| 4758 | flags |= WF_ALLCAP; |
| 4759 | else if (first) |
| 4760 | flags |= WF_ONECAP; |
| Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 4761 | |
| 4762 | if (u >= 2 && l >= 2) /* maCARONI maCAroni */ |
| 4763 | flags |= WF_MIXCAP; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4764 | } |
| 4765 | return flags; |
| 4766 | } |
| 4767 | |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4768 | # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO) |
| 4769 | /* |
| 4770 | * Free all languages. |
| 4771 | */ |
| 4772 | void |
| 4773 | spell_free_all() |
| 4774 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4775 | slang_T *slang; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4776 | buf_T *buf; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4777 | char_u fname[MAXPATHL]; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4778 | |
| 4779 | /* Go through all buffers and handle 'spelllang'. */ |
| 4780 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 4781 | ga_clear(&buf->b_langp); |
| 4782 | |
| 4783 | while (first_lang != NULL) |
| 4784 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4785 | slang = first_lang; |
| 4786 | first_lang = slang->sl_next; |
| 4787 | slang_free(slang); |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4788 | } |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4789 | |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4790 | if (int_wordlist != NULL) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4791 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 4792 | /* Delete the internal wordlist and its .spl file */ |
| 4793 | mch_remove(int_wordlist); |
| 4794 | int_wordlist_spl(fname); |
| 4795 | mch_remove(fname); |
| 4796 | vim_free(int_wordlist); |
| 4797 | int_wordlist = NULL; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 4800 | init_spell_chartab(); |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4801 | |
| 4802 | vim_free(repl_to); |
| 4803 | repl_to = NULL; |
| 4804 | vim_free(repl_from); |
| 4805 | repl_from = NULL; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4806 | } |
| 4807 | # endif |
| 4808 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4809 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 4810 | /* |
| 4811 | * Clear all spelling tables and reload them. |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 4812 | * Used after 'encoding' is set and when ":mkspell" was used. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4813 | */ |
| 4814 | void |
| 4815 | spell_reload() |
| 4816 | { |
| 4817 | buf_T *buf; |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4818 | win_T *wp; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4819 | |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 4820 | /* Initialize the table for spell_iswordp(). */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4821 | init_spell_chartab(); |
| 4822 | |
| 4823 | /* Unload all allocated memory. */ |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 4824 | spell_free_all(); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4825 | |
| 4826 | /* Go through all buffers and handle 'spelllang'. */ |
| 4827 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 4828 | { |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4829 | /* Only load the wordlists when 'spelllang' is set and there is a |
| 4830 | * window for this buffer in which 'spell' is set. */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4831 | if (*buf->b_p_spl != NUL) |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 4832 | { |
| 4833 | FOR_ALL_WINDOWS(wp) |
| 4834 | if (wp->w_buffer == buf && wp->w_p_spell) |
| 4835 | { |
| 4836 | (void)did_set_spelllang(buf); |
| 4837 | # ifdef FEAT_WINDOWS |
| 4838 | break; |
| 4839 | # endif |
| 4840 | } |
| 4841 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4842 | } |
| 4843 | } |
| 4844 | # endif |
| 4845 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4846 | /* |
| 4847 | * Reload the spell file "fname" if it's loaded. |
| 4848 | */ |
| 4849 | static void |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4850 | spell_reload_one(fname, added_word) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4851 | char_u *fname; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4852 | int added_word; /* invoked through "zg" */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4853 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4854 | slang_T *slang; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4855 | int didit = FALSE; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4856 | |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4857 | for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 4858 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4859 | if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4860 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4861 | slang_clear(slang); |
| 4862 | if (spell_load_file(fname, NULL, slang, FALSE) == NULL) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 4863 | /* reloading failed, clear the language */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 4864 | slang_clear(slang); |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 4865 | redraw_all_later(SOME_VALID); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4866 | didit = TRUE; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4867 | } |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 4868 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4869 | |
| 4870 | /* When "zg" was used and the file wasn't loaded yet, should redo |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4871 | * 'spelllang' to load it now. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4872 | if (added_word && !didit) |
| 4873 | did_set_spelllang(curbuf); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4877 | /* |
| 4878 | * Functions for ":mkspell". |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4879 | */ |
| 4880 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4881 | #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] | 4882 | and .dic file. */ |
| 4883 | /* |
| 4884 | * Main structure to store the contents of a ".aff" file. |
| 4885 | */ |
| 4886 | typedef struct afffile_S |
| 4887 | { |
| 4888 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 4889 | int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */ |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 4890 | unsigned af_rare; /* RARE ID for rare word */ |
| 4891 | unsigned af_keepcase; /* KEEPCASE ID for keep-case word */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4892 | unsigned af_bad; /* BAD ID for banned word */ |
| 4893 | unsigned af_needaffix; /* NEEDAFFIX ID */ |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 4894 | unsigned af_circumfix; /* CIRCUMFIX ID */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 4895 | unsigned af_needcomp; /* NEEDCOMPOUND ID */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 4896 | unsigned af_comproot; /* COMPOUNDROOT ID */ |
| 4897 | unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */ |
| 4898 | unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */ |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 4899 | unsigned af_nosuggest; /* NOSUGGEST ID */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 4900 | int af_pfxpostpone; /* postpone prefixes without chop string and |
| 4901 | without flags */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4902 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 4903 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4904 | hashtab_T af_comp; /* hashtable for compound flags, compitem_T */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4905 | } afffile_T; |
| 4906 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4907 | #define AFT_CHAR 0 /* flags are one character */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 4908 | #define AFT_LONG 1 /* flags are two characters */ |
| 4909 | #define AFT_CAPLONG 2 /* flags are one or two characters */ |
| 4910 | #define AFT_NUM 3 /* flags are numbers, comma separated */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4911 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4912 | typedef struct affentry_S affentry_T; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4913 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 4914 | struct affentry_S |
| 4915 | { |
| 4916 | affentry_T *ae_next; /* next affix with same name/number */ |
| 4917 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 4918 | char_u *ae_add; /* text to add to basic word (can be NULL) */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 4919 | char_u *ae_flags; /* flags on the affix (can be NULL) */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4920 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 4921 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 4922 | char ae_compforbid; /* COMPOUNDFORBIDFLAG found */ |
| 4923 | char ae_comppermit; /* COMPOUNDPERMITFLAG found */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4924 | }; |
| 4925 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4926 | #ifdef FEAT_MBYTE |
| 4927 | # define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */ |
| 4928 | #else |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 4929 | # define AH_KEY_LEN 7 /* 6 digits + NUL */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4930 | #endif |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 4931 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4932 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 4933 | typedef struct affheader_S |
| 4934 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4935 | char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */ |
| 4936 | unsigned ah_flag; /* affix name as number, uses "af_flagtype" */ |
| 4937 | int ah_newID; /* prefix ID after renumbering; 0 if not used */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4938 | int ah_combine; /* suffix may combine with prefix */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 4939 | int ah_follows; /* another affix block should be following */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4940 | affentry_T *ah_first; /* first affix entry */ |
| 4941 | } affheader_T; |
| 4942 | |
| 4943 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 4944 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 4945 | /* Flag used in compound items. */ |
| 4946 | typedef struct compitem_S |
| 4947 | { |
| 4948 | char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */ |
| 4949 | unsigned ci_flag; /* affix name as number, uses "af_flagtype" */ |
| 4950 | int ci_newID; /* affix ID after renumbering. */ |
| 4951 | } compitem_T; |
| 4952 | |
| 4953 | #define HI2CI(hi) ((compitem_T *)(hi)->hi_key) |
| 4954 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4955 | /* |
| 4956 | * Structure that is used to store the items in the word tree. This avoids |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4957 | * the need to keep track of each allocated thing, everything is freed all at |
| 4958 | * once after ":mkspell" is done. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4959 | */ |
| 4960 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 4961 | typedef struct sblock_S sblock_T; |
| 4962 | struct sblock_S |
| 4963 | { |
| 4964 | sblock_T *sb_next; /* next block in list */ |
| 4965 | int sb_used; /* nr of bytes already in use */ |
| 4966 | char_u sb_data[1]; /* data, actually longer */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4967 | }; |
| 4968 | |
| 4969 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4970 | * A node in the tree. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4971 | */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4972 | typedef struct wordnode_S wordnode_T; |
| 4973 | struct wordnode_S |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4974 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4975 | union /* shared to save space */ |
| 4976 | { |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 4977 | char_u hashkey[6]; /* the hash key, only used while compressing */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 4978 | int index; /* index in written nodes (valid after first |
| 4979 | round) */ |
| 4980 | } wn_u1; |
| 4981 | union /* shared to save space */ |
| 4982 | { |
| 4983 | wordnode_T *next; /* next node with same hash key */ |
| 4984 | wordnode_T *wnode; /* parent node that will write this node */ |
| 4985 | } wn_u2; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4986 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 4987 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 4988 | always sorted) */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4989 | int wn_refs; /* Nr. of references to this node. Only |
| 4990 | relevant for first node in a list of |
| 4991 | siblings, in following siblings it is |
| 4992 | always one. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4993 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 4994 | |
| 4995 | /* Info for when "wn_byte" is NUL. |
| 4996 | * In PREFIXTREE "wn_region" is used for the prefcondnr. |
| 4997 | * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and |
| 4998 | * "wn_region" the LSW of the wordnr. */ |
| 4999 | char_u wn_affixID; /* supported/required prefix ID or 0 */ |
| 5000 | short_u wn_flags; /* WF_ flags */ |
| 5001 | short wn_region; /* region mask */ |
| 5002 | |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 5003 | #ifdef SPELL_PRINTTREE |
| 5004 | int wn_nr; /* sequence nr for printing */ |
| 5005 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5006 | }; |
| 5007 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5008 | #define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */ |
| 5009 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5010 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5011 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5012 | /* |
| 5013 | * Info used while reading the spell files. |
| 5014 | */ |
| 5015 | typedef struct spellinfo_S |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5016 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5017 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5018 | long si_foldwcount; /* nr of words in si_foldroot */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5019 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5020 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5021 | long si_keepwcount; /* nr of words in si_keeproot */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5022 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5023 | wordnode_T *si_prefroot; /* tree with postponed prefixes */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5024 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5025 | long si_sugtree; /* creating the soundfolding trie */ |
| 5026 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5027 | sblock_T *si_blocks; /* memory blocks used */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 5028 | long si_blocks_cnt; /* memory blocks allocated */ |
| 5029 | long si_compress_cnt; /* words to add before lowering |
| 5030 | compression limit */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5031 | wordnode_T *si_first_free; /* List of nodes that have been freed during |
| 5032 | compression, linked by "wn_child" field. */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 5033 | long si_free_count; /* number of nodes in si_first_free */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5034 | #ifdef SPELL_PRINTTREE |
| 5035 | int si_wordnode_nr; /* sequence nr for nodes */ |
| 5036 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5037 | buf_T *si_spellbuf; /* buffer used to store soundfold word table */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5038 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5039 | int si_ascii; /* handling only ASCII words */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5040 | int si_add; /* addition file */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 5041 | int si_clear_chartab; /* when TRUE clear char tables */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5042 | int si_region; /* region mask */ |
| 5043 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
| Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 5044 | int si_memtot; /* runtime memory used */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5045 | int si_verbose; /* verbose messages */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5046 | int si_msg_count; /* number of words added since last message */ |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5047 | char_u *si_info; /* info text chars or NULL */ |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5048 | int si_region_count; /* number of regions supported (1 when there |
| 5049 | are no regions) */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5050 | char_u si_region_name[16]; /* region names; used only if |
| 5051 | * si_region_count > 1) */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5052 | |
| 5053 | garray_T si_rep; /* list of fromto_T entries from REP lines */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5054 | garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5055 | garray_T si_sal; /* list of fromto_T entries from SAL lines */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5056 | char_u *si_sofofr; /* SOFOFROM text */ |
| 5057 | char_u *si_sofoto; /* SOFOTO text */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5058 | int si_nosugfile; /* NOSUGFILE item found */ |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 5059 | int si_nosplitsugs; /* NOSPLITSUGS item found */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5060 | int si_followup; /* soundsalike: ? */ |
| 5061 | int si_collapse; /* soundsalike: ? */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5062 | hashtab_T si_commonwords; /* hashtable for common words */ |
| 5063 | time_t si_sugtime; /* timestamp for .sug file */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5064 | int si_rem_accents; /* soundsalike: remove accents */ |
| 5065 | garray_T si_map; /* MAP info concatenated */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5066 | char_u *si_midword; /* MIDWORD chars or NULL */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5067 | int si_compmax; /* max nr of words for compounding */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5068 | int si_compminlen; /* minimal length for compounding */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5069 | int si_compsylmax; /* max nr of syllables for compounding */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5070 | int si_compoptions; /* COMP_ flags */ |
| 5071 | garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as |
| 5072 | a string */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5073 | char_u *si_compflags; /* flags used for compounding */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 5074 | char_u si_nobreak; /* NOBREAK */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5075 | char_u *si_syllable; /* syllable string */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5076 | garray_T si_prefcond; /* table with conditions for postponed |
| 5077 | * prefixes, each stored as a string */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5078 | int si_newprefID; /* current value for ah_newID */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5079 | int si_newcompID; /* current value for compound ID */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5080 | } spellinfo_T; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5081 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5082 | static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname)); |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5083 | static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount)); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5084 | static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry)); |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5085 | static int spell_info_item __ARGS((char_u *s)); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5086 | static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum)); |
| 5087 | static unsigned get_affitem __ARGS((int flagtype, char_u **pp)); |
| 5088 | static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags)); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5089 | static void check_renumber __ARGS((spellinfo_T *spin)); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5090 | static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag)); |
| 5091 | static void aff_check_number __ARGS((int spinval, int affval, char *name)); |
| 5092 | static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name)); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5093 | static int str_equal __ARGS((char_u *s1, char_u *s2)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5094 | static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to)); |
| 5095 | static int sal_to_bool __ARGS((char_u *s)); |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5096 | static int has_non_ascii __ARGS((char_u *s)); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5097 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5098 | static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile)); |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 5099 | static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5100 | static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist)); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5101 | static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist)); |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 5102 | static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5103 | static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname)); |
| 5104 | static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align)); |
| 5105 | static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s)); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5106 | static void free_blocks __ARGS((sblock_T *bl)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5107 | static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin)); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5108 | static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix)); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5109 | static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5110 | static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5111 | static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5112 | static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n)); |
| 5113 | static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root)); |
| 5114 | static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot)); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5115 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5116 | static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd)); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5117 | static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname)); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5118 | static void clear_node __ARGS((wordnode_T *node)); |
| Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 5119 | static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5120 | static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname)); |
| 5121 | static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang)); |
| 5122 | static int sug_maketable __ARGS((spellinfo_T *spin)); |
| 5123 | static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap)); |
| 5124 | static int offset2bytes __ARGS((int nr, char_u *buf)); |
| 5125 | static int bytes2offset __ARGS((char_u **pp)); |
| 5126 | static void sug_write __ARGS((spellinfo_T *spin, char_u *fname)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5127 | static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word)); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5128 | static void spell_message __ARGS((spellinfo_T *spin, char_u *str)); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5129 | static void init_spellfile __ARGS((void)); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5130 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5131 | /* In the postponed prefixes tree wn_flags is used to store the WFP_ flags, |
| 5132 | * but it must be negative to indicate the prefix tree to tree_add_word(). |
| 5133 | * Use a negative number with the lower 8 bits zero. */ |
| 5134 | #define PFX_FLAGS -256 |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5135 | |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 5136 | /* flags for "condit" argument of store_aff_word() */ |
| 5137 | #define CONDIT_COMB 1 /* affix must combine */ |
| 5138 | #define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */ |
| 5139 | #define CONDIT_SUF 4 /* add a suffix for matching flags */ |
| 5140 | #define CONDIT_AFF 8 /* word already has an affix */ |
| 5141 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5142 | /* |
| 5143 | * Tunable parameters for when the tree is compressed. See 'mkspellmem'. |
| 5144 | */ |
| 5145 | static long compress_start = 30000; /* memory / SBLOCKSIZE */ |
| 5146 | static long compress_inc = 100; /* memory / SBLOCKSIZE */ |
| 5147 | static long compress_added = 500000; /* word count */ |
| 5148 | |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 5149 | #ifdef SPELL_PRINTTREE |
| 5150 | /* |
| 5151 | * For debugging the tree code: print the current tree in a (more or less) |
| 5152 | * readable format, so that we can see what happens when adding a word and/or |
| 5153 | * compressing the tree. |
| 5154 | * Based on code from Olaf Seibert. |
| 5155 | */ |
| 5156 | #define PRINTLINESIZE 1000 |
| 5157 | #define PRINTWIDTH 6 |
| 5158 | |
| 5159 | #define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \ |
| 5160 | PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2) |
| 5161 | |
| 5162 | static char line1[PRINTLINESIZE]; |
| 5163 | static char line2[PRINTLINESIZE]; |
| 5164 | static char line3[PRINTLINESIZE]; |
| 5165 | |
| 5166 | static void |
| 5167 | spell_clear_flags(wordnode_T *node) |
| 5168 | { |
| 5169 | wordnode_T *np; |
| 5170 | |
| 5171 | for (np = node; np != NULL; np = np->wn_sibling) |
| 5172 | { |
| 5173 | np->wn_u1.index = FALSE; |
| 5174 | spell_clear_flags(np->wn_child); |
| 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | static void |
| 5179 | spell_print_node(wordnode_T *node, int depth) |
| 5180 | { |
| 5181 | if (node->wn_u1.index) |
| 5182 | { |
| 5183 | /* Done this node before, print the reference. */ |
| 5184 | PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0); |
| 5185 | PRINTSOME(line2, depth, " ", 0, 0); |
| 5186 | PRINTSOME(line3, depth, " ", 0, 0); |
| 5187 | msg(line1); |
| 5188 | msg(line2); |
| 5189 | msg(line3); |
| 5190 | } |
| 5191 | else |
| 5192 | { |
| 5193 | node->wn_u1.index = TRUE; |
| 5194 | |
| 5195 | if (node->wn_byte != NUL) |
| 5196 | { |
| 5197 | if (node->wn_child != NULL) |
| 5198 | PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0); |
| 5199 | else |
| 5200 | /* Cannot happen? */ |
| 5201 | PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0); |
| 5202 | } |
| 5203 | else |
| 5204 | PRINTSOME(line1, depth, " $ ", 0, 0); |
| 5205 | |
| 5206 | PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs); |
| 5207 | |
| 5208 | if (node->wn_sibling != NULL) |
| 5209 | PRINTSOME(line3, depth, " | ", 0, 0); |
| 5210 | else |
| 5211 | PRINTSOME(line3, depth, " ", 0, 0); |
| 5212 | |
| 5213 | if (node->wn_byte == NUL) |
| 5214 | { |
| 5215 | msg(line1); |
| 5216 | msg(line2); |
| 5217 | msg(line3); |
| 5218 | } |
| 5219 | |
| 5220 | /* do the children */ |
| 5221 | if (node->wn_byte != NUL && node->wn_child != NULL) |
| 5222 | spell_print_node(node->wn_child, depth + 1); |
| 5223 | |
| 5224 | /* do the siblings */ |
| 5225 | if (node->wn_sibling != NULL) |
| 5226 | { |
| 5227 | /* get rid of all parent details except | */ |
| 5228 | STRCPY(line1, line3); |
| 5229 | STRCPY(line2, line3); |
| 5230 | spell_print_node(node->wn_sibling, depth); |
| 5231 | } |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | static void |
| 5236 | spell_print_tree(wordnode_T *root) |
| 5237 | { |
| 5238 | if (root != NULL) |
| 5239 | { |
| 5240 | /* Clear the "wn_u1.index" fields, used to remember what has been |
| 5241 | * done. */ |
| 5242 | spell_clear_flags(root); |
| 5243 | |
| 5244 | /* Recursively print the tree. */ |
| 5245 | spell_print_node(root, 0); |
| 5246 | } |
| 5247 | } |
| 5248 | #endif /* SPELL_PRINTTREE */ |
| 5249 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5250 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5251 | * Read the affix file "fname". |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 5252 | * Returns an afffile_T, NULL for complete failure. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5253 | */ |
| 5254 | static afffile_T * |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5255 | spell_read_aff(spin, fname) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5256 | spellinfo_T *spin; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5257 | char_u *fname; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5258 | { |
| 5259 | FILE *fd; |
| 5260 | afffile_T *aff; |
| 5261 | char_u rline[MAXLINELEN]; |
| 5262 | char_u *line; |
| 5263 | char_u *pc = NULL; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5264 | #define MAXITEMCNT 30 |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5265 | char_u *(items[MAXITEMCNT]); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5266 | int itemcnt; |
| 5267 | char_u *p; |
| 5268 | int lnum = 0; |
| 5269 | affheader_T *cur_aff = NULL; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5270 | int did_postpone_prefix = FALSE; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5271 | int aff_todo = 0; |
| 5272 | hashtab_T *tp; |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5273 | char_u *low = NULL; |
| 5274 | char_u *fol = NULL; |
| 5275 | char_u *upp = NULL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5276 | int do_rep; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5277 | int do_repsal; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5278 | int do_sal; |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5279 | int do_mapline; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5280 | int found_map = FALSE; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 5281 | hashitem_T *hi; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5282 | int l; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5283 | int compminlen = 0; /* COMPOUNDMIN value */ |
| 5284 | int compsylmax = 0; /* COMPOUNDSYLMAX value */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5285 | int compoptions = 0; /* COMP_ flags */ |
| 5286 | int compmax = 0; /* COMPOUNDWORDMAX value */ |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5287 | char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5288 | concatenated */ |
| 5289 | char_u *midword = NULL; /* MIDWORD value */ |
| 5290 | char_u *syllable = NULL; /* SYLLABLE value */ |
| 5291 | char_u *sofofrom = NULL; /* SOFOFROM value */ |
| 5292 | char_u *sofoto = NULL; /* SOFOTO value */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5293 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5294 | /* |
| 5295 | * Open the file. |
| 5296 | */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5297 | fd = mch_fopen((char *)fname, "r"); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5298 | if (fd == NULL) |
| 5299 | { |
| 5300 | EMSG2(_(e_notopen), fname); |
| 5301 | return NULL; |
| 5302 | } |
| 5303 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5304 | vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname); |
| 5305 | spell_message(spin, IObuff); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5306 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5307 | /* Only do REP lines when not done in another .aff file already. */ |
| 5308 | do_rep = spin->si_rep.ga_len == 0; |
| 5309 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5310 | /* Only do REPSAL lines when not done in another .aff file already. */ |
| 5311 | do_repsal = spin->si_repsal.ga_len == 0; |
| 5312 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5313 | /* Only do SAL lines when not done in another .aff file already. */ |
| 5314 | do_sal = spin->si_sal.ga_len == 0; |
| 5315 | |
| 5316 | /* Only do MAP lines when not done in another .aff file already. */ |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5317 | do_mapline = spin->si_map.ga_len == 0; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5318 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5319 | /* |
| 5320 | * Allocate and init the afffile_T structure. |
| 5321 | */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5322 | aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5323 | if (aff == NULL) |
| Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5324 | { |
| 5325 | fclose(fd); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5326 | return NULL; |
| Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 5327 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5328 | hash_init(&aff->af_pref); |
| 5329 | hash_init(&aff->af_suff); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5330 | hash_init(&aff->af_comp); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5331 | |
| 5332 | /* |
| 5333 | * Read all the lines in the file one by one. |
| 5334 | */ |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5335 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5336 | { |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5337 | line_breakcheck(); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5338 | ++lnum; |
| 5339 | |
| 5340 | /* Skip comment lines. */ |
| 5341 | if (*rline == '#') |
| 5342 | continue; |
| 5343 | |
| 5344 | /* Convert from "SET" to 'encoding' when needed. */ |
| 5345 | vim_free(pc); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5346 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5347 | if (spin->si_conv.vc_type != CONV_NONE) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5348 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5349 | pc = string_convert(&spin->si_conv, rline, NULL); |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5350 | if (pc == NULL) |
| 5351 | { |
| 5352 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 5353 | fname, lnum, rline); |
| 5354 | continue; |
| 5355 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5356 | line = pc; |
| 5357 | } |
| 5358 | else |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5359 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5360 | { |
| 5361 | pc = NULL; |
| 5362 | line = rline; |
| 5363 | } |
| 5364 | |
| 5365 | /* Split the line up in white separated items. Put a NUL after each |
| 5366 | * item. */ |
| 5367 | itemcnt = 0; |
| 5368 | for (p = line; ; ) |
| 5369 | { |
| 5370 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 5371 | ++p; |
| 5372 | if (*p == NUL) |
| 5373 | break; |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5374 | if (itemcnt == MAXITEMCNT) /* too many items */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5375 | break; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5376 | items[itemcnt++] = p; |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5377 | /* A few items have arbitrary text argument, don't split them. */ |
| 5378 | if (itemcnt == 2 && spell_info_item(items[0])) |
| 5379 | while (*p >= ' ' || *p == TAB) /* skip until CR/NL */ |
| 5380 | ++p; |
| 5381 | else |
| 5382 | while (*p > ' ') /* skip until white space or CR/NL */ |
| 5383 | ++p; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5384 | if (*p == NUL) |
| 5385 | break; |
| 5386 | *p++ = NUL; |
| 5387 | } |
| 5388 | |
| 5389 | /* Handle non-empty lines. */ |
| 5390 | if (itemcnt > 0) |
| 5391 | { |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5392 | if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5393 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5394 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5395 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 5396 | aff->af_enc = enc_canonize(items[1]); |
| 5397 | if (aff->af_enc != NULL && !spin->si_ascii |
| 5398 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 5399 | p_enc) == FAIL) |
| 5400 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 5401 | fname, aff->af_enc, p_enc); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 5402 | spin->si_conv.vc_fail = TRUE; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 5403 | #else |
| 5404 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 5405 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5406 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5407 | else if (is_aff_rule(items, itemcnt, "FLAG", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5408 | && aff->af_flagtype == AFT_CHAR) |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5409 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5410 | if (STRCMP(items[1], "long") == 0) |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5411 | aff->af_flagtype = AFT_LONG; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5412 | else if (STRCMP(items[1], "num") == 0) |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5413 | aff->af_flagtype = AFT_NUM; |
| 5414 | else if (STRCMP(items[1], "caplong") == 0) |
| 5415 | aff->af_flagtype = AFT_CAPLONG; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5416 | else |
| 5417 | smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"), |
| 5418 | fname, lnum, items[1]); |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5419 | if (aff->af_rare != 0 |
| 5420 | || aff->af_keepcase != 0 |
| 5421 | || aff->af_bad != 0 |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5422 | || aff->af_needaffix != 0 |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 5423 | || aff->af_circumfix != 0 |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5424 | || aff->af_needcomp != 0 |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5425 | || aff->af_comproot != 0 |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 5426 | || aff->af_nosuggest != 0 |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5427 | || compflags != NULL |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5428 | || aff->af_suff.ht_used > 0 |
| 5429 | || aff->af_pref.ht_used > 0) |
| 5430 | smsg((char_u *)_("FLAG after using flags in %s line %d: %s"), |
| 5431 | fname, lnum, items[1]); |
| 5432 | } |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5433 | else if (spell_info_item(items[0])) |
| 5434 | { |
| 5435 | p = (char_u *)getroom(spin, |
| 5436 | (spin->si_info == NULL ? 0 : STRLEN(spin->si_info)) |
| 5437 | + STRLEN(items[0]) |
| 5438 | + STRLEN(items[1]) + 3, FALSE); |
| 5439 | if (p != NULL) |
| 5440 | { |
| 5441 | if (spin->si_info != NULL) |
| 5442 | { |
| 5443 | STRCPY(p, spin->si_info); |
| 5444 | STRCAT(p, "\n"); |
| 5445 | } |
| 5446 | STRCAT(p, items[0]); |
| 5447 | STRCAT(p, " "); |
| 5448 | STRCAT(p, items[1]); |
| 5449 | spin->si_info = p; |
| 5450 | } |
| 5451 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5452 | else if (is_aff_rule(items, itemcnt, "MIDWORD", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5453 | && midword == NULL) |
| 5454 | { |
| 5455 | midword = getroom_save(spin, items[1]); |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5456 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5457 | else if (is_aff_rule(items, itemcnt, "TRY", 2)) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5458 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5459 | /* ignored, we look in the tree for what chars may appear */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5460 | } |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5461 | /* TODO: remove "RAR" later */ |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5462 | else if ((is_aff_rule(items, itemcnt, "RAR", 2) |
| 5463 | || is_aff_rule(items, itemcnt, "RARE", 2)) |
| 5464 | && aff->af_rare == 0) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5465 | { |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5466 | aff->af_rare = affitem2flag(aff->af_flagtype, items[1], |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5467 | fname, lnum); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5468 | } |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5469 | /* TODO: remove "KEP" later */ |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5470 | else if ((is_aff_rule(items, itemcnt, "KEP", 2) |
| 5471 | || is_aff_rule(items, itemcnt, "KEEPCASE", 2)) |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5472 | && aff->af_keepcase == 0) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5473 | { |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5474 | aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1], |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5475 | fname, lnum); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 5476 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5477 | else if ((is_aff_rule(items, itemcnt, "BAD", 2) |
| 5478 | || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2)) |
| 5479 | && aff->af_bad == 0) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5480 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5481 | aff->af_bad = affitem2flag(aff->af_flagtype, items[1], |
| 5482 | fname, lnum); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 5483 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5484 | else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5485 | && aff->af_needaffix == 0) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5486 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5487 | aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1], |
| 5488 | fname, lnum); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5489 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5490 | else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2) |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 5491 | && aff->af_circumfix == 0) |
| 5492 | { |
| 5493 | aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1], |
| 5494 | fname, lnum); |
| 5495 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5496 | else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2) |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 5497 | && aff->af_nosuggest == 0) |
| 5498 | { |
| 5499 | aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1], |
| 5500 | fname, lnum); |
| 5501 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5502 | else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2) |
| 5503 | || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2)) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5504 | && aff->af_needcomp == 0) |
| 5505 | { |
| 5506 | aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1], |
| 5507 | fname, lnum); |
| 5508 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5509 | else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5510 | && aff->af_comproot == 0) |
| 5511 | { |
| 5512 | aff->af_comproot = affitem2flag(aff->af_flagtype, items[1], |
| 5513 | fname, lnum); |
| 5514 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5515 | else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2) |
| 5516 | && aff->af_compforbid == 0) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5517 | { |
| 5518 | aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1], |
| 5519 | fname, lnum); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5520 | if (aff->af_pref.ht_used > 0) |
| 5521 | smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"), |
| 5522 | fname, lnum); |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5523 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5524 | else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2) |
| 5525 | && aff->af_comppermit == 0) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5526 | { |
| 5527 | aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1], |
| 5528 | fname, lnum); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5529 | if (aff->af_pref.ht_used > 0) |
| 5530 | smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"), |
| 5531 | fname, lnum); |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5532 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5533 | else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5534 | && compflags == NULL) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5535 | { |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5536 | /* Turn flag "c" into COMPOUNDRULE compatible string "c+", |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5537 | * "Na" into "Na+", "1234" into "1234+". */ |
| 5538 | p = getroom(spin, STRLEN(items[1]) + 2, FALSE); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5539 | if (p != NULL) |
| 5540 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5541 | STRCPY(p, items[1]); |
| 5542 | STRCAT(p, "+"); |
| 5543 | compflags = p; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5544 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5545 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5546 | else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2)) |
| 5547 | { |
| 5548 | /* We don't use the count, but do check that it's a number and |
| 5549 | * not COMPOUNDRULE mistyped. */ |
| 5550 | if (atoi((char *)items[1]) == 0) |
| 5551 | smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"), |
| 5552 | fname, lnum, items[1]); |
| 5553 | } |
| 5554 | else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2)) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5555 | { |
| 5556 | /* Concatenate this string to previously defined ones, using a |
| 5557 | * slash to separate them. */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5558 | l = (int)STRLEN(items[1]) + 1; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5559 | if (compflags != NULL) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5560 | l += (int)STRLEN(compflags) + 1; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5561 | p = getroom(spin, l, FALSE); |
| 5562 | if (p != NULL) |
| 5563 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5564 | if (compflags != NULL) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5565 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5566 | STRCPY(p, compflags); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5567 | STRCAT(p, "/"); |
| 5568 | } |
| 5569 | STRCAT(p, items[1]); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5570 | compflags = p; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5571 | } |
| 5572 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5573 | else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5574 | && compmax == 0) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5575 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5576 | compmax = atoi((char *)items[1]); |
| 5577 | if (compmax == 0) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5578 | smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"), |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5579 | fname, lnum, items[1]); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5580 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5581 | else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5582 | && compminlen == 0) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5583 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5584 | compminlen = atoi((char *)items[1]); |
| 5585 | if (compminlen == 0) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5586 | smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"), |
| 5587 | fname, lnum, items[1]); |
| 5588 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5589 | else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5590 | && compsylmax == 0) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5591 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5592 | compsylmax = atoi((char *)items[1]); |
| 5593 | if (compsylmax == 0) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5594 | smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"), |
| 5595 | fname, lnum, items[1]); |
| 5596 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5597 | else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1)) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5598 | { |
| 5599 | compoptions |= COMP_CHECKDUP; |
| 5600 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5601 | else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1)) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5602 | { |
| 5603 | compoptions |= COMP_CHECKREP; |
| 5604 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5605 | else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1)) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5606 | { |
| 5607 | compoptions |= COMP_CHECKCASE; |
| 5608 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5609 | else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1)) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5610 | { |
| 5611 | compoptions |= COMP_CHECKTRIPLE; |
| 5612 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5613 | else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2)) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5614 | { |
| 5615 | if (atoi((char *)items[1]) == 0) |
| 5616 | smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"), |
| 5617 | fname, lnum, items[1]); |
| 5618 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5619 | else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3)) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5620 | { |
| 5621 | garray_T *gap = &spin->si_comppat; |
| 5622 | int i; |
| 5623 | |
| 5624 | /* Only add the couple if it isn't already there. */ |
| 5625 | for (i = 0; i < gap->ga_len - 1; i += 2) |
| 5626 | if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0 |
| 5627 | && STRCMP(((char_u **)(gap->ga_data))[i + 1], |
| 5628 | items[2]) == 0) |
| 5629 | break; |
| 5630 | if (i >= gap->ga_len && ga_grow(gap, 2) == OK) |
| 5631 | { |
| 5632 | ((char_u **)(gap->ga_data))[gap->ga_len++] |
| 5633 | = getroom_save(spin, items[1]); |
| 5634 | ((char_u **)(gap->ga_data))[gap->ga_len++] |
| 5635 | = getroom_save(spin, items[2]); |
| 5636 | } |
| 5637 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5638 | else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5639 | && syllable == NULL) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5640 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5641 | syllable = getroom_save(spin, items[1]); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 5642 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5643 | else if (is_aff_rule(items, itemcnt, "NOBREAK", 1)) |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 5644 | { |
| 5645 | spin->si_nobreak = TRUE; |
| 5646 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5647 | else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1)) |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 5648 | { |
| 5649 | spin->si_nosplitsugs = TRUE; |
| 5650 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5651 | else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1)) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5652 | { |
| 5653 | spin->si_nosugfile = TRUE; |
| 5654 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5655 | else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1)) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5656 | { |
| 5657 | aff->af_pfxpostpone = TRUE; |
| 5658 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5659 | else if ((STRCMP(items[0], "PFX") == 0 |
| 5660 | || STRCMP(items[0], "SFX") == 0) |
| 5661 | && aff_todo == 0 |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5662 | && itemcnt >= 4) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5663 | { |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5664 | int lasti = 4; |
| 5665 | char_u key[AH_KEY_LEN]; |
| 5666 | |
| 5667 | if (*items[0] == 'P') |
| 5668 | tp = &aff->af_pref; |
| 5669 | else |
| 5670 | tp = &aff->af_suff; |
| 5671 | |
| 5672 | /* Myspell allows the same affix name to be used multiple |
| 5673 | * times. The affix files that do this have an undocumented |
| 5674 | * "S" flag on all but the last block, thus we check for that |
| 5675 | * and store it in ah_follows. */ |
| 5676 | vim_strncpy(key, items[1], AH_KEY_LEN - 1); |
| 5677 | hi = hash_find(tp, key); |
| 5678 | if (!HASHITEM_EMPTY(hi)) |
| 5679 | { |
| 5680 | cur_aff = HI2AH(hi); |
| 5681 | if (cur_aff->ah_combine != (*items[2] == 'Y')) |
| 5682 | smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"), |
| 5683 | fname, lnum, items[1]); |
| 5684 | if (!cur_aff->ah_follows) |
| 5685 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 5686 | fname, lnum, items[1]); |
| 5687 | } |
| 5688 | else |
| 5689 | { |
| 5690 | /* New affix letter. */ |
| 5691 | cur_aff = (affheader_T *)getroom(spin, |
| 5692 | sizeof(affheader_T), TRUE); |
| 5693 | if (cur_aff == NULL) |
| 5694 | break; |
| 5695 | cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1], |
| 5696 | fname, lnum); |
| 5697 | if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN) |
| 5698 | break; |
| 5699 | if (cur_aff->ah_flag == aff->af_bad |
| Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 5700 | || cur_aff->ah_flag == aff->af_rare |
| 5701 | || cur_aff->ah_flag == aff->af_keepcase |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5702 | || cur_aff->ah_flag == aff->af_needaffix |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 5703 | || cur_aff->ah_flag == aff->af_circumfix |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 5704 | || cur_aff->ah_flag == aff->af_nosuggest |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5705 | || cur_aff->ah_flag == aff->af_needcomp |
| 5706 | || cur_aff->ah_flag == aff->af_comproot) |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 5707 | smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"), |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5708 | fname, lnum, items[1]); |
| 5709 | STRCPY(cur_aff->ah_key, items[1]); |
| 5710 | hash_add(tp, cur_aff->ah_key); |
| 5711 | |
| 5712 | cur_aff->ah_combine = (*items[2] == 'Y'); |
| 5713 | } |
| 5714 | |
| 5715 | /* Check for the "S" flag, which apparently means that another |
| 5716 | * block with the same affix name is following. */ |
| 5717 | if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0) |
| 5718 | { |
| 5719 | ++lasti; |
| 5720 | cur_aff->ah_follows = TRUE; |
| 5721 | } |
| 5722 | else |
| 5723 | cur_aff->ah_follows = FALSE; |
| 5724 | |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5725 | /* Myspell allows extra text after the item, but that might |
| 5726 | * mean mistakes go unnoticed. Require a comment-starter. */ |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5727 | if (itemcnt > lasti && *items[lasti] != '#') |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5728 | smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]); |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5729 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5730 | if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5731 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 5732 | fname, lnum, items[2]); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5733 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5734 | if (*items[0] == 'P' && aff->af_pfxpostpone) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5735 | { |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5736 | if (cur_aff->ah_newID == 0) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5737 | { |
| 5738 | /* Use a new number in the .spl file later, to be able |
| 5739 | * to handle multiple .aff files. */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 5740 | check_renumber(spin); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5741 | cur_aff->ah_newID = ++spin->si_newprefID; |
| 5742 | |
| 5743 | /* We only really use ah_newID if the prefix is |
| 5744 | * postponed. We know that only after handling all |
| 5745 | * the items. */ |
| 5746 | did_postpone_prefix = FALSE; |
| 5747 | } |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5748 | else |
| 5749 | /* Did use the ID in a previous block. */ |
| 5750 | did_postpone_prefix = TRUE; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5751 | } |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 5752 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5753 | aff_todo = atoi((char *)items[3]); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5754 | } |
| 5755 | else if ((STRCMP(items[0], "PFX") == 0 |
| 5756 | || STRCMP(items[0], "SFX") == 0) |
| 5757 | && aff_todo > 0 |
| 5758 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5759 | && itemcnt >= 5) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5760 | { |
| 5761 | affentry_T *aff_entry; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5762 | int upper = FALSE; |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 5763 | int lasti = 5; |
| 5764 | |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5765 | /* Myspell allows extra text after the item, but that might |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5766 | * mean mistakes go unnoticed. Require a comment-starter. |
| 5767 | * Hunspell uses a "-" item. */ |
| 5768 | if (itemcnt > lasti && *items[lasti] != '#' |
| 5769 | && (STRCMP(items[lasti], "-") != 0 |
| 5770 | || itemcnt != lasti + 1)) |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 5771 | smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]); |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 5772 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5773 | /* New item for an affix letter. */ |
| 5774 | --aff_todo; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5775 | aff_entry = (affentry_T *)getroom(spin, |
| Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 5776 | sizeof(affentry_T), TRUE); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5777 | if (aff_entry == NULL) |
| 5778 | break; |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5779 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5780 | if (STRCMP(items[2], "0") != 0) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5781 | aff_entry->ae_chop = getroom_save(spin, items[2]); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5782 | if (STRCMP(items[3], "0") != 0) |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5783 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5784 | aff_entry->ae_add = getroom_save(spin, items[3]); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5785 | |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5786 | /* Recognize flags on the affix: abcd/XYZ */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5787 | aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/'); |
| 5788 | if (aff_entry->ae_flags != NULL) |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5789 | { |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5790 | *aff_entry->ae_flags++ = NUL; |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5791 | aff_process_flags(aff, aff_entry); |
| 5792 | } |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5795 | /* Don't use an affix entry with non-ASCII characters when |
| 5796 | * "spin->si_ascii" is TRUE. */ |
| 5797 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5798 | || has_non_ascii(aff_entry->ae_add))) |
| 5799 | { |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5800 | aff_entry->ae_next = cur_aff->ah_first; |
| 5801 | cur_aff->ah_first = aff_entry; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5802 | |
| 5803 | if (STRCMP(items[4], ".") != 0) |
| 5804 | { |
| 5805 | char_u buf[MAXLINELEN]; |
| 5806 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5807 | aff_entry->ae_cond = getroom_save(spin, items[4]); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5808 | if (*items[0] == 'P') |
| 5809 | sprintf((char *)buf, "^%s", items[4]); |
| 5810 | else |
| 5811 | sprintf((char *)buf, "%s$", items[4]); |
| 5812 | aff_entry->ae_prog = vim_regcomp(buf, |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 5813 | RE_MAGIC + RE_STRING + RE_STRICT); |
| 5814 | if (aff_entry->ae_prog == NULL) |
| 5815 | smsg((char_u *)_("Broken condition in %s line %d: %s"), |
| 5816 | fname, lnum, items[4]); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 5817 | } |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5818 | |
| 5819 | /* For postponed prefixes we need an entry in si_prefcond |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5820 | * for the condition. Use an existing one if possible. |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5821 | * Can't be done for an affix with flags, ignoring |
| 5822 | * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5823 | if (*items[0] == 'P' && aff->af_pfxpostpone |
| 5824 | && aff_entry->ae_flags == NULL) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5825 | { |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5826 | /* When the chop string is one lower-case letter and |
| 5827 | * the add string ends in the upper-case letter we set |
| 5828 | * the "upper" flag, clear "ae_chop" and remove the |
| 5829 | * letters from "ae_add". The condition must either |
| 5830 | * be empty or start with the same letter. */ |
| 5831 | if (aff_entry->ae_chop != NULL |
| 5832 | && aff_entry->ae_add != NULL |
| 5833 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5834 | && aff_entry->ae_chop[(*mb_ptr2len)( |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5835 | aff_entry->ae_chop)] == NUL |
| 5836 | #else |
| 5837 | && aff_entry->ae_chop[1] == NUL |
| 5838 | #endif |
| 5839 | ) |
| 5840 | { |
| 5841 | int c, c_up; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5842 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5843 | c = PTR2CHAR(aff_entry->ae_chop); |
| 5844 | c_up = SPELL_TOUPPER(c); |
| 5845 | if (c_up != c |
| 5846 | && (aff_entry->ae_cond == NULL |
| 5847 | || PTR2CHAR(aff_entry->ae_cond) == c)) |
| 5848 | { |
| 5849 | p = aff_entry->ae_add |
| 5850 | + STRLEN(aff_entry->ae_add); |
| 5851 | mb_ptr_back(aff_entry->ae_add, p); |
| 5852 | if (PTR2CHAR(p) == c_up) |
| 5853 | { |
| 5854 | upper = TRUE; |
| 5855 | aff_entry->ae_chop = NULL; |
| 5856 | *p = NUL; |
| 5857 | |
| 5858 | /* The condition is matched with the |
| 5859 | * actual word, thus must check for the |
| 5860 | * upper-case letter. */ |
| 5861 | if (aff_entry->ae_cond != NULL) |
| 5862 | { |
| 5863 | char_u buf[MAXLINELEN]; |
| 5864 | #ifdef FEAT_MBYTE |
| 5865 | if (has_mbyte) |
| 5866 | { |
| 5867 | onecap_copy(items[4], buf, TRUE); |
| 5868 | aff_entry->ae_cond = getroom_save( |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5869 | spin, buf); |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5870 | } |
| 5871 | else |
| 5872 | #endif |
| 5873 | *aff_entry->ae_cond = c_up; |
| 5874 | if (aff_entry->ae_cond != NULL) |
| 5875 | { |
| 5876 | sprintf((char *)buf, "^%s", |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5877 | aff_entry->ae_cond); |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5878 | vim_free(aff_entry->ae_prog); |
| 5879 | aff_entry->ae_prog = vim_regcomp( |
| 5880 | buf, RE_MAGIC + RE_STRING); |
| 5881 | } |
| 5882 | } |
| 5883 | } |
| 5884 | } |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5885 | } |
| 5886 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5887 | if (aff_entry->ae_chop == NULL |
| 5888 | && aff_entry->ae_flags == NULL) |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5889 | { |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5890 | int idx; |
| 5891 | char_u **pp; |
| 5892 | int n; |
| 5893 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5894 | /* Find a previously used condition. */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5895 | for (idx = spin->si_prefcond.ga_len - 1; idx >= 0; |
| 5896 | --idx) |
| 5897 | { |
| 5898 | p = ((char_u **)spin->si_prefcond.ga_data)[idx]; |
| 5899 | if (str_equal(p, aff_entry->ae_cond)) |
| 5900 | break; |
| 5901 | } |
| 5902 | if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK) |
| 5903 | { |
| 5904 | /* Not found, add a new condition. */ |
| 5905 | idx = spin->si_prefcond.ga_len++; |
| 5906 | pp = ((char_u **)spin->si_prefcond.ga_data) |
| 5907 | + idx; |
| 5908 | if (aff_entry->ae_cond == NULL) |
| 5909 | *pp = NULL; |
| 5910 | else |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5911 | *pp = getroom_save(spin, |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5912 | aff_entry->ae_cond); |
| 5913 | } |
| 5914 | |
| 5915 | /* Add the prefix to the prefix tree. */ |
| 5916 | if (aff_entry->ae_add == NULL) |
| 5917 | p = (char_u *)""; |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 5918 | else |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5919 | p = aff_entry->ae_add; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 5920 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5921 | /* PFX_FLAGS is a negative number, so that |
| 5922 | * tree_add_word() knows this is the prefix tree. */ |
| 5923 | n = PFX_FLAGS; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5924 | if (!cur_aff->ah_combine) |
| 5925 | n |= WFP_NC; |
| 5926 | if (upper) |
| 5927 | n |= WFP_UP; |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 5928 | if (aff_entry->ae_comppermit) |
| 5929 | n |= WFP_COMPPERMIT; |
| 5930 | if (aff_entry->ae_compforbid) |
| 5931 | n |= WFP_COMPFORBID; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5932 | tree_add_word(spin, p, spin->si_prefroot, n, |
| 5933 | idx, cur_aff->ah_newID); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5934 | did_postpone_prefix = TRUE; |
| 5935 | } |
| 5936 | |
| 5937 | /* Didn't actually use ah_newID, backup si_newprefID. */ |
| 5938 | if (aff_todo == 0 && !did_postpone_prefix) |
| 5939 | { |
| 5940 | --spin->si_newprefID; |
| 5941 | cur_aff->ah_newID = 0; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 5942 | } |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 5943 | } |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 5944 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5945 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5946 | else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL) |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5947 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5948 | fol = vim_strsave(items[1]); |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5949 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5950 | else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL) |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5951 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5952 | low = vim_strsave(items[1]); |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5953 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5954 | else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL) |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5955 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 5956 | upp = vim_strsave(items[1]); |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 5957 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5958 | else if (is_aff_rule(items, itemcnt, "REP", 2) |
| 5959 | || is_aff_rule(items, itemcnt, "REPSAL", 2)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5960 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5961 | /* Ignore REP/REPSAL count */; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5962 | if (!isdigit(*items[1])) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5963 | smsg((char_u *)_("Expected REP(SAL) count in %s line %d"), |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5964 | fname, lnum); |
| 5965 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5966 | else if ((STRCMP(items[0], "REP") == 0 |
| 5967 | || STRCMP(items[0], "REPSAL") == 0) |
| 5968 | && itemcnt >= 3) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 5969 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5970 | /* REP/REPSAL item */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 5971 | /* Myspell ignores extra arguments, we require it starts with |
| 5972 | * # to detect mistakes. */ |
| 5973 | if (itemcnt > 3 && items[3][0] != '#') |
| 5974 | smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5975 | if (items[0][3] == 'S' ? do_repsal : do_rep) |
| Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 5976 | { |
| 5977 | /* Replace underscore with space (can't include a space |
| 5978 | * directly). */ |
| 5979 | for (p = items[1]; *p != NUL; mb_ptr_adv(p)) |
| 5980 | if (*p == '_') |
| 5981 | *p = ' '; |
| 5982 | for (p = items[2]; *p != NUL; mb_ptr_adv(p)) |
| 5983 | if (*p == '_') |
| 5984 | *p = ' '; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5985 | add_fromto(spin, items[0][3] == 'S' |
| 5986 | ? &spin->si_repsal |
| 5987 | : &spin->si_rep, items[1], items[2]); |
| Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 5988 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5989 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 5990 | else if (is_aff_rule(items, itemcnt, "MAP", 2)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 5991 | { |
| 5992 | /* MAP item or count */ |
| 5993 | if (!found_map) |
| 5994 | { |
| 5995 | /* First line contains the count. */ |
| 5996 | found_map = TRUE; |
| 5997 | if (!isdigit(*items[1])) |
| 5998 | smsg((char_u *)_("Expected MAP count in %s line %d"), |
| 5999 | fname, lnum); |
| 6000 | } |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 6001 | else if (do_mapline) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6002 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 6003 | int c; |
| 6004 | |
| 6005 | /* Check that every character appears only once. */ |
| 6006 | for (p = items[1]; *p != NUL; ) |
| 6007 | { |
| 6008 | #ifdef FEAT_MBYTE |
| 6009 | c = mb_ptr2char_adv(&p); |
| 6010 | #else |
| 6011 | c = *p++; |
| 6012 | #endif |
| 6013 | if ((spin->si_map.ga_len > 0 |
| 6014 | && vim_strchr(spin->si_map.ga_data, c) |
| 6015 | != NULL) |
| 6016 | || vim_strchr(p, c) != NULL) |
| 6017 | smsg((char_u *)_("Duplicate character in MAP in %s line %d"), |
| 6018 | fname, lnum); |
| 6019 | } |
| 6020 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6021 | /* We simply concatenate all the MAP strings, separated by |
| 6022 | * slashes. */ |
| 6023 | ga_concat(&spin->si_map, items[1]); |
| 6024 | ga_append(&spin->si_map, '/'); |
| 6025 | } |
| 6026 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 6027 | /* Accept "SAL from to" and "SAL from to #comment". */ |
| 6028 | else if (is_aff_rule(items, itemcnt, "SAL", 3)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6029 | { |
| 6030 | if (do_sal) |
| 6031 | { |
| 6032 | /* SAL item (sounds-a-like) |
| 6033 | * Either one of the known keys or a from-to pair. */ |
| 6034 | if (STRCMP(items[1], "followup") == 0) |
| 6035 | spin->si_followup = sal_to_bool(items[2]); |
| 6036 | else if (STRCMP(items[1], "collapse_result") == 0) |
| 6037 | spin->si_collapse = sal_to_bool(items[2]); |
| 6038 | else if (STRCMP(items[1], "remove_accents") == 0) |
| 6039 | spin->si_rem_accents = sal_to_bool(items[2]); |
| 6040 | else |
| 6041 | /* when "to" is "_" it means empty */ |
| 6042 | add_fromto(spin, &spin->si_sal, items[1], |
| 6043 | STRCMP(items[2], "_") == 0 ? (char_u *)"" |
| 6044 | : items[2]); |
| 6045 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6046 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 6047 | else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6048 | && sofofrom == NULL) |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6049 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6050 | sofofrom = getroom_save(spin, items[1]); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6051 | } |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 6052 | else if (is_aff_rule(items, itemcnt, "SOFOTO", 2) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6053 | && sofoto == NULL) |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6054 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6055 | sofoto = getroom_save(spin, items[1]); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6056 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 6057 | else if (STRCMP(items[0], "COMMON") == 0) |
| 6058 | { |
| 6059 | int i; |
| 6060 | |
| 6061 | for (i = 1; i < itemcnt; ++i) |
| 6062 | { |
| 6063 | if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords, |
| 6064 | items[i]))) |
| 6065 | { |
| 6066 | p = vim_strsave(items[i]); |
| 6067 | if (p == NULL) |
| 6068 | break; |
| 6069 | hash_add(&spin->si_commonwords, p); |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6073 | else |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6074 | smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"), |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6075 | fname, lnum, items[0]); |
| 6076 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6077 | } |
| 6078 | |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6079 | if (fol != NULL || low != NULL || upp != NULL) |
| 6080 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6081 | if (spin->si_clear_chartab) |
| 6082 | { |
| 6083 | /* Clear the char type tables, don't want to use any of the |
| 6084 | * currently used spell properties. */ |
| 6085 | init_spell_chartab(); |
| 6086 | spin->si_clear_chartab = FALSE; |
| 6087 | } |
| 6088 | |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 6089 | /* |
| 6090 | * Don't write a word table for an ASCII file, so that we don't check |
| 6091 | * for conflicts with a word table that matches 'encoding'. |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6092 | * Don't write one for utf-8 either, we use utf_*() and |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 6093 | * mb_get_class(), the list of chars in the file will be incomplete. |
| 6094 | */ |
| 6095 | if (!spin->si_ascii |
| 6096 | #ifdef FEAT_MBYTE |
| 6097 | && !enc_utf8 |
| 6098 | #endif |
| 6099 | ) |
| Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 6100 | { |
| 6101 | if (fol == NULL || low == NULL || upp == NULL) |
| 6102 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 6103 | else |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 6104 | (void)set_spell_chartab(fol, low, upp); |
| Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 6105 | } |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6106 | |
| 6107 | vim_free(fol); |
| 6108 | vim_free(low); |
| 6109 | vim_free(upp); |
| 6110 | } |
| 6111 | |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6112 | /* Use compound specifications of the .aff file for the spell info. */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6113 | if (compmax != 0) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6114 | { |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 6115 | aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX"); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6116 | spin->si_compmax = compmax; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6119 | if (compminlen != 0) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6120 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6121 | aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN"); |
| 6122 | spin->si_compminlen = compminlen; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6123 | } |
| 6124 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6125 | if (compsylmax != 0) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6126 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6127 | if (syllable == NULL) |
| 6128 | smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE")); |
| 6129 | aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX"); |
| 6130 | spin->si_compsylmax = compsylmax; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 6133 | if (compoptions != 0) |
| 6134 | { |
| 6135 | aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options"); |
| 6136 | spin->si_compoptions |= compoptions; |
| 6137 | } |
| 6138 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6139 | if (compflags != NULL) |
| 6140 | process_compflags(spin, aff, compflags); |
| 6141 | |
| 6142 | /* Check that we didn't use too many renumbered flags. */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 6143 | if (spin->si_newcompID < spin->si_newprefID) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6144 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 6145 | if (spin->si_newcompID == 127 || spin->si_newcompID == 255) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6146 | MSG(_("Too many postponed prefixes")); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 6147 | else if (spin->si_newprefID == 0 || spin->si_newprefID == 127) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6148 | MSG(_("Too many compound flags")); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6149 | else |
| Bram Moolenaar | 6949d1d | 2008-08-25 02:14:05 +0000 | [diff] [blame] | 6150 | MSG(_("Too many postponed prefixes and/or compound flags")); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6153 | if (syllable != NULL) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6154 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6155 | aff_check_string(spin->si_syllable, syllable, "SYLLABLE"); |
| 6156 | spin->si_syllable = syllable; |
| 6157 | } |
| 6158 | |
| 6159 | if (sofofrom != NULL || sofoto != NULL) |
| 6160 | { |
| 6161 | if (sofofrom == NULL || sofoto == NULL) |
| 6162 | smsg((char_u *)_("Missing SOFO%s line in %s"), |
| 6163 | sofofrom == NULL ? "FROM" : "TO", fname); |
| 6164 | else if (spin->si_sal.ga_len > 0) |
| 6165 | smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6166 | else |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6167 | { |
| 6168 | aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM"); |
| 6169 | aff_check_string(spin->si_sofoto, sofoto, "SOFOTO"); |
| 6170 | spin->si_sofofr = sofofrom; |
| 6171 | spin->si_sofoto = sofoto; |
| 6172 | } |
| 6173 | } |
| 6174 | |
| 6175 | if (midword != NULL) |
| 6176 | { |
| 6177 | aff_check_string(spin->si_midword, midword, "MIDWORD"); |
| 6178 | spin->si_midword = midword; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6179 | } |
| 6180 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6181 | vim_free(pc); |
| 6182 | fclose(fd); |
| 6183 | return aff; |
| 6184 | } |
| 6185 | |
| 6186 | /* |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 6187 | * Return TRUE when items[0] equals "rulename", there are "mincount" items or |
| 6188 | * a comment is following after item "mincount". |
| 6189 | */ |
| 6190 | static int |
| 6191 | is_aff_rule(items, itemcnt, rulename, mincount) |
| 6192 | char_u **items; |
| 6193 | int itemcnt; |
| 6194 | char *rulename; |
| 6195 | int mincount; |
| 6196 | { |
| 6197 | return (STRCMP(items[0], rulename) == 0 |
| 6198 | && (itemcnt == mincount |
| 6199 | || (itemcnt > mincount && items[mincount][0] == '#'))); |
| 6200 | } |
| 6201 | |
| 6202 | /* |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6203 | * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from |
| 6204 | * ae_flags to ae_comppermit and ae_compforbid. |
| 6205 | */ |
| 6206 | static void |
| 6207 | aff_process_flags(affile, entry) |
| 6208 | afffile_T *affile; |
| 6209 | affentry_T *entry; |
| 6210 | { |
| 6211 | char_u *p; |
| 6212 | char_u *prevp; |
| Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 6213 | unsigned flag; |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6214 | |
| 6215 | if (entry->ae_flags != NULL |
| 6216 | && (affile->af_compforbid != 0 || affile->af_comppermit != 0)) |
| 6217 | { |
| 6218 | for (p = entry->ae_flags; *p != NUL; ) |
| 6219 | { |
| 6220 | prevp = p; |
| 6221 | flag = get_affitem(affile->af_flagtype, &p); |
| 6222 | if (flag == affile->af_comppermit || flag == affile->af_compforbid) |
| 6223 | { |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 6224 | STRMOVE(prevp, p); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6225 | p = prevp; |
| 6226 | if (flag == affile->af_comppermit) |
| 6227 | entry->ae_comppermit = TRUE; |
| 6228 | else |
| 6229 | entry->ae_compforbid = TRUE; |
| 6230 | } |
| 6231 | if (affile->af_flagtype == AFT_NUM && *p == ',') |
| 6232 | ++p; |
| 6233 | } |
| 6234 | if (*entry->ae_flags == NUL) |
| 6235 | entry->ae_flags = NULL; /* nothing left */ |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | /* |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 6240 | * Return TRUE if "s" is the name of an info item in the affix file. |
| 6241 | */ |
| 6242 | static int |
| 6243 | spell_info_item(s) |
| 6244 | char_u *s; |
| 6245 | { |
| 6246 | return STRCMP(s, "NAME") == 0 |
| 6247 | || STRCMP(s, "HOME") == 0 |
| 6248 | || STRCMP(s, "VERSION") == 0 |
| 6249 | || STRCMP(s, "AUTHOR") == 0 |
| 6250 | || STRCMP(s, "EMAIL") == 0 |
| 6251 | || STRCMP(s, "COPYRIGHT") == 0; |
| 6252 | } |
| 6253 | |
| 6254 | /* |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6255 | * Turn an affix flag name into a number, according to the FLAG type. |
| 6256 | * returns zero for failure. |
| 6257 | */ |
| 6258 | static unsigned |
| 6259 | affitem2flag(flagtype, item, fname, lnum) |
| 6260 | int flagtype; |
| 6261 | char_u *item; |
| 6262 | char_u *fname; |
| 6263 | int lnum; |
| 6264 | { |
| 6265 | unsigned res; |
| 6266 | char_u *p = item; |
| 6267 | |
| 6268 | res = get_affitem(flagtype, &p); |
| 6269 | if (res == 0) |
| 6270 | { |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6271 | if (flagtype == AFT_NUM) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6272 | smsg((char_u *)_("Flag is not a number in %s line %d: %s"), |
| 6273 | fname, lnum, item); |
| 6274 | else |
| 6275 | smsg((char_u *)_("Illegal flag in %s line %d: %s"), |
| 6276 | fname, lnum, item); |
| 6277 | } |
| 6278 | if (*p != NUL) |
| 6279 | { |
| 6280 | smsg((char_u *)_(e_affname), fname, lnum, item); |
| 6281 | return 0; |
| 6282 | } |
| 6283 | |
| 6284 | return res; |
| 6285 | } |
| 6286 | |
| 6287 | /* |
| 6288 | * Get one affix name from "*pp" and advance the pointer. |
| 6289 | * Returns zero for an error, still advances the pointer then. |
| 6290 | */ |
| 6291 | static unsigned |
| 6292 | get_affitem(flagtype, pp) |
| 6293 | int flagtype; |
| 6294 | char_u **pp; |
| 6295 | { |
| 6296 | int res; |
| 6297 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6298 | if (flagtype == AFT_NUM) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6299 | { |
| 6300 | if (!VIM_ISDIGIT(**pp)) |
| 6301 | { |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6302 | ++*pp; /* always advance, avoid getting stuck */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6303 | return 0; |
| 6304 | } |
| 6305 | res = getdigits(pp); |
| 6306 | } |
| 6307 | else |
| 6308 | { |
| 6309 | #ifdef FEAT_MBYTE |
| 6310 | res = mb_ptr2char_adv(pp); |
| 6311 | #else |
| 6312 | res = *(*pp)++; |
| 6313 | #endif |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6314 | if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6315 | && res >= 'A' && res <= 'Z')) |
| 6316 | { |
| 6317 | if (**pp == NUL) |
| 6318 | return 0; |
| 6319 | #ifdef FEAT_MBYTE |
| 6320 | res = mb_ptr2char_adv(pp) + (res << 16); |
| 6321 | #else |
| 6322 | res = *(*pp)++ + (res << 16); |
| 6323 | #endif |
| 6324 | } |
| 6325 | } |
| 6326 | return res; |
| 6327 | } |
| 6328 | |
| 6329 | /* |
| 6330 | * Process the "compflags" string used in an affix file and append it to |
| 6331 | * spin->si_compflags. |
| 6332 | * The processing involves changing the affix names to ID numbers, so that |
| 6333 | * they fit in one byte. |
| 6334 | */ |
| 6335 | static void |
| 6336 | process_compflags(spin, aff, compflags) |
| 6337 | spellinfo_T *spin; |
| 6338 | afffile_T *aff; |
| 6339 | char_u *compflags; |
| 6340 | { |
| 6341 | char_u *p; |
| 6342 | char_u *prevp; |
| 6343 | unsigned flag; |
| 6344 | compitem_T *ci; |
| 6345 | int id; |
| 6346 | int len; |
| 6347 | char_u *tp; |
| 6348 | char_u key[AH_KEY_LEN]; |
| 6349 | hashitem_T *hi; |
| 6350 | |
| 6351 | /* Make room for the old and the new compflags, concatenated with a / in |
| 6352 | * between. Processing it makes it shorter, but we don't know by how |
| 6353 | * much, thus allocate the maximum. */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6354 | len = (int)STRLEN(compflags) + 1; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6355 | if (spin->si_compflags != NULL) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6356 | len += (int)STRLEN(spin->si_compflags) + 1; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6357 | p = getroom(spin, len, FALSE); |
| 6358 | if (p == NULL) |
| 6359 | return; |
| 6360 | if (spin->si_compflags != NULL) |
| 6361 | { |
| 6362 | STRCPY(p, spin->si_compflags); |
| 6363 | STRCAT(p, "/"); |
| 6364 | } |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6365 | spin->si_compflags = p; |
| 6366 | tp = p + STRLEN(p); |
| 6367 | |
| 6368 | for (p = compflags; *p != NUL; ) |
| 6369 | { |
| 6370 | if (vim_strchr((char_u *)"/*+[]", *p) != NULL) |
| 6371 | /* Copy non-flag characters directly. */ |
| 6372 | *tp++ = *p++; |
| 6373 | else |
| 6374 | { |
| 6375 | /* First get the flag number, also checks validity. */ |
| 6376 | prevp = p; |
| 6377 | flag = get_affitem(aff->af_flagtype, &p); |
| 6378 | if (flag != 0) |
| 6379 | { |
| 6380 | /* Find the flag in the hashtable. If it was used before, use |
| 6381 | * the existing ID. Otherwise add a new entry. */ |
| 6382 | vim_strncpy(key, prevp, p - prevp); |
| 6383 | hi = hash_find(&aff->af_comp, key); |
| 6384 | if (!HASHITEM_EMPTY(hi)) |
| 6385 | id = HI2CI(hi)->ci_newID; |
| 6386 | else |
| 6387 | { |
| 6388 | ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE); |
| 6389 | if (ci == NULL) |
| 6390 | break; |
| 6391 | STRCPY(ci->ci_key, key); |
| 6392 | ci->ci_flag = flag; |
| 6393 | /* Avoid using a flag ID that has a special meaning in a |
| 6394 | * regexp (also inside []). */ |
| 6395 | do |
| 6396 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 6397 | check_renumber(spin); |
| 6398 | id = spin->si_newcompID--; |
| 6399 | } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6400 | ci->ci_newID = id; |
| 6401 | hash_add(&aff->af_comp, ci->ci_key); |
| 6402 | } |
| 6403 | *tp++ = id; |
| 6404 | } |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6405 | if (aff->af_flagtype == AFT_NUM && *p == ',') |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6406 | ++p; |
| 6407 | } |
| 6408 | } |
| 6409 | |
| 6410 | *tp = NUL; |
| 6411 | } |
| 6412 | |
| 6413 | /* |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 6414 | * Check that the new IDs for postponed affixes and compounding don't overrun |
| 6415 | * each other. We have almost 255 available, but start at 0-127 to avoid |
| 6416 | * using two bytes for utf-8. When the 0-127 range is used up go to 128-255. |
| 6417 | * When that is used up an error message is given. |
| 6418 | */ |
| 6419 | static void |
| 6420 | check_renumber(spin) |
| 6421 | spellinfo_T *spin; |
| 6422 | { |
| 6423 | if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128) |
| 6424 | { |
| 6425 | spin->si_newprefID = 127; |
| 6426 | spin->si_newcompID = 255; |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | /* |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6431 | * Return TRUE if flag "flag" appears in affix list "afflist". |
| 6432 | */ |
| 6433 | static int |
| 6434 | flag_in_afflist(flagtype, afflist, flag) |
| 6435 | int flagtype; |
| 6436 | char_u *afflist; |
| 6437 | unsigned flag; |
| 6438 | { |
| 6439 | char_u *p; |
| 6440 | unsigned n; |
| 6441 | |
| 6442 | switch (flagtype) |
| 6443 | { |
| 6444 | case AFT_CHAR: |
| 6445 | return vim_strchr(afflist, flag) != NULL; |
| 6446 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6447 | case AFT_CAPLONG: |
| 6448 | case AFT_LONG: |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6449 | for (p = afflist; *p != NUL; ) |
| 6450 | { |
| 6451 | #ifdef FEAT_MBYTE |
| 6452 | n = mb_ptr2char_adv(&p); |
| 6453 | #else |
| 6454 | n = *p++; |
| 6455 | #endif |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6456 | if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z')) |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6457 | && *p != NUL) |
| 6458 | #ifdef FEAT_MBYTE |
| 6459 | n = mb_ptr2char_adv(&p) + (n << 16); |
| 6460 | #else |
| 6461 | n = *p++ + (n << 16); |
| 6462 | #endif |
| 6463 | if (n == flag) |
| 6464 | return TRUE; |
| 6465 | } |
| 6466 | break; |
| 6467 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6468 | case AFT_NUM: |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6469 | for (p = afflist; *p != NUL; ) |
| 6470 | { |
| 6471 | n = getdigits(&p); |
| 6472 | if (n == flag) |
| 6473 | return TRUE; |
| 6474 | if (*p != NUL) /* skip over comma */ |
| 6475 | ++p; |
| 6476 | } |
| 6477 | break; |
| 6478 | } |
| 6479 | return FALSE; |
| 6480 | } |
| 6481 | |
| 6482 | /* |
| 6483 | * Give a warning when "spinval" and "affval" numbers are set and not the same. |
| 6484 | */ |
| 6485 | static void |
| 6486 | aff_check_number(spinval, affval, name) |
| 6487 | int spinval; |
| 6488 | int affval; |
| 6489 | char *name; |
| 6490 | { |
| 6491 | if (spinval != 0 && spinval != affval) |
| 6492 | smsg((char_u *)_("%s value differs from what is used in another .aff file"), name); |
| 6493 | } |
| 6494 | |
| 6495 | /* |
| 6496 | * Give a warning when "spinval" and "affval" strings are set and not the same. |
| 6497 | */ |
| 6498 | static void |
| 6499 | aff_check_string(spinval, affval, name) |
| 6500 | char_u *spinval; |
| 6501 | char_u *affval; |
| 6502 | char *name; |
| 6503 | { |
| 6504 | if (spinval != NULL && STRCMP(spinval, affval) != 0) |
| 6505 | smsg((char_u *)_("%s value differs from what is used in another .aff file"), name); |
| 6506 | } |
| 6507 | |
| 6508 | /* |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6509 | * Return TRUE if strings "s1" and "s2" are equal. Also consider both being |
| 6510 | * NULL as equal. |
| 6511 | */ |
| 6512 | static int |
| 6513 | str_equal(s1, s2) |
| 6514 | char_u *s1; |
| 6515 | char_u *s2; |
| 6516 | { |
| 6517 | if (s1 == NULL || s2 == NULL) |
| 6518 | return s1 == s2; |
| 6519 | return STRCMP(s1, s2) == 0; |
| 6520 | } |
| 6521 | |
| 6522 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6523 | * Add a from-to item to "gap". Used for REP and SAL items. |
| 6524 | * They are stored case-folded. |
| 6525 | */ |
| 6526 | static void |
| 6527 | add_fromto(spin, gap, from, to) |
| 6528 | spellinfo_T *spin; |
| 6529 | garray_T *gap; |
| 6530 | char_u *from; |
| 6531 | char_u *to; |
| 6532 | { |
| 6533 | fromto_T *ftp; |
| 6534 | char_u word[MAXWLEN]; |
| 6535 | |
| 6536 | if (ga_grow(gap, 1) == OK) |
| 6537 | { |
| 6538 | ftp = ((fromto_T *)gap->ga_data) + gap->ga_len; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6539 | (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6540 | ftp->ft_from = getroom_save(spin, word); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6541 | (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6542 | ftp->ft_to = getroom_save(spin, word); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 6543 | ++gap->ga_len; |
| 6544 | } |
| 6545 | } |
| 6546 | |
| 6547 | /* |
| 6548 | * Convert a boolean argument in a SAL line to TRUE or FALSE; |
| 6549 | */ |
| 6550 | static int |
| 6551 | sal_to_bool(s) |
| 6552 | char_u *s; |
| 6553 | { |
| 6554 | return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0; |
| 6555 | } |
| 6556 | |
| 6557 | /* |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 6558 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 6559 | * When "s" is NULL FALSE is returned. |
| 6560 | */ |
| 6561 | static int |
| 6562 | has_non_ascii(s) |
| 6563 | char_u *s; |
| 6564 | { |
| 6565 | char_u *p; |
| 6566 | |
| 6567 | if (s != NULL) |
| 6568 | for (p = s; *p != NUL; ++p) |
| 6569 | if (*p >= 128) |
| 6570 | return TRUE; |
| 6571 | return FALSE; |
| 6572 | } |
| 6573 | |
| 6574 | /* |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6575 | * Free the structure filled by spell_read_aff(). |
| 6576 | */ |
| 6577 | static void |
| 6578 | spell_free_aff(aff) |
| 6579 | afffile_T *aff; |
| 6580 | { |
| 6581 | hashtab_T *ht; |
| 6582 | hashitem_T *hi; |
| 6583 | int todo; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6584 | affheader_T *ah; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6585 | affentry_T *ae; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6586 | |
| 6587 | vim_free(aff->af_enc); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6588 | |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6589 | /* All this trouble to free the "ae_prog" items... */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6590 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 6591 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6592 | todo = (int)ht->ht_used; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6593 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 6594 | { |
| 6595 | if (!HASHITEM_EMPTY(hi)) |
| 6596 | { |
| 6597 | --todo; |
| 6598 | ah = HI2AH(hi); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6599 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 6600 | vim_free(ae->ae_prog); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6601 | } |
| 6602 | } |
| 6603 | if (ht == &aff->af_suff) |
| 6604 | break; |
| 6605 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6606 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6607 | hash_clear(&aff->af_pref); |
| 6608 | hash_clear(&aff->af_suff); |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6609 | hash_clear(&aff->af_comp); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6610 | } |
| 6611 | |
| 6612 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6613 | * Read dictionary file "fname". |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6614 | * Returns OK or FAIL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6615 | */ |
| 6616 | static int |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6617 | spell_read_dic(spin, fname, affile) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6618 | spellinfo_T *spin; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6619 | char_u *fname; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6620 | afffile_T *affile; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6621 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6622 | hashtab_T ht; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6623 | char_u line[MAXLINELEN]; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6624 | char_u *p; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6625 | char_u *afflist; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6626 | char_u store_afflist[MAXWLEN]; |
| 6627 | int pfxlen; |
| 6628 | int need_affix; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6629 | char_u *dw; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6630 | char_u *pc; |
| 6631 | char_u *w; |
| 6632 | int l; |
| 6633 | hash_T hash; |
| 6634 | hashitem_T *hi; |
| 6635 | FILE *fd; |
| 6636 | int lnum = 1; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6637 | int non_ascii = 0; |
| 6638 | int retval = OK; |
| 6639 | char_u message[MAXLINELEN + MAXWLEN]; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6640 | int flags; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6641 | int duplicate = 0; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6642 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6643 | /* |
| 6644 | * Open the file. |
| 6645 | */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 6646 | fd = mch_fopen((char *)fname, "r"); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6647 | if (fd == NULL) |
| 6648 | { |
| 6649 | EMSG2(_(e_notopen), fname); |
| 6650 | return FAIL; |
| 6651 | } |
| 6652 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6653 | /* The hashtable is only used to detect duplicated words. */ |
| 6654 | hash_init(&ht); |
| 6655 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 6656 | vim_snprintf((char *)IObuff, IOSIZE, |
| 6657 | _("Reading dictionary file %s ..."), fname); |
| 6658 | spell_message(spin, IObuff); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6659 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6660 | /* start with a message for the first line */ |
| 6661 | spin->si_msg_count = 999999; |
| 6662 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6663 | /* Read and ignore the first line: word count. */ |
| 6664 | (void)vim_fgets(line, MAXLINELEN, fd); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 6665 | if (!vim_isdigit(*skipwhite(line))) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6666 | EMSG2(_("E760: No word count in %s"), fname); |
| 6667 | |
| 6668 | /* |
| 6669 | * Read all the lines in the file one by one. |
| 6670 | * The words are converted to 'encoding' here, before being added to |
| 6671 | * the hashtable. |
| 6672 | */ |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6673 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6674 | { |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6675 | line_breakcheck(); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6676 | ++lnum; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 6677 | if (line[0] == '#' || line[0] == '/') |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 6678 | continue; /* comment line */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6679 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6680 | /* Remove CR, LF and white space from the end. White space halfway |
| 6681 | * the word is kept to allow e.g., "et al.". */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6682 | l = (int)STRLEN(line); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6683 | while (l > 0 && line[l - 1] <= ' ') |
| 6684 | --l; |
| 6685 | if (l == 0) |
| 6686 | continue; /* empty line */ |
| 6687 | line[l] = NUL; |
| 6688 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 6689 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6690 | /* Convert from "SET" to 'encoding' when needed. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6691 | if (spin->si_conv.vc_type != CONV_NONE) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6692 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6693 | pc = string_convert(&spin->si_conv, line, NULL); |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6694 | if (pc == NULL) |
| 6695 | { |
| 6696 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 6697 | fname, lnum, line); |
| 6698 | continue; |
| 6699 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6700 | w = pc; |
| 6701 | } |
| 6702 | else |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 6703 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6704 | { |
| 6705 | pc = NULL; |
| 6706 | w = line; |
| 6707 | } |
| 6708 | |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6709 | /* Truncate the word at the "/", set "afflist" to what follows. |
| 6710 | * Replace "\/" by "/" and "\\" by "\". */ |
| 6711 | afflist = NULL; |
| 6712 | for (p = w; *p != NUL; mb_ptr_adv(p)) |
| 6713 | { |
| 6714 | if (*p == '\\' && (p[1] == '\\' || p[1] == '/')) |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 6715 | STRMOVE(p, p + 1); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6716 | else if (*p == '/') |
| 6717 | { |
| 6718 | *p = NUL; |
| 6719 | afflist = p + 1; |
| 6720 | break; |
| 6721 | } |
| 6722 | } |
| 6723 | |
| 6724 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 6725 | if (spin->si_ascii && has_non_ascii(w)) |
| 6726 | { |
| 6727 | ++non_ascii; |
| Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 6728 | vim_free(pc); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6729 | continue; |
| 6730 | } |
| 6731 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6732 | /* This takes time, print a message every 10000 words. */ |
| 6733 | if (spin->si_verbose && spin->si_msg_count > 10000) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6734 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6735 | spin->si_msg_count = 0; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6736 | vim_snprintf((char *)message, sizeof(message), |
| 6737 | _("line %6d, word %6d - %s"), |
| 6738 | lnum, spin->si_foldwcount + spin->si_keepwcount, w); |
| 6739 | msg_start(); |
| 6740 | msg_puts_long_attr(message, 0); |
| 6741 | msg_clr_eos(); |
| 6742 | msg_didout = FALSE; |
| 6743 | msg_col = 0; |
| 6744 | out_flush(); |
| 6745 | } |
| 6746 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6747 | /* Store the word in the hashtable to be able to find duplicates. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6748 | dw = (char_u *)getroom_save(spin, w); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6749 | if (dw == NULL) |
| Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 6750 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6751 | retval = FAIL; |
| Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 6752 | vim_free(pc); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6753 | break; |
| Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 6754 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6755 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6756 | hash = hash_hash(dw); |
| 6757 | hi = hash_lookup(&ht, dw, hash); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6758 | if (!HASHITEM_EMPTY(hi)) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6759 | { |
| 6760 | if (p_verbose > 0) |
| 6761 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 6762 | fname, lnum, dw); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6763 | else if (duplicate == 0) |
| 6764 | smsg((char_u *)_("First duplicate word in %s line %d: %s"), |
| 6765 | fname, lnum, dw); |
| 6766 | ++duplicate; |
| 6767 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6768 | else |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6769 | hash_add_item(&ht, hi, dw, hash); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6770 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6771 | flags = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6772 | store_afflist[0] = NUL; |
| 6773 | pfxlen = 0; |
| 6774 | need_affix = FALSE; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6775 | if (afflist != NULL) |
| 6776 | { |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6777 | /* Extract flags from the affix list. */ |
| 6778 | flags |= get_affix_flags(affile, afflist); |
| 6779 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6780 | if (affile->af_needaffix != 0 && flag_in_afflist( |
| 6781 | affile->af_flagtype, afflist, affile->af_needaffix)) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6782 | need_affix = TRUE; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6783 | |
| 6784 | if (affile->af_pfxpostpone) |
| 6785 | /* Need to store the list of prefix IDs with the word. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6786 | pfxlen = get_pfxlist(affile, afflist, store_afflist); |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 6787 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6788 | if (spin->si_compflags != NULL) |
| 6789 | /* Need to store the list of compound flags with the word. |
| 6790 | * Concatenate them to the list of prefix IDs. */ |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6791 | get_compflags(affile, afflist, store_afflist + pfxlen); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6794 | /* Add the word to the word tree(s). */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6795 | if (store_word(spin, dw, flags, spin->si_region, |
| 6796 | store_afflist, need_affix) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6797 | retval = FAIL; |
| 6798 | |
| 6799 | if (afflist != NULL) |
| 6800 | { |
| 6801 | /* Find all matching suffixes and add the resulting words. |
| 6802 | * Additionally do matching prefixes that combine. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6803 | if (store_aff_word(spin, dw, afflist, affile, |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6804 | &affile->af_suff, &affile->af_pref, |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6805 | CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6806 | retval = FAIL; |
| 6807 | |
| 6808 | /* Find all matching prefixes and add the resulting words. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6809 | if (store_aff_word(spin, dw, afflist, affile, |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6810 | &affile->af_pref, NULL, |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6811 | CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6812 | retval = FAIL; |
| 6813 | } |
| Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 6814 | |
| 6815 | vim_free(pc); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6816 | } |
| 6817 | |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6818 | if (duplicate > 0) |
| 6819 | smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6820 | if (spin->si_ascii && non_ascii > 0) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6821 | smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"), |
| 6822 | non_ascii, fname); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6823 | hash_clear(&ht); |
| 6824 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6825 | fclose(fd); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6826 | return retval; |
| 6827 | } |
| 6828 | |
| 6829 | /* |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6830 | * Check for affix flags in "afflist" that are turned into word flags. |
| 6831 | * Return WF_ flags. |
| 6832 | */ |
| 6833 | static int |
| 6834 | get_affix_flags(affile, afflist) |
| 6835 | afffile_T *affile; |
| 6836 | char_u *afflist; |
| 6837 | { |
| 6838 | int flags = 0; |
| 6839 | |
| 6840 | if (affile->af_keepcase != 0 && flag_in_afflist( |
| 6841 | affile->af_flagtype, afflist, affile->af_keepcase)) |
| 6842 | flags |= WF_KEEPCAP | WF_FIXCAP; |
| 6843 | if (affile->af_rare != 0 && flag_in_afflist( |
| 6844 | affile->af_flagtype, afflist, affile->af_rare)) |
| 6845 | flags |= WF_RARE; |
| 6846 | if (affile->af_bad != 0 && flag_in_afflist( |
| 6847 | affile->af_flagtype, afflist, affile->af_bad)) |
| 6848 | flags |= WF_BANNED; |
| 6849 | if (affile->af_needcomp != 0 && flag_in_afflist( |
| 6850 | affile->af_flagtype, afflist, affile->af_needcomp)) |
| 6851 | flags |= WF_NEEDCOMP; |
| 6852 | if (affile->af_comproot != 0 && flag_in_afflist( |
| 6853 | affile->af_flagtype, afflist, affile->af_comproot)) |
| 6854 | flags |= WF_COMPROOT; |
| 6855 | if (affile->af_nosuggest != 0 && flag_in_afflist( |
| 6856 | affile->af_flagtype, afflist, affile->af_nosuggest)) |
| 6857 | flags |= WF_NOSUGGEST; |
| 6858 | return flags; |
| 6859 | } |
| 6860 | |
| 6861 | /* |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6862 | * Get the list of prefix IDs from the affix list "afflist". |
| 6863 | * Used for PFXPOSTPONE. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6864 | * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL |
| 6865 | * and return the number of affixes. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6866 | */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6867 | static int |
| 6868 | get_pfxlist(affile, afflist, store_afflist) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6869 | afffile_T *affile; |
| 6870 | char_u *afflist; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6871 | char_u *store_afflist; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6872 | { |
| 6873 | char_u *p; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6874 | char_u *prevp; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6875 | int cnt = 0; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6876 | int id; |
| 6877 | char_u key[AH_KEY_LEN]; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6878 | hashitem_T *hi; |
| 6879 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6880 | for (p = afflist; *p != NUL; ) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6881 | { |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6882 | prevp = p; |
| 6883 | if (get_affitem(affile->af_flagtype, &p) != 0) |
| 6884 | { |
| 6885 | /* A flag is a postponed prefix flag if it appears in "af_pref" |
| 6886 | * and it's ID is not zero. */ |
| 6887 | vim_strncpy(key, prevp, p - prevp); |
| 6888 | hi = hash_find(&affile->af_pref, key); |
| 6889 | if (!HASHITEM_EMPTY(hi)) |
| 6890 | { |
| 6891 | id = HI2AH(hi)->ah_newID; |
| 6892 | if (id != 0) |
| 6893 | store_afflist[cnt++] = id; |
| 6894 | } |
| 6895 | } |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6896 | if (affile->af_flagtype == AFT_NUM && *p == ',') |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6897 | ++p; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6900 | store_afflist[cnt] = NUL; |
| 6901 | return cnt; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | /* |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6905 | * Get the list of compound IDs from the affix list "afflist" that are used |
| 6906 | * for compound words. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6907 | * Puts the flags in "store_afflist[]". |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6908 | */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6909 | static void |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6910 | get_compflags(affile, afflist, store_afflist) |
| 6911 | afffile_T *affile; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6912 | char_u *afflist; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6913 | char_u *store_afflist; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6914 | { |
| 6915 | char_u *p; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6916 | char_u *prevp; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6917 | int cnt = 0; |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6918 | char_u key[AH_KEY_LEN]; |
| 6919 | hashitem_T *hi; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6920 | |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6921 | for (p = afflist; *p != NUL; ) |
| 6922 | { |
| 6923 | prevp = p; |
| 6924 | if (get_affitem(affile->af_flagtype, &p) != 0) |
| 6925 | { |
| 6926 | /* A flag is a compound flag if it appears in "af_comp". */ |
| 6927 | vim_strncpy(key, prevp, p - prevp); |
| 6928 | hi = hash_find(&affile->af_comp, key); |
| 6929 | if (!HASHITEM_EMPTY(hi)) |
| 6930 | store_afflist[cnt++] = HI2CI(hi)->ci_newID; |
| 6931 | } |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 6932 | if (affile->af_flagtype == AFT_NUM && *p == ',') |
| Bram Moolenaar | 6de6853 | 2005-08-24 22:08:48 +0000 | [diff] [blame] | 6933 | ++p; |
| 6934 | } |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6935 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6936 | store_afflist[cnt] = NUL; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 6937 | } |
| 6938 | |
| 6939 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6940 | * Apply affixes to a word and store the resulting words. |
| 6941 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 6942 | * prefixes or suffixes. |
| 6943 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 6944 | * the resulting words for combining affixes. |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6945 | * |
| 6946 | * Returns FAIL when out of memory. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6947 | */ |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6948 | static int |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6949 | store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags, |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6950 | pfxlist, pfxlen) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6951 | spellinfo_T *spin; /* spell info */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6952 | char_u *word; /* basic word start */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6953 | char_u *afflist; /* list of names of supported affixes */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6954 | afffile_T *affile; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6955 | hashtab_T *ht; |
| 6956 | hashtab_T *xht; |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6957 | int condit; /* CONDIT_SUF et al. */ |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 6958 | int flags; /* flags for the word */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 6959 | char_u *pfxlist; /* list of prefix IDs */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6960 | int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest |
| 6961 | * is compound flags */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6962 | { |
| 6963 | int todo; |
| 6964 | hashitem_T *hi; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6965 | affheader_T *ah; |
| 6966 | affentry_T *ae; |
| 6967 | regmatch_T regmatch; |
| 6968 | char_u newword[MAXWLEN]; |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 6969 | int retval = OK; |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6970 | int i, j; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6971 | char_u *p; |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 6972 | int use_flags; |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 6973 | char_u *use_pfxlist; |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6974 | int use_pfxlen; |
| 6975 | int need_affix; |
| 6976 | char_u store_afflist[MAXWLEN]; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6977 | char_u pfx_pfxlist[MAXWLEN]; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 6978 | size_t wordlen = STRLEN(word); |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6979 | int use_condit; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6980 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6981 | todo = (int)ht->ht_used; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6982 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6983 | { |
| 6984 | if (!HASHITEM_EMPTY(hi)) |
| 6985 | { |
| 6986 | --todo; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6987 | ah = HI2AH(hi); |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 6988 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6989 | /* Check that the affix combines, if required, and that the word |
| 6990 | * supports this affix. */ |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6991 | if (((condit & CONDIT_COMB) == 0 || ah->ah_combine) |
| 6992 | && flag_in_afflist(affile->af_flagtype, afflist, |
| 6993 | ah->ah_flag)) |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 6994 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6995 | /* Loop over all affix entries with this name. */ |
| 6996 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6997 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 6998 | /* Check the condition. It's not logical to match case |
| 6999 | * here, but it is required for compatibility with |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7000 | * Myspell. |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7001 | * Another requirement from Myspell is that the chop |
| 7002 | * string is shorter than the word itself. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7003 | * For prefixes, when "PFXPOSTPONE" was used, only do |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7004 | * prefixes with a chop string and/or flags. |
| 7005 | * When a previously added affix had CIRCUMFIX this one |
| 7006 | * must have it too, if it had not then this one must not |
| 7007 | * have one either. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7008 | regmatch.regprog = ae->ae_prog; |
| 7009 | regmatch.rm_ic = FALSE; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7010 | if ((xht != NULL || !affile->af_pfxpostpone |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 7011 | || ae->ae_chop != NULL |
| 7012 | || ae->ae_flags != NULL) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7013 | && (ae->ae_chop == NULL |
| 7014 | || STRLEN(ae->ae_chop) < wordlen) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7015 | && (ae->ae_prog == NULL |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7016 | || vim_regexec(®match, word, (colnr_T)0)) |
| 7017 | && (((condit & CONDIT_CFIX) == 0) |
| 7018 | == ((condit & CONDIT_AFF) == 0 |
| 7019 | || ae->ae_flags == NULL |
| 7020 | || !flag_in_afflist(affile->af_flagtype, |
| 7021 | ae->ae_flags, affile->af_circumfix)))) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7022 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7023 | /* Match. Remove the chop and add the affix. */ |
| 7024 | if (xht == NULL) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7025 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7026 | /* prefix: chop/add at the start of the word */ |
| 7027 | if (ae->ae_add == NULL) |
| 7028 | *newword = NUL; |
| 7029 | else |
| 7030 | STRCPY(newword, ae->ae_add); |
| 7031 | p = word; |
| 7032 | if (ae->ae_chop != NULL) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7033 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7034 | /* Skip chop string. */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7035 | #ifdef FEAT_MBYTE |
| 7036 | if (has_mbyte) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7037 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7038 | i = mb_charlen(ae->ae_chop); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7039 | for ( ; i > 0; --i) |
| 7040 | mb_ptr_adv(p); |
| 7041 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7042 | else |
| 7043 | #endif |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 7044 | p += STRLEN(ae->ae_chop); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7045 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7046 | STRCAT(newword, p); |
| 7047 | } |
| 7048 | else |
| 7049 | { |
| 7050 | /* suffix: chop/add at the end of the word */ |
| 7051 | STRCPY(newword, word); |
| 7052 | if (ae->ae_chop != NULL) |
| 7053 | { |
| 7054 | /* Remove chop string. */ |
| 7055 | p = newword + STRLEN(newword); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7056 | i = (int)MB_CHARLEN(ae->ae_chop); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7057 | for ( ; i > 0; --i) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7058 | mb_ptr_back(newword, p); |
| 7059 | *p = NUL; |
| 7060 | } |
| 7061 | if (ae->ae_add != NULL) |
| 7062 | STRCAT(newword, ae->ae_add); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7063 | } |
| 7064 | |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7065 | use_flags = flags; |
| 7066 | use_pfxlist = pfxlist; |
| 7067 | use_pfxlen = pfxlen; |
| 7068 | need_affix = FALSE; |
| 7069 | use_condit = condit | CONDIT_COMB | CONDIT_AFF; |
| 7070 | if (ae->ae_flags != NULL) |
| 7071 | { |
| 7072 | /* Extract flags from the affix list. */ |
| 7073 | use_flags |= get_affix_flags(affile, ae->ae_flags); |
| 7074 | |
| 7075 | if (affile->af_needaffix != 0 && flag_in_afflist( |
| 7076 | affile->af_flagtype, ae->ae_flags, |
| 7077 | affile->af_needaffix)) |
| 7078 | need_affix = TRUE; |
| 7079 | |
| 7080 | /* When there is a CIRCUMFIX flag the other affix |
| 7081 | * must also have it and we don't add the word |
| 7082 | * with one affix. */ |
| 7083 | if (affile->af_circumfix != 0 && flag_in_afflist( |
| 7084 | affile->af_flagtype, ae->ae_flags, |
| 7085 | affile->af_circumfix)) |
| 7086 | { |
| 7087 | use_condit |= CONDIT_CFIX; |
| 7088 | if ((condit & CONDIT_CFIX) == 0) |
| 7089 | need_affix = TRUE; |
| 7090 | } |
| 7091 | |
| 7092 | if (affile->af_pfxpostpone |
| 7093 | || spin->si_compflags != NULL) |
| 7094 | { |
| 7095 | if (affile->af_pfxpostpone) |
| 7096 | /* Get prefix IDS from the affix list. */ |
| 7097 | use_pfxlen = get_pfxlist(affile, |
| 7098 | ae->ae_flags, store_afflist); |
| 7099 | else |
| 7100 | use_pfxlen = 0; |
| 7101 | use_pfxlist = store_afflist; |
| 7102 | |
| 7103 | /* Combine the prefix IDs. Avoid adding the |
| 7104 | * same ID twice. */ |
| 7105 | for (i = 0; i < pfxlen; ++i) |
| 7106 | { |
| 7107 | for (j = 0; j < use_pfxlen; ++j) |
| 7108 | if (pfxlist[i] == use_pfxlist[j]) |
| 7109 | break; |
| 7110 | if (j == use_pfxlen) |
| 7111 | use_pfxlist[use_pfxlen++] = pfxlist[i]; |
| 7112 | } |
| 7113 | |
| 7114 | if (spin->si_compflags != NULL) |
| 7115 | /* Get compound IDS from the affix list. */ |
| 7116 | get_compflags(affile, ae->ae_flags, |
| 7117 | use_pfxlist + use_pfxlen); |
| 7118 | |
| 7119 | /* Combine the list of compound flags. |
| 7120 | * Concatenate them to the prefix IDs list. |
| 7121 | * Avoid adding the same ID twice. */ |
| 7122 | for (i = pfxlen; pfxlist[i] != NUL; ++i) |
| 7123 | { |
| 7124 | for (j = use_pfxlen; |
| 7125 | use_pfxlist[j] != NUL; ++j) |
| 7126 | if (pfxlist[i] == use_pfxlist[j]) |
| 7127 | break; |
| 7128 | if (use_pfxlist[j] == NUL) |
| 7129 | { |
| 7130 | use_pfxlist[j++] = pfxlist[i]; |
| 7131 | use_pfxlist[j] = NUL; |
| 7132 | } |
| 7133 | } |
| 7134 | } |
| 7135 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7136 | |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 7137 | /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 7138 | * use the compound flags. */ |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 7139 | if (use_pfxlist != NULL && ae->ae_compforbid) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7140 | { |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7141 | vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7142 | use_pfxlist = pfx_pfxlist; |
| 7143 | } |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7144 | |
| 7145 | /* When there are postponed prefixes... */ |
| Bram Moolenaar | 551f84f | 2005-07-06 22:29:20 +0000 | [diff] [blame] | 7146 | if (spin->si_prefroot != NULL |
| 7147 | && spin->si_prefroot->wn_sibling != NULL) |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7148 | { |
| 7149 | /* ... add a flag to indicate an affix was used. */ |
| 7150 | use_flags |= WF_HAS_AFF; |
| 7151 | |
| 7152 | /* ... don't use a prefix list if combining |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7153 | * affixes is not allowed. But do use the |
| 7154 | * compound flags after them. */ |
| Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 7155 | if (!ah->ah_combine && use_pfxlist != NULL) |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7156 | use_pfxlist += use_pfxlen; |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7157 | } |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7158 | |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 7159 | /* When compounding is supported and there is no |
| 7160 | * "COMPOUNDPERMITFLAG" then forbid compounding on the |
| 7161 | * side where the affix is applied. */ |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 7162 | if (spin->si_compflags != NULL && !ae->ae_comppermit) |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 7163 | { |
| 7164 | if (xht != NULL) |
| 7165 | use_flags |= WF_NOCOMPAFT; |
| 7166 | else |
| 7167 | use_flags |= WF_NOCOMPBEF; |
| 7168 | } |
| 7169 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7170 | /* Store the modified word. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7171 | if (store_word(spin, newword, use_flags, |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7172 | spin->si_region, use_pfxlist, |
| 7173 | need_affix) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7174 | retval = FAIL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7175 | |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7176 | /* When added a prefix or a first suffix and the affix |
| 7177 | * has flags may add a(nother) suffix. RECURSIVE! */ |
| 7178 | if ((condit & CONDIT_SUF) && ae->ae_flags != NULL) |
| 7179 | if (store_aff_word(spin, newword, ae->ae_flags, |
| 7180 | affile, &affile->af_suff, xht, |
| 7181 | use_condit & (xht == NULL |
| 7182 | ? ~0 : ~CONDIT_SUF), |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7183 | use_flags, use_pfxlist, pfxlen) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7184 | retval = FAIL; |
| Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 7185 | |
| 7186 | /* When added a suffix and combining is allowed also |
| 7187 | * try adding a prefix additionally. Both for the |
| 7188 | * word flags and for the affix flags. RECURSIVE! */ |
| 7189 | if (xht != NULL && ah->ah_combine) |
| 7190 | { |
| 7191 | if (store_aff_word(spin, newword, |
| 7192 | afflist, affile, |
| 7193 | xht, NULL, use_condit, |
| 7194 | use_flags, use_pfxlist, |
| 7195 | pfxlen) == FAIL |
| 7196 | || (ae->ae_flags != NULL |
| 7197 | && store_aff_word(spin, newword, |
| 7198 | ae->ae_flags, affile, |
| 7199 | xht, NULL, use_condit, |
| 7200 | use_flags, use_pfxlist, |
| 7201 | pfxlen) == FAIL)) |
| 7202 | retval = FAIL; |
| 7203 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7204 | } |
| 7205 | } |
| 7206 | } |
| 7207 | } |
| 7208 | } |
| 7209 | |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 7210 | return retval; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7211 | } |
| 7212 | |
| 7213 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7214 | * Read a file with a list of words. |
| 7215 | */ |
| 7216 | static int |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7217 | spell_read_wordfile(spin, fname) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7218 | spellinfo_T *spin; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7219 | char_u *fname; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7220 | { |
| 7221 | FILE *fd; |
| 7222 | long lnum = 0; |
| 7223 | char_u rline[MAXLINELEN]; |
| 7224 | char_u *line; |
| 7225 | char_u *pc = NULL; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7226 | char_u *p; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7227 | int l; |
| 7228 | int retval = OK; |
| 7229 | int did_word = FALSE; |
| 7230 | int non_ascii = 0; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7231 | int flags; |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7232 | int regionmask; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7233 | |
| 7234 | /* |
| 7235 | * Open the file. |
| 7236 | */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7237 | fd = mch_fopen((char *)fname, "r"); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7238 | if (fd == NULL) |
| 7239 | { |
| 7240 | EMSG2(_(e_notopen), fname); |
| 7241 | return FAIL; |
| 7242 | } |
| 7243 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7244 | vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname); |
| 7245 | spell_message(spin, IObuff); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7246 | |
| 7247 | /* |
| 7248 | * Read all the lines in the file one by one. |
| 7249 | */ |
| 7250 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 7251 | { |
| 7252 | line_breakcheck(); |
| 7253 | ++lnum; |
| 7254 | |
| 7255 | /* Skip comment lines. */ |
| 7256 | if (*rline == '#') |
| 7257 | continue; |
| 7258 | |
| 7259 | /* Remove CR, LF and white space from the end. */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7260 | l = (int)STRLEN(rline); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7261 | while (l > 0 && rline[l - 1] <= ' ') |
| 7262 | --l; |
| 7263 | if (l == 0) |
| 7264 | continue; /* empty or blank line */ |
| 7265 | rline[l] = NUL; |
| 7266 | |
| Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 7267 | /* Convert from "/encoding={encoding}" to 'encoding' when needed. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7268 | vim_free(pc); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7269 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7270 | if (spin->si_conv.vc_type != CONV_NONE) |
| 7271 | { |
| 7272 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 7273 | if (pc == NULL) |
| 7274 | { |
| 7275 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 7276 | fname, lnum, rline); |
| 7277 | continue; |
| 7278 | } |
| 7279 | line = pc; |
| 7280 | } |
| 7281 | else |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7282 | #endif |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7283 | { |
| 7284 | pc = NULL; |
| 7285 | line = rline; |
| 7286 | } |
| 7287 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7288 | if (*line == '/') |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7289 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7290 | ++line; |
| 7291 | if (STRNCMP(line, "encoding=", 9) == 0) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7292 | { |
| 7293 | if (spin->si_conv.vc_type != CONV_NONE) |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7294 | smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"), |
| 7295 | fname, lnum, line - 1); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7296 | else if (did_word) |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7297 | smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"), |
| 7298 | fname, lnum, line - 1); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7299 | else |
| 7300 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7301 | #ifdef FEAT_MBYTE |
| 7302 | char_u *enc; |
| 7303 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7304 | /* Setup for conversion to 'encoding'. */ |
| Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 7305 | line += 9; |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7306 | enc = enc_canonize(line); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7307 | if (enc != NULL && !spin->si_ascii |
| 7308 | && convert_setup(&spin->si_conv, enc, |
| 7309 | p_enc) == FAIL) |
| 7310 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7311 | fname, line, p_enc); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7312 | vim_free(enc); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 7313 | spin->si_conv.vc_fail = TRUE; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7314 | #else |
| 7315 | smsg((char_u *)_("Conversion in %s not supported"), fname); |
| 7316 | #endif |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7317 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7318 | continue; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7319 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7320 | |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7321 | if (STRNCMP(line, "regions=", 8) == 0) |
| 7322 | { |
| 7323 | if (spin->si_region_count > 1) |
| 7324 | smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"), |
| 7325 | fname, lnum, line); |
| 7326 | else |
| 7327 | { |
| 7328 | line += 8; |
| 7329 | if (STRLEN(line) > 16) |
| 7330 | smsg((char_u *)_("Too many regions in %s line %d: %s"), |
| 7331 | fname, lnum, line); |
| 7332 | else |
| 7333 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7334 | spin->si_region_count = (int)STRLEN(line) / 2; |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7335 | STRCPY(spin->si_region_name, line); |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 7336 | |
| 7337 | /* Adjust the mask for a word valid in all regions. */ |
| 7338 | spin->si_region = (1 << spin->si_region_count) - 1; |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7339 | } |
| 7340 | } |
| 7341 | continue; |
| 7342 | } |
| 7343 | |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7344 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
| 7345 | fname, lnum, line - 1); |
| 7346 | continue; |
| 7347 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7348 | |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7349 | flags = 0; |
| 7350 | regionmask = spin->si_region; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7351 | |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7352 | /* Check for flags and region after a slash. */ |
| 7353 | p = vim_strchr(line, '/'); |
| 7354 | if (p != NULL) |
| 7355 | { |
| 7356 | *p++ = NUL; |
| 7357 | while (*p != NUL) |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7358 | { |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7359 | if (*p == '=') /* keep-case word */ |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 7360 | flags |= WF_KEEPCAP | WF_FIXCAP; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7361 | else if (*p == '!') /* Bad, bad, wicked word. */ |
| 7362 | flags |= WF_BANNED; |
| 7363 | else if (*p == '?') /* Rare word. */ |
| 7364 | flags |= WF_RARE; |
| 7365 | else if (VIM_ISDIGIT(*p)) /* region number(s) */ |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7366 | { |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7367 | if ((flags & WF_REGION) == 0) /* first one */ |
| 7368 | regionmask = 0; |
| 7369 | flags |= WF_REGION; |
| 7370 | |
| 7371 | l = *p - '0'; |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7372 | if (l > spin->si_region_count) |
| 7373 | { |
| 7374 | smsg((char_u *)_("Invalid region nr in %s line %d: %s"), |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7375 | fname, lnum, p); |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7376 | break; |
| 7377 | } |
| 7378 | regionmask |= 1 << (l - 1); |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7379 | } |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 7380 | else |
| 7381 | { |
| 7382 | smsg((char_u *)_("Unrecognized flags in %s line %d: %s"), |
| 7383 | fname, lnum, p); |
| 7384 | break; |
| 7385 | } |
| 7386 | ++p; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7387 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
| 7390 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 7391 | if (spin->si_ascii && has_non_ascii(line)) |
| 7392 | { |
| 7393 | ++non_ascii; |
| 7394 | continue; |
| 7395 | } |
| 7396 | |
| 7397 | /* Normal word: store it. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7398 | if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7399 | { |
| 7400 | retval = FAIL; |
| 7401 | break; |
| 7402 | } |
| 7403 | did_word = TRUE; |
| 7404 | } |
| 7405 | |
| 7406 | vim_free(pc); |
| 7407 | fclose(fd); |
| 7408 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7409 | if (spin->si_ascii && non_ascii > 0) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7410 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7411 | vim_snprintf((char *)IObuff, IOSIZE, |
| 7412 | _("Ignored %d words with non-ASCII characters"), non_ascii); |
| 7413 | spell_message(spin, IObuff); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7414 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7415 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7416 | return retval; |
| 7417 | } |
| 7418 | |
| 7419 | /* |
| 7420 | * Get part of an sblock_T, "len" bytes long. |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7421 | * This avoids calling free() for every little struct we use (and keeping |
| 7422 | * track of them). |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7423 | * The memory is cleared to all zeros. |
| 7424 | * Returns NULL when out of memory. |
| 7425 | */ |
| 7426 | static void * |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7427 | getroom(spin, len, align) |
| 7428 | spellinfo_T *spin; |
| Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 7429 | size_t len; /* length needed */ |
| 7430 | int align; /* align for pointer */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7431 | { |
| 7432 | char_u *p; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7433 | sblock_T *bl = spin->si_blocks; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7434 | |
| Bram Moolenaar | cfc7d63 | 2005-07-28 22:28:16 +0000 | [diff] [blame] | 7435 | if (align && bl != NULL) |
| 7436 | /* Round size up for alignment. On some systems structures need to be |
| 7437 | * aligned to the size of a pointer (e.g., SPARC). */ |
| 7438 | bl->sb_used = (bl->sb_used + sizeof(char *) - 1) |
| 7439 | & ~(sizeof(char *) - 1); |
| 7440 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7441 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 7442 | { |
| 7443 | /* Allocate a block of memory. This is not freed until much later. */ |
| 7444 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 7445 | if (bl == NULL) |
| 7446 | return NULL; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7447 | bl->sb_next = spin->si_blocks; |
| 7448 | spin->si_blocks = bl; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7449 | bl->sb_used = 0; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7450 | ++spin->si_blocks_cnt; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
| 7453 | p = bl->sb_data + bl->sb_used; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7454 | bl->sb_used += (int)len; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7455 | |
| 7456 | return p; |
| 7457 | } |
| 7458 | |
| 7459 | /* |
| 7460 | * Make a copy of a string into memory allocated with getroom(). |
| 7461 | */ |
| 7462 | static char_u * |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7463 | getroom_save(spin, s) |
| 7464 | spellinfo_T *spin; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7465 | char_u *s; |
| 7466 | { |
| 7467 | char_u *sc; |
| 7468 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7469 | sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7470 | if (sc != NULL) |
| 7471 | STRCPY(sc, s); |
| 7472 | return sc; |
| 7473 | } |
| 7474 | |
| 7475 | |
| 7476 | /* |
| 7477 | * Free the list of allocated sblock_T. |
| 7478 | */ |
| 7479 | static void |
| 7480 | free_blocks(bl) |
| 7481 | sblock_T *bl; |
| 7482 | { |
| 7483 | sblock_T *next; |
| 7484 | |
| 7485 | while (bl != NULL) |
| 7486 | { |
| 7487 | next = bl->sb_next; |
| 7488 | vim_free(bl); |
| 7489 | bl = next; |
| 7490 | } |
| 7491 | } |
| 7492 | |
| 7493 | /* |
| 7494 | * Allocate the root of a word tree. |
| 7495 | */ |
| 7496 | static wordnode_T * |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7497 | wordtree_alloc(spin) |
| 7498 | spellinfo_T *spin; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7499 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7500 | return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7501 | } |
| 7502 | |
| 7503 | /* |
| 7504 | * Store a word in the tree(s). |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7505 | * Always store it in the case-folded tree. For a keep-case word this is |
| 7506 | * useful when the word can also be used with all caps (no WF_FIXCAP flag) and |
| 7507 | * used to find suggestions. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7508 | * For a keep-case word also store it in the keep-case tree. |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7509 | * When "pfxlist" is not NULL store the word for each postponed prefix ID and |
| 7510 | * compound flag. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7511 | */ |
| 7512 | static int |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7513 | store_word(spin, word, flags, region, pfxlist, need_affix) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7514 | spellinfo_T *spin; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7515 | char_u *word; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7516 | int flags; /* extra flags, WF_BANNED */ |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 7517 | int region; /* supported region(s) */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7518 | char_u *pfxlist; /* list of prefix IDs or NULL */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7519 | int need_affix; /* only store word with affix ID */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7520 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7521 | int len = (int)STRLEN(word); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7522 | int ct = captype(word, word + len); |
| 7523 | char_u foldword[MAXWLEN]; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7524 | int res = OK; |
| 7525 | char_u *p; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7526 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 7527 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7528 | for (p = pfxlist; res == OK; ++p) |
| 7529 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7530 | if (!need_affix || (p != NULL && *p != NUL)) |
| 7531 | res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags, |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7532 | region, p == NULL ? 0 : *p); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7533 | if (p == NULL || *p == NUL) |
| 7534 | break; |
| 7535 | } |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 7536 | ++spin->si_foldwcount; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 7537 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7538 | if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP))) |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 7539 | { |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7540 | for (p = pfxlist; res == OK; ++p) |
| 7541 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7542 | if (!need_affix || (p != NULL && *p != NUL)) |
| 7543 | res = tree_add_word(spin, word, spin->si_keeproot, flags, |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7544 | region, p == NULL ? 0 : *p); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7545 | if (p == NULL || *p == NUL) |
| 7546 | break; |
| 7547 | } |
| Bram Moolenaar | 8db7318 | 2005-06-17 21:51:16 +0000 | [diff] [blame] | 7548 | ++spin->si_keepwcount; |
| 7549 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7550 | return res; |
| 7551 | } |
| 7552 | |
| 7553 | /* |
| 7554 | * Add word "word" to a word tree at "root". |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7555 | * When "flags" < 0 we are adding to the prefix tree where "flags" is used for |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 7556 | * "rare" and "region" is the condition nr. |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 7557 | * Returns FAIL when out of memory. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7558 | */ |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 7559 | static int |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7560 | tree_add_word(spin, word, root, flags, region, affixID) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7561 | spellinfo_T *spin; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7562 | char_u *word; |
| 7563 | wordnode_T *root; |
| 7564 | int flags; |
| 7565 | int region; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7566 | int affixID; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7567 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7568 | wordnode_T *node = root; |
| 7569 | wordnode_T *np; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7570 | wordnode_T *copyp, **copyprev; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7571 | wordnode_T **prev = NULL; |
| 7572 | int i; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7573 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7574 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 7575 | for (i = 0; ; ++i) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7576 | { |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7577 | /* When there is more than one reference to this node we need to make |
| 7578 | * a copy, so that we can modify it. Copy the whole list of siblings |
| 7579 | * (we don't optimize for a partly shared list of siblings). */ |
| 7580 | if (node != NULL && node->wn_refs > 1) |
| 7581 | { |
| 7582 | --node->wn_refs; |
| 7583 | copyprev = prev; |
| 7584 | for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling) |
| 7585 | { |
| 7586 | /* Allocate a new node and copy the info. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7587 | np = get_wordnode(spin); |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7588 | if (np == NULL) |
| 7589 | return FAIL; |
| 7590 | np->wn_child = copyp->wn_child; |
| 7591 | if (np->wn_child != NULL) |
| 7592 | ++np->wn_child->wn_refs; /* child gets extra ref */ |
| 7593 | np->wn_byte = copyp->wn_byte; |
| 7594 | if (np->wn_byte == NUL) |
| 7595 | { |
| 7596 | np->wn_flags = copyp->wn_flags; |
| 7597 | np->wn_region = copyp->wn_region; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7598 | np->wn_affixID = copyp->wn_affixID; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7599 | } |
| 7600 | |
| 7601 | /* Link the new node in the list, there will be one ref. */ |
| 7602 | np->wn_refs = 1; |
| Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 7603 | if (copyprev != NULL) |
| 7604 | *copyprev = np; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7605 | copyprev = &np->wn_sibling; |
| 7606 | |
| 7607 | /* Let "node" point to the head of the copied list. */ |
| 7608 | if (copyp == node) |
| 7609 | node = np; |
| 7610 | } |
| 7611 | } |
| 7612 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7613 | /* Look for the sibling that has the same character. They are sorted |
| 7614 | * 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] | 7615 | * higher byte value. For zero bytes (end of word) the sorting is |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7616 | * done on flags and then on affixID. */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7617 | while (node != NULL |
| 7618 | && (node->wn_byte < word[i] |
| 7619 | || (node->wn_byte == NUL |
| 7620 | && (flags < 0 |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7621 | ? node->wn_affixID < (unsigned)affixID |
| 7622 | : (node->wn_flags < (unsigned)(flags & WN_MASK) |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7623 | || (node->wn_flags == (flags & WN_MASK) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7624 | && (spin->si_sugtree |
| 7625 | ? (node->wn_region & 0xffff) < region |
| 7626 | : node->wn_affixID |
| 7627 | < (unsigned)affixID))))))) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7628 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7629 | prev = &node->wn_sibling; |
| 7630 | node = *prev; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7631 | } |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 7632 | if (node == NULL |
| 7633 | || node->wn_byte != word[i] |
| 7634 | || (word[i] == NUL |
| 7635 | && (flags < 0 |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7636 | || spin->si_sugtree |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 7637 | || node->wn_flags != (flags & WN_MASK) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7638 | || node->wn_affixID != affixID))) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7639 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7640 | /* Allocate a new node. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7641 | np = get_wordnode(spin); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7642 | if (np == NULL) |
| 7643 | return FAIL; |
| 7644 | np->wn_byte = word[i]; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7645 | |
| 7646 | /* If "node" is NULL this is a new child or the end of the sibling |
| 7647 | * list: ref count is one. Otherwise use ref count of sibling and |
| 7648 | * make ref count of sibling one (matters when inserting in front |
| 7649 | * of the list of siblings). */ |
| 7650 | if (node == NULL) |
| 7651 | np->wn_refs = 1; |
| 7652 | else |
| 7653 | { |
| 7654 | np->wn_refs = node->wn_refs; |
| 7655 | node->wn_refs = 1; |
| 7656 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7657 | *prev = np; |
| 7658 | np->wn_sibling = node; |
| 7659 | node = np; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7660 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7661 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7662 | if (word[i] == NUL) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7663 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7664 | node->wn_flags = flags; |
| 7665 | node->wn_region |= region; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7666 | node->wn_affixID = affixID; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7667 | break; |
| Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 7668 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7669 | prev = &node->wn_child; |
| 7670 | node = *prev; |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 7671 | } |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7672 | #ifdef SPELL_PRINTTREE |
| 7673 | smsg("Added \"%s\"", word); |
| 7674 | spell_print_tree(root->wn_sibling); |
| 7675 | #endif |
| 7676 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7677 | /* count nr of words added since last message */ |
| 7678 | ++spin->si_msg_count; |
| 7679 | |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7680 | if (spin->si_compress_cnt > 1) |
| 7681 | { |
| 7682 | if (--spin->si_compress_cnt == 1) |
| 7683 | /* Did enough words to lower the block count limit. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7684 | spin->si_blocks_cnt += compress_inc; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7685 | } |
| 7686 | |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7687 | /* |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7688 | * When we have allocated lots of memory we need to compress the word tree |
| 7689 | * to free up some room. But compression is slow, and we might actually |
| 7690 | * need that room, thus only compress in the following situations: |
| 7691 | * 1. When not compressed before (si_compress_cnt == 0): when using |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7692 | * "compress_start" blocks. |
| 7693 | * 2. When compressed before and used "compress_inc" blocks before |
| 7694 | * adding "compress_added" words (si_compress_cnt > 1). |
| 7695 | * 3. When compressed before, added "compress_added" words |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7696 | * (si_compress_cnt == 1) and the number of free nodes drops below the |
| 7697 | * maximum word length. |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7698 | */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7699 | #ifndef SPELL_PRINTTREE |
| 7700 | if (spin->si_compress_cnt == 1 |
| 7701 | ? spin->si_free_count < MAXWLEN |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7702 | : spin->si_blocks_cnt >= compress_start) |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7703 | #endif |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7704 | { |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7705 | /* Decrement the block counter. The effect is that we compress again |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7706 | * when the freed up room has been used and another "compress_inc" |
| 7707 | * blocks have been allocated. Unless "compress_added" words have |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7708 | * been added, then the limit is put back again. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7709 | spin->si_blocks_cnt -= compress_inc; |
| 7710 | spin->si_compress_cnt = compress_added; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7711 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7712 | if (spin->si_verbose) |
| 7713 | { |
| 7714 | msg_start(); |
| 7715 | msg_puts((char_u *)_(msg_compressing)); |
| 7716 | msg_clr_eos(); |
| 7717 | msg_didout = FALSE; |
| 7718 | msg_col = 0; |
| 7719 | out_flush(); |
| 7720 | } |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7721 | |
| 7722 | /* Compress both trees. Either they both have many nodes, which makes |
| 7723 | * compression useful, or one of them is small, which means |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7724 | * compression goes fast. But when filling the souldfold word tree |
| 7725 | * there is no keep-case tree. */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7726 | wordtree_compress(spin, spin->si_foldroot); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7727 | if (affixID >= 0) |
| 7728 | wordtree_compress(spin, spin->si_keeproot); |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7729 | } |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 7730 | |
| 7731 | return OK; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7732 | } |
| 7733 | |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7734 | /* |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7735 | * Check the 'mkspellmem' option. Return FAIL if it's wrong. |
| 7736 | * Sets "sps_flags". |
| 7737 | */ |
| 7738 | int |
| 7739 | spell_check_msm() |
| 7740 | { |
| 7741 | char_u *p = p_msm; |
| 7742 | long start = 0; |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 7743 | long incr = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7744 | long added = 0; |
| 7745 | |
| 7746 | if (!VIM_ISDIGIT(*p)) |
| 7747 | return FAIL; |
| 7748 | /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/ |
| 7749 | start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102); |
| 7750 | if (*p != ',') |
| 7751 | return FAIL; |
| 7752 | ++p; |
| 7753 | if (!VIM_ISDIGIT(*p)) |
| 7754 | return FAIL; |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 7755 | incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7756 | if (*p != ',') |
| 7757 | return FAIL; |
| 7758 | ++p; |
| 7759 | if (!VIM_ISDIGIT(*p)) |
| 7760 | return FAIL; |
| 7761 | added = getdigits(&p) * 1024; |
| 7762 | if (*p != NUL) |
| 7763 | return FAIL; |
| 7764 | |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 7765 | if (start == 0 || incr == 0 || added == 0 || incr > start) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7766 | return FAIL; |
| 7767 | |
| 7768 | compress_start = start; |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 7769 | compress_inc = incr; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 7770 | compress_added = added; |
| 7771 | return OK; |
| 7772 | } |
| 7773 | |
| 7774 | |
| 7775 | /* |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7776 | * Get a wordnode_T, either from the list of previously freed nodes or |
| 7777 | * allocate a new one. |
| 7778 | */ |
| 7779 | static wordnode_T * |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7780 | get_wordnode(spin) |
| 7781 | spellinfo_T *spin; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7782 | { |
| 7783 | wordnode_T *n; |
| 7784 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7785 | if (spin->si_first_free == NULL) |
| 7786 | n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE); |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7787 | else |
| 7788 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7789 | n = spin->si_first_free; |
| 7790 | spin->si_first_free = n->wn_child; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7791 | vim_memset(n, 0, sizeof(wordnode_T)); |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7792 | --spin->si_free_count; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7793 | } |
| 7794 | #ifdef SPELL_PRINTTREE |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7795 | n->wn_nr = ++spin->si_wordnode_nr; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7796 | #endif |
| 7797 | return n; |
| 7798 | } |
| 7799 | |
| 7800 | /* |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7801 | * Decrement the reference count on a node (which is the head of a list of |
| 7802 | * siblings). If the reference count becomes zero free the node and its |
| 7803 | * siblings. |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7804 | * Returns the number of nodes actually freed. |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7805 | */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7806 | static int |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7807 | deref_wordnode(spin, node) |
| 7808 | spellinfo_T *spin; |
| 7809 | wordnode_T *node; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7810 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7811 | wordnode_T *np; |
| 7812 | int cnt = 0; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7813 | |
| 7814 | if (--node->wn_refs == 0) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7815 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7816 | for (np = node; np != NULL; np = np->wn_sibling) |
| 7817 | { |
| 7818 | if (np->wn_child != NULL) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7819 | cnt += deref_wordnode(spin, np->wn_child); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7820 | free_wordnode(spin, np); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7821 | ++cnt; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7822 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7823 | ++cnt; /* length field */ |
| 7824 | } |
| 7825 | return cnt; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7826 | } |
| 7827 | |
| 7828 | /* |
| 7829 | * Free a wordnode_T for re-use later. |
| 7830 | * Only the "wn_child" field becomes invalid. |
| 7831 | */ |
| 7832 | static void |
| 7833 | free_wordnode(spin, n) |
| 7834 | spellinfo_T *spin; |
| 7835 | wordnode_T *n; |
| 7836 | { |
| 7837 | n->wn_child = spin->si_first_free; |
| 7838 | spin->si_first_free = n; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7839 | ++spin->si_free_count; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7840 | } |
| 7841 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7842 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7843 | * Compress a tree: find tails that are identical and can be shared. |
| 7844 | */ |
| 7845 | static void |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7846 | wordtree_compress(spin, root) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7847 | spellinfo_T *spin; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7848 | wordnode_T *root; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7849 | { |
| 7850 | hashtab_T ht; |
| 7851 | int n; |
| 7852 | int tot = 0; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7853 | int perc; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7854 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7855 | /* Skip the root itself, it's not actually used. The first sibling is the |
| 7856 | * start of the tree. */ |
| 7857 | if (root->wn_sibling != NULL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7858 | { |
| 7859 | hash_init(&ht); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7860 | n = node_compress(spin, root->wn_sibling, &ht, &tot); |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7861 | |
| 7862 | #ifndef SPELL_PRINTTREE |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7863 | if (spin->si_verbose || p_verbose > 2) |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7864 | #endif |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7865 | { |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7866 | if (tot > 1000000) |
| 7867 | perc = (tot - n) / (tot / 100); |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7868 | else if (tot == 0) |
| 7869 | perc = 0; |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7870 | else |
| 7871 | perc = (tot - n) * 100 / tot; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7872 | vim_snprintf((char *)IObuff, IOSIZE, |
| 7873 | _("Compressed %d of %d nodes; %d (%d%%) remaining"), |
| 7874 | n, tot, tot - n, perc); |
| 7875 | spell_message(spin, IObuff); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 7876 | } |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7877 | #ifdef SPELL_PRINTTREE |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7878 | spell_print_tree(root->wn_sibling); |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7879 | #endif |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7880 | hash_clear(&ht); |
| 7881 | } |
| 7882 | } |
| 7883 | |
| 7884 | /* |
| 7885 | * Compress a node, its siblings and its children, depth first. |
| 7886 | * Returns the number of compressed nodes. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7887 | */ |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 7888 | static int |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7889 | node_compress(spin, node, ht, tot) |
| 7890 | spellinfo_T *spin; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7891 | wordnode_T *node; |
| 7892 | hashtab_T *ht; |
| 7893 | int *tot; /* total count of nodes before compressing, |
| 7894 | incremented while going through the tree */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7895 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7896 | wordnode_T *np; |
| 7897 | wordnode_T *tp; |
| 7898 | wordnode_T *child; |
| 7899 | hash_T hash; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7900 | hashitem_T *hi; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7901 | int len = 0; |
| 7902 | unsigned nr, n; |
| 7903 | int compressed = 0; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7904 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7905 | /* |
| 7906 | * Go through the list of siblings. Compress each child and then try |
| 7907 | * finding an identical child to replace it. |
| 7908 | * Note that with "child" we mean not just the node that is pointed to, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7909 | * but the whole list of siblings of which the child node is the first. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7910 | */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7911 | for (np = node; np != NULL && !got_int; np = np->wn_sibling) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7912 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7913 | ++len; |
| 7914 | if ((child = np->wn_child) != NULL) |
| 7915 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7916 | /* Compress the child first. This fills hashkey. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7917 | compressed += node_compress(spin, child, ht, tot); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7918 | |
| 7919 | /* Try to find an identical child. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7920 | hash = hash_hash(child->wn_u1.hashkey); |
| 7921 | hi = hash_lookup(ht, child->wn_u1.hashkey, hash); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7922 | if (!HASHITEM_EMPTY(hi)) |
| 7923 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7924 | /* There are children we encountered before with a hash value |
| 7925 | * identical to the current child. Now check if there is one |
| 7926 | * that is really identical. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7927 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7928 | if (node_equal(child, tp)) |
| 7929 | { |
| 7930 | /* Found one! Now use that child in place of the |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 7931 | * current one. This means the current child and all |
| 7932 | * its siblings is unlinked from the tree. */ |
| 7933 | ++tp->wn_refs; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7934 | compressed += deref_wordnode(spin, child); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7935 | np->wn_child = tp; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7936 | break; |
| 7937 | } |
| 7938 | if (tp == NULL) |
| 7939 | { |
| 7940 | /* No other child with this hash value equals the child of |
| 7941 | * the node, add it to the linked list after the first |
| 7942 | * item. */ |
| 7943 | tp = HI2WN(hi); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7944 | child->wn_u2.next = tp->wn_u2.next; |
| 7945 | tp->wn_u2.next = child; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7946 | } |
| 7947 | } |
| 7948 | else |
| 7949 | /* No other child has this hash value, add it to the |
| 7950 | * hashtable. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7951 | hash_add_item(ht, hi, child->wn_u1.hashkey, hash); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7952 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7953 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 7954 | *tot += len + 1; /* add one for the node that stores the length */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7955 | |
| 7956 | /* |
| 7957 | * Make a hash key for the node and its siblings, so that we can quickly |
| 7958 | * find a lookalike node. This must be done after compressing the sibling |
| 7959 | * list, otherwise the hash key would become invalid by the compression. |
| 7960 | */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7961 | node->wn_u1.hashkey[0] = len; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7962 | nr = 0; |
| 7963 | for (np = node; np != NULL; np = np->wn_sibling) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7964 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7965 | if (np->wn_byte == NUL) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 7966 | /* end node: use wn_flags, wn_region and wn_affixID */ |
| 7967 | n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7968 | else |
| 7969 | /* byte node: use the byte value and the child pointer */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7970 | n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8)); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7971 | nr = nr * 101 + n; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7972 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7973 | |
| 7974 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 7975 | n = nr & 0xff; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7976 | node->wn_u1.hashkey[1] = n == 0 ? 1 : n; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7977 | n = (nr >> 8) & 0xff; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7978 | node->wn_u1.hashkey[2] = n == 0 ? 1 : n; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7979 | n = (nr >> 16) & 0xff; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7980 | node->wn_u1.hashkey[3] = n == 0 ? 1 : n; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7981 | n = (nr >> 24) & 0xff; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 7982 | node->wn_u1.hashkey[4] = n == 0 ? 1 : n; |
| 7983 | node->wn_u1.hashkey[5] = NUL; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7984 | |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 7985 | /* Check for CTRL-C pressed now and then. */ |
| 7986 | fast_breakcheck(); |
| 7987 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 7988 | return compressed; |
| 7989 | } |
| 7990 | |
| 7991 | /* |
| 7992 | * Return TRUE when two nodes have identical siblings and children. |
| 7993 | */ |
| 7994 | static int |
| 7995 | node_equal(n1, n2) |
| 7996 | wordnode_T *n1; |
| 7997 | wordnode_T *n2; |
| 7998 | { |
| 7999 | wordnode_T *p1; |
| 8000 | wordnode_T *p2; |
| 8001 | |
| 8002 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 8003 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 8004 | if (p1->wn_byte != p2->wn_byte |
| 8005 | || (p1->wn_byte == NUL |
| 8006 | ? (p1->wn_flags != p2->wn_flags |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8007 | || p1->wn_region != p2->wn_region |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8008 | || p1->wn_affixID != p2->wn_affixID) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8009 | : (p1->wn_child != p2->wn_child))) |
| 8010 | break; |
| 8011 | |
| 8012 | return p1 == NULL && p2 == NULL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8013 | } |
| 8014 | |
| 8015 | /* |
| 8016 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 8017 | */ |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 8018 | void |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8019 | put_bytes(fd, nr, len) |
| 8020 | FILE *fd; |
| 8021 | long_u nr; |
| 8022 | int len; |
| 8023 | { |
| 8024 | int i; |
| 8025 | |
| 8026 | for (i = len - 1; i >= 0; --i) |
| 8027 | putc((int)(nr >> (i * 8)), fd); |
| 8028 | } |
| 8029 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8030 | #ifdef _MSC_VER |
| 8031 | # if (_MSC_VER <= 1200) |
| 8032 | /* This line is required for VC6 without the service pack. Also see the |
| 8033 | * matching #pragma below. */ |
| Bram Moolenaar | 5fdec47 | 2007-07-24 08:45:13 +0000 | [diff] [blame] | 8034 | # pragma optimize("", off) |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8035 | # endif |
| 8036 | #endif |
| 8037 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8038 | /* |
| 8039 | * Write spin->si_sugtime to file "fd". |
| 8040 | */ |
| 8041 | static void |
| 8042 | put_sugtime(spin, fd) |
| 8043 | spellinfo_T *spin; |
| 8044 | FILE *fd; |
| 8045 | { |
| 8046 | int c; |
| 8047 | int i; |
| 8048 | |
| 8049 | /* time_t can be up to 8 bytes in size, more than long_u, thus we |
| 8050 | * can't use put_bytes() here. */ |
| 8051 | for (i = 7; i >= 0; --i) |
| 8052 | if (i + 1 > sizeof(time_t)) |
| 8053 | /* ">>" doesn't work well when shifting more bits than avail */ |
| 8054 | putc(0, fd); |
| 8055 | else |
| 8056 | { |
| 8057 | c = (unsigned)spin->si_sugtime >> (i * 8); |
| 8058 | putc(c, fd); |
| 8059 | } |
| 8060 | } |
| 8061 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8062 | #ifdef _MSC_VER |
| 8063 | # if (_MSC_VER <= 1200) |
| Bram Moolenaar | 5fdec47 | 2007-07-24 08:45:13 +0000 | [diff] [blame] | 8064 | # pragma optimize("", on) |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8065 | # endif |
| 8066 | #endif |
| 8067 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8068 | static int |
| 8069 | #ifdef __BORLANDC__ |
| 8070 | _RTLENTRYF |
| 8071 | #endif |
| 8072 | rep_compare __ARGS((const void *s1, const void *s2)); |
| 8073 | |
| 8074 | /* |
| 8075 | * Function given to qsort() to sort the REP items on "from" string. |
| 8076 | */ |
| 8077 | static int |
| 8078 | #ifdef __BORLANDC__ |
| 8079 | _RTLENTRYF |
| 8080 | #endif |
| 8081 | rep_compare(s1, s2) |
| 8082 | const void *s1; |
| 8083 | const void *s2; |
| 8084 | { |
| 8085 | fromto_T *p1 = (fromto_T *)s1; |
| 8086 | fromto_T *p2 = (fromto_T *)s2; |
| 8087 | |
| 8088 | return STRCMP(p1->ft_from, p2->ft_from); |
| 8089 | } |
| 8090 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8091 | /* |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8092 | * Write the Vim .spl file "fname". |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8093 | * Return FAIL or OK; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8094 | */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8095 | static int |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8096 | write_vim_spell(spin, fname) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8097 | spellinfo_T *spin; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8098 | char_u *fname; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8099 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8100 | FILE *fd; |
| 8101 | int regionmask; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8102 | int round; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8103 | wordnode_T *tree; |
| 8104 | int nodecount; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8105 | int i; |
| 8106 | int l; |
| 8107 | garray_T *gap; |
| 8108 | fromto_T *ftp; |
| 8109 | char_u *p; |
| 8110 | int rr; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8111 | int retval = OK; |
| Bram Moolenaar | 2eb6eb3 | 2008-11-29 19:19:19 +0000 | [diff] [blame] | 8112 | size_t fwv = 1; /* collect return value of fwrite() to avoid |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8113 | warnings from picky compiler */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8114 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 8115 | fd = mch_fopen((char *)fname, "w"); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8116 | if (fd == NULL) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8117 | { |
| 8118 | EMSG2(_(e_notopen), fname); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8119 | return FAIL; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8122 | /* <HEADER>: <fileID> <versionnr> */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8123 | /* <fileID> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8124 | fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd); |
| Bram Moolenaar | 2eb6eb3 | 2008-11-29 19:19:19 +0000 | [diff] [blame] | 8125 | if (fwv != (size_t)1) |
| 8126 | /* Catch first write error, don't try writing more. */ |
| 8127 | goto theend; |
| 8128 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8129 | putc(VIMSPELLVERSION, fd); /* <versionnr> */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8130 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8131 | /* |
| 8132 | * <SECTIONS>: <section> ... <sectionend> |
| 8133 | */ |
| 8134 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8135 | /* SN_INFO: <infotext> */ |
| 8136 | if (spin->si_info != NULL) |
| 8137 | { |
| 8138 | putc(SN_INFO, fd); /* <sectionID> */ |
| 8139 | putc(0, fd); /* <sectionflags> */ |
| 8140 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8141 | i = (int)STRLEN(spin->si_info); |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8142 | put_bytes(fd, (long_u)i, 4); /* <sectionlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8143 | fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */ |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8144 | } |
| 8145 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8146 | /* SN_REGION: <regionname> ... |
| 8147 | * Write the region names only if there is more than one. */ |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 8148 | if (spin->si_region_count > 1) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8149 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8150 | putc(SN_REGION, fd); /* <sectionID> */ |
| 8151 | putc(SNF_REQUIRED, fd); /* <sectionflags> */ |
| 8152 | l = spin->si_region_count * 2; |
| 8153 | put_bytes(fd, (long_u)l, 4); /* <sectionlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8154 | fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8155 | /* <regionname> ... */ |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 8156 | regionmask = (1 << spin->si_region_count) - 1; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8157 | } |
| 8158 | else |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8159 | regionmask = 0; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8160 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8161 | /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars> |
| 8162 | * |
| 8163 | * The table with character flags and the table for case folding. |
| 8164 | * This makes sure the same characters are recognized as word characters |
| 8165 | * when generating an when using a spell file. |
| Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 8166 | * 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] | 8167 | * 'encoding'. |
| 8168 | * Also skip this for an .add.spl file, the main spell file must contain |
| 8169 | * the table (avoids that it conflicts). File is shorter too. |
| 8170 | */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8171 | if (!spin->si_ascii && !spin->si_add) |
| Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 8172 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8173 | char_u folchars[128 * 8]; |
| 8174 | int flags; |
| 8175 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 8176 | putc(SN_CHARFLAGS, fd); /* <sectionID> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8177 | putc(SNF_REQUIRED, fd); /* <sectionflags> */ |
| 8178 | |
| 8179 | /* Form the <folchars> string first, we need to know its length. */ |
| 8180 | l = 0; |
| 8181 | for (i = 128; i < 256; ++i) |
| 8182 | { |
| 8183 | #ifdef FEAT_MBYTE |
| 8184 | if (has_mbyte) |
| 8185 | l += mb_char2bytes(spelltab.st_fold[i], folchars + l); |
| 8186 | else |
| 8187 | #endif |
| 8188 | folchars[l++] = spelltab.st_fold[i]; |
| 8189 | } |
| 8190 | put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */ |
| 8191 | |
| 8192 | fputc(128, fd); /* <charflagslen> */ |
| 8193 | for (i = 128; i < 256; ++i) |
| 8194 | { |
| 8195 | flags = 0; |
| 8196 | if (spelltab.st_isw[i]) |
| 8197 | flags |= CF_WORD; |
| 8198 | if (spelltab.st_isu[i]) |
| 8199 | flags |= CF_UPPER; |
| 8200 | fputc(flags, fd); /* <charflags> */ |
| 8201 | } |
| 8202 | |
| 8203 | put_bytes(fd, (long_u)l, 2); /* <folcharslen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8204 | fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */ |
| Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 8205 | } |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 8206 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8207 | /* SN_MIDWORD: <midword> */ |
| 8208 | if (spin->si_midword != NULL) |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8209 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8210 | putc(SN_MIDWORD, fd); /* <sectionID> */ |
| 8211 | putc(SNF_REQUIRED, fd); /* <sectionflags> */ |
| 8212 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8213 | i = (int)STRLEN(spin->si_midword); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8214 | put_bytes(fd, (long_u)i, 4); /* <sectionlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8215 | fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); |
| 8216 | /* <midword> */ |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8217 | } |
| 8218 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8219 | /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */ |
| 8220 | if (spin->si_prefcond.ga_len > 0) |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8221 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8222 | putc(SN_PREFCOND, fd); /* <sectionID> */ |
| 8223 | putc(SNF_REQUIRED, fd); /* <sectionflags> */ |
| 8224 | |
| 8225 | l = write_spell_prefcond(NULL, &spin->si_prefcond); |
| 8226 | put_bytes(fd, (long_u)l, 4); /* <sectionlen> */ |
| 8227 | |
| 8228 | write_spell_prefcond(fd, &spin->si_prefcond); |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8229 | } |
| 8230 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8231 | /* SN_REP: <repcount> <rep> ... |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8232 | * SN_SAL: <salflags> <salcount> <sal> ... |
| 8233 | * SN_REPSAL: <repcount> <rep> ... */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8234 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8235 | /* round 1: SN_REP section |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8236 | * round 2: SN_SAL section (unless SN_SOFO is used) |
| 8237 | * round 3: SN_REPSAL section */ |
| 8238 | for (round = 1; round <= 3; ++round) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8239 | { |
| 8240 | if (round == 1) |
| 8241 | gap = &spin->si_rep; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8242 | else if (round == 2) |
| 8243 | { |
| 8244 | /* Don't write SN_SAL when using a SN_SOFO section */ |
| 8245 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 8246 | continue; |
| 8247 | gap = &spin->si_sal; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8248 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8249 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8250 | gap = &spin->si_repsal; |
| 8251 | |
| 8252 | /* Don't write the section if there are no items. */ |
| 8253 | if (gap->ga_len == 0) |
| 8254 | continue; |
| 8255 | |
| 8256 | /* Sort the REP/REPSAL items. */ |
| 8257 | if (round != 2) |
| 8258 | qsort(gap->ga_data, (size_t)gap->ga_len, |
| 8259 | sizeof(fromto_T), rep_compare); |
| 8260 | |
| 8261 | i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL); |
| 8262 | putc(i, fd); /* <sectionID> */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8263 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8264 | /* This is for making suggestions, section is not required. */ |
| 8265 | putc(0, fd); /* <sectionflags> */ |
| 8266 | |
| 8267 | /* Compute the length of what follows. */ |
| 8268 | l = 2; /* count <repcount> or <salcount> */ |
| 8269 | for (i = 0; i < gap->ga_len; ++i) |
| 8270 | { |
| 8271 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8272 | l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */ |
| 8273 | l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8274 | } |
| 8275 | if (round == 2) |
| 8276 | ++l; /* count <salflags> */ |
| 8277 | put_bytes(fd, (long_u)l, 4); /* <sectionlen> */ |
| 8278 | |
| 8279 | if (round == 2) |
| 8280 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8281 | i = 0; |
| 8282 | if (spin->si_followup) |
| 8283 | i |= SAL_F0LLOWUP; |
| 8284 | if (spin->si_collapse) |
| 8285 | i |= SAL_COLLAPSE; |
| 8286 | if (spin->si_rem_accents) |
| 8287 | i |= SAL_REM_ACCENTS; |
| 8288 | putc(i, fd); /* <salflags> */ |
| 8289 | } |
| 8290 | |
| 8291 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */ |
| 8292 | for (i = 0; i < gap->ga_len; ++i) |
| 8293 | { |
| 8294 | /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */ |
| 8295 | /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */ |
| 8296 | ftp = &((fromto_T *)gap->ga_data)[i]; |
| 8297 | for (rr = 1; rr <= 2; ++rr) |
| 8298 | { |
| 8299 | p = rr == 1 ? ftp->ft_from : ftp->ft_to; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8300 | l = (int)STRLEN(p); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8301 | putc(l, fd); |
| Bram Moolenaar | 9bf1361 | 2008-11-29 19:11:40 +0000 | [diff] [blame] | 8302 | if (l > 0) |
| 8303 | fwv &= fwrite(p, l, (size_t)1, fd); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8304 | } |
| 8305 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8306 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8307 | } |
| 8308 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8309 | /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto> |
| 8310 | * This is for making suggestions, section is not required. */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8311 | if (spin->si_sofofr != NULL && spin->si_sofoto != NULL) |
| 8312 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8313 | putc(SN_SOFO, fd); /* <sectionID> */ |
| 8314 | putc(0, fd); /* <sectionflags> */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8315 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8316 | l = (int)STRLEN(spin->si_sofofr); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8317 | put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4); |
| 8318 | /* <sectionlen> */ |
| 8319 | |
| 8320 | put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8321 | fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8322 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8323 | l = (int)STRLEN(spin->si_sofoto); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8324 | put_bytes(fd, (long_u)l, 2); /* <sofotolen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8325 | fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8328 | /* SN_WORDS: <word> ... |
| 8329 | * This is for making suggestions, section is not required. */ |
| 8330 | if (spin->si_commonwords.ht_used > 0) |
| 8331 | { |
| 8332 | putc(SN_WORDS, fd); /* <sectionID> */ |
| 8333 | putc(0, fd); /* <sectionflags> */ |
| 8334 | |
| 8335 | /* round 1: count the bytes |
| 8336 | * round 2: write the bytes */ |
| 8337 | for (round = 1; round <= 2; ++round) |
| 8338 | { |
| 8339 | int todo; |
| 8340 | int len = 0; |
| 8341 | hashitem_T *hi; |
| 8342 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8343 | todo = (int)spin->si_commonwords.ht_used; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8344 | for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi) |
| 8345 | if (!HASHITEM_EMPTY(hi)) |
| 8346 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8347 | l = (int)STRLEN(hi->hi_key) + 1; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8348 | len += l; |
| 8349 | if (round == 2) /* <word> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8350 | fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8351 | --todo; |
| 8352 | } |
| 8353 | if (round == 1) |
| 8354 | put_bytes(fd, (long_u)len, 4); /* <sectionlen> */ |
| 8355 | } |
| 8356 | } |
| 8357 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8358 | /* SN_MAP: <mapstr> |
| 8359 | * This is for making suggestions, section is not required. */ |
| 8360 | if (spin->si_map.ga_len > 0) |
| 8361 | { |
| 8362 | putc(SN_MAP, fd); /* <sectionID> */ |
| 8363 | putc(0, fd); /* <sectionflags> */ |
| 8364 | l = spin->si_map.ga_len; |
| 8365 | put_bytes(fd, (long_u)l, 4); /* <sectionlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8366 | fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8367 | /* <mapstr> */ |
| 8368 | } |
| 8369 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8370 | /* SN_SUGFILE: <timestamp> |
| 8371 | * This is used to notify that a .sug file may be available and at the |
| 8372 | * same time allows for checking that a .sug file that is found matches |
| 8373 | * with this .spl file. That's because the word numbers must be exactly |
| 8374 | * right. */ |
| 8375 | if (!spin->si_nosugfile |
| 8376 | && (spin->si_sal.ga_len > 0 |
| 8377 | || (spin->si_sofofr != NULL && spin->si_sofoto != NULL))) |
| 8378 | { |
| 8379 | putc(SN_SUGFILE, fd); /* <sectionID> */ |
| 8380 | putc(0, fd); /* <sectionflags> */ |
| 8381 | put_bytes(fd, (long_u)8, 4); /* <sectionlen> */ |
| 8382 | |
| 8383 | /* Set si_sugtime and write it to the file. */ |
| 8384 | spin->si_sugtime = time(NULL); |
| 8385 | put_sugtime(spin, fd); /* <timestamp> */ |
| 8386 | } |
| 8387 | |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 8388 | /* SN_NOSPLITSUGS: nothing |
| 8389 | * This is used to notify that no suggestions with word splits are to be |
| 8390 | * made. */ |
| 8391 | if (spin->si_nosplitsugs) |
| 8392 | { |
| 8393 | putc(SN_NOSPLITSUGS, fd); /* <sectionID> */ |
| 8394 | putc(0, fd); /* <sectionflags> */ |
| 8395 | put_bytes(fd, (long_u)0, 4); /* <sectionlen> */ |
| 8396 | } |
| 8397 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8398 | /* SN_COMPOUND: compound info. |
| 8399 | * We don't mark it required, when not supported all compound words will |
| 8400 | * be bad words. */ |
| 8401 | if (spin->si_compflags != NULL) |
| 8402 | { |
| 8403 | putc(SN_COMPOUND, fd); /* <sectionID> */ |
| 8404 | putc(0, fd); /* <sectionflags> */ |
| 8405 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8406 | l = (int)STRLEN(spin->si_compflags); |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 8407 | for (i = 0; i < spin->si_comppat.ga_len; ++i) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8408 | l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1; |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 8409 | put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */ |
| 8410 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8411 | putc(spin->si_compmax, fd); /* <compmax> */ |
| 8412 | putc(spin->si_compminlen, fd); /* <compminlen> */ |
| 8413 | putc(spin->si_compsylmax, fd); /* <compsylmax> */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 8414 | putc(0, fd); /* for Vim 7.0b compatibility */ |
| 8415 | putc(spin->si_compoptions, fd); /* <compoptions> */ |
| 8416 | put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2); |
| 8417 | /* <comppatcount> */ |
| 8418 | for (i = 0; i < spin->si_comppat.ga_len; ++i) |
| 8419 | { |
| 8420 | p = ((char_u **)(spin->si_comppat.ga_data))[i]; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8421 | putc((int)STRLEN(p), fd); /* <comppatlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8422 | fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd); |
| 8423 | /* <comppattext> */ |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 8424 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8425 | /* <compflags> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8426 | fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags), |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 8427 | (size_t)1, fd); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 8430 | /* SN_NOBREAK: NOBREAK flag */ |
| 8431 | if (spin->si_nobreak) |
| 8432 | { |
| 8433 | putc(SN_NOBREAK, fd); /* <sectionID> */ |
| 8434 | putc(0, fd); /* <sectionflags> */ |
| 8435 | |
| Bram Moolenaar | f711faf | 2007-05-10 16:48:19 +0000 | [diff] [blame] | 8436 | /* It's empty, the presence of the section flags the feature. */ |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 8437 | put_bytes(fd, (long_u)0, 4); /* <sectionlen> */ |
| 8438 | } |
| 8439 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8440 | /* SN_SYLLABLE: syllable info. |
| 8441 | * We don't mark it required, when not supported syllables will not be |
| 8442 | * counted. */ |
| 8443 | if (spin->si_syllable != NULL) |
| 8444 | { |
| 8445 | putc(SN_SYLLABLE, fd); /* <sectionID> */ |
| 8446 | putc(0, fd); /* <sectionflags> */ |
| 8447 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8448 | l = (int)STRLEN(spin->si_syllable); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8449 | put_bytes(fd, (long_u)l, 4); /* <sectionlen> */ |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8450 | fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); |
| 8451 | /* <syllable> */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | /* end of <SECTIONS> */ |
| 8455 | putc(SN_END, fd); /* <sectionend> */ |
| 8456 | |
| Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 8457 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8458 | /* |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8459 | * <LWORDTREE> <KWORDTREE> <PREFIXTREE> |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8460 | */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8461 | spin->si_memtot = 0; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8462 | for (round = 1; round <= 3; ++round) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8463 | { |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8464 | if (round == 1) |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 8465 | tree = spin->si_foldroot->wn_sibling; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8466 | else if (round == 2) |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 8467 | tree = spin->si_keeproot->wn_sibling; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8468 | else |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 8469 | tree = spin->si_prefroot->wn_sibling; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8470 | |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8471 | /* Clear the index and wnode fields in the tree. */ |
| 8472 | clear_node(tree); |
| 8473 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8474 | /* Count the number of nodes. Needed to be able to allocate the |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8475 | * memory when reading the nodes. Also fills in index for shared |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8476 | * nodes. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8477 | nodecount = put_node(NULL, tree, 0, regionmask, round == 3); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8478 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8479 | /* number of nodes in 4 bytes */ |
| 8480 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
| Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 8481 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8482 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8483 | /* Write the nodes. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8484 | (void)put_node(fd, tree, 0, regionmask, round == 3); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8485 | } |
| 8486 | |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8487 | /* Write another byte to check for errors (file system full). */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8488 | if (putc(0, fd) == EOF) |
| 8489 | retval = FAIL; |
| Bram Moolenaar | 2eb6eb3 | 2008-11-29 19:19:19 +0000 | [diff] [blame] | 8490 | theend: |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8491 | if (fclose(fd) == EOF) |
| 8492 | retval = FAIL; |
| 8493 | |
| Bram Moolenaar | 2eb6eb3 | 2008-11-29 19:19:19 +0000 | [diff] [blame] | 8494 | if (fwv != (size_t)1) |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 8495 | retval = FAIL; |
| 8496 | if (retval == FAIL) |
| 8497 | EMSG(_(e_write)); |
| 8498 | |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 8499 | return retval; |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 8500 | } |
| 8501 | |
| 8502 | /* |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8503 | * Clear the index and wnode fields of "node", it siblings and its |
| 8504 | * children. This is needed because they are a union with other items to save |
| 8505 | * space. |
| 8506 | */ |
| 8507 | static void |
| 8508 | clear_node(node) |
| 8509 | wordnode_T *node; |
| 8510 | { |
| 8511 | wordnode_T *np; |
| 8512 | |
| 8513 | if (node != NULL) |
| 8514 | for (np = node; np != NULL; np = np->wn_sibling) |
| 8515 | { |
| 8516 | np->wn_u1.index = 0; |
| 8517 | np->wn_u2.wnode = NULL; |
| 8518 | |
| 8519 | if (np->wn_byte != NUL) |
| 8520 | clear_node(np->wn_child); |
| 8521 | } |
| 8522 | } |
| 8523 | |
| 8524 | |
| 8525 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8526 | * Dump a word tree at node "node". |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8527 | * |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8528 | * This first writes the list of possible bytes (siblings). Then for each |
| 8529 | * byte recursively write the children. |
| 8530 | * |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8531 | * NOTE: The code here must match the code in read_tree_node(), since |
| 8532 | * assumptions are made about the indexes (so that we don't have to write them |
| 8533 | * in the file). |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8534 | * |
| 8535 | * Returns the number of nodes used. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8536 | */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8537 | static int |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 8538 | put_node(fd, node, idx, regionmask, prefixtree) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8539 | FILE *fd; /* NULL when only counting */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8540 | wordnode_T *node; |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 8541 | int idx; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8542 | int regionmask; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8543 | int prefixtree; /* TRUE for PREFIXTREE */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8544 | { |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 8545 | int newindex = idx; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8546 | int siblingcount = 0; |
| 8547 | wordnode_T *np; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8548 | int flags; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8549 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8550 | /* If "node" is zero the tree is empty. */ |
| 8551 | if (node == NULL) |
| 8552 | return 0; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8553 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8554 | /* Store the index where this node is written. */ |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 8555 | node->wn_u1.index = idx; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8556 | |
| 8557 | /* Count the number of siblings. */ |
| 8558 | for (np = node; np != NULL; np = np->wn_sibling) |
| 8559 | ++siblingcount; |
| 8560 | |
| 8561 | /* Write the sibling count. */ |
| 8562 | if (fd != NULL) |
| 8563 | putc(siblingcount, fd); /* <siblingcount> */ |
| 8564 | |
| 8565 | /* Write each sibling byte and optionally extra info. */ |
| 8566 | for (np = node; np != NULL; np = np->wn_sibling) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8567 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8568 | if (np->wn_byte == 0) |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 8569 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8570 | if (fd != NULL) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8571 | { |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8572 | /* For a NUL byte (end of word) write the flags etc. */ |
| 8573 | if (prefixtree) |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 8574 | { |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8575 | /* In PREFIXTREE write the required affixID and the |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 8576 | * associated condition nr (stored in wn_region). The |
| 8577 | * byte value is misused to store the "rare" and "not |
| 8578 | * combining" flags */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 8579 | if (np->wn_flags == (short_u)PFX_FLAGS) |
| 8580 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 8581 | else |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 8582 | { |
| 8583 | putc(BY_FLAGS, fd); /* <byte> */ |
| 8584 | putc(np->wn_flags, fd); /* <pflags> */ |
| 8585 | } |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8586 | putc(np->wn_affixID, fd); /* <affixID> */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8587 | put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8588 | } |
| 8589 | else |
| 8590 | { |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8591 | /* For word trees we write the flag/region items. */ |
| 8592 | flags = np->wn_flags; |
| 8593 | if (regionmask != 0 && np->wn_region != regionmask) |
| 8594 | flags |= WF_REGION; |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8595 | if (np->wn_affixID != 0) |
| 8596 | flags |= WF_AFX; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8597 | if (flags == 0) |
| 8598 | { |
| 8599 | /* word without flags or region */ |
| 8600 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 8601 | } |
| 8602 | else |
| 8603 | { |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 8604 | if (np->wn_flags >= 0x100) |
| 8605 | { |
| 8606 | putc(BY_FLAGS2, fd); /* <byte> */ |
| 8607 | putc(flags, fd); /* <flags> */ |
| 8608 | putc((unsigned)flags >> 8, fd); /* <flags2> */ |
| 8609 | } |
| 8610 | else |
| 8611 | { |
| 8612 | putc(BY_FLAGS, fd); /* <byte> */ |
| 8613 | putc(flags, fd); /* <flags> */ |
| 8614 | } |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8615 | if (flags & WF_REGION) |
| 8616 | putc(np->wn_region, fd); /* <region> */ |
| Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 8617 | if (flags & WF_AFX) |
| 8618 | putc(np->wn_affixID, fd); /* <affixID> */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8619 | } |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 8620 | } |
| 8621 | } |
| Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 8622 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8623 | else |
| 8624 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8625 | if (np->wn_child->wn_u1.index != 0 |
| 8626 | && np->wn_child->wn_u2.wnode != node) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8627 | { |
| 8628 | /* The child is written elsewhere, write the reference. */ |
| 8629 | if (fd != NULL) |
| 8630 | { |
| 8631 | putc(BY_INDEX, fd); /* <byte> */ |
| 8632 | /* <nodeidx> */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8633 | put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8634 | } |
| 8635 | } |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8636 | else if (np->wn_child->wn_u2.wnode == NULL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8637 | /* We will write the child below and give it an index. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8638 | np->wn_child->wn_u2.wnode = node; |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 8639 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8640 | if (fd != NULL) |
| 8641 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 8642 | { |
| 8643 | EMSG(_(e_write)); |
| 8644 | return 0; |
| 8645 | } |
| 8646 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8647 | } |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8648 | |
| 8649 | /* Space used in the array when reading: one for each sibling and one for |
| 8650 | * the count. */ |
| 8651 | newindex += siblingcount + 1; |
| 8652 | |
| 8653 | /* Recursively dump the children of each sibling. */ |
| 8654 | for (np = node; np != NULL; np = np->wn_sibling) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 8655 | if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node) |
| 8656 | newindex = put_node(fd, np->wn_child, newindex, regionmask, |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 8657 | prefixtree); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 8658 | |
| 8659 | return newindex; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8660 | } |
| 8661 | |
| 8662 | |
| 8663 | /* |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 8664 | * ":mkspell [-ascii] outfile infile ..." |
| 8665 | * ":mkspell [-ascii] addfile" |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 8666 | */ |
| 8667 | void |
| 8668 | ex_mkspell(eap) |
| 8669 | exarg_T *eap; |
| 8670 | { |
| 8671 | int fcount; |
| 8672 | char_u **fnames; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 8673 | char_u *arg = eap->arg; |
| 8674 | int ascii = FALSE; |
| 8675 | |
| 8676 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 8677 | { |
| 8678 | ascii = TRUE; |
| 8679 | arg = skipwhite(arg + 6); |
| 8680 | } |
| 8681 | |
| 8682 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 8683 | if (get_arglist_exp(arg, &fcount, &fnames) == OK) |
| 8684 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8685 | mkspell(fcount, fnames, ascii, eap->forceit, FALSE); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 8686 | FreeWild(fcount, fnames); |
| 8687 | } |
| 8688 | } |
| 8689 | |
| 8690 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8691 | * Create the .sug file. |
| 8692 | * Uses the soundfold info in "spin". |
| 8693 | * Writes the file with the name "wfname", with ".spl" changed to ".sug". |
| 8694 | */ |
| 8695 | static void |
| 8696 | spell_make_sugfile(spin, wfname) |
| 8697 | spellinfo_T *spin; |
| 8698 | char_u *wfname; |
| 8699 | { |
| 8700 | char_u fname[MAXPATHL]; |
| 8701 | int len; |
| 8702 | slang_T *slang; |
| 8703 | int free_slang = FALSE; |
| 8704 | |
| 8705 | /* |
| 8706 | * Read back the .spl file that was written. This fills the required |
| 8707 | * info for soundfolding. This also uses less memory than the |
| 8708 | * pointer-linked version of the trie. And it avoids having two versions |
| 8709 | * of the code for the soundfolding stuff. |
| 8710 | * It might have been done already by spell_reload_one(). |
| 8711 | */ |
| 8712 | for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
| 8713 | if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME) |
| 8714 | break; |
| 8715 | if (slang == NULL) |
| 8716 | { |
| 8717 | spell_message(spin, (char_u *)_("Reading back spell file...")); |
| 8718 | slang = spell_load_file(wfname, NULL, NULL, FALSE); |
| 8719 | if (slang == NULL) |
| 8720 | return; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8721 | free_slang = TRUE; |
| 8722 | } |
| 8723 | |
| 8724 | /* |
| 8725 | * Clear the info in "spin" that is used. |
| 8726 | */ |
| 8727 | spin->si_blocks = NULL; |
| 8728 | spin->si_blocks_cnt = 0; |
| 8729 | spin->si_compress_cnt = 0; /* will stay at 0 all the time*/ |
| 8730 | spin->si_free_count = 0; |
| 8731 | spin->si_first_free = NULL; |
| 8732 | spin->si_foldwcount = 0; |
| 8733 | |
| 8734 | /* |
| 8735 | * Go through the trie of good words, soundfold each word and add it to |
| 8736 | * the soundfold trie. |
| 8737 | */ |
| 8738 | spell_message(spin, (char_u *)_("Performing soundfolding...")); |
| 8739 | if (sug_filltree(spin, slang) == FAIL) |
| 8740 | goto theend; |
| 8741 | |
| 8742 | /* |
| 8743 | * Create the table which links each soundfold word with a list of the |
| 8744 | * good words it may come from. Creates buffer "spin->si_spellbuf". |
| 8745 | * This also removes the wordnr from the NUL byte entries to make |
| 8746 | * compression possible. |
| 8747 | */ |
| 8748 | if (sug_maketable(spin) == FAIL) |
| 8749 | goto theend; |
| 8750 | |
| 8751 | smsg((char_u *)_("Number of words after soundfolding: %ld"), |
| 8752 | (long)spin->si_spellbuf->b_ml.ml_line_count); |
| 8753 | |
| 8754 | /* |
| 8755 | * Compress the soundfold trie. |
| 8756 | */ |
| 8757 | spell_message(spin, (char_u *)_(msg_compressing)); |
| 8758 | wordtree_compress(spin, spin->si_foldroot); |
| 8759 | |
| 8760 | /* |
| 8761 | * Write the .sug file. |
| 8762 | * Make the file name by changing ".spl" to ".sug". |
| 8763 | */ |
| 8764 | STRCPY(fname, wfname); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8765 | len = (int)STRLEN(fname); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8766 | fname[len - 2] = 'u'; |
| 8767 | fname[len - 1] = 'g'; |
| 8768 | sug_write(spin, fname); |
| 8769 | |
| 8770 | theend: |
| 8771 | if (free_slang) |
| 8772 | slang_free(slang); |
| 8773 | free_blocks(spin->si_blocks); |
| 8774 | close_spellbuf(spin->si_spellbuf); |
| 8775 | } |
| 8776 | |
| 8777 | /* |
| 8778 | * Build the soundfold trie for language "slang". |
| 8779 | */ |
| 8780 | static int |
| 8781 | sug_filltree(spin, slang) |
| 8782 | spellinfo_T *spin; |
| 8783 | slang_T *slang; |
| 8784 | { |
| 8785 | char_u *byts; |
| 8786 | idx_T *idxs; |
| 8787 | int depth; |
| 8788 | idx_T arridx[MAXWLEN]; |
| 8789 | int curi[MAXWLEN]; |
| 8790 | char_u tword[MAXWLEN]; |
| 8791 | char_u tsalword[MAXWLEN]; |
| 8792 | int c; |
| 8793 | idx_T n; |
| 8794 | unsigned words_done = 0; |
| 8795 | int wordcount[MAXWLEN]; |
| 8796 | |
| 8797 | /* We use si_foldroot for the souldfolded trie. */ |
| 8798 | spin->si_foldroot = wordtree_alloc(spin); |
| 8799 | if (spin->si_foldroot == NULL) |
| 8800 | return FAIL; |
| 8801 | |
| 8802 | /* let tree_add_word() know we're adding to the soundfolded tree */ |
| 8803 | spin->si_sugtree = TRUE; |
| 8804 | |
| 8805 | /* |
| 8806 | * Go through the whole case-folded tree, soundfold each word and put it |
| 8807 | * in the trie. |
| 8808 | */ |
| 8809 | byts = slang->sl_fbyts; |
| 8810 | idxs = slang->sl_fidxs; |
| 8811 | |
| 8812 | arridx[0] = 0; |
| 8813 | curi[0] = 1; |
| 8814 | wordcount[0] = 0; |
| 8815 | |
| 8816 | depth = 0; |
| 8817 | while (depth >= 0 && !got_int) |
| 8818 | { |
| 8819 | if (curi[depth] > byts[arridx[depth]]) |
| 8820 | { |
| 8821 | /* Done all bytes at this node, go up one level. */ |
| 8822 | idxs[arridx[depth]] = wordcount[depth]; |
| 8823 | if (depth > 0) |
| 8824 | wordcount[depth - 1] += wordcount[depth]; |
| 8825 | |
| 8826 | --depth; |
| 8827 | line_breakcheck(); |
| 8828 | } |
| 8829 | else |
| 8830 | { |
| 8831 | |
| 8832 | /* Do one more byte at this node. */ |
| 8833 | n = arridx[depth] + curi[depth]; |
| 8834 | ++curi[depth]; |
| 8835 | |
| 8836 | c = byts[n]; |
| 8837 | if (c == 0) |
| 8838 | { |
| 8839 | /* Sound-fold the word. */ |
| 8840 | tword[depth] = NUL; |
| 8841 | spell_soundfold(slang, tword, TRUE, tsalword); |
| 8842 | |
| 8843 | /* We use the "flags" field for the MSB of the wordnr, |
| 8844 | * "region" for the LSB of the wordnr. */ |
| 8845 | if (tree_add_word(spin, tsalword, spin->si_foldroot, |
| 8846 | words_done >> 16, words_done & 0xffff, |
| 8847 | 0) == FAIL) |
| 8848 | return FAIL; |
| 8849 | |
| 8850 | ++words_done; |
| 8851 | ++wordcount[depth]; |
| 8852 | |
| 8853 | /* Reset the block count each time to avoid compression |
| 8854 | * kicking in. */ |
| 8855 | spin->si_blocks_cnt = 0; |
| 8856 | |
| 8857 | /* Skip over any other NUL bytes (same word with different |
| 8858 | * flags). */ |
| 8859 | while (byts[n + 1] == 0) |
| 8860 | { |
| 8861 | ++n; |
| 8862 | ++curi[depth]; |
| 8863 | } |
| 8864 | } |
| 8865 | else |
| 8866 | { |
| 8867 | /* Normal char, go one level deeper. */ |
| 8868 | tword[depth++] = c; |
| 8869 | arridx[depth] = idxs[n]; |
| 8870 | curi[depth] = 1; |
| 8871 | wordcount[depth] = 0; |
| 8872 | } |
| 8873 | } |
| 8874 | } |
| 8875 | |
| 8876 | smsg((char_u *)_("Total number of words: %d"), words_done); |
| 8877 | |
| 8878 | return OK; |
| 8879 | } |
| 8880 | |
| 8881 | /* |
| 8882 | * Make the table that links each word in the soundfold trie to the words it |
| 8883 | * can be produced from. |
| 8884 | * This is not unlike lines in a file, thus use a memfile to be able to access |
| 8885 | * the table efficiently. |
| 8886 | * Returns FAIL when out of memory. |
| 8887 | */ |
| 8888 | static int |
| 8889 | sug_maketable(spin) |
| 8890 | spellinfo_T *spin; |
| 8891 | { |
| 8892 | garray_T ga; |
| 8893 | int res = OK; |
| 8894 | |
| 8895 | /* Allocate a buffer, open a memline for it and create the swap file |
| 8896 | * (uses a temp file, not a .swp file). */ |
| 8897 | spin->si_spellbuf = open_spellbuf(); |
| 8898 | if (spin->si_spellbuf == NULL) |
| 8899 | return FAIL; |
| 8900 | |
| 8901 | /* Use a buffer to store the line info, avoids allocating many small |
| 8902 | * pieces of memory. */ |
| 8903 | ga_init2(&ga, 1, 100); |
| 8904 | |
| 8905 | /* recursively go through the tree */ |
| 8906 | if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1) |
| 8907 | res = FAIL; |
| 8908 | |
| 8909 | ga_clear(&ga); |
| 8910 | return res; |
| 8911 | } |
| 8912 | |
| 8913 | /* |
| 8914 | * Fill the table for one node and its children. |
| 8915 | * Returns the wordnr at the start of the node. |
| 8916 | * Returns -1 when out of memory. |
| 8917 | */ |
| 8918 | static int |
| 8919 | sug_filltable(spin, node, startwordnr, gap) |
| 8920 | spellinfo_T *spin; |
| 8921 | wordnode_T *node; |
| 8922 | int startwordnr; |
| 8923 | garray_T *gap; /* place to store line of numbers */ |
| 8924 | { |
| 8925 | wordnode_T *p, *np; |
| 8926 | int wordnr = startwordnr; |
| 8927 | int nr; |
| 8928 | int prev_nr; |
| 8929 | |
| 8930 | for (p = node; p != NULL; p = p->wn_sibling) |
| 8931 | { |
| 8932 | if (p->wn_byte == NUL) |
| 8933 | { |
| 8934 | gap->ga_len = 0; |
| 8935 | prev_nr = 0; |
| 8936 | for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling) |
| 8937 | { |
| 8938 | if (ga_grow(gap, 10) == FAIL) |
| 8939 | return -1; |
| 8940 | |
| 8941 | nr = (np->wn_flags << 16) + (np->wn_region & 0xffff); |
| 8942 | /* Compute the offset from the previous nr and store the |
| 8943 | * offset in a way that it takes a minimum number of bytes. |
| 8944 | * It's a bit like utf-8, but without the need to mark |
| 8945 | * following bytes. */ |
| 8946 | nr -= prev_nr; |
| 8947 | prev_nr += nr; |
| 8948 | gap->ga_len += offset2bytes(nr, |
| 8949 | (char_u *)gap->ga_data + gap->ga_len); |
| 8950 | } |
| 8951 | |
| 8952 | /* add the NUL byte */ |
| 8953 | ((char_u *)gap->ga_data)[gap->ga_len++] = NUL; |
| 8954 | |
| 8955 | if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr, |
| 8956 | gap->ga_data, gap->ga_len, TRUE) == FAIL) |
| 8957 | return -1; |
| 8958 | ++wordnr; |
| 8959 | |
| 8960 | /* Remove extra NUL entries, we no longer need them. We don't |
| 8961 | * bother freeing the nodes, the won't be reused anyway. */ |
| 8962 | while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL) |
| 8963 | p->wn_sibling = p->wn_sibling->wn_sibling; |
| 8964 | |
| 8965 | /* Clear the flags on the remaining NUL node, so that compression |
| 8966 | * works a lot better. */ |
| 8967 | p->wn_flags = 0; |
| 8968 | p->wn_region = 0; |
| 8969 | } |
| 8970 | else |
| 8971 | { |
| 8972 | wordnr = sug_filltable(spin, p->wn_child, wordnr, gap); |
| 8973 | if (wordnr == -1) |
| 8974 | return -1; |
| 8975 | } |
| 8976 | } |
| 8977 | return wordnr; |
| 8978 | } |
| 8979 | |
| 8980 | /* |
| 8981 | * Convert an offset into a minimal number of bytes. |
| 8982 | * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL |
| 8983 | * bytes. |
| 8984 | */ |
| 8985 | static int |
| 8986 | offset2bytes(nr, buf) |
| 8987 | int nr; |
| 8988 | char_u *buf; |
| 8989 | { |
| 8990 | int rem; |
| 8991 | int b1, b2, b3, b4; |
| 8992 | |
| 8993 | /* Split the number in parts of base 255. We need to avoid NUL bytes. */ |
| 8994 | b1 = nr % 255 + 1; |
| 8995 | rem = nr / 255; |
| 8996 | b2 = rem % 255 + 1; |
| 8997 | rem = rem / 255; |
| 8998 | b3 = rem % 255 + 1; |
| 8999 | b4 = rem / 255 + 1; |
| 9000 | |
| 9001 | if (b4 > 1 || b3 > 0x1f) /* 4 bytes */ |
| 9002 | { |
| 9003 | buf[0] = 0xe0 + b4; |
| 9004 | buf[1] = b3; |
| 9005 | buf[2] = b2; |
| 9006 | buf[3] = b1; |
| 9007 | return 4; |
| 9008 | } |
| 9009 | if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */ |
| 9010 | { |
| 9011 | buf[0] = 0xc0 + b3; |
| 9012 | buf[1] = b2; |
| 9013 | buf[2] = b1; |
| 9014 | return 3; |
| 9015 | } |
| 9016 | if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */ |
| 9017 | { |
| 9018 | buf[0] = 0x80 + b2; |
| 9019 | buf[1] = b1; |
| 9020 | return 2; |
| 9021 | } |
| 9022 | /* 1 byte */ |
| 9023 | buf[0] = b1; |
| 9024 | return 1; |
| 9025 | } |
| 9026 | |
| 9027 | /* |
| 9028 | * Opposite of offset2bytes(). |
| 9029 | * "pp" points to the bytes and is advanced over it. |
| 9030 | * Returns the offset. |
| 9031 | */ |
| 9032 | static int |
| 9033 | bytes2offset(pp) |
| 9034 | char_u **pp; |
| 9035 | { |
| 9036 | char_u *p = *pp; |
| 9037 | int nr; |
| 9038 | int c; |
| 9039 | |
| 9040 | c = *p++; |
| 9041 | if ((c & 0x80) == 0x00) /* 1 byte */ |
| 9042 | { |
| 9043 | nr = c - 1; |
| 9044 | } |
| 9045 | else if ((c & 0xc0) == 0x80) /* 2 bytes */ |
| 9046 | { |
| 9047 | nr = (c & 0x3f) - 1; |
| 9048 | nr = nr * 255 + (*p++ - 1); |
| 9049 | } |
| 9050 | else if ((c & 0xe0) == 0xc0) /* 3 bytes */ |
| 9051 | { |
| 9052 | nr = (c & 0x1f) - 1; |
| 9053 | nr = nr * 255 + (*p++ - 1); |
| 9054 | nr = nr * 255 + (*p++ - 1); |
| 9055 | } |
| 9056 | else /* 4 bytes */ |
| 9057 | { |
| 9058 | nr = (c & 0x0f) - 1; |
| 9059 | nr = nr * 255 + (*p++ - 1); |
| 9060 | nr = nr * 255 + (*p++ - 1); |
| 9061 | nr = nr * 255 + (*p++ - 1); |
| 9062 | } |
| 9063 | |
| 9064 | *pp = p; |
| 9065 | return nr; |
| 9066 | } |
| 9067 | |
| 9068 | /* |
| 9069 | * Write the .sug file in "fname". |
| 9070 | */ |
| 9071 | static void |
| 9072 | sug_write(spin, fname) |
| 9073 | spellinfo_T *spin; |
| 9074 | char_u *fname; |
| 9075 | { |
| 9076 | FILE *fd; |
| 9077 | wordnode_T *tree; |
| 9078 | int nodecount; |
| 9079 | int wcount; |
| 9080 | char_u *line; |
| 9081 | linenr_T lnum; |
| 9082 | int len; |
| 9083 | |
| 9084 | /* Create the file. Note that an existing file is silently overwritten! */ |
| 9085 | fd = mch_fopen((char *)fname, "w"); |
| 9086 | if (fd == NULL) |
| 9087 | { |
| 9088 | EMSG2(_(e_notopen), fname); |
| 9089 | return; |
| 9090 | } |
| 9091 | |
| 9092 | vim_snprintf((char *)IObuff, IOSIZE, |
| 9093 | _("Writing suggestion file %s ..."), fname); |
| 9094 | spell_message(spin, IObuff); |
| 9095 | |
| 9096 | /* |
| 9097 | * <SUGHEADER>: <fileID> <versionnr> <timestamp> |
| 9098 | */ |
| 9099 | if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */ |
| 9100 | { |
| 9101 | EMSG(_(e_write)); |
| 9102 | goto theend; |
| 9103 | } |
| 9104 | putc(VIMSUGVERSION, fd); /* <versionnr> */ |
| 9105 | |
| 9106 | /* Write si_sugtime to the file. */ |
| 9107 | put_sugtime(spin, fd); /* <timestamp> */ |
| 9108 | |
| 9109 | /* |
| 9110 | * <SUGWORDTREE> |
| 9111 | */ |
| 9112 | spin->si_memtot = 0; |
| 9113 | tree = spin->si_foldroot->wn_sibling; |
| 9114 | |
| 9115 | /* Clear the index and wnode fields in the tree. */ |
| 9116 | clear_node(tree); |
| 9117 | |
| 9118 | /* Count the number of nodes. Needed to be able to allocate the |
| 9119 | * memory when reading the nodes. Also fills in index for shared |
| 9120 | * nodes. */ |
| 9121 | nodecount = put_node(NULL, tree, 0, 0, FALSE); |
| 9122 | |
| 9123 | /* number of nodes in 4 bytes */ |
| 9124 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
| 9125 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
| 9126 | |
| 9127 | /* Write the nodes. */ |
| 9128 | (void)put_node(fd, tree, 0, 0, FALSE); |
| 9129 | |
| 9130 | /* |
| 9131 | * <SUGTABLE>: <sugwcount> <sugline> ... |
| 9132 | */ |
| 9133 | wcount = spin->si_spellbuf->b_ml.ml_line_count; |
| 9134 | put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */ |
| 9135 | |
| 9136 | for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum) |
| 9137 | { |
| 9138 | /* <sugline>: <sugnr> ... NUL */ |
| 9139 | line = ml_get_buf(spin->si_spellbuf, lnum, FALSE); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9140 | len = (int)STRLEN(line) + 1; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9141 | if (fwrite(line, (size_t)len, (size_t)1, fd) == 0) |
| 9142 | { |
| 9143 | EMSG(_(e_write)); |
| 9144 | goto theend; |
| 9145 | } |
| 9146 | spin->si_memtot += len; |
| 9147 | } |
| 9148 | |
| 9149 | /* Write another byte to check for errors. */ |
| 9150 | if (putc(0, fd) == EOF) |
| 9151 | EMSG(_(e_write)); |
| 9152 | |
| 9153 | vim_snprintf((char *)IObuff, IOSIZE, |
| 9154 | _("Estimated runtime memory use: %d bytes"), spin->si_memtot); |
| 9155 | spell_message(spin, IObuff); |
| 9156 | |
| 9157 | theend: |
| 9158 | /* close the file */ |
| 9159 | fclose(fd); |
| 9160 | } |
| 9161 | |
| 9162 | /* |
| 9163 | * Open a spell buffer. This is a nameless buffer that is not in the buffer |
| 9164 | * list and only contains text lines. Can use a swapfile to reduce memory |
| 9165 | * use. |
| 9166 | * Most other fields are invalid! Esp. watch out for string options being |
| 9167 | * NULL and there is no undo info. |
| 9168 | * Returns NULL when out of memory. |
| 9169 | */ |
| 9170 | static buf_T * |
| 9171 | open_spellbuf() |
| 9172 | { |
| 9173 | buf_T *buf; |
| 9174 | |
| 9175 | buf = (buf_T *)alloc_clear(sizeof(buf_T)); |
| 9176 | if (buf != NULL) |
| 9177 | { |
| 9178 | buf->b_spell = TRUE; |
| 9179 | buf->b_p_swf = TRUE; /* may create a swap file */ |
| 9180 | ml_open(buf); |
| 9181 | ml_open_file(buf); /* create swap file now */ |
| 9182 | } |
| 9183 | return buf; |
| 9184 | } |
| 9185 | |
| 9186 | /* |
| 9187 | * Close the buffer used for spell info. |
| 9188 | */ |
| 9189 | static void |
| 9190 | close_spellbuf(buf) |
| 9191 | buf_T *buf; |
| 9192 | { |
| 9193 | if (buf != NULL) |
| 9194 | { |
| 9195 | ml_close(buf, TRUE); |
| 9196 | vim_free(buf); |
| 9197 | } |
| 9198 | } |
| 9199 | |
| 9200 | |
| 9201 | /* |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9202 | * Create a Vim spell file from one or more word lists. |
| 9203 | * "fnames[0]" is the output file name. |
| 9204 | * "fnames[fcount - 1]" is the last input file name. |
| 9205 | * Exception: when "fnames[0]" ends in ".add" it's used as the input file name |
| 9206 | * and ".spl" is appended to make the output file name. |
| 9207 | */ |
| 9208 | static void |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9209 | mkspell(fcount, fnames, ascii, overwrite, added_word) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9210 | int fcount; |
| 9211 | char_u **fnames; |
| 9212 | int ascii; /* -ascii argument given */ |
| 9213 | int overwrite; /* overwrite existing output file */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9214 | int added_word; /* invoked through "zg" */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9215 | { |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9216 | char_u fname[MAXPATHL]; |
| 9217 | char_u wfname[MAXPATHL]; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9218 | char_u **innames; |
| 9219 | int incount; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9220 | afffile_T *(afile[8]); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9221 | int i; |
| 9222 | int len; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9223 | struct stat st; |
| Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 9224 | int error = FALSE; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9225 | spellinfo_T spin; |
| 9226 | |
| 9227 | vim_memset(&spin, 0, sizeof(spin)); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9228 | spin.si_verbose = !added_word; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9229 | spin.si_ascii = ascii; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9230 | spin.si_followup = TRUE; |
| 9231 | spin.si_rem_accents = TRUE; |
| 9232 | ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9233 | ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9234 | ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20); |
| 9235 | ga_init2(&spin.si_map, (int)sizeof(char_u), 100); |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 9236 | ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 9237 | ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9238 | hash_init(&spin.si_commonwords); |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 9239 | spin.si_newcompID = 127; /* start compound ID at first maximum */ |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9240 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9241 | /* default: fnames[0] is output file, following are input files */ |
| 9242 | innames = &fnames[1]; |
| 9243 | incount = fcount - 1; |
| 9244 | |
| 9245 | if (fcount >= 1) |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 9246 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9247 | len = (int)STRLEN(fnames[0]); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9248 | if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0) |
| 9249 | { |
| 9250 | /* For ":mkspell path/en.latin1.add" output file is |
| 9251 | * "path/en.latin1.add.spl". */ |
| 9252 | innames = &fnames[0]; |
| 9253 | incount = 1; |
| 9254 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]); |
| 9255 | } |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9256 | else if (fcount == 1) |
| 9257 | { |
| 9258 | /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */ |
| 9259 | innames = &fnames[0]; |
| 9260 | incount = 1; |
| 9261 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 9262 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 9263 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9264 | else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0) |
| 9265 | { |
| 9266 | /* Name ends in ".spl", use as the file name. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9267 | vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9268 | } |
| 9269 | else |
| 9270 | /* Name should be language, make the file name from it. */ |
| 9271 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
| 9272 | spin.si_ascii ? (char_u *)"ascii" : spell_enc()); |
| 9273 | |
| 9274 | /* Check for .ascii.spl. */ |
| 9275 | if (strstr((char *)gettail(wfname), ".ascii.") != NULL) |
| 9276 | spin.si_ascii = TRUE; |
| 9277 | |
| 9278 | /* Check for .add.spl. */ |
| 9279 | if (strstr((char *)gettail(wfname), ".add.") != NULL) |
| 9280 | spin.si_add = TRUE; |
| Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9283 | if (incount <= 0) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9284 | EMSG(_(e_invarg)); /* need at least output and input names */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9285 | else if (vim_strchr(gettail(wfname), '_') != NULL) |
| 9286 | EMSG(_("E751: Output file name must not have region name")); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9287 | else if (incount > 8) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9288 | EMSG(_("E754: Only up to 8 regions supported")); |
| 9289 | else |
| 9290 | { |
| 9291 | /* Check for overwriting before doing things that may take a lot of |
| 9292 | * time. */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9293 | if (!overwrite && mch_stat((char *)wfname, &st) >= 0) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9294 | { |
| 9295 | EMSG(_(e_exists)); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9296 | return; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9297 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9298 | if (mch_isdir(wfname)) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9299 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9300 | EMSG2(_(e_isadir2), wfname); |
| 9301 | return; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9302 | } |
| 9303 | |
| 9304 | /* |
| 9305 | * Init the aff and dic pointers. |
| 9306 | * Get the region names if there are more than 2 arguments. |
| 9307 | */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9308 | for (i = 0; i < incount; ++i) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9309 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9310 | afile[i] = NULL; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9311 | |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 9312 | if (incount > 1) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9313 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9314 | len = (int)STRLEN(innames[i]); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9315 | if (STRLEN(gettail(innames[i])) < 5 |
| 9316 | || innames[i][len - 3] != '_') |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9317 | { |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9318 | EMSG2(_("E755: Invalid region in %s"), innames[i]); |
| 9319 | return; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9320 | } |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 9321 | spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]); |
| 9322 | spin.si_region_name[i * 2 + 1] = |
| 9323 | TOLOWER_ASC(innames[i][len - 1]); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9324 | } |
| 9325 | } |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 9326 | spin.si_region_count = incount; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9327 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9328 | spin.si_foldroot = wordtree_alloc(&spin); |
| 9329 | spin.si_keeproot = wordtree_alloc(&spin); |
| 9330 | spin.si_prefroot = wordtree_alloc(&spin); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 9331 | if (spin.si_foldroot == NULL |
| 9332 | || spin.si_keeproot == NULL |
| 9333 | || spin.si_prefroot == NULL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9334 | { |
| Bram Moolenaar | 329cc7e | 2005-08-10 07:51:35 +0000 | [diff] [blame] | 9335 | free_blocks(spin.si_blocks); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9336 | return; |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9337 | } |
| 9338 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9339 | /* When not producing a .add.spl file clear the character table when |
| 9340 | * we encounter one in the .aff file. This means we dump the current |
| 9341 | * one in the .spl file if the .aff file doesn't define one. That's |
| 9342 | * better than guessing the contents, the table will match a |
| 9343 | * previously loaded spell file. */ |
| 9344 | if (!spin.si_add) |
| 9345 | spin.si_clear_chartab = TRUE; |
| 9346 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9347 | /* |
| 9348 | * Read all the .aff and .dic files. |
| 9349 | * Text is converted to 'encoding'. |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9350 | * Words are stored in the case-folded and keep-case trees. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9351 | */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9352 | for (i = 0; i < incount && !error; ++i) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9353 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9354 | spin.si_conv.vc_type = CONV_NONE; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9355 | spin.si_region = 1 << i; |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9356 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9357 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9358 | if (mch_stat((char *)fname, &st) >= 0) |
| 9359 | { |
| 9360 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 9361 | * "SET" line. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9362 | afile[i] = spell_read_aff(&spin, fname); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9363 | if (afile[i] == NULL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9364 | error = TRUE; |
| 9365 | else |
| 9366 | { |
| 9367 | /* Read the .dic file and store the words in the trees. */ |
| 9368 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9369 | innames[i]); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9370 | if (spell_read_dic(&spin, fname, afile[i]) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9371 | error = TRUE; |
| 9372 | } |
| 9373 | } |
| 9374 | else |
| 9375 | { |
| 9376 | /* No .aff file, try reading the file as a word list. Store |
| 9377 | * the words in the trees. */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9378 | if (spell_read_wordfile(&spin, innames[i]) == FAIL) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9379 | error = TRUE; |
| 9380 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9381 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9382 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9383 | /* Free any conversion stuff. */ |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9384 | convert_setup(&spin.si_conv, NULL, NULL); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9385 | #endif |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9386 | } |
| 9387 | |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 9388 | if (spin.si_compflags != NULL && spin.si_nobreak) |
| 9389 | MSG(_("Warning: both compounding and NOBREAK specified")); |
| 9390 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9391 | if (!error && !got_int) |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9392 | { |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9393 | /* |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9394 | * Combine tails in the tree. |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9395 | */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9396 | spell_message(&spin, (char_u *)_(msg_compressing)); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9397 | wordtree_compress(&spin, spin.si_foldroot); |
| 9398 | wordtree_compress(&spin, spin.si_keeproot); |
| 9399 | wordtree_compress(&spin, spin.si_prefroot); |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9400 | } |
| 9401 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9402 | if (!error && !got_int) |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9403 | { |
| 9404 | /* |
| 9405 | * Write the info in the spell file. |
| 9406 | */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9407 | vim_snprintf((char *)IObuff, IOSIZE, |
| 9408 | _("Writing spell file %s ..."), wfname); |
| 9409 | spell_message(&spin, IObuff); |
| Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 9410 | |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 9411 | error = write_vim_spell(&spin, wfname) == FAIL; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9412 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9413 | spell_message(&spin, (char_u *)_("Done!")); |
| 9414 | vim_snprintf((char *)IObuff, IOSIZE, |
| 9415 | _("Estimated runtime memory use: %d bytes"), spin.si_memtot); |
| 9416 | spell_message(&spin, IObuff); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9417 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9418 | /* |
| 9419 | * If the file is loaded need to reload it. |
| 9420 | */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 9421 | if (!error) |
| 9422 | spell_reload_one(wfname, added_word); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | /* Free the allocated memory. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9426 | ga_clear(&spin.si_rep); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9427 | ga_clear(&spin.si_repsal); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 9428 | ga_clear(&spin.si_sal); |
| 9429 | ga_clear(&spin.si_map); |
| Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 9430 | ga_clear(&spin.si_comppat); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 9431 | ga_clear(&spin.si_prefcond); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9432 | hash_clear_all(&spin.si_commonwords, 0); |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9433 | |
| 9434 | /* Free the .aff file structures. */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9435 | for (i = 0; i < incount; ++i) |
| 9436 | if (afile[i] != NULL) |
| 9437 | spell_free_aff(afile[i]); |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 9438 | |
| 9439 | /* Free all the bits and pieces at once. */ |
| 9440 | free_blocks(spin.si_blocks); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9441 | |
| 9442 | /* |
| 9443 | * If there is soundfolding info and no NOSUGFILE item create the |
| 9444 | * .sug file with the soundfolded word trie. |
| 9445 | */ |
| 9446 | if (spin.si_sugtime != 0 && !error && !got_int) |
| 9447 | spell_make_sugfile(&spin, wfname); |
| 9448 | |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9449 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9450 | } |
| 9451 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9452 | /* |
| 9453 | * Display a message for spell file processing when 'verbose' is set or using |
| 9454 | * ":mkspell". "str" can be IObuff. |
| 9455 | */ |
| 9456 | static void |
| 9457 | spell_message(spin, str) |
| 9458 | spellinfo_T *spin; |
| 9459 | char_u *str; |
| 9460 | { |
| 9461 | if (spin->si_verbose || p_verbose > 2) |
| 9462 | { |
| 9463 | if (!spin->si_verbose) |
| 9464 | verbose_enter(); |
| 9465 | MSG(str); |
| 9466 | out_flush(); |
| 9467 | if (!spin->si_verbose) |
| 9468 | verbose_leave(); |
| 9469 | } |
| 9470 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9471 | |
| 9472 | /* |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9473 | * ":[count]spellgood {word}" |
| 9474 | * ":[count]spellwrong {word}" |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9475 | * ":[count]spellundo {word}" |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9476 | */ |
| 9477 | void |
| 9478 | ex_spell(eap) |
| 9479 | exarg_T *eap; |
| 9480 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9481 | spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong, |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9482 | eap->forceit ? 0 : (int)eap->line2, |
| 9483 | eap->cmdidx == CMD_spellundo); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
| 9486 | /* |
| 9487 | * Add "word[len]" to 'spellfile' as a good or bad word. |
| 9488 | */ |
| 9489 | void |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 9490 | spell_add_word(word, len, bad, idx, undo) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9491 | char_u *word; |
| 9492 | int len; |
| 9493 | int bad; |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 9494 | int idx; /* "zG" and "zW": zero, otherwise index in |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9495 | 'spellfile' */ |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9496 | int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9497 | { |
| Bram Moolenaar | a391707 | 2006-09-14 08:48:14 +0000 | [diff] [blame] | 9498 | FILE *fd = NULL; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9499 | buf_T *buf = NULL; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9500 | int new_spf = FALSE; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9501 | char_u *fname; |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9502 | char_u fnamebuf[MAXPATHL]; |
| 9503 | char_u line[MAXWLEN * 2]; |
| 9504 | long fpos, fpos_next = 0; |
| 9505 | int i; |
| 9506 | char_u *spf; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9507 | |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 9508 | if (idx == 0) /* use internal wordlist */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9509 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9510 | if (int_wordlist == NULL) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9511 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9512 | int_wordlist = vim_tempname('s'); |
| 9513 | if (int_wordlist == NULL) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9514 | return; |
| 9515 | } |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9516 | fname = int_wordlist; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9517 | } |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9518 | else |
| 9519 | { |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9520 | /* If 'spellfile' isn't set figure out a good default value. */ |
| 9521 | if (*curbuf->b_p_spf == NUL) |
| 9522 | { |
| 9523 | init_spellfile(); |
| 9524 | new_spf = TRUE; |
| 9525 | } |
| 9526 | |
| 9527 | if (*curbuf->b_p_spf == NUL) |
| 9528 | { |
| Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 9529 | EMSG2(_(e_notset), "spellfile"); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9530 | return; |
| 9531 | } |
| 9532 | |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9533 | for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i) |
| 9534 | { |
| 9535 | copy_option_part(&spf, fnamebuf, MAXPATHL, ","); |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 9536 | if (i == idx) |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9537 | break; |
| 9538 | if (*spf == NUL) |
| 9539 | { |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 9540 | EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx); |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9541 | return; |
| 9542 | } |
| 9543 | } |
| 9544 | |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9545 | /* Check that the user isn't editing the .add file somewhere. */ |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9546 | buf = buflist_findname_exp(fnamebuf); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9547 | if (buf != NULL && buf->b_ml.ml_mfp == NULL) |
| 9548 | buf = NULL; |
| 9549 | if (buf != NULL && bufIsChanged(buf)) |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9550 | { |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9551 | EMSG(_(e_bufloaded)); |
| 9552 | return; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9553 | } |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9554 | |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9555 | fname = fnamebuf; |
| 9556 | } |
| 9557 | |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9558 | if (bad || undo) |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9559 | { |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9560 | /* When the word appears as good word we need to remove that one, |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9561 | * since its flags sort before the one with WF_BANNED. */ |
| 9562 | fd = mch_fopen((char *)fname, "r"); |
| 9563 | if (fd != NULL) |
| 9564 | { |
| 9565 | while (!vim_fgets(line, MAXWLEN * 2, fd)) |
| 9566 | { |
| 9567 | fpos = fpos_next; |
| 9568 | fpos_next = ftell(fd); |
| 9569 | if (STRNCMP(word, line, len) == 0 |
| 9570 | && (line[len] == '/' || line[len] < ' ')) |
| 9571 | { |
| 9572 | /* Found duplicate word. Remove it by writing a '#' at |
| 9573 | * the start of the line. Mixing reading and writing |
| 9574 | * doesn't work for all systems, close the file first. */ |
| 9575 | fclose(fd); |
| 9576 | fd = mch_fopen((char *)fname, "r+"); |
| 9577 | if (fd == NULL) |
| 9578 | break; |
| 9579 | if (fseek(fd, fpos, SEEK_SET) == 0) |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9580 | { |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9581 | fputc('#', fd); |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9582 | if (undo) |
| Bram Moolenaar | 2113a1d | 2006-09-11 19:38:08 +0000 | [diff] [blame] | 9583 | { |
| 9584 | home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE); |
| Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 9585 | smsg((char_u *)_("Word removed from %s"), NameBuff); |
| Bram Moolenaar | 2113a1d | 2006-09-11 19:38:08 +0000 | [diff] [blame] | 9586 | } |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9587 | } |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 9588 | fseek(fd, fpos_next, SEEK_SET); |
| 9589 | } |
| 9590 | } |
| 9591 | fclose(fd); |
| 9592 | } |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9593 | } |
| Bram Moolenaar | ac2adc7 | 2006-09-12 20:25:24 +0000 | [diff] [blame] | 9594 | |
| 9595 | if (!undo) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9596 | { |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9597 | fd = mch_fopen((char *)fname, "a"); |
| 9598 | if (fd == NULL && new_spf) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9599 | { |
| Bram Moolenaar | ac2adc7 | 2006-09-12 20:25:24 +0000 | [diff] [blame] | 9600 | char_u *p; |
| 9601 | |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9602 | /* We just initialized the 'spellfile' option and can't open the |
| 9603 | * file. We may need to create the "spell" directory first. We |
| 9604 | * already checked the runtime directory is writable in |
| 9605 | * init_spellfile(). */ |
| Bram Moolenaar | ac2adc7 | 2006-09-12 20:25:24 +0000 | [diff] [blame] | 9606 | if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname) |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9607 | { |
| Bram Moolenaar | ac2adc7 | 2006-09-12 20:25:24 +0000 | [diff] [blame] | 9608 | int c = *p; |
| 9609 | |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9610 | /* The directory doesn't exist. Try creating it and opening |
| 9611 | * the file again. */ |
| Bram Moolenaar | ac2adc7 | 2006-09-12 20:25:24 +0000 | [diff] [blame] | 9612 | *p = NUL; |
| 9613 | vim_mkdir(fname, 0755); |
| 9614 | *p = c; |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9615 | fd = mch_fopen((char *)fname, "a"); |
| 9616 | } |
| 9617 | } |
| 9618 | |
| 9619 | if (fd == NULL) |
| 9620 | EMSG2(_(e_notopen), fname); |
| 9621 | else |
| 9622 | { |
| 9623 | if (bad) |
| 9624 | fprintf(fd, "%.*s/!\n", len, word); |
| 9625 | else |
| 9626 | fprintf(fd, "%.*s\n", len, word); |
| 9627 | fclose(fd); |
| 9628 | |
| 9629 | home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE); |
| 9630 | smsg((char_u *)_("Word added to %s"), NameBuff); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9631 | } |
| 9632 | } |
| 9633 | |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 9634 | if (fd != NULL) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9635 | { |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9636 | /* Update the .add.spl file. */ |
| 9637 | mkspell(1, &fname, FALSE, TRUE, TRUE); |
| 9638 | |
| 9639 | /* If the .add file is edited somewhere, reload it. */ |
| 9640 | if (buf != NULL) |
| Bram Moolenaar | ea8bd73 | 2006-01-14 21:15:59 +0000 | [diff] [blame] | 9641 | buf_reload(buf, buf->b_orig_mode); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 9642 | |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 9643 | redraw_all_later(SOME_VALID); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9644 | } |
| 9645 | } |
| 9646 | |
| 9647 | /* |
| 9648 | * Initialize 'spellfile' for the current buffer. |
| 9649 | */ |
| 9650 | static void |
| 9651 | init_spellfile() |
| 9652 | { |
| 9653 | char_u buf[MAXPATHL]; |
| 9654 | int l; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 9655 | char_u *fname; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9656 | char_u *rtp; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9657 | char_u *lend; |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9658 | int aspath = FALSE; |
| 9659 | char_u *lstart = curbuf->b_p_spl; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9660 | |
| 9661 | if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0) |
| 9662 | { |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9663 | /* Find the end of the language name. Exclude the region. If there |
| 9664 | * is a path separator remember the start of the tail. */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9665 | for (lend = curbuf->b_p_spl; *lend != NUL |
| 9666 | && vim_strchr((char_u *)",._", *lend) == NULL; ++lend) |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9667 | if (vim_ispathsep(*lend)) |
| 9668 | { |
| 9669 | aspath = TRUE; |
| 9670 | lstart = lend + 1; |
| 9671 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 9672 | |
| 9673 | /* Loop over all entries in 'runtimepath'. Use the first one where we |
| 9674 | * are allowed to write. */ |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9675 | rtp = p_rtp; |
| 9676 | while (*rtp != NUL) |
| 9677 | { |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9678 | if (aspath) |
| 9679 | /* Use directory of an entry with path, e.g., for |
| 9680 | * "/dir/lg.utf-8.spl" use "/dir". */ |
| 9681 | vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1); |
| 9682 | else |
| 9683 | /* Copy the path from 'runtimepath' to buf[]. */ |
| 9684 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9685 | if (filewritable(buf) == 2) |
| 9686 | { |
| Bram Moolenaar | 3982c54 | 2005-06-08 21:56:31 +0000 | [diff] [blame] | 9687 | /* Use the first language name from 'spelllang' and the |
| 9688 | * encoding used in the first loaded .spl file. */ |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9689 | if (aspath) |
| 9690 | vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl); |
| 9691 | else |
| 9692 | { |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 9693 | /* Create the "spell" directory if it doesn't exist yet. */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9694 | l = (int)STRLEN(buf); |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 9695 | vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell"); |
| 9696 | if (!filewritable(buf) != 2) |
| 9697 | vim_mkdir(buf, 0755); |
| 9698 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9699 | l = (int)STRLEN(buf); |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9700 | vim_snprintf((char *)buf + l, MAXPATHL - l, |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 9701 | "/%.*s", (int)(lend - lstart), lstart); |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9702 | } |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 9703 | l = (int)STRLEN(buf); |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9704 | fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname; |
| 9705 | vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add", |
| 9706 | fname != NULL |
| 9707 | && strstr((char *)gettail(fname), ".ascii.") != NULL |
| 9708 | ? (char_u *)"ascii" : spell_enc()); |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9709 | set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL); |
| 9710 | break; |
| 9711 | } |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 9712 | aspath = FALSE; |
| Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 9713 | } |
| 9714 | } |
| 9715 | } |
| Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 9716 | |
| Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 9717 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9718 | /* |
| 9719 | * Init the chartab used for spelling for ASCII. |
| 9720 | * EBCDIC is not supported! |
| 9721 | */ |
| 9722 | static void |
| 9723 | clear_spell_chartab(sp) |
| 9724 | spelltab_T *sp; |
| 9725 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9726 | int i; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9727 | |
| 9728 | /* Init everything to FALSE. */ |
| 9729 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 9730 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 9731 | for (i = 0; i < 256; ++i) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9732 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9733 | sp->st_fold[i] = i; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9734 | sp->st_upper[i] = i; |
| 9735 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9736 | |
| 9737 | /* We include digits. A word shouldn't start with a digit, but handling |
| 9738 | * that is done separately. */ |
| 9739 | for (i = '0'; i <= '9'; ++i) |
| 9740 | sp->st_isw[i] = TRUE; |
| 9741 | for (i = 'A'; i <= 'Z'; ++i) |
| 9742 | { |
| 9743 | sp->st_isw[i] = TRUE; |
| 9744 | sp->st_isu[i] = TRUE; |
| 9745 | sp->st_fold[i] = i + 0x20; |
| 9746 | } |
| 9747 | for (i = 'a'; i <= 'z'; ++i) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9748 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9749 | sp->st_isw[i] = TRUE; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9750 | sp->st_upper[i] = i - 0x20; |
| 9751 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9752 | } |
| 9753 | |
| 9754 | /* |
| 9755 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 9756 | * Called once while starting up and when 'encoding' changes. |
| 9757 | * The default is to use isalpha(), but the spell file should define the word |
| 9758 | * characters to make it possible that 'encoding' differs from the current |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 9759 | * locale. For utf-8 we don't use isalpha() but our own functions. |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9760 | */ |
| 9761 | void |
| 9762 | init_spell_chartab() |
| 9763 | { |
| 9764 | int i; |
| 9765 | |
| 9766 | did_set_spelltab = FALSE; |
| 9767 | clear_spell_chartab(&spelltab); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9768 | #ifdef FEAT_MBYTE |
| 9769 | if (enc_dbcs) |
| 9770 | { |
| 9771 | /* DBCS: assume double-wide characters are word characters. */ |
| 9772 | for (i = 128; i <= 255; ++i) |
| 9773 | if (MB_BYTE2LEN(i) == 2) |
| 9774 | spelltab.st_isw[i] = TRUE; |
| 9775 | } |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9776 | else if (enc_utf8) |
| 9777 | { |
| 9778 | for (i = 128; i < 256; ++i) |
| 9779 | { |
| 9780 | spelltab.st_isu[i] = utf_isupper(i); |
| 9781 | spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); |
| 9782 | spelltab.st_fold[i] = utf_fold(i); |
| 9783 | spelltab.st_upper[i] = utf_toupper(i); |
| 9784 | } |
| 9785 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9786 | else |
| 9787 | #endif |
| 9788 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9789 | /* Rough guess: use locale-dependent library functions. */ |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9790 | for (i = 128; i < 256; ++i) |
| 9791 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9792 | if (MB_ISUPPER(i)) |
| 9793 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9794 | spelltab.st_isw[i] = TRUE; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9795 | spelltab.st_isu[i] = TRUE; |
| 9796 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 9797 | } |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9798 | else if (MB_ISLOWER(i)) |
| 9799 | { |
| 9800 | spelltab.st_isw[i] = TRUE; |
| 9801 | spelltab.st_upper[i] = MB_TOUPPER(i); |
| 9802 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9803 | } |
| 9804 | } |
| 9805 | } |
| 9806 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9807 | /* |
| 9808 | * Set the spell character tables from strings in the affix file. |
| 9809 | */ |
| 9810 | static int |
| 9811 | set_spell_chartab(fol, low, upp) |
| 9812 | char_u *fol; |
| 9813 | char_u *low; |
| 9814 | char_u *upp; |
| 9815 | { |
| 9816 | /* We build the new tables here first, so that we can compare with the |
| 9817 | * previous one. */ |
| 9818 | spelltab_T new_st; |
| 9819 | char_u *pf = fol, *pl = low, *pu = upp; |
| 9820 | int f, l, u; |
| 9821 | |
| 9822 | clear_spell_chartab(&new_st); |
| 9823 | |
| 9824 | while (*pf != NUL) |
| 9825 | { |
| 9826 | if (*pl == NUL || *pu == NUL) |
| 9827 | { |
| 9828 | EMSG(_(e_affform)); |
| 9829 | return FAIL; |
| 9830 | } |
| 9831 | #ifdef FEAT_MBYTE |
| 9832 | f = mb_ptr2char_adv(&pf); |
| 9833 | l = mb_ptr2char_adv(&pl); |
| 9834 | u = mb_ptr2char_adv(&pu); |
| 9835 | #else |
| 9836 | f = *pf++; |
| 9837 | l = *pl++; |
| 9838 | u = *pu++; |
| 9839 | #endif |
| 9840 | /* Every character that appears is a word character. */ |
| 9841 | if (f < 256) |
| 9842 | new_st.st_isw[f] = TRUE; |
| 9843 | if (l < 256) |
| 9844 | new_st.st_isw[l] = TRUE; |
| 9845 | if (u < 256) |
| 9846 | new_st.st_isw[u] = TRUE; |
| 9847 | |
| 9848 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 9849 | * case-folding */ |
| 9850 | if (l < 256 && l != f) |
| 9851 | { |
| 9852 | if (f >= 256) |
| 9853 | { |
| 9854 | EMSG(_(e_affrange)); |
| 9855 | return FAIL; |
| 9856 | } |
| 9857 | new_st.st_fold[l] = f; |
| 9858 | } |
| 9859 | |
| 9860 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9861 | * case-folding, it's upper case and the "UPP" is the upper case of |
| 9862 | * "FOL" . */ |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9863 | if (u < 256 && u != f) |
| 9864 | { |
| 9865 | if (f >= 256) |
| 9866 | { |
| 9867 | EMSG(_(e_affrange)); |
| 9868 | return FAIL; |
| 9869 | } |
| 9870 | new_st.st_fold[u] = f; |
| 9871 | new_st.st_isu[u] = TRUE; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9872 | new_st.st_upper[f] = u; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9873 | } |
| 9874 | } |
| 9875 | |
| 9876 | if (*pl != NUL || *pu != NUL) |
| 9877 | { |
| 9878 | EMSG(_(e_affform)); |
| 9879 | return FAIL; |
| 9880 | } |
| 9881 | |
| 9882 | return set_spell_finish(&new_st); |
| 9883 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9884 | |
| 9885 | /* |
| 9886 | * Set the spell character tables from strings in the .spl file. |
| 9887 | */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 9888 | static void |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9889 | set_spell_charflags(flags, cnt, fol) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9890 | char_u *flags; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9891 | int cnt; /* length of "flags" */ |
| 9892 | char_u *fol; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9893 | { |
| 9894 | /* We build the new tables here first, so that we can compare with the |
| 9895 | * previous one. */ |
| 9896 | spelltab_T new_st; |
| 9897 | int i; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9898 | char_u *p = fol; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9899 | int c; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9900 | |
| 9901 | clear_spell_chartab(&new_st); |
| 9902 | |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9903 | for (i = 0; i < 128; ++i) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9904 | { |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9905 | if (i < cnt) |
| 9906 | { |
| 9907 | new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0; |
| 9908 | new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0; |
| 9909 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9910 | |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9911 | if (*p != NUL) |
| 9912 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9913 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9914 | c = mb_ptr2char_adv(&p); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9915 | #else |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9916 | c = *p++; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9917 | #endif |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 9918 | new_st.st_fold[i + 128] = c; |
| 9919 | if (i + 128 != c && new_st.st_isu[i + 128] && c < 256) |
| 9920 | new_st.st_upper[c] = i + 128; |
| 9921 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9922 | } |
| 9923 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 9924 | (void)set_spell_finish(&new_st); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9925 | } |
| 9926 | |
| 9927 | static int |
| 9928 | set_spell_finish(new_st) |
| 9929 | spelltab_T *new_st; |
| 9930 | { |
| 9931 | int i; |
| 9932 | |
| 9933 | if (did_set_spelltab) |
| 9934 | { |
| 9935 | /* check that it's the same table */ |
| 9936 | for (i = 0; i < 256; ++i) |
| 9937 | { |
| 9938 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 9939 | || spelltab.st_isu[i] != new_st->st_isu[i] |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 9940 | || spelltab.st_fold[i] != new_st->st_fold[i] |
| 9941 | || spelltab.st_upper[i] != new_st->st_upper[i]) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9942 | { |
| 9943 | EMSG(_("E763: Word characters differ between spell files")); |
| 9944 | return FAIL; |
| 9945 | } |
| 9946 | } |
| 9947 | } |
| 9948 | else |
| 9949 | { |
| 9950 | /* copy the new spelltab into the one being used */ |
| 9951 | spelltab = *new_st; |
| 9952 | did_set_spelltab = TRUE; |
| 9953 | } |
| 9954 | |
| 9955 | return OK; |
| 9956 | } |
| 9957 | |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 9958 | /* |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 9959 | * Return TRUE if "p" points to a word character. |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9960 | * 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] | 9961 | * 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] | 9962 | * 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] | 9963 | */ |
| 9964 | static int |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9965 | spell_iswordp(p, buf) |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 9966 | char_u *p; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9967 | buf_T *buf; /* buffer used */ |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 9968 | { |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 9969 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9970 | char_u *s; |
| 9971 | int l; |
| 9972 | int c; |
| 9973 | |
| 9974 | if (has_mbyte) |
| 9975 | { |
| 9976 | l = MB_BYTE2LEN(*p); |
| 9977 | s = p; |
| 9978 | if (l == 1) |
| 9979 | { |
| 9980 | /* be quick for ASCII */ |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9981 | if (buf->b_spell_ismw[*p]) |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9982 | { |
| 9983 | s = p + 1; /* skip a mid-word character */ |
| 9984 | l = MB_BYTE2LEN(*s); |
| 9985 | } |
| 9986 | } |
| 9987 | else |
| 9988 | { |
| 9989 | c = mb_ptr2char(p); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 9990 | if (c < 256 ? buf->b_spell_ismw[c] |
| 9991 | : (buf->b_spell_ismw_mb != NULL |
| 9992 | && vim_strchr(buf->b_spell_ismw_mb, c) != NULL)) |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 9993 | { |
| 9994 | s = p + l; |
| 9995 | l = MB_BYTE2LEN(*s); |
| 9996 | } |
| 9997 | } |
| 9998 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 9999 | c = mb_ptr2char(s); |
| 10000 | if (c > 255) |
| Bram Moolenaar | 7a91a4a | 2008-04-09 13:49:57 +0000 | [diff] [blame] | 10001 | return spell_mb_isword_class(mb_get_class(s)); |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10002 | return spelltab.st_isw[c]; |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 10003 | } |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 10004 | #endif |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 10005 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10006 | return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]]; |
| 10007 | } |
| 10008 | |
| 10009 | /* |
| 10010 | * Return TRUE if "p" points to a word character. |
| 10011 | * Unlike spell_iswordp() this doesn't check for "midword" characters. |
| 10012 | */ |
| 10013 | static int |
| 10014 | spell_iswordp_nmw(p) |
| 10015 | char_u *p; |
| 10016 | { |
| 10017 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10018 | int c; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10019 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10020 | if (has_mbyte) |
| 10021 | { |
| 10022 | c = mb_ptr2char(p); |
| 10023 | if (c > 255) |
| Bram Moolenaar | 7a91a4a | 2008-04-09 13:49:57 +0000 | [diff] [blame] | 10024 | return spell_mb_isword_class(mb_get_class(p)); |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10025 | return spelltab.st_isw[c]; |
| 10026 | } |
| 10027 | #endif |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10028 | return spelltab.st_isw[*p]; |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 10029 | } |
| 10030 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10031 | #ifdef FEAT_MBYTE |
| 10032 | /* |
| Bram Moolenaar | 7a91a4a | 2008-04-09 13:49:57 +0000 | [diff] [blame] | 10033 | * Return TRUE if word class indicates a word character. |
| 10034 | * Only for characters above 255. |
| 10035 | * Unicode subscript and superscript are not considered word characters. |
| 10036 | */ |
| 10037 | static int |
| 10038 | spell_mb_isword_class(cl) |
| 10039 | int cl; |
| 10040 | { |
| 10041 | return cl >= 2 && cl != 0x2070 && cl != 0x2080; |
| 10042 | } |
| 10043 | |
| 10044 | /* |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10045 | * Return TRUE if "p" points to a word character. |
| 10046 | * Wide version of spell_iswordp(). |
| 10047 | */ |
| 10048 | static int |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10049 | spell_iswordp_w(p, buf) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10050 | int *p; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10051 | buf_T *buf; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10052 | { |
| 10053 | int *s; |
| 10054 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 10055 | if (*p < 256 ? buf->b_spell_ismw[*p] |
| 10056 | : (buf->b_spell_ismw_mb != NULL |
| 10057 | && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL)) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10058 | s = p + 1; |
| 10059 | else |
| 10060 | s = p; |
| 10061 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10062 | if (*s > 255) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10063 | { |
| 10064 | if (enc_utf8) |
| Bram Moolenaar | 7a91a4a | 2008-04-09 13:49:57 +0000 | [diff] [blame] | 10065 | return spell_mb_isword_class(utf_class(*s)); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10066 | if (enc_dbcs) |
| 10067 | return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2; |
| 10068 | return 0; |
| 10069 | } |
| 10070 | return spelltab.st_isw[*s]; |
| 10071 | } |
| 10072 | #endif |
| 10073 | |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 10074 | /* |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10075 | * Write the table with prefix conditions to the .spl file. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10076 | * When "fd" is NULL only count the length of what is written. |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10077 | */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10078 | static int |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10079 | write_spell_prefcond(fd, gap) |
| 10080 | FILE *fd; |
| 10081 | garray_T *gap; |
| 10082 | { |
| 10083 | int i; |
| 10084 | char_u *p; |
| 10085 | int len; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10086 | int totlen; |
| Bram Moolenaar | 2eb6eb3 | 2008-11-29 19:19:19 +0000 | [diff] [blame] | 10087 | size_t x = 1; /* collect return value of fwrite() */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10088 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10089 | if (fd != NULL) |
| 10090 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */ |
| 10091 | |
| 10092 | totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */ |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10093 | |
| 10094 | for (i = 0; i < gap->ga_len; ++i) |
| 10095 | { |
| 10096 | /* <prefcond> : <condlen> <condstr> */ |
| 10097 | p = ((char_u **)gap->ga_data)[i]; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10098 | if (p != NULL) |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10099 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10100 | len = (int)STRLEN(p); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10101 | if (fd != NULL) |
| 10102 | { |
| 10103 | fputc(len, fd); |
| Bram Moolenaar | 3f3766b | 2008-11-28 09:08:51 +0000 | [diff] [blame] | 10104 | x &= fwrite(p, (size_t)len, (size_t)1, fd); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10105 | } |
| 10106 | totlen += len; |
| Bram Moolenaar | 1d73c88 | 2005-06-19 22:48:47 +0000 | [diff] [blame] | 10107 | } |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10108 | else if (fd != NULL) |
| 10109 | fputc(0, fd); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10110 | } |
| 10111 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10112 | return totlen; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10113 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10114 | |
| 10115 | /* |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10116 | * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
| 10117 | * Uses the character definitions from the .spl file. |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10118 | * When using a multi-byte 'encoding' the length may change! |
| 10119 | * Returns FAIL when something wrong. |
| 10120 | */ |
| 10121 | static int |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10122 | spell_casefold(str, len, buf, buflen) |
| 10123 | char_u *str; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10124 | int len; |
| 10125 | char_u *buf; |
| 10126 | int buflen; |
| 10127 | { |
| 10128 | int i; |
| 10129 | |
| 10130 | if (len >= buflen) |
| 10131 | { |
| 10132 | buf[0] = NUL; |
| 10133 | return FAIL; /* result will not fit */ |
| 10134 | } |
| 10135 | |
| 10136 | #ifdef FEAT_MBYTE |
| 10137 | if (has_mbyte) |
| 10138 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10139 | int outi = 0; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10140 | char_u *p; |
| 10141 | int c; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10142 | |
| 10143 | /* Fold one character at a time. */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10144 | for (p = str; p < str + len; ) |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10145 | { |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10146 | if (outi + MB_MAXBYTES > buflen) |
| 10147 | { |
| 10148 | buf[outi] = NUL; |
| 10149 | return FAIL; |
| 10150 | } |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10151 | c = mb_cptr2char_adv(&p); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10152 | outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10153 | } |
| 10154 | buf[outi] = NUL; |
| 10155 | } |
| 10156 | else |
| 10157 | #endif |
| 10158 | { |
| 10159 | /* Be quick for non-multibyte encodings. */ |
| 10160 | for (i = 0; i < len; ++i) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10161 | buf[i] = spelltab.st_fold[str[i]]; |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 10162 | buf[i] = NUL; |
| 10163 | } |
| 10164 | |
| 10165 | return OK; |
| 10166 | } |
| 10167 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10168 | /* values for sps_flags */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10169 | #define SPS_BEST 1 |
| 10170 | #define SPS_FAST 2 |
| 10171 | #define SPS_DOUBLE 4 |
| 10172 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10173 | static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */ |
| 10174 | static int sps_limit = 9999; /* max nr of suggestions given */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10175 | |
| 10176 | /* |
| 10177 | * Check the 'spellsuggest' option. Return FAIL if it's wrong. |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10178 | * Sets "sps_flags" and "sps_limit". |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10179 | */ |
| 10180 | int |
| 10181 | spell_check_sps() |
| 10182 | { |
| 10183 | char_u *p; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10184 | char_u *s; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10185 | char_u buf[MAXPATHL]; |
| 10186 | int f; |
| 10187 | |
| 10188 | sps_flags = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10189 | sps_limit = 9999; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10190 | |
| 10191 | for (p = p_sps; *p != NUL; ) |
| 10192 | { |
| 10193 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 10194 | |
| 10195 | f = 0; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10196 | if (VIM_ISDIGIT(*buf)) |
| 10197 | { |
| 10198 | s = buf; |
| 10199 | sps_limit = getdigits(&s); |
| 10200 | if (*s != NUL && !VIM_ISDIGIT(*s)) |
| 10201 | f = -1; |
| 10202 | } |
| 10203 | else if (STRCMP(buf, "best") == 0) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10204 | f = SPS_BEST; |
| 10205 | else if (STRCMP(buf, "fast") == 0) |
| 10206 | f = SPS_FAST; |
| 10207 | else if (STRCMP(buf, "double") == 0) |
| 10208 | f = SPS_DOUBLE; |
| 10209 | else if (STRNCMP(buf, "expr:", 5) != 0 |
| 10210 | && STRNCMP(buf, "file:", 5) != 0) |
| 10211 | f = -1; |
| 10212 | |
| 10213 | if (f == -1 || (sps_flags != 0 && f != 0)) |
| 10214 | { |
| 10215 | sps_flags = SPS_BEST; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10216 | sps_limit = 9999; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10217 | return FAIL; |
| 10218 | } |
| 10219 | if (f != 0) |
| 10220 | sps_flags = f; |
| 10221 | } |
| 10222 | |
| 10223 | if (sps_flags == 0) |
| 10224 | sps_flags = SPS_BEST; |
| 10225 | |
| 10226 | return OK; |
| 10227 | } |
| 10228 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10229 | /* |
| 10230 | * "z?": Find badly spelled word under or after the cursor. |
| 10231 | * Give suggestions for the properly spelled word. |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10232 | * In Visual mode use the highlighted word as the bad word. |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10233 | * When "count" is non-zero use that suggestion. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10234 | */ |
| 10235 | void |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10236 | spell_suggest(count) |
| 10237 | int count; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10238 | { |
| 10239 | char_u *line; |
| 10240 | pos_T prev_cursor = curwin->w_cursor; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10241 | char_u wcopy[MAXWLEN + 2]; |
| 10242 | char_u *p; |
| 10243 | int i; |
| 10244 | int c; |
| 10245 | suginfo_T sug; |
| 10246 | suggest_T *stp; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10247 | int mouse_used; |
| Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 10248 | int need_cap; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10249 | int limit; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10250 | int selected = count; |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10251 | int badlen = 0; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10252 | |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10253 | if (no_spell_checking(curwin)) |
| 10254 | return; |
| 10255 | |
| 10256 | #ifdef FEAT_VISUAL |
| 10257 | if (VIsual_active) |
| 10258 | { |
| 10259 | /* Use the Visually selected text as the bad word. But reject |
| 10260 | * a multi-line selection. */ |
| 10261 | if (curwin->w_cursor.lnum != VIsual.lnum) |
| 10262 | { |
| 10263 | vim_beep(); |
| 10264 | return; |
| 10265 | } |
| 10266 | badlen = (int)curwin->w_cursor.col - (int)VIsual.col; |
| 10267 | if (badlen < 0) |
| 10268 | badlen = -badlen; |
| 10269 | else |
| 10270 | curwin->w_cursor.col = VIsual.col; |
| 10271 | ++badlen; |
| 10272 | end_visual_mode(); |
| 10273 | } |
| 10274 | else |
| 10275 | #endif |
| 10276 | /* Find the start of the badly spelled word. */ |
| 10277 | if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0 |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10278 | || curwin->w_cursor.col > prev_cursor.col) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10279 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10280 | /* No bad word or it starts after the cursor: use the word under the |
| 10281 | * cursor. */ |
| 10282 | curwin->w_cursor = prev_cursor; |
| 10283 | line = ml_get_curline(); |
| 10284 | p = line + curwin->w_cursor.col; |
| 10285 | /* Backup to before start of word. */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10286 | while (p > line && spell_iswordp_nmw(p)) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10287 | mb_ptr_back(line, p); |
| 10288 | /* Forward to start of word. */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10289 | while (*p != NUL && !spell_iswordp_nmw(p)) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10290 | mb_ptr_adv(p); |
| 10291 | |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 10292 | if (!spell_iswordp_nmw(p)) /* No word found. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10293 | { |
| 10294 | beep_flush(); |
| 10295 | return; |
| 10296 | } |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10297 | curwin->w_cursor.col = (colnr_T)(p - line); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10298 | } |
| 10299 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10300 | /* Get the word and its length. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10301 | |
| Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 10302 | /* Figure out if the word should be capitalised. */ |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 10303 | need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 10304 | |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 10305 | line = ml_get_curline(); |
| Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 10306 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10307 | /* Get the list of suggestions. Limit to 'lines' - 2 or the number in |
| 10308 | * 'spellsuggest', whatever is smaller. */ |
| 10309 | if (sps_limit > (int)Rows - 2) |
| 10310 | limit = (int)Rows - 2; |
| 10311 | else |
| 10312 | limit = sps_limit; |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10313 | spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10314 | TRUE, need_cap, TRUE); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10315 | |
| 10316 | if (sug.su_ga.ga_len == 0) |
| 10317 | MSG(_("Sorry, no suggestions")); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10318 | else if (count > 0) |
| 10319 | { |
| 10320 | if (count > sug.su_ga.ga_len) |
| 10321 | smsg((char_u *)_("Sorry, only %ld suggestions"), |
| 10322 | (long)sug.su_ga.ga_len); |
| 10323 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10324 | else |
| 10325 | { |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10326 | vim_free(repl_from); |
| 10327 | repl_from = NULL; |
| 10328 | vim_free(repl_to); |
| 10329 | repl_to = NULL; |
| 10330 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10331 | #ifdef FEAT_RIGHTLEFT |
| 10332 | /* When 'rightleft' is set the list is drawn right-left. */ |
| 10333 | cmdmsg_rl = curwin->w_p_rl; |
| 10334 | if (cmdmsg_rl) |
| 10335 | msg_col = Columns - 1; |
| 10336 | #endif |
| 10337 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10338 | /* List the suggestions. */ |
| 10339 | msg_start(); |
| Bram Moolenaar | 412f744 | 2006-07-23 19:51:57 +0000 | [diff] [blame] | 10340 | msg_row = Rows - 1; /* for when 'cmdheight' > 1 */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10341 | lines_left = Rows; /* avoid more prompt */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10342 | vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
| 10343 | sug.su_badlen, sug.su_badptr); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10344 | #ifdef FEAT_RIGHTLEFT |
| 10345 | if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0) |
| 10346 | { |
| 10347 | /* And now the rabbit from the high hat: Avoid showing the |
| 10348 | * untranslated message rightleft. */ |
| 10349 | vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC", |
| 10350 | sug.su_badlen, sug.su_badptr); |
| 10351 | } |
| 10352 | #endif |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10353 | msg_puts(IObuff); |
| 10354 | msg_clr_eos(); |
| 10355 | msg_putchar('\n'); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10356 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10357 | msg_scroll = TRUE; |
| 10358 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 10359 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10360 | stp = &SUG(sug.su_ga, i); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10361 | |
| 10362 | /* The suggested word may replace only part of the bad word, add |
| 10363 | * the not replaced part. */ |
| 10364 | STRCPY(wcopy, stp->st_word); |
| 10365 | if (sug.su_badlen > stp->st_orglen) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10366 | vim_strncpy(wcopy + stp->st_wordlen, |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10367 | sug.su_badptr + stp->st_orglen, |
| 10368 | sug.su_badlen - stp->st_orglen); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10369 | vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1); |
| 10370 | #ifdef FEAT_RIGHTLEFT |
| 10371 | if (cmdmsg_rl) |
| 10372 | rl_mirror(IObuff); |
| 10373 | #endif |
| 10374 | msg_puts(IObuff); |
| 10375 | |
| 10376 | vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10377 | msg_puts(IObuff); |
| 10378 | |
| 10379 | /* The word may replace more than "su_badlen". */ |
| 10380 | if (sug.su_badlen < stp->st_orglen) |
| 10381 | { |
| 10382 | vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), |
| 10383 | stp->st_orglen, sug.su_badptr); |
| 10384 | msg_puts(IObuff); |
| 10385 | } |
| 10386 | |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 10387 | if (p_verbose > 0) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10388 | { |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10389 | /* Add the score. */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 10390 | if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10391 | vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)", |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10392 | stp->st_salscore ? "s " : "", |
| 10393 | stp->st_score, stp->st_altscore); |
| 10394 | else |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10395 | vim_snprintf((char *)IObuff, IOSIZE, " (%d)", |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10396 | stp->st_score); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10397 | #ifdef FEAT_RIGHTLEFT |
| 10398 | if (cmdmsg_rl) |
| 10399 | /* Mirror the numbers, but keep the leading space. */ |
| 10400 | rl_mirror(IObuff + 1); |
| 10401 | #endif |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10402 | msg_advance(30); |
| 10403 | msg_puts(IObuff); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10404 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10405 | msg_putchar('\n'); |
| 10406 | } |
| 10407 | |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10408 | #ifdef FEAT_RIGHTLEFT |
| 10409 | cmdmsg_rl = FALSE; |
| 10410 | msg_col = 0; |
| 10411 | #endif |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10412 | /* Ask for choice. */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10413 | selected = prompt_for_number(&mouse_used); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10414 | if (mouse_used) |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10415 | selected -= lines_left; |
| Bram Moolenaar | 0fd9289 | 2006-03-09 22:27:48 +0000 | [diff] [blame] | 10416 | lines_left = Rows; /* avoid more prompt */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 10417 | } |
| 10418 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10419 | if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK) |
| 10420 | { |
| 10421 | /* Save the from and to text for :spellrepall. */ |
| 10422 | stp = &SUG(sug.su_ga, selected - 1); |
| Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 10423 | if (sug.su_badlen > stp->st_orglen) |
| 10424 | { |
| 10425 | /* Replacing less than "su_badlen", append the remainder to |
| 10426 | * repl_to. */ |
| 10427 | repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen); |
| 10428 | vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word, |
| 10429 | sug.su_badlen - stp->st_orglen, |
| 10430 | sug.su_badptr + stp->st_orglen); |
| 10431 | repl_to = vim_strsave(IObuff); |
| 10432 | } |
| 10433 | else |
| 10434 | { |
| 10435 | /* Replacing su_badlen or more, use the whole word. */ |
| 10436 | repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); |
| 10437 | repl_to = vim_strsave(stp->st_word); |
| 10438 | } |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10439 | |
| 10440 | /* Replace the word. */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10441 | p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10442 | if (p != NULL) |
| 10443 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10444 | c = (int)(sug.su_badptr - line); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10445 | mch_memmove(p, line, c); |
| 10446 | STRCPY(p + c, stp->st_word); |
| 10447 | STRCAT(p, sug.su_badptr + stp->st_orglen); |
| 10448 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 10449 | curwin->w_cursor.col = c; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10450 | |
| 10451 | /* For redo we use a change-word command. */ |
| 10452 | ResetRedobuff(); |
| 10453 | AppendToRedobuff((char_u *)"ciw"); |
| Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 10454 | AppendToRedobuffLit(p + c, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10455 | stp->st_wordlen + sug.su_badlen - stp->st_orglen); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10456 | AppendCharToRedobuff(ESC); |
| Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 10457 | |
| 10458 | /* After this "p" may be invalid. */ |
| 10459 | changed_bytes(curwin->w_cursor.lnum, c); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 10460 | } |
| 10461 | } |
| 10462 | else |
| 10463 | curwin->w_cursor = prev_cursor; |
| 10464 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10465 | spell_find_cleanup(&sug); |
| 10466 | } |
| 10467 | |
| 10468 | /* |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 10469 | * Check if the word at line "lnum" column "col" is required to start with a |
| 10470 | * capital. This uses 'spellcapcheck' of the current buffer. |
| 10471 | */ |
| 10472 | static int |
| 10473 | check_need_cap(lnum, col) |
| 10474 | linenr_T lnum; |
| 10475 | colnr_T col; |
| 10476 | { |
| 10477 | int need_cap = FALSE; |
| 10478 | char_u *line; |
| 10479 | char_u *line_copy = NULL; |
| 10480 | char_u *p; |
| 10481 | colnr_T endcol; |
| 10482 | regmatch_T regmatch; |
| 10483 | |
| 10484 | if (curbuf->b_cap_prog == NULL) |
| 10485 | return FALSE; |
| 10486 | |
| 10487 | line = ml_get_curline(); |
| 10488 | endcol = 0; |
| 10489 | if ((int)(skipwhite(line) - line) >= (int)col) |
| 10490 | { |
| 10491 | /* At start of line, check if previous line is empty or sentence |
| 10492 | * ends there. */ |
| 10493 | if (lnum == 1) |
| 10494 | need_cap = TRUE; |
| 10495 | else |
| 10496 | { |
| 10497 | line = ml_get(lnum - 1); |
| 10498 | if (*skipwhite(line) == NUL) |
| 10499 | need_cap = TRUE; |
| 10500 | else |
| 10501 | { |
| 10502 | /* Append a space in place of the line break. */ |
| 10503 | line_copy = concat_str(line, (char_u *)" "); |
| 10504 | line = line_copy; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10505 | endcol = (colnr_T)STRLEN(line); |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 10506 | } |
| 10507 | } |
| 10508 | } |
| 10509 | else |
| 10510 | endcol = col; |
| 10511 | |
| 10512 | if (endcol > 0) |
| 10513 | { |
| 10514 | /* Check if sentence ends before the bad word. */ |
| 10515 | regmatch.regprog = curbuf->b_cap_prog; |
| 10516 | regmatch.rm_ic = FALSE; |
| 10517 | p = line + endcol; |
| 10518 | for (;;) |
| 10519 | { |
| 10520 | mb_ptr_back(line, p); |
| 10521 | if (p == line || spell_iswordp_nmw(p)) |
| 10522 | break; |
| 10523 | if (vim_regexec(®match, p, 0) |
| 10524 | && regmatch.endp[0] == line + endcol) |
| 10525 | { |
| 10526 | need_cap = TRUE; |
| 10527 | break; |
| 10528 | } |
| 10529 | } |
| 10530 | } |
| 10531 | |
| 10532 | vim_free(line_copy); |
| 10533 | |
| 10534 | return need_cap; |
| 10535 | } |
| 10536 | |
| 10537 | |
| 10538 | /* |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10539 | * ":spellrepall" |
| 10540 | */ |
| 10541 | /*ARGSUSED*/ |
| 10542 | void |
| 10543 | ex_spellrepall(eap) |
| 10544 | exarg_T *eap; |
| 10545 | { |
| 10546 | pos_T pos = curwin->w_cursor; |
| 10547 | char_u *frompat; |
| 10548 | int addlen; |
| 10549 | char_u *line; |
| 10550 | char_u *p; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10551 | int save_ws = p_ws; |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10552 | linenr_T prev_lnum = 0; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10553 | |
| 10554 | if (repl_from == NULL || repl_to == NULL) |
| 10555 | { |
| 10556 | EMSG(_("E752: No previous spell replacement")); |
| 10557 | return; |
| 10558 | } |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10559 | addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from)); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10560 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10561 | frompat = alloc((unsigned)STRLEN(repl_from) + 7); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10562 | if (frompat == NULL) |
| 10563 | return; |
| 10564 | sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); |
| 10565 | p_ws = FALSE; |
| 10566 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10567 | sub_nsubs = 0; |
| 10568 | sub_nlines = 0; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10569 | curwin->w_cursor.lnum = 0; |
| 10570 | while (!got_int) |
| 10571 | { |
| Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 10572 | if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0 |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10573 | || u_save_cursor() == FAIL) |
| 10574 | break; |
| 10575 | |
| 10576 | /* Only replace when the right word isn't there yet. This happens |
| 10577 | * when changing "etc" to "etc.". */ |
| 10578 | line = ml_get_curline(); |
| 10579 | if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, |
| 10580 | repl_to, STRLEN(repl_to)) != 0) |
| 10581 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10582 | p = alloc((unsigned)STRLEN(line) + addlen + 1); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10583 | if (p == NULL) |
| 10584 | break; |
| 10585 | mch_memmove(p, line, curwin->w_cursor.col); |
| 10586 | STRCPY(p + curwin->w_cursor.col, repl_to); |
| 10587 | STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); |
| 10588 | ml_replace(curwin->w_cursor.lnum, p, FALSE); |
| 10589 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10590 | |
| 10591 | if (curwin->w_cursor.lnum != prev_lnum) |
| 10592 | { |
| 10593 | ++sub_nlines; |
| 10594 | prev_lnum = curwin->w_cursor.lnum; |
| 10595 | } |
| 10596 | ++sub_nsubs; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10597 | } |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10598 | curwin->w_cursor.col += (colnr_T)STRLEN(repl_to); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10599 | } |
| 10600 | |
| 10601 | p_ws = save_ws; |
| 10602 | curwin->w_cursor = pos; |
| 10603 | vim_free(frompat); |
| 10604 | |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10605 | if (sub_nsubs == 0) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10606 | EMSG2(_("E753: Not found: %s"), repl_from); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 10607 | else |
| 10608 | do_sub_msg(FALSE); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10609 | } |
| 10610 | |
| 10611 | /* |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10612 | * Find spell suggestions for "word". Return them in the growarray "*gap" as |
| 10613 | * a list of allocated strings. |
| 10614 | */ |
| 10615 | void |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10616 | spell_suggest_list(gap, word, maxcount, need_cap, interactive) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10617 | garray_T *gap; |
| 10618 | char_u *word; |
| 10619 | int maxcount; /* maximum nr of suggestions */ |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 10620 | int need_cap; /* 'spellcapcheck' matched */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10621 | int interactive; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10622 | { |
| 10623 | suginfo_T sug; |
| 10624 | int i; |
| 10625 | suggest_T *stp; |
| 10626 | char_u *wcopy; |
| 10627 | |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10628 | spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10629 | |
| 10630 | /* Make room in "gap". */ |
| 10631 | ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 10632 | if (ga_grow(gap, sug.su_ga.ga_len) == OK) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10633 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 10634 | for (i = 0; i < sug.su_ga.ga_len; ++i) |
| 10635 | { |
| 10636 | stp = &SUG(sug.su_ga, i); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10637 | |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 10638 | /* The suggested word may replace only part of "word", add the not |
| 10639 | * replaced part. */ |
| 10640 | wcopy = alloc(stp->st_wordlen |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 10641 | + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 10642 | if (wcopy == NULL) |
| 10643 | break; |
| 10644 | STRCPY(wcopy, stp->st_word); |
| 10645 | STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen); |
| 10646 | ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; |
| 10647 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10648 | } |
| 10649 | |
| 10650 | spell_find_cleanup(&sug); |
| 10651 | } |
| 10652 | |
| 10653 | /* |
| 10654 | * Find spell suggestions for the word at the start of "badptr". |
| 10655 | * Return the suggestions in "su->su_ga". |
| 10656 | * The maximum number of suggestions is "maxcount". |
| 10657 | * Note: does use info for the current window. |
| 10658 | * This is based on the mechanisms of Aspell, but completely reimplemented. |
| 10659 | */ |
| 10660 | static void |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10661 | spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10662 | char_u *badptr; |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10663 | int badlen; /* length of bad word or 0 if unknown */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10664 | suginfo_T *su; |
| 10665 | int maxcount; |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 10666 | int banbadword; /* don't include badword in suggestions */ |
| Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 10667 | int need_cap; /* word should start with capital */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10668 | int interactive; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10669 | { |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 10670 | hlf_T attr = HLF_COUNT; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10671 | char_u buf[MAXPATHL]; |
| 10672 | char_u *p; |
| 10673 | int do_combine = FALSE; |
| 10674 | char_u *sps_copy; |
| 10675 | #ifdef FEAT_EVAL |
| 10676 | static int expr_busy = FALSE; |
| 10677 | #endif |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 10678 | int c; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 10679 | int i; |
| 10680 | langp_T *lp; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10681 | |
| 10682 | /* |
| 10683 | * Set the info in "*su". |
| 10684 | */ |
| 10685 | vim_memset(su, 0, sizeof(suginfo_T)); |
| 10686 | ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); |
| 10687 | ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 10688 | if (*badptr == NUL) |
| 10689 | return; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10690 | hash_init(&su->su_banned); |
| 10691 | |
| 10692 | su->su_badptr = badptr; |
| Bram Moolenaar | 66fa271 | 2006-01-22 23:22:22 +0000 | [diff] [blame] | 10693 | if (badlen != 0) |
| 10694 | su->su_badlen = badlen; |
| 10695 | else |
| 10696 | su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10697 | su->su_maxcount = maxcount; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10698 | su->su_maxscore = SCORE_MAXINIT; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10699 | |
| 10700 | if (su->su_badlen >= MAXWLEN) |
| 10701 | su->su_badlen = MAXWLEN - 1; /* just in case */ |
| 10702 | vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); |
| 10703 | (void)spell_casefold(su->su_badptr, su->su_badlen, |
| 10704 | su->su_fbadword, MAXWLEN); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10705 | /* get caps flags for bad word */ |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10706 | su->su_badflags = badword_captype(su->su_badptr, |
| 10707 | su->su_badptr + su->su_badlen); |
| Bram Moolenaar | 7d1f5db | 2005-07-03 21:39:27 +0000 | [diff] [blame] | 10708 | if (need_cap) |
| 10709 | su->su_badflags |= WF_ONECAP; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10710 | |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 10711 | /* Find the default language for sound folding. We simply use the first |
| 10712 | * one in 'spelllang' that supports sound folding. That's good for when |
| 10713 | * using multiple files for one language, it's not that bad when mixing |
| 10714 | * languages (e.g., "pl,en"). */ |
| 10715 | for (i = 0; i < curbuf->b_langp.ga_len; ++i) |
| 10716 | { |
| 10717 | lp = LANGP_ENTRY(curbuf->b_langp, i); |
| 10718 | if (lp->lp_sallang != NULL) |
| 10719 | { |
| 10720 | su->su_sallang = lp->lp_sallang; |
| 10721 | break; |
| 10722 | } |
| 10723 | } |
| 10724 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 10725 | /* Soundfold the bad word with the default sound folding, so that we don't |
| 10726 | * have to do this many times. */ |
| 10727 | if (su->su_sallang != NULL) |
| 10728 | spell_soundfold(su->su_sallang, su->su_fbadword, TRUE, |
| 10729 | su->su_sal_badword); |
| 10730 | |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 10731 | /* If the word is not capitalised and spell_check() doesn't consider the |
| 10732 | * word to be bad then it might need to be capitalised. Add a suggestion |
| 10733 | * for that. */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 10734 | c = PTR2CHAR(su->su_badptr); |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 10735 | if (!SPELL_ISUPPER(c) && attr == HLF_COUNT) |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 10736 | { |
| 10737 | make_case_word(su->su_badword, buf, WF_ONECAP); |
| 10738 | add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10739 | 0, TRUE, su->su_sallang, FALSE); |
| Bram Moolenaar | f9184a1 | 2005-07-02 23:10:47 +0000 | [diff] [blame] | 10740 | } |
| 10741 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10742 | /* Ban the bad word itself. It may appear in another region. */ |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 10743 | if (banbadword) |
| 10744 | add_banned(su, su->su_badword); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10745 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10746 | /* Make a copy of 'spellsuggest', because the expression may change it. */ |
| 10747 | sps_copy = vim_strsave(p_sps); |
| 10748 | if (sps_copy == NULL) |
| 10749 | return; |
| 10750 | |
| 10751 | /* Loop over the items in 'spellsuggest'. */ |
| 10752 | for (p = sps_copy; *p != NUL; ) |
| 10753 | { |
| 10754 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 10755 | |
| 10756 | if (STRNCMP(buf, "expr:", 5) == 0) |
| 10757 | { |
| 10758 | #ifdef FEAT_EVAL |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 10759 | /* Evaluate an expression. Skip this when called recursively, |
| 10760 | * when using spellsuggest() in the expression. */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10761 | if (!expr_busy) |
| 10762 | { |
| 10763 | expr_busy = TRUE; |
| 10764 | spell_suggest_expr(su, buf + 5); |
| 10765 | expr_busy = FALSE; |
| 10766 | } |
| 10767 | #endif |
| 10768 | } |
| 10769 | else if (STRNCMP(buf, "file:", 5) == 0) |
| 10770 | /* Use list of suggestions in a file. */ |
| 10771 | spell_suggest_file(su, buf + 5); |
| 10772 | else |
| 10773 | { |
| 10774 | /* Use internal method. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10775 | spell_suggest_intern(su, interactive); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10776 | if (sps_flags & SPS_DOUBLE) |
| 10777 | do_combine = TRUE; |
| 10778 | } |
| 10779 | } |
| 10780 | |
| 10781 | vim_free(sps_copy); |
| 10782 | |
| 10783 | if (do_combine) |
| 10784 | /* Combine the two list of suggestions. This must be done last, |
| 10785 | * because sorting changes the order again. */ |
| 10786 | score_combine(su); |
| 10787 | } |
| 10788 | |
| 10789 | #ifdef FEAT_EVAL |
| 10790 | /* |
| 10791 | * Find suggestions by evaluating expression "expr". |
| 10792 | */ |
| 10793 | static void |
| 10794 | spell_suggest_expr(su, expr) |
| 10795 | suginfo_T *su; |
| 10796 | char_u *expr; |
| 10797 | { |
| 10798 | list_T *list; |
| 10799 | listitem_T *li; |
| 10800 | int score; |
| 10801 | char_u *p; |
| 10802 | |
| 10803 | /* The work is split up in a few parts to avoid having to export |
| 10804 | * suginfo_T. |
| 10805 | * First evaluate the expression and get the resulting list. */ |
| 10806 | list = eval_spell_expr(su->su_badword, expr); |
| 10807 | if (list != NULL) |
| 10808 | { |
| 10809 | /* Loop over the items in the list. */ |
| 10810 | for (li = list->lv_first; li != NULL; li = li->li_next) |
| 10811 | if (li->li_tv.v_type == VAR_LIST) |
| 10812 | { |
| 10813 | /* Get the word and the score from the items. */ |
| 10814 | score = get_spellword(li->li_tv.vval.v_list, &p); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10815 | if (score >= 0 && score <= su->su_maxscore) |
| 10816 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| 10817 | score, 0, TRUE, su->su_sallang, FALSE); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10818 | } |
| 10819 | list_unref(list); |
| 10820 | } |
| 10821 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10822 | /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
| 10823 | check_suggestions(su, &su->su_ga); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10824 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 10825 | } |
| 10826 | #endif |
| 10827 | |
| 10828 | /* |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10829 | * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'. |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10830 | */ |
| 10831 | static void |
| 10832 | spell_suggest_file(su, fname) |
| 10833 | suginfo_T *su; |
| 10834 | char_u *fname; |
| 10835 | { |
| 10836 | FILE *fd; |
| 10837 | char_u line[MAXWLEN * 2]; |
| 10838 | char_u *p; |
| 10839 | int len; |
| 10840 | char_u cword[MAXWLEN]; |
| 10841 | |
| 10842 | /* Open the file. */ |
| 10843 | fd = mch_fopen((char *)fname, "r"); |
| 10844 | if (fd == NULL) |
| 10845 | { |
| 10846 | EMSG2(_(e_notopen), fname); |
| 10847 | return; |
| 10848 | } |
| 10849 | |
| 10850 | /* Read it line by line. */ |
| 10851 | while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) |
| 10852 | { |
| 10853 | line_breakcheck(); |
| 10854 | |
| 10855 | p = vim_strchr(line, '/'); |
| 10856 | if (p == NULL) |
| 10857 | continue; /* No Tab found, just skip the line. */ |
| 10858 | *p++ = NUL; |
| 10859 | if (STRICMP(su->su_badword, line) == 0) |
| 10860 | { |
| 10861 | /* Match! Isolate the good word, until CR or NL. */ |
| 10862 | for (len = 0; p[len] >= ' '; ++len) |
| 10863 | ; |
| 10864 | p[len] = NUL; |
| 10865 | |
| 10866 | /* If the suggestion doesn't have specific case duplicate the case |
| 10867 | * of the bad word. */ |
| 10868 | if (captype(p, NULL) == 0) |
| 10869 | { |
| 10870 | make_case_word(p, cword, su->su_badflags); |
| 10871 | p = cword; |
| 10872 | } |
| 10873 | |
| 10874 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10875 | SCORE_FILE, 0, TRUE, su->su_sallang, FALSE); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10876 | } |
| 10877 | } |
| 10878 | |
| 10879 | fclose(fd); |
| 10880 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10881 | /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
| 10882 | check_suggestions(su, &su->su_ga); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10883 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| 10884 | } |
| 10885 | |
| 10886 | /* |
| 10887 | * Find suggestions for the internal method indicated by "sps_flags". |
| 10888 | */ |
| 10889 | static void |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10890 | spell_suggest_intern(su, interactive) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10891 | suginfo_T *su; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10892 | int interactive; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10893 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10894 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10895 | * Load the .sug file(s) that are available and not done yet. |
| 10896 | */ |
| 10897 | suggest_load_files(); |
| 10898 | |
| 10899 | /* |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10900 | * 1. Try special cases, such as repeating a word: "the the" -> "the". |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10901 | * |
| 10902 | * Set a maximum score to limit the combination of operations that is |
| 10903 | * tried. |
| 10904 | */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10905 | suggest_try_special(su); |
| 10906 | |
| 10907 | /* |
| 10908 | * 2. Try inserting/deleting/swapping/changing a letter, use REP entries |
| 10909 | * from the .aff file and inserting a space (split the word). |
| 10910 | */ |
| 10911 | suggest_try_change(su); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10912 | |
| 10913 | /* For the resulting top-scorers compute the sound-a-like score. */ |
| 10914 | if (sps_flags & SPS_DOUBLE) |
| 10915 | score_comp_sal(su); |
| 10916 | |
| 10917 | /* |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10918 | * 3. Try finding sound-a-like words. |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10919 | */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10920 | if ((sps_flags & SPS_FAST) == 0) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10921 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10922 | if (sps_flags & SPS_BEST) |
| 10923 | /* Adjust the word score for the suggestions found so far for how |
| 10924 | * they sounds like. */ |
| 10925 | rescore_suggestions(su); |
| 10926 | |
| 10927 | /* |
| 10928 | * While going throught the soundfold tree "su_maxscore" is the score |
| 10929 | * for the soundfold word, limits the changes that are being tried, |
| 10930 | * and "su_sfmaxscore" the rescored score, which is set by |
| 10931 | * cleanup_suggestions(). |
| 10932 | * First find words with a small edit distance, because this is much |
| 10933 | * faster and often already finds the top-N suggestions. If we didn't |
| 10934 | * find many suggestions try again with a higher edit distance. |
| 10935 | * "sl_sounddone" is used to avoid doing the same word twice. |
| 10936 | */ |
| 10937 | suggest_try_soundalike_prep(); |
| 10938 | su->su_maxscore = SCORE_SFMAX1; |
| 10939 | su->su_sfmaxscore = SCORE_MAXINIT * 3; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 10940 | suggest_try_soundalike(su); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10941 | if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su)) |
| 10942 | { |
| 10943 | /* We didn't find enough matches, try again, allowing more |
| 10944 | * changes to the soundfold word. */ |
| 10945 | su->su_maxscore = SCORE_SFMAX2; |
| 10946 | suggest_try_soundalike(su); |
| 10947 | if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su)) |
| 10948 | { |
| 10949 | /* Still didn't find enough matches, try again, allowing even |
| 10950 | * more changes to the soundfold word. */ |
| 10951 | su->su_maxscore = SCORE_SFMAX3; |
| 10952 | suggest_try_soundalike(su); |
| 10953 | } |
| 10954 | } |
| 10955 | su->su_maxscore = su->su_sfmaxscore; |
| 10956 | suggest_try_soundalike_finish(); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10957 | } |
| 10958 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10959 | /* When CTRL-C was hit while searching do show the results. Only clear |
| 10960 | * got_int when using a command, not for spellsuggest(). */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10961 | ui_breakcheck(); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10962 | if (interactive && got_int) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10963 | { |
| 10964 | (void)vgetc(); |
| 10965 | got_int = FALSE; |
| 10966 | } |
| 10967 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10968 | if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10969 | { |
| 10970 | if (sps_flags & SPS_BEST) |
| 10971 | /* Adjust the word score for how it sounds like. */ |
| 10972 | rescore_suggestions(su); |
| 10973 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10974 | /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
| 10975 | check_suggestions(su, &su->su_ga); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 10976 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 10977 | } |
| 10978 | } |
| 10979 | |
| 10980 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 10981 | * Load the .sug files for languages that have one and weren't loaded yet. |
| 10982 | */ |
| 10983 | static void |
| 10984 | suggest_load_files() |
| 10985 | { |
| 10986 | langp_T *lp; |
| 10987 | int lpi; |
| 10988 | slang_T *slang; |
| 10989 | char_u *dotp; |
| 10990 | FILE *fd; |
| 10991 | char_u buf[MAXWLEN]; |
| 10992 | int i; |
| 10993 | time_t timestamp; |
| 10994 | int wcount; |
| 10995 | int wordnr; |
| 10996 | garray_T ga; |
| 10997 | int c; |
| 10998 | |
| 10999 | /* Do this for all languages that support sound folding. */ |
| 11000 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| 11001 | { |
| 11002 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| 11003 | slang = lp->lp_slang; |
| 11004 | if (slang->sl_sugtime != 0 && !slang->sl_sugloaded) |
| 11005 | { |
| 11006 | /* Change ".spl" to ".sug" and open the file. When the file isn't |
| 11007 | * found silently skip it. Do set "sl_sugloaded" so that we |
| 11008 | * don't try again and again. */ |
| 11009 | slang->sl_sugloaded = TRUE; |
| 11010 | |
| 11011 | dotp = vim_strrchr(slang->sl_fname, '.'); |
| 11012 | if (dotp == NULL || fnamecmp(dotp, ".spl") != 0) |
| 11013 | continue; |
| 11014 | STRCPY(dotp, ".sug"); |
| Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 11015 | fd = mch_fopen((char *)slang->sl_fname, "r"); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11016 | if (fd == NULL) |
| 11017 | goto nextone; |
| 11018 | |
| 11019 | /* |
| 11020 | * <SUGHEADER>: <fileID> <versionnr> <timestamp> |
| 11021 | */ |
| 11022 | for (i = 0; i < VIMSUGMAGICL; ++i) |
| 11023 | buf[i] = getc(fd); /* <fileID> */ |
| 11024 | if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0) |
| 11025 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 11026 | EMSG2(_("E778: This does not look like a .sug file: %s"), |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11027 | slang->sl_fname); |
| 11028 | goto nextone; |
| 11029 | } |
| 11030 | c = getc(fd); /* <versionnr> */ |
| 11031 | if (c < VIMSUGVERSION) |
| 11032 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 11033 | EMSG2(_("E779: Old .sug file, needs to be updated: %s"), |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11034 | slang->sl_fname); |
| 11035 | goto nextone; |
| 11036 | } |
| 11037 | else if (c > VIMSUGVERSION) |
| 11038 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 11039 | EMSG2(_("E780: .sug file is for newer version of Vim: %s"), |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11040 | slang->sl_fname); |
| 11041 | goto nextone; |
| 11042 | } |
| 11043 | |
| 11044 | /* Check the timestamp, it must be exactly the same as the one in |
| 11045 | * the .spl file. Otherwise the word numbers won't match. */ |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 11046 | timestamp = get8c(fd); /* <timestamp> */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11047 | if (timestamp != slang->sl_sugtime) |
| 11048 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 11049 | EMSG2(_("E781: .sug file doesn't match .spl file: %s"), |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11050 | slang->sl_fname); |
| 11051 | goto nextone; |
| 11052 | } |
| 11053 | |
| 11054 | /* |
| 11055 | * <SUGWORDTREE>: <wordtree> |
| 11056 | * Read the trie with the soundfolded words. |
| 11057 | */ |
| 11058 | if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs, |
| 11059 | FALSE, 0) != 0) |
| 11060 | { |
| 11061 | someerror: |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 11062 | EMSG2(_("E782: error while reading .sug file: %s"), |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11063 | slang->sl_fname); |
| 11064 | slang_clear_sug(slang); |
| 11065 | goto nextone; |
| 11066 | } |
| 11067 | |
| 11068 | /* |
| 11069 | * <SUGTABLE>: <sugwcount> <sugline> ... |
| 11070 | * |
| 11071 | * Read the table with word numbers. We use a file buffer for |
| 11072 | * this, because it's so much like a file with lines. Makes it |
| 11073 | * possible to swap the info and save on memory use. |
| 11074 | */ |
| 11075 | slang->sl_sugbuf = open_spellbuf(); |
| 11076 | if (slang->sl_sugbuf == NULL) |
| 11077 | goto someerror; |
| 11078 | /* <sugwcount> */ |
| Bram Moolenaar | b388adb | 2006-02-28 23:50:17 +0000 | [diff] [blame] | 11079 | wcount = get4c(fd); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11080 | if (wcount < 0) |
| 11081 | goto someerror; |
| 11082 | |
| 11083 | /* Read all the wordnr lists into the buffer, one NUL terminated |
| 11084 | * list per line. */ |
| 11085 | ga_init2(&ga, 1, 100); |
| 11086 | for (wordnr = 0; wordnr < wcount; ++wordnr) |
| 11087 | { |
| 11088 | ga.ga_len = 0; |
| 11089 | for (;;) |
| 11090 | { |
| 11091 | c = getc(fd); /* <sugline> */ |
| 11092 | if (c < 0 || ga_grow(&ga, 1) == FAIL) |
| 11093 | goto someerror; |
| 11094 | ((char_u *)ga.ga_data)[ga.ga_len++] = c; |
| 11095 | if (c == NUL) |
| 11096 | break; |
| 11097 | } |
| 11098 | if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr, |
| 11099 | ga.ga_data, ga.ga_len, TRUE) == FAIL) |
| 11100 | goto someerror; |
| 11101 | } |
| 11102 | ga_clear(&ga); |
| 11103 | |
| 11104 | /* |
| 11105 | * Need to put word counts in the word tries, so that we can find |
| 11106 | * a word by its number. |
| 11107 | */ |
| 11108 | tree_count_words(slang->sl_fbyts, slang->sl_fidxs); |
| 11109 | tree_count_words(slang->sl_sbyts, slang->sl_sidxs); |
| 11110 | |
| 11111 | nextone: |
| 11112 | if (fd != NULL) |
| 11113 | fclose(fd); |
| 11114 | STRCPY(dotp, ".spl"); |
| 11115 | } |
| 11116 | } |
| 11117 | } |
| 11118 | |
| 11119 | |
| 11120 | /* |
| 11121 | * Fill in the wordcount fields for a trie. |
| 11122 | * Returns the total number of words. |
| 11123 | */ |
| 11124 | static void |
| 11125 | tree_count_words(byts, idxs) |
| 11126 | char_u *byts; |
| 11127 | idx_T *idxs; |
| 11128 | { |
| 11129 | int depth; |
| 11130 | idx_T arridx[MAXWLEN]; |
| 11131 | int curi[MAXWLEN]; |
| 11132 | int c; |
| 11133 | idx_T n; |
| 11134 | int wordcount[MAXWLEN]; |
| 11135 | |
| 11136 | arridx[0] = 0; |
| 11137 | curi[0] = 1; |
| 11138 | wordcount[0] = 0; |
| 11139 | depth = 0; |
| 11140 | while (depth >= 0 && !got_int) |
| 11141 | { |
| 11142 | if (curi[depth] > byts[arridx[depth]]) |
| 11143 | { |
| 11144 | /* Done all bytes at this node, go up one level. */ |
| 11145 | idxs[arridx[depth]] = wordcount[depth]; |
| 11146 | if (depth > 0) |
| 11147 | wordcount[depth - 1] += wordcount[depth]; |
| 11148 | |
| 11149 | --depth; |
| 11150 | fast_breakcheck(); |
| 11151 | } |
| 11152 | else |
| 11153 | { |
| 11154 | /* Do one more byte at this node. */ |
| 11155 | n = arridx[depth] + curi[depth]; |
| 11156 | ++curi[depth]; |
| 11157 | |
| 11158 | c = byts[n]; |
| 11159 | if (c == 0) |
| 11160 | { |
| 11161 | /* End of word, count it. */ |
| 11162 | ++wordcount[depth]; |
| 11163 | |
| 11164 | /* Skip over any other NUL bytes (same word with different |
| 11165 | * flags). */ |
| 11166 | while (byts[n + 1] == 0) |
| 11167 | { |
| 11168 | ++n; |
| 11169 | ++curi[depth]; |
| 11170 | } |
| 11171 | } |
| 11172 | else |
| 11173 | { |
| 11174 | /* Normal char, go one level deeper to count the words. */ |
| 11175 | ++depth; |
| 11176 | arridx[depth] = idxs[n]; |
| 11177 | curi[depth] = 1; |
| 11178 | wordcount[depth] = 0; |
| 11179 | } |
| 11180 | } |
| 11181 | } |
| 11182 | } |
| 11183 | |
| 11184 | /* |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 11185 | * Free the info put in "*su" by spell_find_suggest(). |
| 11186 | */ |
| 11187 | static void |
| 11188 | spell_find_cleanup(su) |
| 11189 | suginfo_T *su; |
| 11190 | { |
| 11191 | int i; |
| 11192 | |
| 11193 | /* Free the suggestions. */ |
| 11194 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 11195 | vim_free(SUG(su->su_ga, i).st_word); |
| 11196 | ga_clear(&su->su_ga); |
| 11197 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 11198 | vim_free(SUG(su->su_sga, i).st_word); |
| 11199 | ga_clear(&su->su_sga); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11200 | |
| 11201 | /* Free the banned words. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11202 | hash_clear_all(&su->su_banned, 0); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11203 | } |
| 11204 | |
| 11205 | /* |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 11206 | * Make a copy of "word", with the first letter upper or lower cased, to |
| 11207 | * "wcopy[MAXWLEN]". "word" must not be empty. |
| 11208 | * The result is NUL terminated. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11209 | */ |
| 11210 | static void |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 11211 | onecap_copy(word, wcopy, upper) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11212 | char_u *word; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11213 | char_u *wcopy; |
| 11214 | int upper; /* TRUE: first letter made upper case */ |
| 11215 | { |
| 11216 | char_u *p; |
| 11217 | int c; |
| 11218 | int l; |
| 11219 | |
| 11220 | p = word; |
| 11221 | #ifdef FEAT_MBYTE |
| 11222 | if (has_mbyte) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 11223 | c = mb_cptr2char_adv(&p); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11224 | else |
| 11225 | #endif |
| 11226 | c = *p++; |
| 11227 | if (upper) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 11228 | c = SPELL_TOUPPER(c); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11229 | else |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 11230 | c = SPELL_TOFOLD(c); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11231 | #ifdef FEAT_MBYTE |
| 11232 | if (has_mbyte) |
| 11233 | l = mb_char2bytes(c, wcopy); |
| 11234 | else |
| 11235 | #endif |
| 11236 | { |
| 11237 | l = 1; |
| 11238 | wcopy[0] = c; |
| 11239 | } |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11240 | vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11241 | } |
| 11242 | |
| 11243 | /* |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 11244 | * Make a copy of "word" with all the letters upper cased into |
| 11245 | * "wcopy[MAXWLEN]". The result is NUL terminated. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11246 | */ |
| 11247 | static void |
| 11248 | allcap_copy(word, wcopy) |
| 11249 | char_u *word; |
| 11250 | char_u *wcopy; |
| 11251 | { |
| 11252 | char_u *s; |
| 11253 | char_u *d; |
| 11254 | int c; |
| 11255 | |
| 11256 | d = wcopy; |
| 11257 | for (s = word; *s != NUL; ) |
| 11258 | { |
| 11259 | #ifdef FEAT_MBYTE |
| 11260 | if (has_mbyte) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 11261 | c = mb_cptr2char_adv(&s); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11262 | else |
| 11263 | #endif |
| 11264 | c = *s++; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11265 | |
| 11266 | #ifdef FEAT_MBYTE |
| 11267 | /* We only change ß to SS when we are certain latin1 is used. It |
| 11268 | * would cause weird errors in other 8-bit encodings. */ |
| 11269 | if (enc_latin1like && c == 0xdf) |
| 11270 | { |
| 11271 | c = 'S'; |
| 11272 | if (d - wcopy >= MAXWLEN - 1) |
| 11273 | break; |
| 11274 | *d++ = c; |
| 11275 | } |
| 11276 | else |
| 11277 | #endif |
| 11278 | c = SPELL_TOUPPER(c); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11279 | |
| 11280 | #ifdef FEAT_MBYTE |
| 11281 | if (has_mbyte) |
| 11282 | { |
| 11283 | if (d - wcopy >= MAXWLEN - MB_MAXBYTES) |
| 11284 | break; |
| 11285 | d += mb_char2bytes(c, d); |
| 11286 | } |
| 11287 | else |
| 11288 | #endif |
| 11289 | { |
| 11290 | if (d - wcopy >= MAXWLEN - 1) |
| 11291 | break; |
| 11292 | *d++ = c; |
| 11293 | } |
| 11294 | } |
| 11295 | *d = NUL; |
| 11296 | } |
| 11297 | |
| 11298 | /* |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11299 | * Try finding suggestions by recognizing specific situations. |
| 11300 | */ |
| 11301 | static void |
| 11302 | suggest_try_special(su) |
| 11303 | suginfo_T *su; |
| 11304 | { |
| 11305 | char_u *p; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11306 | size_t len; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11307 | int c; |
| 11308 | char_u word[MAXWLEN]; |
| 11309 | |
| 11310 | /* |
| 11311 | * Recognize a word that is repeated: "the the". |
| 11312 | */ |
| 11313 | p = skiptowhite(su->su_fbadword); |
| 11314 | len = p - su->su_fbadword; |
| 11315 | p = skipwhite(p); |
| 11316 | if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) |
| 11317 | { |
| 11318 | /* Include badflags: if the badword is onecap or allcap |
| 11319 | * use that for the goodword too: "The the" -> "The". */ |
| 11320 | c = su->su_fbadword[len]; |
| 11321 | su->su_fbadword[len] = NUL; |
| 11322 | make_case_word(su->su_fbadword, word, su->su_badflags); |
| 11323 | su->su_fbadword[len] = c; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11324 | |
| 11325 | /* Give a soundalike score of 0, compute the score as if deleting one |
| 11326 | * character. */ |
| 11327 | add_suggestion(su, &su->su_ga, word, su->su_badlen, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11328 | RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11329 | } |
| 11330 | } |
| 11331 | |
| 11332 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11333 | * Try finding suggestions by adding/removing/swapping letters. |
| 11334 | */ |
| 11335 | static void |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11336 | suggest_try_change(su) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11337 | suginfo_T *su; |
| 11338 | { |
| 11339 | char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 11340 | int n; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11341 | char_u *p; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11342 | int lpi; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11343 | langp_T *lp; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11344 | |
| 11345 | /* 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] | 11346 | * to find matches (esp. REP items). Append some more text, changing |
| 11347 | * chars after the bad word may help. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11348 | STRCPY(fword, su->su_fbadword); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 11349 | n = (int)STRLEN(fword); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11350 | p = su->su_badptr + su->su_badlen; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 11351 | (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11352 | |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 11353 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11354 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 11355 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11356 | |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11357 | /* If reloading a spell file fails it's still in the list but |
| 11358 | * everything has been cleared. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11359 | if (lp->lp_slang->sl_fbyts == NULL) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11360 | continue; |
| 11361 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11362 | /* Try it for this language. Will add possible suggestions. */ |
| 11363 | suggest_trie_walk(su, lp, fword, FALSE); |
| 11364 | } |
| 11365 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11366 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11367 | /* Check the maximum score, if we go over it we won't try this change. */ |
| 11368 | #define TRY_DEEPER(su, stack, depth, add) \ |
| 11369 | (stack[depth].ts_score + (add) < su->su_maxscore) |
| 11370 | |
| 11371 | /* |
| 11372 | * Try finding suggestions by adding/removing/swapping letters. |
| 11373 | * |
| 11374 | * This uses a state machine. At each node in the tree we try various |
| 11375 | * operations. When trying if an operation works "depth" is increased and the |
| 11376 | * stack[] is used to store info. This allows combinations, thus insert one |
| 11377 | * character, replace one and delete another. The number of changes is |
| 11378 | * limited by su->su_maxscore. |
| 11379 | * |
| 11380 | * After implementing this I noticed an article by Kemal Oflazer that |
| 11381 | * describes something similar: "Error-tolerant Finite State Recognition with |
| 11382 | * Applications to Morphological Analysis and Spelling Correction" (1996). |
| 11383 | * The implementation in the article is simplified and requires a stack of |
| 11384 | * unknown depth. The implementation here only needs a stack depth equal to |
| 11385 | * the length of the word. |
| 11386 | * |
| 11387 | * This is also used for the sound-folded word, "soundfold" is TRUE then. |
| 11388 | * The mechanism is the same, but we find a match with a sound-folded word |
| 11389 | * that comes from one or more original words. Each of these words may be |
| 11390 | * added, this is done by add_sound_suggest(). |
| 11391 | * Don't use: |
| 11392 | * the prefix tree or the keep-case tree |
| 11393 | * "su->su_badlen" |
| 11394 | * anything to do with upper and lower case |
| 11395 | * anything to do with word or non-word characters ("spell_iswordp()") |
| 11396 | * banned words |
| 11397 | * word flags (rare, region, compounding) |
| 11398 | * word splitting for now |
| 11399 | * "similar_chars()" |
| 11400 | * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep" |
| 11401 | */ |
| 11402 | static void |
| 11403 | suggest_trie_walk(su, lp, fword, soundfold) |
| 11404 | suginfo_T *su; |
| 11405 | langp_T *lp; |
| 11406 | char_u *fword; |
| 11407 | int soundfold; |
| 11408 | { |
| 11409 | char_u tword[MAXWLEN]; /* good word collected so far */ |
| 11410 | trystate_T stack[MAXWLEN]; |
| 11411 | char_u preword[MAXWLEN * 3]; /* word found with proper case; |
| 11412 | * concatanation of prefix compound |
| 11413 | * words and split word. NUL terminated |
| 11414 | * when going deeper but not when coming |
| 11415 | * back. */ |
| 11416 | char_u compflags[MAXWLEN]; /* compound flags, one for each word */ |
| 11417 | trystate_T *sp; |
| 11418 | int newscore; |
| 11419 | int score; |
| 11420 | char_u *byts, *fbyts, *pbyts; |
| 11421 | idx_T *idxs, *fidxs, *pidxs; |
| 11422 | int depth; |
| 11423 | int c, c2, c3; |
| 11424 | int n = 0; |
| 11425 | int flags; |
| 11426 | garray_T *gap; |
| 11427 | idx_T arridx; |
| 11428 | int len; |
| 11429 | char_u *p; |
| 11430 | fromto_T *ftp; |
| 11431 | int fl = 0, tl; |
| 11432 | int repextra = 0; /* extra bytes in fword[] from REP item */ |
| 11433 | slang_T *slang = lp->lp_slang; |
| 11434 | int fword_ends; |
| 11435 | int goodword_ends; |
| 11436 | #ifdef DEBUG_TRIEWALK |
| 11437 | /* Stores the name of the change made at each level. */ |
| 11438 | char_u changename[MAXWLEN][80]; |
| 11439 | #endif |
| 11440 | int breakcheckcount = 1000; |
| 11441 | int compound_ok; |
| 11442 | |
| 11443 | /* |
| 11444 | * Go through the whole case-fold tree, try changes at each node. |
| 11445 | * "tword[]" contains the word collected from nodes in the tree. |
| 11446 | * "fword[]" the word we are trying to match with (initially the bad |
| 11447 | * word). |
| 11448 | */ |
| 11449 | depth = 0; |
| 11450 | sp = &stack[0]; |
| 11451 | vim_memset(sp, 0, sizeof(trystate_T)); |
| 11452 | sp->ts_curi = 1; |
| 11453 | |
| 11454 | if (soundfold) |
| 11455 | { |
| 11456 | /* Going through the soundfold tree. */ |
| 11457 | byts = fbyts = slang->sl_sbyts; |
| 11458 | idxs = fidxs = slang->sl_sidxs; |
| 11459 | pbyts = NULL; |
| 11460 | pidxs = NULL; |
| 11461 | sp->ts_prefixdepth = PFD_NOPREFIX; |
| 11462 | sp->ts_state = STATE_START; |
| 11463 | } |
| 11464 | else |
| 11465 | { |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 11466 | /* |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11467 | * When there are postponed prefixes we need to use these first. At |
| 11468 | * the end of the prefix we continue in the case-fold tree. |
| 11469 | */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11470 | fbyts = slang->sl_fbyts; |
| 11471 | fidxs = slang->sl_fidxs; |
| 11472 | pbyts = slang->sl_pbyts; |
| 11473 | pidxs = slang->sl_pidxs; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11474 | if (pbyts != NULL) |
| 11475 | { |
| 11476 | byts = pbyts; |
| 11477 | idxs = pidxs; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11478 | sp->ts_prefixdepth = PFD_PREFIXTREE; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11479 | sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
| 11480 | } |
| 11481 | else |
| 11482 | { |
| 11483 | byts = fbyts; |
| 11484 | idxs = fidxs; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11485 | sp->ts_prefixdepth = PFD_NOPREFIX; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11486 | sp->ts_state = STATE_START; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11487 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11488 | } |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11489 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11490 | /* |
| 11491 | * Loop to find all suggestions. At each round we either: |
| 11492 | * - For the current state try one operation, advance "ts_curi", |
| 11493 | * increase "depth". |
| 11494 | * - When a state is done go to the next, set "ts_state". |
| 11495 | * - When all states are tried decrease "depth". |
| 11496 | */ |
| 11497 | while (depth >= 0 && !got_int) |
| 11498 | { |
| 11499 | sp = &stack[depth]; |
| 11500 | switch (sp->ts_state) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11501 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11502 | case STATE_START: |
| 11503 | case STATE_NOPREFIX: |
| 11504 | /* |
| 11505 | * Start of node: Deal with NUL bytes, which means |
| 11506 | * tword[] may end here. |
| 11507 | */ |
| 11508 | arridx = sp->ts_arridx; /* current node in the tree */ |
| 11509 | len = byts[arridx]; /* bytes in this node */ |
| 11510 | arridx += sp->ts_curi; /* index of current byte */ |
| 11511 | |
| 11512 | if (sp->ts_prefixdepth == PFD_PREFIXTREE) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11513 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11514 | /* Skip over the NUL bytes, we use them later. */ |
| 11515 | for (n = 0; n < len && byts[arridx + n] == 0; ++n) |
| 11516 | ; |
| 11517 | sp->ts_curi += n; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11518 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11519 | /* Always past NUL bytes now. */ |
| 11520 | n = (int)sp->ts_state; |
| 11521 | sp->ts_state = STATE_ENDNUL; |
| 11522 | sp->ts_save_badflags = su->su_badflags; |
| 11523 | |
| 11524 | /* At end of a prefix or at start of prefixtree: check for |
| 11525 | * following word. */ |
| 11526 | if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX) |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11527 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11528 | /* Set su->su_badflags to the caps type at this position. |
| 11529 | * Use the caps type until here for the prefix itself. */ |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 11530 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11531 | if (has_mbyte) |
| 11532 | n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
| 11533 | else |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 11534 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11535 | n = sp->ts_fidx; |
| 11536 | flags = badword_captype(su->su_badptr, su->su_badptr + n); |
| 11537 | su->su_badflags = badword_captype(su->su_badptr + n, |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 11538 | su->su_badptr + su->su_badlen); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11539 | #ifdef DEBUG_TRIEWALK |
| 11540 | sprintf(changename[depth], "prefix"); |
| 11541 | #endif |
| 11542 | go_deeper(stack, depth, 0); |
| 11543 | ++depth; |
| 11544 | sp = &stack[depth]; |
| 11545 | sp->ts_prefixdepth = depth - 1; |
| 11546 | byts = fbyts; |
| 11547 | idxs = fidxs; |
| 11548 | sp->ts_arridx = 0; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11549 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11550 | /* Move the prefix to preword[] with the right case |
| 11551 | * and make find_keepcap_word() works. */ |
| 11552 | tword[sp->ts_twordlen] = NUL; |
| 11553 | make_case_word(tword + sp->ts_splitoff, |
| 11554 | preword + sp->ts_prewordlen, flags); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 11555 | sp->ts_prewordlen = (char_u)STRLEN(preword); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11556 | sp->ts_splitoff = sp->ts_twordlen; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11557 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11558 | break; |
| 11559 | } |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11560 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11561 | if (sp->ts_curi > len || byts[arridx] != 0) |
| 11562 | { |
| 11563 | /* Past bytes in node and/or past NUL bytes. */ |
| 11564 | sp->ts_state = STATE_ENDNUL; |
| 11565 | sp->ts_save_badflags = su->su_badflags; |
| 11566 | break; |
| 11567 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11568 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11569 | /* |
| 11570 | * End of word in tree. |
| 11571 | */ |
| 11572 | ++sp->ts_curi; /* eat one NUL byte */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11573 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11574 | flags = (int)idxs[arridx]; |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 11575 | |
| 11576 | /* Skip words with the NOSUGGEST flag. */ |
| 11577 | if (flags & WF_NOSUGGEST) |
| 11578 | break; |
| 11579 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11580 | fword_ends = (fword[sp->ts_fidx] == NUL |
| 11581 | || (soundfold |
| 11582 | ? vim_iswhite(fword[sp->ts_fidx]) |
| 11583 | : !spell_iswordp(fword + sp->ts_fidx, curbuf))); |
| 11584 | tword[sp->ts_twordlen] = NUL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11585 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11586 | if (sp->ts_prefixdepth <= PFD_NOTSPECIAL |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11587 | && (sp->ts_flags & TSF_PREFIXOK) == 0) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11588 | { |
| 11589 | /* There was a prefix before the word. Check that the prefix |
| 11590 | * can be used with this word. */ |
| 11591 | /* Count the length of the NULs in the prefix. If there are |
| 11592 | * none this must be the first try without a prefix. */ |
| 11593 | n = stack[sp->ts_prefixdepth].ts_arridx; |
| 11594 | len = pbyts[n++]; |
| 11595 | for (c = 0; c < len && pbyts[n + c] == 0; ++c) |
| 11596 | ; |
| 11597 | if (c > 0) |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11598 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11599 | c = valid_word_prefix(c, n, flags, |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 11600 | tword + sp->ts_splitoff, slang, FALSE); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11601 | if (c == 0) |
| 11602 | break; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11603 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11604 | /* Use the WF_RARE flag for a rare prefix. */ |
| 11605 | if (c & WF_RAREPFX) |
| 11606 | flags |= WF_RARE; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11607 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11608 | /* Tricky: when checking for both prefix and compounding |
| 11609 | * we run into the prefix flag first. |
| 11610 | * Remember that it's OK, so that we accept the prefix |
| 11611 | * when arriving at a compound flag. */ |
| 11612 | sp->ts_flags |= TSF_PREFIXOK; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11613 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11614 | } |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 11615 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11616 | /* Check NEEDCOMPOUND: can't use word without compounding. Do try |
| 11617 | * appending another compound word below. */ |
| 11618 | if (sp->ts_complen == sp->ts_compsplit && fword_ends |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11619 | && (flags & WF_NEEDCOMP)) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11620 | goodword_ends = FALSE; |
| 11621 | else |
| 11622 | goodword_ends = TRUE; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11623 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11624 | p = NULL; |
| 11625 | compound_ok = TRUE; |
| 11626 | if (sp->ts_complen > sp->ts_compsplit) |
| 11627 | { |
| 11628 | if (slang->sl_nobreak) |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11629 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11630 | /* There was a word before this word. When there was no |
| 11631 | * change in this word (it was correct) add the first word |
| 11632 | * as a suggestion. If this word was corrected too, we |
| 11633 | * need to check if a correct word follows. */ |
| 11634 | if (sp->ts_fidx - sp->ts_splitfidx |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11635 | == sp->ts_twordlen - sp->ts_splitoff |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11636 | && STRNCMP(fword + sp->ts_splitfidx, |
| 11637 | tword + sp->ts_splitoff, |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11638 | sp->ts_fidx - sp->ts_splitfidx) == 0) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11639 | { |
| 11640 | preword[sp->ts_prewordlen] = NUL; |
| 11641 | newscore = score_wordcount_adj(slang, sp->ts_score, |
| 11642 | preword + sp->ts_prewordlen, |
| 11643 | sp->ts_prewordlen > 0); |
| 11644 | /* Add the suggestion if the score isn't too bad. */ |
| 11645 | if (newscore <= su->su_maxscore) |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11646 | add_suggestion(su, &su->su_ga, preword, |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 11647 | sp->ts_splitfidx - repextra, |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11648 | newscore, 0, FALSE, |
| 11649 | lp->lp_sallang, FALSE); |
| 11650 | break; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11651 | } |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11652 | } |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 11653 | else |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11654 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11655 | /* There was a compound word before this word. If this |
| 11656 | * word does not support compounding then give up |
| 11657 | * (splitting is tried for the word without compound |
| 11658 | * flag). */ |
| 11659 | if (((unsigned)flags >> 24) == 0 |
| 11660 | || sp->ts_twordlen - sp->ts_splitoff |
| 11661 | < slang->sl_compminlen) |
| 11662 | break; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11663 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11664 | /* For multi-byte chars check character length against |
| 11665 | * COMPOUNDMIN. */ |
| 11666 | if (has_mbyte |
| 11667 | && slang->sl_compminlen > 0 |
| 11668 | && mb_charlen(tword + sp->ts_splitoff) |
| 11669 | < slang->sl_compminlen) |
| 11670 | break; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11671 | #endif |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 11672 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11673 | compflags[sp->ts_complen] = ((unsigned)flags >> 24); |
| 11674 | compflags[sp->ts_complen + 1] = NUL; |
| 11675 | vim_strncpy(preword + sp->ts_prewordlen, |
| 11676 | tword + sp->ts_splitoff, |
| 11677 | sp->ts_twordlen - sp->ts_splitoff); |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 11678 | |
| 11679 | /* Verify CHECKCOMPOUNDPATTERN rules. */ |
| 11680 | if (match_checkcompoundpattern(preword, sp->ts_prewordlen, |
| 11681 | &slang->sl_comppat)) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11682 | compound_ok = FALSE; |
| 11683 | |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 11684 | if (compound_ok) |
| 11685 | { |
| 11686 | p = preword; |
| 11687 | while (*skiptowhite(p) != NUL) |
| 11688 | p = skipwhite(skiptowhite(p)); |
| 11689 | if (fword_ends && !can_compound(slang, p, |
| 11690 | compflags + sp->ts_compsplit)) |
| 11691 | /* Compound is not allowed. But it may still be |
| 11692 | * possible if we add another (short) word. */ |
| 11693 | compound_ok = FALSE; |
| 11694 | } |
| 11695 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11696 | /* Get pointer to last char of previous word. */ |
| 11697 | p = preword + sp->ts_prewordlen; |
| 11698 | mb_ptr_back(preword, p); |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11699 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11700 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11701 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11702 | /* |
| 11703 | * Form the word with proper case in preword. |
| 11704 | * If there is a word from a previous split, append. |
| 11705 | * For the soundfold tree don't change the case, simply append. |
| 11706 | */ |
| 11707 | if (soundfold) |
| 11708 | STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff); |
| 11709 | else if (flags & WF_KEEPCAP) |
| 11710 | /* Must find the word in the keep-case tree. */ |
| 11711 | find_keepcap_word(slang, tword + sp->ts_splitoff, |
| 11712 | preword + sp->ts_prewordlen); |
| 11713 | else |
| 11714 | { |
| 11715 | /* Include badflags: If the badword is onecap or allcap |
| 11716 | * use that for the goodword too. But if the badword is |
| 11717 | * allcap and it's only one char long use onecap. */ |
| 11718 | c = su->su_badflags; |
| 11719 | if ((c & WF_ALLCAP) |
| 11720 | #ifdef FEAT_MBYTE |
| 11721 | && su->su_badlen == (*mb_ptr2len)(su->su_badptr) |
| 11722 | #else |
| 11723 | && su->su_badlen == 1 |
| 11724 | #endif |
| 11725 | ) |
| 11726 | c = WF_ONECAP; |
| 11727 | c |= flags; |
| 11728 | |
| 11729 | /* When appending a compound word after a word character don't |
| 11730 | * use Onecap. */ |
| 11731 | if (p != NULL && spell_iswordp_nmw(p)) |
| 11732 | c &= ~WF_ONECAP; |
| 11733 | make_case_word(tword + sp->ts_splitoff, |
| 11734 | preword + sp->ts_prewordlen, c); |
| 11735 | } |
| 11736 | |
| 11737 | if (!soundfold) |
| 11738 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11739 | /* Don't use a banned word. It may appear again as a good |
| 11740 | * word, thus remember it. */ |
| 11741 | if (flags & WF_BANNED) |
| 11742 | { |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 11743 | add_banned(su, preword + sp->ts_prewordlen); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11744 | break; |
| 11745 | } |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11746 | if ((sp->ts_complen == sp->ts_compsplit |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11747 | && WAS_BANNED(su, preword + sp->ts_prewordlen)) |
| 11748 | || WAS_BANNED(su, preword)) |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11749 | { |
| 11750 | if (slang->sl_compprog == NULL) |
| 11751 | break; |
| 11752 | /* the word so far was banned but we may try compounding */ |
| 11753 | goodword_ends = FALSE; |
| 11754 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11755 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11756 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11757 | newscore = 0; |
| 11758 | if (!soundfold) /* soundfold words don't have flags */ |
| 11759 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11760 | if ((flags & WF_REGION) |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 11761 | && (((unsigned)flags >> 16) & lp->lp_region) == 0) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11762 | newscore += SCORE_REGION; |
| 11763 | if (flags & WF_RARE) |
| 11764 | newscore += SCORE_RARE; |
| 11765 | |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11766 | if (!spell_valid_case(su->su_badflags, |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 11767 | captype(preword + sp->ts_prewordlen, NULL))) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11768 | newscore += SCORE_ICASE; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11769 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11770 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11771 | /* TODO: how about splitting in the soundfold tree? */ |
| 11772 | if (fword_ends |
| 11773 | && goodword_ends |
| 11774 | && sp->ts_fidx >= sp->ts_fidxtry |
| 11775 | && compound_ok) |
| 11776 | { |
| 11777 | /* The badword also ends: add suggestions. */ |
| 11778 | #ifdef DEBUG_TRIEWALK |
| 11779 | if (soundfold && STRCMP(preword, "smwrd") == 0) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11780 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11781 | int j; |
| 11782 | |
| 11783 | /* print the stack of changes that brought us here */ |
| 11784 | smsg("------ %s -------", fword); |
| 11785 | for (j = 0; j < depth; ++j) |
| 11786 | smsg("%s", changename[j]); |
| 11787 | } |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 11788 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11789 | if (soundfold) |
| 11790 | { |
| 11791 | /* For soundfolded words we need to find the original |
| Bram Moolenaar | f711faf | 2007-05-10 16:48:19 +0000 | [diff] [blame] | 11792 | * words, the edit distance and then add them. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11793 | add_sound_suggest(su, preword, sp->ts_score, lp); |
| 11794 | } |
| 11795 | else |
| 11796 | { |
| 11797 | /* Give a penalty when changing non-word char to word |
| 11798 | * char, e.g., "thes," -> "these". */ |
| 11799 | p = fword + sp->ts_fidx; |
| 11800 | mb_ptr_back(fword, p); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11801 | if (!spell_iswordp(p, curbuf)) |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 11802 | { |
| 11803 | p = preword + STRLEN(preword); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11804 | mb_ptr_back(preword, p); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11805 | if (spell_iswordp(p, curbuf)) |
| Bram Moolenaar | cf6bf39 | 2005-06-27 22:27:46 +0000 | [diff] [blame] | 11806 | newscore += SCORE_NONWORD; |
| 11807 | } |
| 11808 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11809 | /* Give a bonus to words seen before. */ |
| 11810 | score = score_wordcount_adj(slang, |
| 11811 | sp->ts_score + newscore, |
| 11812 | preword + sp->ts_prewordlen, |
| 11813 | sp->ts_prewordlen > 0); |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11814 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11815 | /* Add the suggestion if the score isn't too bad. */ |
| 11816 | if (score <= su->su_maxscore) |
| Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 11817 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11818 | add_suggestion(su, &su->su_ga, preword, |
| 11819 | sp->ts_fidx - repextra, |
| 11820 | score, 0, FALSE, lp->lp_sallang, FALSE); |
| Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 11821 | |
| 11822 | if (su->su_badflags & WF_MIXCAP) |
| 11823 | { |
| 11824 | /* We really don't know if the word should be |
| 11825 | * upper or lower case, add both. */ |
| 11826 | c = captype(preword, NULL); |
| 11827 | if (c == 0 || c == WF_ALLCAP) |
| 11828 | { |
| 11829 | make_case_word(tword + sp->ts_splitoff, |
| 11830 | preword + sp->ts_prewordlen, |
| 11831 | c == 0 ? WF_ALLCAP : 0); |
| 11832 | |
| 11833 | add_suggestion(su, &su->su_ga, preword, |
| 11834 | sp->ts_fidx - repextra, |
| 11835 | score + SCORE_ICASE, 0, FALSE, |
| 11836 | lp->lp_sallang, FALSE); |
| 11837 | } |
| 11838 | } |
| 11839 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11840 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11841 | } |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11842 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11843 | /* |
| 11844 | * Try word split and/or compounding. |
| 11845 | */ |
| 11846 | if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends) |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 11847 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11848 | /* Don't split halfway a character. */ |
| 11849 | && (!has_mbyte || sp->ts_tcharlen == 0) |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 11850 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11851 | ) |
| 11852 | { |
| 11853 | int try_compound; |
| 11854 | int try_split; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11855 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11856 | /* If past the end of the bad word don't try a split. |
| 11857 | * Otherwise try changing the next word. E.g., find |
| 11858 | * suggestions for "the the" where the second "the" is |
| 11859 | * different. It's done like a split. |
| 11860 | * TODO: word split for soundfold words */ |
| 11861 | try_split = (sp->ts_fidx - repextra < su->su_badlen) |
| 11862 | && !soundfold; |
| 11863 | |
| 11864 | /* Get here in several situations: |
| 11865 | * 1. The word in the tree ends: |
| 11866 | * If the word allows compounding try that. Otherwise try |
| 11867 | * a split by inserting a space. For both check that a |
| 11868 | * valid words starts at fword[sp->ts_fidx]. |
| 11869 | * For NOBREAK do like compounding to be able to check if |
| 11870 | * the next word is valid. |
| 11871 | * 2. The badword does end, but it was due to a change (e.g., |
| 11872 | * a swap). No need to split, but do check that the |
| 11873 | * following word is valid. |
| 11874 | * 3. The badword and the word in the tree end. It may still |
| 11875 | * be possible to compound another (short) word. |
| 11876 | */ |
| 11877 | try_compound = FALSE; |
| 11878 | if (!soundfold |
| 11879 | && slang->sl_compprog != NULL |
| 11880 | && ((unsigned)flags >> 24) != 0 |
| 11881 | && sp->ts_twordlen - sp->ts_splitoff |
| 11882 | >= slang->sl_compminlen |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11883 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11884 | && (!has_mbyte |
| 11885 | || slang->sl_compminlen == 0 |
| 11886 | || mb_charlen(tword + sp->ts_splitoff) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 11887 | >= slang->sl_compminlen) |
| 11888 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11889 | && (slang->sl_compsylmax < MAXWLEN |
| 11890 | || sp->ts_complen + 1 - sp->ts_compsplit |
| 11891 | < slang->sl_compmax) |
| Bram Moolenaar | 9f94b05 | 2008-11-30 20:12:46 +0000 | [diff] [blame] | 11892 | && (can_be_compound(sp, slang, |
| 11893 | compflags, ((unsigned)flags >> 24)))) |
| 11894 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11895 | { |
| 11896 | try_compound = TRUE; |
| 11897 | compflags[sp->ts_complen] = ((unsigned)flags >> 24); |
| 11898 | compflags[sp->ts_complen + 1] = NUL; |
| 11899 | } |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11900 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11901 | /* For NOBREAK we never try splitting, it won't make any word |
| 11902 | * valid. */ |
| 11903 | if (slang->sl_nobreak) |
| 11904 | try_compound = TRUE; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11905 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11906 | /* If we could add a compound word, and it's also possible to |
| 11907 | * split at this point, do the split first and set |
| 11908 | * TSF_DIDSPLIT to avoid doing it again. */ |
| 11909 | else if (!fword_ends |
| 11910 | && try_compound |
| 11911 | && (sp->ts_flags & TSF_DIDSPLIT) == 0) |
| 11912 | { |
| 11913 | try_compound = FALSE; |
| 11914 | sp->ts_flags |= TSF_DIDSPLIT; |
| 11915 | --sp->ts_curi; /* do the same NUL again */ |
| 11916 | compflags[sp->ts_complen] = NUL; |
| 11917 | } |
| 11918 | else |
| 11919 | sp->ts_flags &= ~TSF_DIDSPLIT; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11920 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11921 | if (try_split || try_compound) |
| 11922 | { |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11923 | if (!try_compound && (!fword_ends || !goodword_ends)) |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11924 | { |
| 11925 | /* If we're going to split need to check that the |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 11926 | * words so far are valid for compounding. If there |
| 11927 | * is only one word it must not have the NEEDCOMPOUND |
| 11928 | * flag. */ |
| 11929 | if (sp->ts_complen == sp->ts_compsplit |
| 11930 | && (flags & WF_NEEDCOMP)) |
| 11931 | break; |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 11932 | p = preword; |
| 11933 | while (*skiptowhite(p) != NUL) |
| 11934 | p = skipwhite(skiptowhite(p)); |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11935 | if (sp->ts_complen > sp->ts_compsplit |
| Bram Moolenaar | e52325c | 2005-08-22 22:54:29 +0000 | [diff] [blame] | 11936 | && !can_compound(slang, p, |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 11937 | compflags + sp->ts_compsplit)) |
| 11938 | break; |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 11939 | |
| 11940 | if (slang->sl_nosplitsugs) |
| 11941 | newscore += SCORE_SPLIT_NO; |
| 11942 | else |
| 11943 | newscore += SCORE_SPLIT; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11944 | |
| 11945 | /* Give a bonus to words seen before. */ |
| 11946 | newscore = score_wordcount_adj(slang, newscore, |
| 11947 | preword + sp->ts_prewordlen, TRUE); |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11948 | } |
| 11949 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11950 | if (TRY_DEEPER(su, stack, depth, newscore)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11951 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11952 | go_deeper(stack, depth, newscore); |
| 11953 | #ifdef DEBUG_TRIEWALK |
| 11954 | if (!try_compound && !fword_ends) |
| 11955 | sprintf(changename[depth], "%.*s-%s: split", |
| 11956 | sp->ts_twordlen, tword, fword + sp->ts_fidx); |
| 11957 | else |
| 11958 | sprintf(changename[depth], "%.*s-%s: compound", |
| 11959 | sp->ts_twordlen, tword, fword + sp->ts_fidx); |
| 11960 | #endif |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11961 | /* Save things to be restored at STATE_SPLITUNDO. */ |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 11962 | sp->ts_save_badflags = su->su_badflags; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11963 | sp->ts_state = STATE_SPLITUNDO; |
| 11964 | |
| 11965 | ++depth; |
| 11966 | sp = &stack[depth]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 11967 | |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11968 | /* Append a space to preword when splitting. */ |
| 11969 | if (!try_compound && !fword_ends) |
| 11970 | STRCAT(preword, " "); |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 11971 | sp->ts_prewordlen = (char_u)STRLEN(preword); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 11972 | sp->ts_splitoff = sp->ts_twordlen; |
| Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 11973 | sp->ts_splitfidx = sp->ts_fidx; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11974 | |
| 11975 | /* If the badword has a non-word character at this |
| 11976 | * position skip it. That means replacing the |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11977 | * non-word character with a space. Always skip a |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 11978 | * character when the word ends. But only when the |
| 11979 | * good word can end. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 11980 | if (((!try_compound && !spell_iswordp_nmw(fword |
| 11981 | + sp->ts_fidx)) |
| 11982 | || fword_ends) |
| 11983 | && fword[sp->ts_fidx] != NUL |
| 11984 | && goodword_ends) |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11985 | { |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11986 | int l; |
| 11987 | |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11988 | #ifdef FEAT_MBYTE |
| 11989 | if (has_mbyte) |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11990 | l = MB_BYTE2LEN(fword[sp->ts_fidx]); |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 11991 | else |
| 11992 | #endif |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11993 | l = 1; |
| 11994 | if (fword_ends) |
| 11995 | { |
| 11996 | /* Copy the skipped character to preword. */ |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 11997 | mch_memmove(preword + sp->ts_prewordlen, |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 11998 | fword + sp->ts_fidx, l); |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 11999 | sp->ts_prewordlen += l; |
| 12000 | preword[sp->ts_prewordlen] = NUL; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 12001 | } |
| 12002 | else |
| 12003 | sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; |
| 12004 | sp->ts_fidx += l; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 12005 | } |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 12006 | |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 12007 | /* When compounding include compound flag in |
| 12008 | * compflags[] (already set above). When splitting we |
| 12009 | * may start compounding over again. */ |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 12010 | if (try_compound) |
| Bram Moolenaar | 5195e45 | 2005-08-19 20:32:47 +0000 | [diff] [blame] | 12011 | ++sp->ts_complen; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 12012 | else |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 12013 | sp->ts_compsplit = sp->ts_complen; |
| 12014 | sp->ts_prefixdepth = PFD_NOPREFIX; |
| Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 12015 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 12016 | /* set su->su_badflags to the caps type at this |
| 12017 | * position */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12018 | #ifdef FEAT_MBYTE |
| 12019 | if (has_mbyte) |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 12020 | n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12021 | else |
| 12022 | #endif |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 12023 | n = sp->ts_fidx; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 12024 | su->su_badflags = badword_captype(su->su_badptr + n, |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 12025 | su->su_badptr + su->su_badlen); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12026 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12027 | /* Restart at top of the tree. */ |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 12028 | sp->ts_arridx = 0; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 12029 | |
| 12030 | /* If there are postponed prefixes, try these too. */ |
| 12031 | if (pbyts != NULL) |
| 12032 | { |
| 12033 | byts = pbyts; |
| 12034 | idxs = pidxs; |
| 12035 | sp->ts_prefixdepth = PFD_PREFIXTREE; |
| 12036 | sp->ts_state = STATE_NOPREFIX; |
| 12037 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12038 | } |
| 12039 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12040 | } |
| 12041 | break; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12042 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12043 | case STATE_SPLITUNDO: |
| 12044 | /* Undo the changes done for word split or compound word. */ |
| 12045 | su->su_badflags = sp->ts_save_badflags; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12046 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12047 | /* Continue looking for NUL bytes. */ |
| 12048 | sp->ts_state = STATE_START; |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 12049 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12050 | /* In case we went into the prefix tree. */ |
| 12051 | byts = fbyts; |
| 12052 | idxs = fidxs; |
| 12053 | break; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12054 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12055 | case STATE_ENDNUL: |
| 12056 | /* Past the NUL bytes in the node. */ |
| 12057 | su->su_badflags = sp->ts_save_badflags; |
| 12058 | if (fword[sp->ts_fidx] == NUL |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 12059 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12060 | && sp->ts_tcharlen == 0 |
| Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 12061 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12062 | ) |
| 12063 | { |
| 12064 | /* The badword ends, can't use STATE_PLAIN. */ |
| 12065 | sp->ts_state = STATE_DEL; |
| 12066 | break; |
| 12067 | } |
| 12068 | sp->ts_state = STATE_PLAIN; |
| 12069 | /*FALLTHROUGH*/ |
| 12070 | |
| 12071 | case STATE_PLAIN: |
| 12072 | /* |
| 12073 | * Go over all possible bytes at this node, add each to tword[] |
| 12074 | * and use child node. "ts_curi" is the index. |
| 12075 | */ |
| 12076 | arridx = sp->ts_arridx; |
| 12077 | if (sp->ts_curi > byts[arridx]) |
| 12078 | { |
| 12079 | /* Done all bytes at this node, do next state. When still at |
| 12080 | * already changed bytes skip the other tricks. */ |
| 12081 | if (sp->ts_fidx >= sp->ts_fidxtry) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12082 | sp->ts_state = STATE_DEL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12083 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12084 | sp->ts_state = STATE_FINAL; |
| 12085 | } |
| 12086 | else |
| 12087 | { |
| 12088 | arridx += sp->ts_curi++; |
| 12089 | c = byts[arridx]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12090 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12091 | /* Normal byte, go one level deeper. If it's not equal to the |
| 12092 | * byte in the bad word adjust the score. But don't even try |
| 12093 | * when the byte was already changed. And don't try when we |
| 12094 | * just deleted this byte, accepting it is always cheaper then |
| 12095 | * delete + substitute. */ |
| 12096 | if (c == fword[sp->ts_fidx] |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12097 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12098 | || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE) |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12099 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12100 | ) |
| 12101 | newscore = 0; |
| 12102 | else |
| 12103 | newscore = SCORE_SUBST; |
| 12104 | if ((newscore == 0 |
| 12105 | || (sp->ts_fidx >= sp->ts_fidxtry |
| 12106 | && ((sp->ts_flags & TSF_DIDDEL) == 0 |
| 12107 | || c != fword[sp->ts_delidx]))) |
| 12108 | && TRY_DEEPER(su, stack, depth, newscore)) |
| 12109 | { |
| 12110 | go_deeper(stack, depth, newscore); |
| 12111 | #ifdef DEBUG_TRIEWALK |
| 12112 | if (newscore > 0) |
| 12113 | sprintf(changename[depth], "%.*s-%s: subst %c to %c", |
| 12114 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12115 | fword[sp->ts_fidx], c); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12116 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12117 | sprintf(changename[depth], "%.*s-%s: accept %c", |
| 12118 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12119 | fword[sp->ts_fidx]); |
| 12120 | #endif |
| 12121 | ++depth; |
| 12122 | sp = &stack[depth]; |
| 12123 | ++sp->ts_fidx; |
| 12124 | tword[sp->ts_twordlen++] = c; |
| 12125 | sp->ts_arridx = idxs[arridx]; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12126 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12127 | if (newscore == SCORE_SUBST) |
| 12128 | sp->ts_isdiff = DIFF_YES; |
| 12129 | if (has_mbyte) |
| 12130 | { |
| 12131 | /* Multi-byte characters are a bit complicated to |
| 12132 | * handle: They differ when any of the bytes differ |
| 12133 | * and then their length may also differ. */ |
| 12134 | if (sp->ts_tcharlen == 0) |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12135 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12136 | /* First byte. */ |
| 12137 | sp->ts_tcharidx = 0; |
| 12138 | sp->ts_tcharlen = MB_BYTE2LEN(c); |
| 12139 | sp->ts_fcharstart = sp->ts_fidx - 1; |
| 12140 | sp->ts_isdiff = (newscore != 0) |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12141 | ? DIFF_YES : DIFF_NONE; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12142 | } |
| 12143 | else if (sp->ts_isdiff == DIFF_INSERT) |
| 12144 | /* When inserting trail bytes don't advance in the |
| 12145 | * bad word. */ |
| 12146 | --sp->ts_fidx; |
| 12147 | if (++sp->ts_tcharidx == sp->ts_tcharlen) |
| 12148 | { |
| 12149 | /* Last byte of character. */ |
| 12150 | if (sp->ts_isdiff == DIFF_YES) |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12151 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12152 | /* Correct ts_fidx for the byte length of the |
| 12153 | * character (we didn't check that before). */ |
| 12154 | sp->ts_fidx = sp->ts_fcharstart |
| 12155 | + MB_BYTE2LEN( |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12156 | fword[sp->ts_fcharstart]); |
| 12157 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12158 | /* For changing a composing character adjust |
| 12159 | * the score from SCORE_SUBST to |
| 12160 | * SCORE_SUBCOMP. */ |
| 12161 | if (enc_utf8 |
| 12162 | && utf_iscomposing( |
| 12163 | mb_ptr2char(tword |
| 12164 | + sp->ts_twordlen |
| Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 12165 | - sp->ts_tcharlen)) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12166 | && utf_iscomposing( |
| 12167 | mb_ptr2char(fword |
| Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 12168 | + sp->ts_fcharstart))) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12169 | sp->ts_score -= |
| Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 12170 | SCORE_SUBST - SCORE_SUBCOMP; |
| 12171 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12172 | /* For a similar character adjust score from |
| 12173 | * SCORE_SUBST to SCORE_SIMILAR. */ |
| 12174 | else if (!soundfold |
| 12175 | && slang->sl_has_map |
| 12176 | && similar_chars(slang, |
| 12177 | mb_ptr2char(tword |
| 12178 | + sp->ts_twordlen |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12179 | - sp->ts_tcharlen), |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12180 | mb_ptr2char(fword |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12181 | + sp->ts_fcharstart))) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12182 | sp->ts_score -= |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12183 | SCORE_SUBST - SCORE_SIMILAR; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12184 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12185 | else if (sp->ts_isdiff == DIFF_INSERT |
| 12186 | && sp->ts_twordlen > sp->ts_tcharlen) |
| 12187 | { |
| 12188 | p = tword + sp->ts_twordlen - sp->ts_tcharlen; |
| 12189 | c = mb_ptr2char(p); |
| 12190 | if (enc_utf8 && utf_iscomposing(c)) |
| 12191 | { |
| 12192 | /* Inserting a composing char doesn't |
| 12193 | * count that much. */ |
| 12194 | sp->ts_score -= SCORE_INS - SCORE_INSCOMP; |
| 12195 | } |
| 12196 | else |
| 12197 | { |
| 12198 | /* If the previous character was the same, |
| 12199 | * thus doubling a character, give a bonus |
| 12200 | * to the score. Also for the soundfold |
| 12201 | * tree (might seem illogical but does |
| 12202 | * give better scores). */ |
| 12203 | mb_ptr_back(tword, p); |
| 12204 | if (c == mb_ptr2char(p)) |
| 12205 | sp->ts_score -= SCORE_INS |
| 12206 | - SCORE_INSDUP; |
| 12207 | } |
| 12208 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12209 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12210 | /* Starting a new char, reset the length. */ |
| 12211 | sp->ts_tcharlen = 0; |
| 12212 | } |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 12213 | } |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12214 | else |
| 12215 | #endif |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 12216 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12217 | /* If we found a similar char adjust the score. |
| 12218 | * We do this after calling go_deeper() because |
| 12219 | * it's slow. */ |
| 12220 | if (newscore != 0 |
| 12221 | && !soundfold |
| 12222 | && slang->sl_has_map |
| 12223 | && similar_chars(slang, |
| 12224 | c, fword[sp->ts_fidx - 1])) |
| 12225 | sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 12226 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12227 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12228 | } |
| 12229 | break; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12230 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12231 | case STATE_DEL: |
| 12232 | #ifdef FEAT_MBYTE |
| 12233 | /* When past the first byte of a multi-byte char don't try |
| 12234 | * delete/insert/swap a character. */ |
| 12235 | if (has_mbyte && sp->ts_tcharlen > 0) |
| 12236 | { |
| 12237 | sp->ts_state = STATE_FINAL; |
| 12238 | break; |
| 12239 | } |
| 12240 | #endif |
| 12241 | /* |
| 12242 | * Try skipping one character in the bad word (delete it). |
| 12243 | */ |
| 12244 | sp->ts_state = STATE_INS_PREP; |
| 12245 | sp->ts_curi = 1; |
| 12246 | if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*') |
| 12247 | /* Deleting a vowel at the start of a word counts less, see |
| 12248 | * soundalike_score(). */ |
| 12249 | newscore = 2 * SCORE_DEL / 3; |
| 12250 | else |
| 12251 | newscore = SCORE_DEL; |
| 12252 | if (fword[sp->ts_fidx] != NUL |
| 12253 | && TRY_DEEPER(su, stack, depth, newscore)) |
| 12254 | { |
| 12255 | go_deeper(stack, depth, newscore); |
| 12256 | #ifdef DEBUG_TRIEWALK |
| 12257 | sprintf(changename[depth], "%.*s-%s: delete %c", |
| 12258 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12259 | fword[sp->ts_fidx]); |
| 12260 | #endif |
| 12261 | ++depth; |
| 12262 | |
| 12263 | /* Remember what character we deleted, so that we can avoid |
| 12264 | * inserting it again. */ |
| 12265 | stack[depth].ts_flags |= TSF_DIDDEL; |
| 12266 | stack[depth].ts_delidx = sp->ts_fidx; |
| 12267 | |
| 12268 | /* Advance over the character in fword[]. Give a bonus to the |
| 12269 | * score if the same character is following "nn" -> "n". It's |
| 12270 | * a bit illogical for soundfold tree but it does give better |
| 12271 | * results. */ |
| 12272 | #ifdef FEAT_MBYTE |
| 12273 | if (has_mbyte) |
| 12274 | { |
| 12275 | c = mb_ptr2char(fword + sp->ts_fidx); |
| 12276 | stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]); |
| 12277 | if (enc_utf8 && utf_iscomposing(c)) |
| 12278 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP; |
| 12279 | else if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) |
| 12280 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 12281 | } |
| 12282 | else |
| 12283 | #endif |
| 12284 | { |
| 12285 | ++stack[depth].ts_fidx; |
| 12286 | if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) |
| 12287 | stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; |
| 12288 | } |
| 12289 | break; |
| 12290 | } |
| 12291 | /*FALLTHROUGH*/ |
| 12292 | |
| 12293 | case STATE_INS_PREP: |
| 12294 | if (sp->ts_flags & TSF_DIDDEL) |
| 12295 | { |
| 12296 | /* If we just deleted a byte then inserting won't make sense, |
| 12297 | * a substitute is always cheaper. */ |
| 12298 | sp->ts_state = STATE_SWAP; |
| 12299 | break; |
| 12300 | } |
| 12301 | |
| 12302 | /* skip over NUL bytes */ |
| 12303 | n = sp->ts_arridx; |
| 12304 | for (;;) |
| 12305 | { |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12306 | if (sp->ts_curi > byts[n]) |
| 12307 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12308 | /* Only NUL bytes at this node, go to next state. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12309 | sp->ts_state = STATE_SWAP; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12310 | break; |
| 12311 | } |
| 12312 | if (byts[n + sp->ts_curi] != NUL) |
| 12313 | { |
| 12314 | /* Found a byte to insert. */ |
| 12315 | sp->ts_state = STATE_INS; |
| 12316 | break; |
| 12317 | } |
| 12318 | ++sp->ts_curi; |
| 12319 | } |
| 12320 | break; |
| 12321 | |
| 12322 | /*FALLTHROUGH*/ |
| 12323 | |
| 12324 | case STATE_INS: |
| 12325 | /* Insert one byte. Repeat this for each possible byte at this |
| 12326 | * node. */ |
| 12327 | n = sp->ts_arridx; |
| 12328 | if (sp->ts_curi > byts[n]) |
| 12329 | { |
| 12330 | /* Done all bytes at this node, go to next state. */ |
| 12331 | sp->ts_state = STATE_SWAP; |
| 12332 | break; |
| 12333 | } |
| 12334 | |
| 12335 | /* Do one more byte at this node, but: |
| 12336 | * - Skip NUL bytes. |
| 12337 | * - Skip the byte if it's equal to the byte in the word, |
| 12338 | * accepting that byte is always better. |
| 12339 | */ |
| 12340 | n += sp->ts_curi++; |
| 12341 | c = byts[n]; |
| 12342 | if (soundfold && sp->ts_twordlen == 0 && c == '*') |
| 12343 | /* Inserting a vowel at the start of a word counts less, |
| 12344 | * see soundalike_score(). */ |
| 12345 | newscore = 2 * SCORE_INS / 3; |
| 12346 | else |
| 12347 | newscore = SCORE_INS; |
| 12348 | if (c != fword[sp->ts_fidx] |
| 12349 | && TRY_DEEPER(su, stack, depth, newscore)) |
| 12350 | { |
| 12351 | go_deeper(stack, depth, newscore); |
| 12352 | #ifdef DEBUG_TRIEWALK |
| 12353 | sprintf(changename[depth], "%.*s-%s: insert %c", |
| 12354 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12355 | c); |
| 12356 | #endif |
| 12357 | ++depth; |
| 12358 | sp = &stack[depth]; |
| 12359 | tword[sp->ts_twordlen++] = c; |
| 12360 | sp->ts_arridx = idxs[n]; |
| 12361 | #ifdef FEAT_MBYTE |
| 12362 | if (has_mbyte) |
| 12363 | { |
| 12364 | fl = MB_BYTE2LEN(c); |
| 12365 | if (fl > 1) |
| 12366 | { |
| 12367 | /* There are following bytes for the same character. |
| 12368 | * We must find all bytes before trying |
| 12369 | * delete/insert/swap/etc. */ |
| 12370 | sp->ts_tcharlen = fl; |
| 12371 | sp->ts_tcharidx = 1; |
| 12372 | sp->ts_isdiff = DIFF_INSERT; |
| 12373 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12374 | } |
| 12375 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12376 | fl = 1; |
| 12377 | if (fl == 1) |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12378 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12379 | { |
| 12380 | /* If the previous character was the same, thus doubling a |
| 12381 | * character, give a bonus to the score. Also for |
| 12382 | * soundfold words (illogical but does give a better |
| 12383 | * score). */ |
| 12384 | if (sp->ts_twordlen >= 2 |
| Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 12385 | && tword[sp->ts_twordlen - 2] == c) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12386 | sp->ts_score -= SCORE_INS - SCORE_INSDUP; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12387 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12388 | } |
| 12389 | break; |
| 12390 | |
| 12391 | case STATE_SWAP: |
| 12392 | /* |
| 12393 | * Swap two bytes in the bad word: "12" -> "21". |
| 12394 | * We change "fword" here, it's changed back afterwards at |
| 12395 | * STATE_UNSWAP. |
| 12396 | */ |
| 12397 | p = fword + sp->ts_fidx; |
| 12398 | c = *p; |
| 12399 | if (c == NUL) |
| 12400 | { |
| 12401 | /* End of word, can't swap or replace. */ |
| 12402 | sp->ts_state = STATE_FINAL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12403 | break; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12404 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12405 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12406 | /* Don't swap if the first character is not a word character. |
| 12407 | * SWAP3 etc. also don't make sense then. */ |
| 12408 | if (!soundfold && !spell_iswordp(p, curbuf)) |
| 12409 | { |
| 12410 | sp->ts_state = STATE_REP_INI; |
| 12411 | break; |
| 12412 | } |
| Bram Moolenaar | bb15b65 | 2005-10-03 21:52:09 +0000 | [diff] [blame] | 12413 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12414 | #ifdef FEAT_MBYTE |
| 12415 | if (has_mbyte) |
| 12416 | { |
| 12417 | n = mb_cptr2len(p); |
| 12418 | c = mb_ptr2char(p); |
| Bram Moolenaar | 3dcfbf7 | 2007-08-05 16:33:12 +0000 | [diff] [blame] | 12419 | if (p[n] == NUL) |
| 12420 | c2 = NUL; |
| 12421 | else if (!soundfold && !spell_iswordp(p + n, curbuf)) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12422 | c2 = c; /* don't swap non-word char */ |
| 12423 | else |
| 12424 | c2 = mb_ptr2char(p + n); |
| 12425 | } |
| 12426 | else |
| 12427 | #endif |
| 12428 | { |
| Bram Moolenaar | 3dcfbf7 | 2007-08-05 16:33:12 +0000 | [diff] [blame] | 12429 | if (p[1] == NUL) |
| 12430 | c2 = NUL; |
| 12431 | else if (!soundfold && !spell_iswordp(p + 1, curbuf)) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12432 | c2 = c; /* don't swap non-word char */ |
| 12433 | else |
| 12434 | c2 = p[1]; |
| 12435 | } |
| Bram Moolenaar | bb15b65 | 2005-10-03 21:52:09 +0000 | [diff] [blame] | 12436 | |
| Bram Moolenaar | 3dcfbf7 | 2007-08-05 16:33:12 +0000 | [diff] [blame] | 12437 | /* When the second character is NUL we can't swap. */ |
| 12438 | if (c2 == NUL) |
| 12439 | { |
| 12440 | sp->ts_state = STATE_REP_INI; |
| 12441 | break; |
| 12442 | } |
| 12443 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12444 | /* When characters are identical, swap won't do anything. |
| 12445 | * Also get here if the second char is not a word character. */ |
| 12446 | if (c == c2) |
| 12447 | { |
| 12448 | sp->ts_state = STATE_SWAP3; |
| 12449 | break; |
| 12450 | } |
| 12451 | if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP)) |
| 12452 | { |
| 12453 | go_deeper(stack, depth, SCORE_SWAP); |
| 12454 | #ifdef DEBUG_TRIEWALK |
| 12455 | sprintf(changename[depth], "%.*s-%s: swap %c and %c", |
| 12456 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12457 | c, c2); |
| 12458 | #endif |
| 12459 | sp->ts_state = STATE_UNSWAP; |
| 12460 | ++depth; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12461 | #ifdef FEAT_MBYTE |
| 12462 | if (has_mbyte) |
| 12463 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12464 | fl = mb_char2len(c2); |
| 12465 | mch_memmove(p, p + n, fl); |
| 12466 | mb_char2bytes(c, p + fl); |
| 12467 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12468 | } |
| 12469 | else |
| 12470 | #endif |
| Bram Moolenaar | bb15b65 | 2005-10-03 21:52:09 +0000 | [diff] [blame] | 12471 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12472 | p[0] = c2; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12473 | p[1] = c; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12474 | stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12475 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12476 | } |
| 12477 | else |
| 12478 | /* If this swap doesn't work then SWAP3 won't either. */ |
| 12479 | sp->ts_state = STATE_REP_INI; |
| 12480 | break; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12481 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12482 | case STATE_UNSWAP: |
| 12483 | /* Undo the STATE_SWAP swap: "21" -> "12". */ |
| 12484 | p = fword + sp->ts_fidx; |
| 12485 | #ifdef FEAT_MBYTE |
| 12486 | if (has_mbyte) |
| 12487 | { |
| 12488 | n = MB_BYTE2LEN(*p); |
| 12489 | c = mb_ptr2char(p + n); |
| 12490 | mch_memmove(p + MB_BYTE2LEN(p[n]), p, n); |
| 12491 | mb_char2bytes(c, p); |
| 12492 | } |
| 12493 | else |
| 12494 | #endif |
| 12495 | { |
| 12496 | c = *p; |
| 12497 | *p = p[1]; |
| 12498 | p[1] = c; |
| 12499 | } |
| 12500 | /*FALLTHROUGH*/ |
| 12501 | |
| 12502 | case STATE_SWAP3: |
| 12503 | /* Swap two bytes, skipping one: "123" -> "321". We change |
| 12504 | * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */ |
| 12505 | p = fword + sp->ts_fidx; |
| 12506 | #ifdef FEAT_MBYTE |
| 12507 | if (has_mbyte) |
| 12508 | { |
| 12509 | n = mb_cptr2len(p); |
| 12510 | c = mb_ptr2char(p); |
| 12511 | fl = mb_cptr2len(p + n); |
| 12512 | c2 = mb_ptr2char(p + n); |
| 12513 | if (!soundfold && !spell_iswordp(p + n + fl, curbuf)) |
| 12514 | c3 = c; /* don't swap non-word char */ |
| 12515 | else |
| 12516 | c3 = mb_ptr2char(p + n + fl); |
| 12517 | } |
| 12518 | else |
| 12519 | #endif |
| 12520 | { |
| 12521 | c = *p; |
| 12522 | c2 = p[1]; |
| 12523 | if (!soundfold && !spell_iswordp(p + 2, curbuf)) |
| 12524 | c3 = c; /* don't swap non-word char */ |
| 12525 | else |
| 12526 | c3 = p[2]; |
| 12527 | } |
| 12528 | |
| 12529 | /* When characters are identical: "121" then SWAP3 result is |
| 12530 | * identical, ROT3L result is same as SWAP: "211", ROT3L result is |
| 12531 | * same as SWAP on next char: "112". Thus skip all swapping. |
| 12532 | * Also skip when c3 is NUL. |
| 12533 | * Also get here when the third character is not a word character. |
| 12534 | * Second character may any char: "a.b" -> "b.a" */ |
| 12535 | if (c == c3 || c3 == NUL) |
| 12536 | { |
| 12537 | sp->ts_state = STATE_REP_INI; |
| 12538 | break; |
| 12539 | } |
| 12540 | if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) |
| 12541 | { |
| 12542 | go_deeper(stack, depth, SCORE_SWAP3); |
| 12543 | #ifdef DEBUG_TRIEWALK |
| 12544 | sprintf(changename[depth], "%.*s-%s: swap3 %c and %c", |
| 12545 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12546 | c, c3); |
| 12547 | #endif |
| 12548 | sp->ts_state = STATE_UNSWAP3; |
| 12549 | ++depth; |
| 12550 | #ifdef FEAT_MBYTE |
| 12551 | if (has_mbyte) |
| 12552 | { |
| 12553 | tl = mb_char2len(c3); |
| 12554 | mch_memmove(p, p + n + fl, tl); |
| 12555 | mb_char2bytes(c2, p + tl); |
| 12556 | mb_char2bytes(c, p + fl + tl); |
| 12557 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; |
| 12558 | } |
| 12559 | else |
| 12560 | #endif |
| 12561 | { |
| 12562 | p[0] = p[2]; |
| 12563 | p[2] = c; |
| 12564 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 12565 | } |
| 12566 | } |
| 12567 | else |
| 12568 | sp->ts_state = STATE_REP_INI; |
| 12569 | break; |
| 12570 | |
| 12571 | case STATE_UNSWAP3: |
| 12572 | /* Undo STATE_SWAP3: "321" -> "123" */ |
| 12573 | p = fword + sp->ts_fidx; |
| 12574 | #ifdef FEAT_MBYTE |
| 12575 | if (has_mbyte) |
| 12576 | { |
| 12577 | n = MB_BYTE2LEN(*p); |
| 12578 | c2 = mb_ptr2char(p + n); |
| 12579 | fl = MB_BYTE2LEN(p[n]); |
| 12580 | c = mb_ptr2char(p + n + fl); |
| 12581 | tl = MB_BYTE2LEN(p[n + fl]); |
| 12582 | mch_memmove(p + fl + tl, p, n); |
| 12583 | mb_char2bytes(c, p); |
| 12584 | mb_char2bytes(c2, p + tl); |
| 12585 | p = p + tl; |
| 12586 | } |
| 12587 | else |
| 12588 | #endif |
| 12589 | { |
| 12590 | c = *p; |
| 12591 | *p = p[2]; |
| 12592 | p[2] = c; |
| 12593 | ++p; |
| 12594 | } |
| 12595 | |
| 12596 | if (!soundfold && !spell_iswordp(p, curbuf)) |
| 12597 | { |
| 12598 | /* Middle char is not a word char, skip the rotate. First and |
| 12599 | * third char were already checked at swap and swap3. */ |
| 12600 | sp->ts_state = STATE_REP_INI; |
| 12601 | break; |
| 12602 | } |
| 12603 | |
| 12604 | /* Rotate three characters left: "123" -> "231". We change |
| 12605 | * "fword" here, it's changed back afterwards at STATE_UNROT3L. */ |
| 12606 | if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) |
| 12607 | { |
| 12608 | go_deeper(stack, depth, SCORE_SWAP3); |
| 12609 | #ifdef DEBUG_TRIEWALK |
| 12610 | p = fword + sp->ts_fidx; |
| 12611 | sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c", |
| 12612 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12613 | p[0], p[1], p[2]); |
| 12614 | #endif |
| 12615 | sp->ts_state = STATE_UNROT3L; |
| 12616 | ++depth; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12617 | p = fword + sp->ts_fidx; |
| 12618 | #ifdef FEAT_MBYTE |
| 12619 | if (has_mbyte) |
| 12620 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 12621 | n = mb_cptr2len(p); |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12622 | c = mb_ptr2char(p); |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 12623 | fl = mb_cptr2len(p + n); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12624 | fl += mb_cptr2len(p + n + fl); |
| 12625 | mch_memmove(p, p + n, fl); |
| 12626 | mb_char2bytes(c, p + fl); |
| 12627 | stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12628 | } |
| 12629 | else |
| 12630 | #endif |
| 12631 | { |
| 12632 | c = *p; |
| 12633 | *p = p[1]; |
| 12634 | p[1] = p[2]; |
| 12635 | p[2] = c; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12636 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12637 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12638 | } |
| 12639 | else |
| 12640 | sp->ts_state = STATE_REP_INI; |
| 12641 | break; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12642 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12643 | case STATE_UNROT3L: |
| 12644 | /* Undo ROT3L: "231" -> "123" */ |
| 12645 | p = fword + sp->ts_fidx; |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12646 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12647 | if (has_mbyte) |
| 12648 | { |
| 12649 | n = MB_BYTE2LEN(*p); |
| 12650 | n += MB_BYTE2LEN(p[n]); |
| 12651 | c = mb_ptr2char(p + n); |
| 12652 | tl = MB_BYTE2LEN(p[n]); |
| 12653 | mch_memmove(p + tl, p, n); |
| 12654 | mb_char2bytes(c, p); |
| 12655 | } |
| 12656 | else |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12657 | #endif |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12658 | { |
| 12659 | c = p[2]; |
| 12660 | p[2] = p[1]; |
| 12661 | p[1] = *p; |
| 12662 | *p = c; |
| 12663 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12664 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12665 | /* Rotate three bytes right: "123" -> "312". We change "fword" |
| 12666 | * here, it's changed back afterwards at STATE_UNROT3R. */ |
| 12667 | if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) |
| 12668 | { |
| 12669 | go_deeper(stack, depth, SCORE_SWAP3); |
| 12670 | #ifdef DEBUG_TRIEWALK |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12671 | p = fword + sp->ts_fidx; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12672 | sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c", |
| 12673 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12674 | p[0], p[1], p[2]); |
| 12675 | #endif |
| 12676 | sp->ts_state = STATE_UNROT3R; |
| 12677 | ++depth; |
| 12678 | p = fword + sp->ts_fidx; |
| 12679 | #ifdef FEAT_MBYTE |
| 12680 | if (has_mbyte) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 12681 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12682 | n = mb_cptr2len(p); |
| 12683 | n += mb_cptr2len(p + n); |
| 12684 | c = mb_ptr2char(p + n); |
| 12685 | tl = mb_cptr2len(p + n); |
| 12686 | mch_memmove(p + tl, p, n); |
| 12687 | mb_char2bytes(c, p); |
| 12688 | stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 12689 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12690 | else |
| 12691 | #endif |
| 12692 | { |
| 12693 | c = p[2]; |
| 12694 | p[2] = p[1]; |
| 12695 | p[1] = *p; |
| 12696 | *p = c; |
| 12697 | stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
| 12698 | } |
| 12699 | } |
| 12700 | else |
| 12701 | sp->ts_state = STATE_REP_INI; |
| 12702 | break; |
| 12703 | |
| 12704 | case STATE_UNROT3R: |
| 12705 | /* Undo ROT3R: "312" -> "123" */ |
| 12706 | p = fword + sp->ts_fidx; |
| 12707 | #ifdef FEAT_MBYTE |
| 12708 | if (has_mbyte) |
| 12709 | { |
| 12710 | c = mb_ptr2char(p); |
| 12711 | tl = MB_BYTE2LEN(*p); |
| 12712 | n = MB_BYTE2LEN(p[tl]); |
| 12713 | n += MB_BYTE2LEN(p[tl + n]); |
| 12714 | mch_memmove(p, p + tl, n); |
| 12715 | mb_char2bytes(c, p + n); |
| 12716 | } |
| 12717 | else |
| 12718 | #endif |
| 12719 | { |
| 12720 | c = *p; |
| 12721 | *p = p[1]; |
| 12722 | p[1] = p[2]; |
| 12723 | p[2] = c; |
| 12724 | } |
| 12725 | /*FALLTHROUGH*/ |
| 12726 | |
| 12727 | case STATE_REP_INI: |
| 12728 | /* Check if matching with REP items from the .aff file would work. |
| 12729 | * Quickly skip if: |
| 12730 | * - there are no REP items and we are not in the soundfold trie |
| 12731 | * - the score is going to be too high anyway |
| 12732 | * - already applied a REP item or swapped here */ |
| 12733 | if ((lp->lp_replang == NULL && !soundfold) |
| 12734 | || sp->ts_score + SCORE_REP >= su->su_maxscore |
| 12735 | || sp->ts_fidx < sp->ts_fidxtry) |
| 12736 | { |
| 12737 | sp->ts_state = STATE_FINAL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12738 | break; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12739 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12740 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12741 | /* Use the first byte to quickly find the first entry that may |
| 12742 | * match. If the index is -1 there is none. */ |
| 12743 | if (soundfold) |
| 12744 | sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]]; |
| 12745 | else |
| 12746 | sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12747 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12748 | if (sp->ts_curi < 0) |
| 12749 | { |
| 12750 | sp->ts_state = STATE_FINAL; |
| 12751 | break; |
| 12752 | } |
| 12753 | |
| 12754 | sp->ts_state = STATE_REP; |
| 12755 | /*FALLTHROUGH*/ |
| 12756 | |
| 12757 | case STATE_REP: |
| 12758 | /* Try matching with REP items from the .aff file. For each match |
| 12759 | * replace the characters and check if the resulting word is |
| 12760 | * valid. */ |
| 12761 | p = fword + sp->ts_fidx; |
| 12762 | |
| 12763 | if (soundfold) |
| 12764 | gap = &slang->sl_repsal; |
| 12765 | else |
| 12766 | gap = &lp->lp_replang->sl_rep; |
| 12767 | while (sp->ts_curi < gap->ga_len) |
| 12768 | { |
| 12769 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; |
| 12770 | if (*ftp->ft_from != *p) |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 12771 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12772 | /* past possible matching entries */ |
| 12773 | sp->ts_curi = gap->ga_len; |
| 12774 | break; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 12775 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12776 | if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
| 12777 | && TRY_DEEPER(su, stack, depth, SCORE_REP)) |
| 12778 | { |
| 12779 | go_deeper(stack, depth, SCORE_REP); |
| 12780 | #ifdef DEBUG_TRIEWALK |
| 12781 | sprintf(changename[depth], "%.*s-%s: replace %s with %s", |
| 12782 | sp->ts_twordlen, tword, fword + sp->ts_fidx, |
| 12783 | ftp->ft_from, ftp->ft_to); |
| 12784 | #endif |
| 12785 | /* Need to undo this afterwards. */ |
| 12786 | sp->ts_state = STATE_REP_UNDO; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 12787 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12788 | /* Change the "from" to the "to" string. */ |
| 12789 | ++depth; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 12790 | fl = (int)STRLEN(ftp->ft_from); |
| 12791 | tl = (int)STRLEN(ftp->ft_to); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12792 | if (fl != tl) |
| 12793 | { |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 12794 | STRMOVE(p + tl, p + fl); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12795 | repextra += tl - fl; |
| 12796 | } |
| 12797 | mch_memmove(p, ftp->ft_to, tl); |
| 12798 | stack[depth].ts_fidxtry = sp->ts_fidx + tl; |
| 12799 | #ifdef FEAT_MBYTE |
| 12800 | stack[depth].ts_tcharlen = 0; |
| 12801 | #endif |
| 12802 | break; |
| 12803 | } |
| 12804 | } |
| 12805 | |
| 12806 | if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP) |
| 12807 | /* No (more) matches. */ |
| 12808 | sp->ts_state = STATE_FINAL; |
| 12809 | |
| 12810 | break; |
| 12811 | |
| 12812 | case STATE_REP_UNDO: |
| 12813 | /* Undo a REP replacement and continue with the next one. */ |
| 12814 | if (soundfold) |
| 12815 | gap = &slang->sl_repsal; |
| 12816 | else |
| 12817 | gap = &lp->lp_replang->sl_rep; |
| 12818 | ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1; |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 12819 | fl = (int)STRLEN(ftp->ft_from); |
| 12820 | tl = (int)STRLEN(ftp->ft_to); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12821 | p = fword + sp->ts_fidx; |
| 12822 | if (fl != tl) |
| 12823 | { |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 12824 | STRMOVE(p + fl, p + tl); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12825 | repextra -= tl - fl; |
| 12826 | } |
| 12827 | mch_memmove(p, ftp->ft_from, fl); |
| 12828 | sp->ts_state = STATE_REP; |
| 12829 | break; |
| 12830 | |
| 12831 | default: |
| 12832 | /* Did all possible states at this level, go up one level. */ |
| 12833 | --depth; |
| 12834 | |
| 12835 | if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE) |
| 12836 | { |
| 12837 | /* Continue in or go back to the prefix tree. */ |
| 12838 | byts = pbyts; |
| 12839 | idxs = pidxs; |
| 12840 | } |
| 12841 | |
| 12842 | /* Don't check for CTRL-C too often, it takes time. */ |
| 12843 | if (--breakcheckcount == 0) |
| 12844 | { |
| 12845 | ui_breakcheck(); |
| 12846 | breakcheckcount = 1000; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 12847 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12848 | } |
| 12849 | } |
| 12850 | } |
| 12851 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12852 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12853 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12854 | * Go one level deeper in the tree. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12855 | */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12856 | static void |
| 12857 | go_deeper(stack, depth, score_add) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12858 | trystate_T *stack; |
| 12859 | int depth; |
| 12860 | int score_add; |
| 12861 | { |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 12862 | stack[depth + 1] = stack[depth]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12863 | stack[depth + 1].ts_state = STATE_START; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 12864 | stack[depth + 1].ts_score = stack[depth].ts_score + score_add; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12865 | stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 12866 | stack[depth + 1].ts_flags = 0; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12867 | } |
| 12868 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 12869 | #ifdef FEAT_MBYTE |
| 12870 | /* |
| 12871 | * Case-folding may change the number of bytes: Count nr of chars in |
| 12872 | * fword[flen] and return the byte length of that many chars in "word". |
| 12873 | */ |
| 12874 | static int |
| 12875 | nofold_len(fword, flen, word) |
| 12876 | char_u *fword; |
| 12877 | int flen; |
| 12878 | char_u *word; |
| 12879 | { |
| 12880 | char_u *p; |
| 12881 | int i = 0; |
| 12882 | |
| 12883 | for (p = fword; p < fword + flen; mb_ptr_adv(p)) |
| 12884 | ++i; |
| 12885 | for (p = word; i > 0; mb_ptr_adv(p)) |
| 12886 | --i; |
| 12887 | return (int)(p - word); |
| 12888 | } |
| 12889 | #endif |
| 12890 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12891 | /* |
| 12892 | * "fword" is a good word with case folded. Find the matching keep-case |
| 12893 | * words and put it in "kword". |
| 12894 | * Theoretically there could be several keep-case words that result in the |
| 12895 | * same case-folded word, but we only find one... |
| 12896 | */ |
| 12897 | static void |
| 12898 | find_keepcap_word(slang, fword, kword) |
| 12899 | slang_T *slang; |
| 12900 | char_u *fword; |
| 12901 | char_u *kword; |
| 12902 | { |
| 12903 | char_u uword[MAXWLEN]; /* "fword" in upper-case */ |
| 12904 | int depth; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12905 | idx_T tryidx; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12906 | |
| 12907 | /* The following arrays are used at each depth in the tree. */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12908 | idx_T arridx[MAXWLEN]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12909 | int round[MAXWLEN]; |
| 12910 | int fwordidx[MAXWLEN]; |
| 12911 | int uwordidx[MAXWLEN]; |
| 12912 | int kwordlen[MAXWLEN]; |
| 12913 | |
| 12914 | int flen, ulen; |
| 12915 | int l; |
| 12916 | int len; |
| 12917 | int c; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12918 | idx_T lo, hi, m; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12919 | char_u *p; |
| 12920 | char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 12921 | idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12922 | |
| 12923 | if (byts == NULL) |
| 12924 | { |
| 12925 | /* array is empty: "cannot happen" */ |
| 12926 | *kword = NUL; |
| 12927 | return; |
| 12928 | } |
| 12929 | |
| 12930 | /* Make an all-cap version of "fword". */ |
| 12931 | allcap_copy(fword, uword); |
| 12932 | |
| 12933 | /* |
| 12934 | * Each character needs to be tried both case-folded and upper-case. |
| 12935 | * All this gets very complicated if we keep in mind that changing case |
| 12936 | * may change the byte length of a multi-byte character... |
| 12937 | */ |
| 12938 | depth = 0; |
| 12939 | arridx[0] = 0; |
| 12940 | round[0] = 0; |
| 12941 | fwordidx[0] = 0; |
| 12942 | uwordidx[0] = 0; |
| 12943 | kwordlen[0] = 0; |
| 12944 | while (depth >= 0) |
| 12945 | { |
| 12946 | if (fword[fwordidx[depth]] == NUL) |
| 12947 | { |
| 12948 | /* We are at the end of "fword". If the tree allows a word to end |
| 12949 | * here we have found a match. */ |
| 12950 | if (byts[arridx[depth] + 1] == 0) |
| 12951 | { |
| 12952 | kword[kwordlen[depth]] = NUL; |
| 12953 | return; |
| 12954 | } |
| 12955 | |
| 12956 | /* kword is getting too long, continue one level up */ |
| 12957 | --depth; |
| 12958 | } |
| 12959 | else if (++round[depth] > 2) |
| 12960 | { |
| 12961 | /* tried both fold-case and upper-case character, continue one |
| 12962 | * level up */ |
| 12963 | --depth; |
| 12964 | } |
| 12965 | else |
| 12966 | { |
| 12967 | /* |
| 12968 | * round[depth] == 1: Try using the folded-case character. |
| 12969 | * round[depth] == 2: Try using the upper-case character. |
| 12970 | */ |
| 12971 | #ifdef FEAT_MBYTE |
| 12972 | if (has_mbyte) |
| 12973 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 12974 | flen = mb_cptr2len(fword + fwordidx[depth]); |
| 12975 | ulen = mb_cptr2len(uword + uwordidx[depth]); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 12976 | } |
| 12977 | else |
| 12978 | #endif |
| 12979 | ulen = flen = 1; |
| 12980 | if (round[depth] == 1) |
| 12981 | { |
| 12982 | p = fword + fwordidx[depth]; |
| 12983 | l = flen; |
| 12984 | } |
| 12985 | else |
| 12986 | { |
| 12987 | p = uword + uwordidx[depth]; |
| 12988 | l = ulen; |
| 12989 | } |
| 12990 | |
| 12991 | for (tryidx = arridx[depth]; l > 0; --l) |
| 12992 | { |
| 12993 | /* Perform a binary search in the list of accepted bytes. */ |
| 12994 | len = byts[tryidx++]; |
| 12995 | c = *p++; |
| 12996 | lo = tryidx; |
| 12997 | hi = tryidx + len - 1; |
| 12998 | while (lo < hi) |
| 12999 | { |
| 13000 | m = (lo + hi) / 2; |
| 13001 | if (byts[m] > c) |
| 13002 | hi = m - 1; |
| 13003 | else if (byts[m] < c) |
| 13004 | lo = m + 1; |
| 13005 | else |
| 13006 | { |
| 13007 | lo = hi = m; |
| 13008 | break; |
| 13009 | } |
| 13010 | } |
| 13011 | |
| 13012 | /* Stop if there is no matching byte. */ |
| 13013 | if (hi < lo || byts[lo] != c) |
| 13014 | break; |
| 13015 | |
| 13016 | /* Continue at the child (if there is one). */ |
| 13017 | tryidx = idxs[lo]; |
| 13018 | } |
| 13019 | |
| 13020 | if (l == 0) |
| 13021 | { |
| 13022 | /* |
| 13023 | * Found the matching char. Copy it to "kword" and go a |
| 13024 | * level deeper. |
| 13025 | */ |
| 13026 | if (round[depth] == 1) |
| 13027 | { |
| 13028 | STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], |
| 13029 | flen); |
| 13030 | kwordlen[depth + 1] = kwordlen[depth] + flen; |
| 13031 | } |
| 13032 | else |
| 13033 | { |
| 13034 | STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], |
| 13035 | ulen); |
| 13036 | kwordlen[depth + 1] = kwordlen[depth] + ulen; |
| 13037 | } |
| 13038 | fwordidx[depth + 1] = fwordidx[depth] + flen; |
| 13039 | uwordidx[depth + 1] = uwordidx[depth] + ulen; |
| 13040 | |
| 13041 | ++depth; |
| 13042 | arridx[depth] = tryidx; |
| 13043 | round[depth] = 0; |
| 13044 | } |
| 13045 | } |
| 13046 | } |
| 13047 | |
| 13048 | /* Didn't find it: "cannot happen". */ |
| 13049 | *kword = NUL; |
| 13050 | } |
| 13051 | |
| 13052 | /* |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13053 | * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
| 13054 | * su->su_sga. |
| 13055 | */ |
| 13056 | static void |
| 13057 | score_comp_sal(su) |
| 13058 | suginfo_T *su; |
| 13059 | { |
| 13060 | langp_T *lp; |
| 13061 | char_u badsound[MAXWLEN]; |
| 13062 | int i; |
| 13063 | suggest_T *stp; |
| 13064 | suggest_T *sstp; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13065 | int score; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 13066 | int lpi; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13067 | |
| 13068 | if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) |
| 13069 | return; |
| 13070 | |
| 13071 | /* Use the sound-folding of the first language that supports it. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13072 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 13073 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13074 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13075 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 13076 | { |
| 13077 | /* soundfold the bad word */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 13078 | spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13079 | |
| 13080 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 13081 | { |
| 13082 | stp = &SUG(su->su_ga, i); |
| 13083 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13084 | /* Case-fold the suggested word, sound-fold it and compute the |
| 13085 | * sound-a-like score. */ |
| 13086 | score = stp_sal_score(stp, su, lp->lp_slang, badsound); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13087 | if (score < SCORE_MAXMAX) |
| 13088 | { |
| 13089 | /* Add the suggestion. */ |
| 13090 | sstp = &SUG(su->su_sga, su->su_sga.ga_len); |
| 13091 | sstp->st_word = vim_strsave(stp->st_word); |
| 13092 | if (sstp->st_word != NULL) |
| 13093 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13094 | sstp->st_wordlen = stp->st_wordlen; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13095 | sstp->st_score = score; |
| 13096 | sstp->st_altscore = 0; |
| 13097 | sstp->st_orglen = stp->st_orglen; |
| 13098 | ++su->su_sga.ga_len; |
| 13099 | } |
| 13100 | } |
| 13101 | } |
| 13102 | break; |
| 13103 | } |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 13104 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13105 | } |
| 13106 | |
| 13107 | /* |
| 13108 | * Combine the list of suggestions in su->su_ga and su->su_sga. |
| 13109 | * They are intwined. |
| 13110 | */ |
| 13111 | static void |
| 13112 | score_combine(su) |
| 13113 | suginfo_T *su; |
| 13114 | { |
| 13115 | int i; |
| 13116 | int j; |
| 13117 | garray_T ga; |
| 13118 | garray_T *gap; |
| 13119 | langp_T *lp; |
| 13120 | suggest_T *stp; |
| 13121 | char_u *p; |
| 13122 | char_u badsound[MAXWLEN]; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13123 | int round; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 13124 | int lpi; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13125 | slang_T *slang = NULL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13126 | |
| 13127 | /* Add the alternate score to su_ga. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13128 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13129 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13130 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13131 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 13132 | { |
| 13133 | /* soundfold the bad word */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13134 | slang = lp->lp_slang; |
| 13135 | spell_soundfold(slang, su->su_fbadword, TRUE, badsound); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13136 | |
| 13137 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| 13138 | { |
| 13139 | stp = &SUG(su->su_ga, i); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13140 | stp->st_altscore = stp_sal_score(stp, su, slang, badsound); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13141 | if (stp->st_altscore == SCORE_MAXMAX) |
| 13142 | stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; |
| 13143 | else |
| 13144 | stp->st_score = (stp->st_score * 3 |
| 13145 | + stp->st_altscore) / 4; |
| 13146 | stp->st_salscore = FALSE; |
| 13147 | } |
| 13148 | break; |
| 13149 | } |
| 13150 | } |
| 13151 | |
| Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 13152 | if (slang == NULL) /* Using "double" without sound folding. */ |
| 13153 | { |
| 13154 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, |
| 13155 | su->su_maxcount); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13156 | return; |
| Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 13157 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13158 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13159 | /* Add the alternate score to su_sga. */ |
| 13160 | for (i = 0; i < su->su_sga.ga_len; ++i) |
| 13161 | { |
| 13162 | stp = &SUG(su->su_sga, i); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13163 | stp->st_altscore = spell_edit_score(slang, |
| 13164 | su->su_badword, stp->st_word); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13165 | if (stp->st_score == SCORE_MAXMAX) |
| 13166 | stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; |
| 13167 | else |
| 13168 | stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; |
| 13169 | stp->st_salscore = TRUE; |
| 13170 | } |
| 13171 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13172 | /* Remove bad suggestions, sort the suggestions and truncate at "maxcount" |
| 13173 | * for both lists. */ |
| 13174 | check_suggestions(su, &su->su_ga); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13175 | (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13176 | check_suggestions(su, &su->su_sga); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13177 | (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
| 13178 | |
| 13179 | ga_init2(&ga, (int)sizeof(suginfo_T), 1); |
| 13180 | if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) |
| 13181 | return; |
| 13182 | |
| 13183 | stp = &SUG(ga, 0); |
| 13184 | for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) |
| 13185 | { |
| 13186 | /* round 1: get a suggestion from su_ga |
| 13187 | * round 2: get a suggestion from su_sga */ |
| 13188 | for (round = 1; round <= 2; ++round) |
| 13189 | { |
| 13190 | gap = round == 1 ? &su->su_ga : &su->su_sga; |
| 13191 | if (i < gap->ga_len) |
| 13192 | { |
| 13193 | /* Don't add a word if it's already there. */ |
| 13194 | p = SUG(*gap, i).st_word; |
| 13195 | for (j = 0; j < ga.ga_len; ++j) |
| 13196 | if (STRCMP(stp[j].st_word, p) == 0) |
| 13197 | break; |
| 13198 | if (j == ga.ga_len) |
| 13199 | stp[ga.ga_len++] = SUG(*gap, i); |
| 13200 | else |
| 13201 | vim_free(p); |
| 13202 | } |
| 13203 | } |
| 13204 | } |
| 13205 | |
| 13206 | ga_clear(&su->su_ga); |
| 13207 | ga_clear(&su->su_sga); |
| 13208 | |
| 13209 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| 13210 | if (ga.ga_len > su->su_maxcount) |
| 13211 | { |
| 13212 | for (i = su->su_maxcount; i < ga.ga_len; ++i) |
| 13213 | vim_free(stp[i].st_word); |
| 13214 | ga.ga_len = su->su_maxcount; |
| 13215 | } |
| 13216 | |
| 13217 | su->su_ga = ga; |
| 13218 | } |
| 13219 | |
| 13220 | /* |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13221 | * For the goodword in "stp" compute the soundalike score compared to the |
| 13222 | * badword. |
| 13223 | */ |
| 13224 | static int |
| 13225 | stp_sal_score(stp, su, slang, badsound) |
| 13226 | suggest_T *stp; |
| 13227 | suginfo_T *su; |
| 13228 | slang_T *slang; |
| 13229 | char_u *badsound; /* sound-folded badword */ |
| 13230 | { |
| 13231 | char_u *p; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13232 | char_u *pbad; |
| 13233 | char_u *pgood; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13234 | char_u badsound2[MAXWLEN]; |
| 13235 | char_u fword[MAXWLEN]; |
| 13236 | char_u goodsound[MAXWLEN]; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13237 | char_u goodword[MAXWLEN]; |
| 13238 | int lendiff; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13239 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13240 | lendiff = (int)(su->su_badlen - stp->st_orglen); |
| 13241 | if (lendiff >= 0) |
| 13242 | pbad = badsound; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13243 | else |
| 13244 | { |
| 13245 | /* soundfold the bad word with more characters following */ |
| 13246 | (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); |
| 13247 | |
| 13248 | /* When joining two words the sound often changes a lot. E.g., "t he" |
| 13249 | * sounds like "t h" while "the" sounds like "@". Avoid that by |
| 13250 | * removing the space. Don't do it when the good word also contains a |
| 13251 | * space. */ |
| 13252 | if (vim_iswhite(su->su_badptr[su->su_badlen]) |
| 13253 | && *skiptowhite(stp->st_word) == NUL) |
| 13254 | for (p = fword; *(p = skiptowhite(p)) != NUL; ) |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 13255 | STRMOVE(p, p + 1); |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13256 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 13257 | spell_soundfold(slang, fword, TRUE, badsound2); |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13258 | pbad = badsound2; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13259 | } |
| 13260 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13261 | if (lendiff > 0) |
| 13262 | { |
| 13263 | /* Add part of the bad word to the good word, so that we soundfold |
| 13264 | * what replaces the bad word. */ |
| 13265 | STRCPY(goodword, stp->st_word); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13266 | vim_strncpy(goodword + stp->st_wordlen, |
| 13267 | su->su_badptr + su->su_badlen - lendiff, lendiff); |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13268 | pgood = goodword; |
| 13269 | } |
| 13270 | else |
| 13271 | pgood = stp->st_word; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13272 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13273 | /* Sound-fold the word and compute the score for the difference. */ |
| 13274 | spell_soundfold(slang, pgood, FALSE, goodsound); |
| 13275 | |
| 13276 | return soundalike_score(goodsound, pbad); |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13277 | } |
| 13278 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13279 | /* structure used to store soundfolded words that add_sound_suggest() has |
| 13280 | * handled already. */ |
| 13281 | typedef struct |
| 13282 | { |
| 13283 | short sft_score; /* lowest score used */ |
| 13284 | char_u sft_word[1]; /* soundfolded word, actually longer */ |
| 13285 | } sftword_T; |
| 13286 | |
| 13287 | static sftword_T dumsft; |
| 13288 | #define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft))) |
| 13289 | #define HI2SFT(hi) HIKEY2SFT((hi)->hi_key) |
| 13290 | |
| 13291 | /* |
| 13292 | * Prepare for calling suggest_try_soundalike(). |
| 13293 | */ |
| 13294 | static void |
| 13295 | suggest_try_soundalike_prep() |
| 13296 | { |
| 13297 | langp_T *lp; |
| 13298 | int lpi; |
| 13299 | slang_T *slang; |
| 13300 | |
| 13301 | /* Do this for all languages that support sound folding and for which a |
| 13302 | * .sug file has been loaded. */ |
| 13303 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| 13304 | { |
| 13305 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| 13306 | slang = lp->lp_slang; |
| 13307 | if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) |
| 13308 | /* prepare the hashtable used by add_sound_suggest() */ |
| 13309 | hash_init(&slang->sl_sounddone); |
| 13310 | } |
| 13311 | } |
| 13312 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13313 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13314 | * Find suggestions by comparing the word in a sound-a-like form. |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13315 | * Note: This doesn't support postponed prefixes. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13316 | */ |
| 13317 | static void |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 13318 | suggest_try_soundalike(su) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13319 | suginfo_T *su; |
| 13320 | { |
| 13321 | char_u salword[MAXWLEN]; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13322 | langp_T *lp; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 13323 | int lpi; |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13324 | slang_T *slang; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13325 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13326 | /* Do this for all languages that support sound folding and for which a |
| 13327 | * .sug file has been loaded. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13328 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13329 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13330 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| 13331 | slang = lp->lp_slang; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13332 | if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13333 | { |
| 13334 | /* soundfold the bad word */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13335 | spell_soundfold(slang, su->su_fbadword, TRUE, salword); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13336 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13337 | /* try all kinds of inserts/deletes/swaps/etc. */ |
| 13338 | /* TODO: also soundfold the next words, so that we can try joining |
| 13339 | * and splitting */ |
| 13340 | suggest_trie_walk(su, lp, salword, TRUE); |
| 13341 | } |
| 13342 | } |
| 13343 | } |
| 13344 | |
| 13345 | /* |
| 13346 | * Finish up after calling suggest_try_soundalike(). |
| 13347 | */ |
| 13348 | static void |
| 13349 | suggest_try_soundalike_finish() |
| 13350 | { |
| 13351 | langp_T *lp; |
| 13352 | int lpi; |
| 13353 | slang_T *slang; |
| 13354 | int todo; |
| 13355 | hashitem_T *hi; |
| 13356 | |
| 13357 | /* Do this for all languages that support sound folding and for which a |
| 13358 | * .sug file has been loaded. */ |
| 13359 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| 13360 | { |
| 13361 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| 13362 | slang = lp->lp_slang; |
| 13363 | if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) |
| 13364 | { |
| 13365 | /* Free the info about handled words. */ |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 13366 | todo = (int)slang->sl_sounddone.ht_used; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13367 | for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi) |
| 13368 | if (!HASHITEM_EMPTY(hi)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13369 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13370 | vim_free(HI2SFT(hi)); |
| 13371 | --todo; |
| 13372 | } |
| Bram Moolenaar | 6417da6 | 2007-03-08 13:49:53 +0000 | [diff] [blame] | 13373 | |
| 13374 | /* Clear the hashtable, it may also be used by another region. */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13375 | hash_clear(&slang->sl_sounddone); |
| Bram Moolenaar | 6417da6 | 2007-03-08 13:49:53 +0000 | [diff] [blame] | 13376 | hash_init(&slang->sl_sounddone); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13377 | } |
| 13378 | } |
| 13379 | } |
| 13380 | |
| 13381 | /* |
| 13382 | * A match with a soundfolded word is found. Add the good word(s) that |
| 13383 | * produce this soundfolded word. |
| 13384 | */ |
| 13385 | static void |
| 13386 | add_sound_suggest(su, goodword, score, lp) |
| 13387 | suginfo_T *su; |
| 13388 | char_u *goodword; |
| 13389 | int score; /* soundfold score */ |
| 13390 | langp_T *lp; |
| 13391 | { |
| 13392 | slang_T *slang = lp->lp_slang; /* language for sound folding */ |
| 13393 | int sfwordnr; |
| 13394 | char_u *nrline; |
| 13395 | int orgnr; |
| 13396 | char_u theword[MAXWLEN]; |
| 13397 | int i; |
| 13398 | int wlen; |
| 13399 | char_u *byts; |
| 13400 | idx_T *idxs; |
| 13401 | int n; |
| 13402 | int wordcount; |
| 13403 | int wc; |
| 13404 | int goodscore; |
| 13405 | hash_T hash; |
| 13406 | hashitem_T *hi; |
| 13407 | sftword_T *sft; |
| 13408 | int bc, gc; |
| 13409 | int limit; |
| 13410 | |
| 13411 | /* |
| 13412 | * It's very well possible that the same soundfold word is found several |
| 13413 | * times with different scores. Since the following is quite slow only do |
| 13414 | * the words that have a better score than before. Use a hashtable to |
| 13415 | * remember the words that have been done. |
| 13416 | */ |
| 13417 | hash = hash_hash(goodword); |
| 13418 | hi = hash_lookup(&slang->sl_sounddone, goodword, hash); |
| 13419 | if (HASHITEM_EMPTY(hi)) |
| 13420 | { |
| Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 13421 | sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T) |
| 13422 | + STRLEN(goodword))); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13423 | if (sft != NULL) |
| 13424 | { |
| 13425 | sft->sft_score = score; |
| 13426 | STRCPY(sft->sft_word, goodword); |
| 13427 | hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash); |
| 13428 | } |
| 13429 | } |
| 13430 | else |
| 13431 | { |
| 13432 | sft = HI2SFT(hi); |
| 13433 | if (score >= sft->sft_score) |
| 13434 | return; |
| 13435 | sft->sft_score = score; |
| 13436 | } |
| 13437 | |
| 13438 | /* |
| 13439 | * Find the word nr in the soundfold tree. |
| 13440 | */ |
| 13441 | sfwordnr = soundfold_find(slang, goodword); |
| 13442 | if (sfwordnr < 0) |
| 13443 | { |
| 13444 | EMSG2(_(e_intern2), "add_sound_suggest()"); |
| 13445 | return; |
| 13446 | } |
| 13447 | |
| 13448 | /* |
| 13449 | * go over the list of good words that produce this soundfold word |
| 13450 | */ |
| 13451 | nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE); |
| 13452 | orgnr = 0; |
| 13453 | while (*nrline != NUL) |
| 13454 | { |
| 13455 | /* The wordnr was stored in a minimal nr of bytes as an offset to the |
| 13456 | * previous wordnr. */ |
| 13457 | orgnr += bytes2offset(&nrline); |
| 13458 | |
| 13459 | byts = slang->sl_fbyts; |
| 13460 | idxs = slang->sl_fidxs; |
| 13461 | |
| 13462 | /* Lookup the word "orgnr" one of the two tries. */ |
| 13463 | n = 0; |
| 13464 | wlen = 0; |
| 13465 | wordcount = 0; |
| 13466 | for (;;) |
| 13467 | { |
| 13468 | i = 1; |
| 13469 | if (wordcount == orgnr && byts[n + 1] == NUL) |
| 13470 | break; /* found end of word */ |
| 13471 | |
| 13472 | if (byts[n + 1] == NUL) |
| 13473 | ++wordcount; |
| 13474 | |
| 13475 | /* skip over the NUL bytes */ |
| 13476 | for ( ; byts[n + i] == NUL; ++i) |
| 13477 | if (i > byts[n]) /* safety check */ |
| 13478 | { |
| 13479 | STRCPY(theword + wlen, "BAD"); |
| 13480 | goto badword; |
| 13481 | } |
| 13482 | |
| 13483 | /* One of the siblings must have the word. */ |
| 13484 | for ( ; i < byts[n]; ++i) |
| 13485 | { |
| 13486 | wc = idxs[idxs[n + i]]; /* nr of words under this byte */ |
| 13487 | if (wordcount + wc > orgnr) |
| 13488 | break; |
| 13489 | wordcount += wc; |
| 13490 | } |
| 13491 | |
| 13492 | theword[wlen++] = byts[n + i]; |
| 13493 | n = idxs[n + i]; |
| 13494 | } |
| 13495 | badword: |
| 13496 | theword[wlen] = NUL; |
| 13497 | |
| 13498 | /* Go over the possible flags and regions. */ |
| 13499 | for (; i <= byts[n] && byts[n + i] == NUL; ++i) |
| 13500 | { |
| 13501 | char_u cword[MAXWLEN]; |
| 13502 | char_u *p; |
| 13503 | int flags = (int)idxs[n + i]; |
| 13504 | |
| Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 13505 | /* Skip words with the NOSUGGEST flag */ |
| 13506 | if (flags & WF_NOSUGGEST) |
| 13507 | continue; |
| 13508 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13509 | if (flags & WF_KEEPCAP) |
| 13510 | { |
| 13511 | /* Must find the word in the keep-case tree. */ |
| 13512 | find_keepcap_word(slang, theword, cword); |
| 13513 | p = cword; |
| 13514 | } |
| 13515 | else |
| 13516 | { |
| 13517 | flags |= su->su_badflags; |
| 13518 | if ((flags & WF_CAPMASK) != 0) |
| 13519 | { |
| 13520 | /* Need to fix case according to "flags". */ |
| 13521 | make_case_word(theword, cword, flags); |
| 13522 | p = cword; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13523 | } |
| 13524 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13525 | p = theword; |
| 13526 | } |
| 13527 | |
| 13528 | /* Add the suggestion. */ |
| 13529 | if (sps_flags & SPS_DOUBLE) |
| 13530 | { |
| 13531 | /* Add the suggestion if the score isn't too bad. */ |
| 13532 | if (score <= su->su_maxscore) |
| 13533 | add_suggestion(su, &su->su_sga, p, su->su_badlen, |
| 13534 | score, 0, FALSE, slang, FALSE); |
| 13535 | } |
| 13536 | else |
| 13537 | { |
| 13538 | /* Add a penalty for words in another region. */ |
| 13539 | if ((flags & WF_REGION) |
| 13540 | && (((unsigned)flags >> 16) & lp->lp_region) == 0) |
| 13541 | goodscore = SCORE_REGION; |
| 13542 | else |
| 13543 | goodscore = 0; |
| 13544 | |
| 13545 | /* Add a small penalty for changing the first letter from |
| 13546 | * lower to upper case. Helps for "tath" -> "Kath", which is |
| 13547 | * less common thatn "tath" -> "path". Don't do it when the |
| 13548 | * letter is the same, that has already been counted. */ |
| 13549 | gc = PTR2CHAR(p); |
| 13550 | if (SPELL_ISUPPER(gc)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13551 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13552 | bc = PTR2CHAR(su->su_badword); |
| 13553 | if (!SPELL_ISUPPER(bc) |
| 13554 | && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc)) |
| 13555 | goodscore += SCORE_ICASE / 2; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13556 | } |
| 13557 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13558 | /* Compute the score for the good word. This only does letter |
| 13559 | * insert/delete/swap/replace. REP items are not considered, |
| 13560 | * which may make the score a bit higher. |
| 13561 | * Use a limit for the score to make it work faster. Use |
| 13562 | * MAXSCORE(), because RESCORE() will change the score. |
| 13563 | * If the limit is very high then the iterative method is |
| 13564 | * inefficient, using an array is quicker. */ |
| 13565 | limit = MAXSCORE(su->su_sfmaxscore - goodscore, score); |
| 13566 | if (limit > SCORE_LIMITMAX) |
| 13567 | goodscore += spell_edit_score(slang, su->su_badword, p); |
| 13568 | else |
| 13569 | goodscore += spell_edit_score_limit(slang, su->su_badword, |
| 13570 | p, limit); |
| 13571 | |
| 13572 | /* When going over the limit don't bother to do the rest. */ |
| 13573 | if (goodscore < SCORE_MAXMAX) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13574 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13575 | /* Give a bonus to words seen before. */ |
| 13576 | goodscore = score_wordcount_adj(slang, goodscore, p, FALSE); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13577 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13578 | /* Add the suggestion if the score isn't too bad. */ |
| 13579 | goodscore = RESCORE(goodscore, score); |
| 13580 | if (goodscore <= su->su_sfmaxscore) |
| 13581 | add_suggestion(su, &su->su_ga, p, su->su_badlen, |
| 13582 | goodscore, score, TRUE, slang, TRUE); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13583 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13584 | } |
| 13585 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13586 | /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13587 | } |
| 13588 | } |
| 13589 | |
| 13590 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13591 | * Find word "word" in fold-case tree for "slang" and return the word number. |
| 13592 | */ |
| 13593 | static int |
| 13594 | soundfold_find(slang, word) |
| 13595 | slang_T *slang; |
| 13596 | char_u *word; |
| 13597 | { |
| 13598 | idx_T arridx = 0; |
| 13599 | int len; |
| 13600 | int wlen = 0; |
| 13601 | int c; |
| 13602 | char_u *ptr = word; |
| 13603 | char_u *byts; |
| 13604 | idx_T *idxs; |
| 13605 | int wordnr = 0; |
| 13606 | |
| 13607 | byts = slang->sl_sbyts; |
| 13608 | idxs = slang->sl_sidxs; |
| 13609 | |
| 13610 | for (;;) |
| 13611 | { |
| 13612 | /* First byte is the number of possible bytes. */ |
| 13613 | len = byts[arridx++]; |
| 13614 | |
| 13615 | /* If the first possible byte is a zero the word could end here. |
| 13616 | * If the word ends we found the word. If not skip the NUL bytes. */ |
| 13617 | c = ptr[wlen]; |
| 13618 | if (byts[arridx] == NUL) |
| 13619 | { |
| 13620 | if (c == NUL) |
| 13621 | break; |
| 13622 | |
| 13623 | /* Skip over the zeros, there can be several. */ |
| 13624 | while (len > 0 && byts[arridx] == NUL) |
| 13625 | { |
| 13626 | ++arridx; |
| 13627 | --len; |
| 13628 | } |
| 13629 | if (len == 0) |
| 13630 | return -1; /* no children, word should have ended here */ |
| 13631 | ++wordnr; |
| 13632 | } |
| 13633 | |
| 13634 | /* If the word ends we didn't find it. */ |
| 13635 | if (c == NUL) |
| 13636 | return -1; |
| 13637 | |
| 13638 | /* Perform a binary search in the list of accepted bytes. */ |
| 13639 | if (c == TAB) /* <Tab> is handled like <Space> */ |
| 13640 | c = ' '; |
| 13641 | while (byts[arridx] < c) |
| 13642 | { |
| 13643 | /* The word count is in the first idxs[] entry of the child. */ |
| 13644 | wordnr += idxs[idxs[arridx]]; |
| 13645 | ++arridx; |
| 13646 | if (--len == 0) /* end of the bytes, didn't find it */ |
| 13647 | return -1; |
| 13648 | } |
| 13649 | if (byts[arridx] != c) /* didn't find the byte */ |
| 13650 | return -1; |
| 13651 | |
| 13652 | /* Continue at the child (if there is one). */ |
| 13653 | arridx = idxs[arridx]; |
| 13654 | ++wlen; |
| 13655 | |
| 13656 | /* One space in the good word may stand for several spaces in the |
| 13657 | * checked word. */ |
| 13658 | if (c == ' ') |
| 13659 | while (ptr[wlen] == ' ' || ptr[wlen] == TAB) |
| 13660 | ++wlen; |
| 13661 | } |
| 13662 | |
| 13663 | return wordnr; |
| 13664 | } |
| 13665 | |
| 13666 | /* |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 13667 | * Copy "fword" to "cword", fixing case according to "flags". |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13668 | */ |
| 13669 | static void |
| 13670 | make_case_word(fword, cword, flags) |
| 13671 | char_u *fword; |
| 13672 | char_u *cword; |
| 13673 | int flags; |
| 13674 | { |
| 13675 | if (flags & WF_ALLCAP) |
| 13676 | /* Make it all upper-case */ |
| 13677 | allcap_copy(fword, cword); |
| 13678 | else if (flags & WF_ONECAP) |
| 13679 | /* Make the first letter upper-case */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 13680 | onecap_copy(fword, cword, TRUE); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13681 | else |
| 13682 | /* Use goodword as-is. */ |
| 13683 | STRCPY(cword, fword); |
| 13684 | } |
| 13685 | |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13686 | /* |
| 13687 | * Use map string "map" for languages "lp". |
| 13688 | */ |
| 13689 | static void |
| 13690 | set_map_str(lp, map) |
| 13691 | slang_T *lp; |
| 13692 | char_u *map; |
| 13693 | { |
| 13694 | char_u *p; |
| 13695 | int headc = 0; |
| 13696 | int c; |
| 13697 | int i; |
| 13698 | |
| 13699 | if (*map == NUL) |
| 13700 | { |
| 13701 | lp->sl_has_map = FALSE; |
| 13702 | return; |
| 13703 | } |
| 13704 | lp->sl_has_map = TRUE; |
| 13705 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13706 | /* Init the array and hash tables empty. */ |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13707 | for (i = 0; i < 256; ++i) |
| 13708 | lp->sl_map_array[i] = 0; |
| 13709 | #ifdef FEAT_MBYTE |
| 13710 | hash_init(&lp->sl_map_hash); |
| 13711 | #endif |
| 13712 | |
| 13713 | /* |
| 13714 | * The similar characters are stored separated with slashes: |
| 13715 | * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and |
| 13716 | * before the same slash. For characters above 255 sl_map_hash is used. |
| 13717 | */ |
| 13718 | for (p = map; *p != NUL; ) |
| 13719 | { |
| 13720 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 13721 | c = mb_cptr2char_adv(&p); |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13722 | #else |
| 13723 | c = *p++; |
| 13724 | #endif |
| 13725 | if (c == '/') |
| 13726 | headc = 0; |
| 13727 | else |
| 13728 | { |
| 13729 | if (headc == 0) |
| 13730 | headc = c; |
| 13731 | |
| 13732 | #ifdef FEAT_MBYTE |
| 13733 | /* Characters above 255 don't fit in sl_map_array[], put them in |
| 13734 | * the hash table. Each entry is the char, a NUL the headchar and |
| 13735 | * a NUL. */ |
| 13736 | if (c >= 256) |
| 13737 | { |
| 13738 | int cl = mb_char2len(c); |
| 13739 | int headcl = mb_char2len(headc); |
| 13740 | char_u *b; |
| 13741 | hash_T hash; |
| 13742 | hashitem_T *hi; |
| 13743 | |
| 13744 | b = alloc((unsigned)(cl + headcl + 2)); |
| 13745 | if (b == NULL) |
| 13746 | return; |
| 13747 | mb_char2bytes(c, b); |
| 13748 | b[cl] = NUL; |
| 13749 | mb_char2bytes(headc, b + cl + 1); |
| 13750 | b[cl + 1 + headcl] = NUL; |
| 13751 | hash = hash_hash(b); |
| 13752 | hi = hash_lookup(&lp->sl_map_hash, b, hash); |
| 13753 | if (HASHITEM_EMPTY(hi)) |
| 13754 | hash_add_item(&lp->sl_map_hash, hi, b, hash); |
| 13755 | else |
| 13756 | { |
| 13757 | /* This should have been checked when generating the .spl |
| 13758 | * file. */ |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 13759 | EMSG(_("E783: duplicate char in MAP entry")); |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13760 | vim_free(b); |
| 13761 | } |
| 13762 | } |
| 13763 | else |
| 13764 | #endif |
| 13765 | lp->sl_map_array[c] = headc; |
| 13766 | } |
| 13767 | } |
| 13768 | } |
| 13769 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13770 | /* |
| 13771 | * Return TRUE if "c1" and "c2" are similar characters according to the MAP |
| 13772 | * lines in the .aff file. |
| 13773 | */ |
| 13774 | static int |
| 13775 | similar_chars(slang, c1, c2) |
| 13776 | slang_T *slang; |
| 13777 | int c1; |
| 13778 | int c2; |
| 13779 | { |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13780 | int m1, m2; |
| 13781 | #ifdef FEAT_MBYTE |
| 13782 | char_u buf[MB_MAXBYTES]; |
| 13783 | hashitem_T *hi; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13784 | |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13785 | if (c1 >= 256) |
| 13786 | { |
| 13787 | buf[mb_char2bytes(c1, buf)] = 0; |
| 13788 | hi = hash_find(&slang->sl_map_hash, buf); |
| 13789 | if (HASHITEM_EMPTY(hi)) |
| 13790 | m1 = 0; |
| 13791 | else |
| 13792 | m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 13793 | } |
| 13794 | else |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 13795 | #endif |
| Bram Moolenaar | ea42416 | 2005-06-16 21:51:00 +0000 | [diff] [blame] | 13796 | m1 = slang->sl_map_array[c1]; |
| 13797 | if (m1 == 0) |
| 13798 | return FALSE; |
| 13799 | |
| 13800 | |
| 13801 | #ifdef FEAT_MBYTE |
| 13802 | if (c2 >= 256) |
| 13803 | { |
| 13804 | buf[mb_char2bytes(c2, buf)] = 0; |
| 13805 | hi = hash_find(&slang->sl_map_hash, buf); |
| 13806 | if (HASHITEM_EMPTY(hi)) |
| 13807 | m2 = 0; |
| 13808 | else |
| 13809 | m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); |
| 13810 | } |
| 13811 | else |
| 13812 | #endif |
| 13813 | m2 = slang->sl_map_array[c2]; |
| 13814 | |
| 13815 | return m1 == m2; |
| 13816 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13817 | |
| 13818 | /* |
| 13819 | * Add a suggestion to the list of suggestions. |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13820 | * For a suggestion that is already in the list the lowest score is remembered. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13821 | */ |
| 13822 | static void |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13823 | add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus, |
| 13824 | slang, maxsf) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13825 | suginfo_T *su; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13826 | garray_T *gap; /* either su_ga or su_sga */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13827 | char_u *goodword; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13828 | int badlenarg; /* len of bad word replaced with "goodword" */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 13829 | int score; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 13830 | int altscore; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 13831 | int had_bonus; /* value for st_had_bonus */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 13832 | slang_T *slang; /* language for sound folding */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13833 | int maxsf; /* su_maxscore applies to soundfold score, |
| 13834 | su_sfmaxscore to the total score. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13835 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13836 | int goodlen; /* len of goodword changed */ |
| 13837 | int badlen; /* len of bad word changed */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13838 | suggest_T *stp; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13839 | suggest_T new_sug; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13840 | int i; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13841 | char_u *pgood, *pbad; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13842 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13843 | /* Minimize "badlen" for consistency. Avoids that changing "the the" to |
| 13844 | * "thee the" is added next to changing the first "the" the "thee". */ |
| 13845 | pgood = goodword + STRLEN(goodword); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13846 | pbad = su->su_badptr + badlenarg; |
| 13847 | for (;;) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 13848 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 13849 | goodlen = (int)(pgood - goodword); |
| 13850 | badlen = (int)(pbad - su->su_badptr); |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13851 | if (goodlen <= 0 || badlen <= 0) |
| 13852 | break; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13853 | mb_ptr_back(goodword, pgood); |
| 13854 | mb_ptr_back(su->su_badptr, pbad); |
| 13855 | #ifdef FEAT_MBYTE |
| 13856 | if (has_mbyte) |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 13857 | { |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13858 | if (mb_ptr2char(pgood) != mb_ptr2char(pbad)) |
| 13859 | break; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 13860 | } |
| 13861 | else |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13862 | #endif |
| 13863 | if (*pgood != *pbad) |
| 13864 | break; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 13865 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13866 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13867 | if (badlen == 0 && goodlen == 0) |
| 13868 | /* goodword doesn't change anything; may happen for "the the" changing |
| 13869 | * the first "the" to itself. */ |
| 13870 | return; |
| Bram Moolenaar | 0c40586 | 2005-06-22 22:26:26 +0000 | [diff] [blame] | 13871 | |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 13872 | if (gap->ga_len == 0) |
| 13873 | i = -1; |
| 13874 | else |
| 13875 | { |
| 13876 | /* Check if the word is already there. Also check the length that is |
| 13877 | * being replaced "thes," -> "these" is a different suggestion from |
| 13878 | * "thes" -> "these". */ |
| 13879 | stp = &SUG(*gap, 0); |
| 13880 | for (i = gap->ga_len; --i >= 0; ++stp) |
| 13881 | if (stp->st_wordlen == goodlen |
| 13882 | && stp->st_orglen == badlen |
| 13883 | && STRNCMP(stp->st_word, goodword, goodlen) == 0) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13884 | { |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 13885 | /* |
| 13886 | * Found it. Remember the word with the lowest score. |
| 13887 | */ |
| 13888 | if (stp->st_slang == NULL) |
| 13889 | stp->st_slang = slang; |
| 13890 | |
| 13891 | new_sug.st_score = score; |
| 13892 | new_sug.st_altscore = altscore; |
| 13893 | new_sug.st_had_bonus = had_bonus; |
| 13894 | |
| 13895 | if (stp->st_had_bonus != had_bonus) |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13896 | { |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 13897 | /* Only one of the two had the soundalike score computed. |
| 13898 | * Need to do that for the other one now, otherwise the |
| 13899 | * scores can't be compared. This happens because |
| 13900 | * suggest_try_change() doesn't compute the soundalike |
| 13901 | * word to keep it fast, while some special methods set |
| 13902 | * the soundalike score to zero. */ |
| 13903 | if (had_bonus) |
| 13904 | rescore_one(su, stp); |
| 13905 | else |
| 13906 | { |
| 13907 | new_sug.st_word = stp->st_word; |
| 13908 | new_sug.st_wordlen = stp->st_wordlen; |
| 13909 | new_sug.st_slang = stp->st_slang; |
| 13910 | new_sug.st_orglen = badlen; |
| 13911 | rescore_one(su, &new_sug); |
| 13912 | } |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 13913 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13914 | |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 13915 | if (stp->st_score > new_sug.st_score) |
| 13916 | { |
| 13917 | stp->st_score = new_sug.st_score; |
| 13918 | stp->st_altscore = new_sug.st_altscore; |
| 13919 | stp->st_had_bonus = new_sug.st_had_bonus; |
| 13920 | } |
| 13921 | break; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13922 | } |
| Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 13923 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13924 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13925 | if (i < 0 && ga_grow(gap, 1) == OK) |
| 13926 | { |
| 13927 | /* Add a suggestion. */ |
| 13928 | stp = &SUG(*gap, gap->ga_len); |
| 13929 | stp->st_word = vim_strnsave(goodword, goodlen); |
| 13930 | if (stp->st_word != NULL) |
| 13931 | { |
| 13932 | stp->st_wordlen = goodlen; |
| 13933 | stp->st_score = score; |
| 13934 | stp->st_altscore = altscore; |
| 13935 | stp->st_had_bonus = had_bonus; |
| 13936 | stp->st_orglen = badlen; |
| 13937 | stp->st_slang = slang; |
| 13938 | ++gap->ga_len; |
| 13939 | |
| 13940 | /* If we have too many suggestions now, sort the list and keep |
| 13941 | * the best suggestions. */ |
| 13942 | if (gap->ga_len > SUG_MAX_COUNT(su)) |
| 13943 | { |
| 13944 | if (maxsf) |
| 13945 | su->su_sfmaxscore = cleanup_suggestions(gap, |
| 13946 | su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); |
| 13947 | else |
| 13948 | { |
| 13949 | i = su->su_maxscore; |
| 13950 | su->su_maxscore = cleanup_suggestions(gap, |
| 13951 | su->su_maxscore, SUG_CLEAN_COUNT(su)); |
| 13952 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13953 | } |
| 13954 | } |
| 13955 | } |
| 13956 | } |
| 13957 | |
| 13958 | /* |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 13959 | * Suggestions may in fact be flagged as errors. Esp. for banned words and |
| 13960 | * for split words, such as "the the". Remove these from the list here. |
| 13961 | */ |
| 13962 | static void |
| 13963 | check_suggestions(su, gap) |
| 13964 | suginfo_T *su; |
| 13965 | garray_T *gap; /* either su_ga or su_sga */ |
| 13966 | { |
| 13967 | suggest_T *stp; |
| 13968 | int i; |
| 13969 | char_u longword[MAXWLEN + 1]; |
| 13970 | int len; |
| 13971 | hlf_T attr; |
| 13972 | |
| 13973 | stp = &SUG(*gap, 0); |
| 13974 | for (i = gap->ga_len - 1; i >= 0; --i) |
| 13975 | { |
| 13976 | /* Need to append what follows to check for "the the". */ |
| 13977 | STRCPY(longword, stp[i].st_word); |
| 13978 | len = stp[i].st_wordlen; |
| 13979 | vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen, |
| 13980 | MAXWLEN - len); |
| 13981 | attr = HLF_COUNT; |
| 13982 | (void)spell_check(curwin, longword, &attr, NULL, FALSE); |
| 13983 | if (attr != HLF_COUNT) |
| 13984 | { |
| 13985 | /* Remove this entry. */ |
| 13986 | vim_free(stp[i].st_word); |
| 13987 | --gap->ga_len; |
| 13988 | if (i < gap->ga_len) |
| 13989 | mch_memmove(stp + i, stp + i + 1, |
| 13990 | sizeof(suggest_T) * (gap->ga_len - i)); |
| 13991 | } |
| 13992 | } |
| 13993 | } |
| 13994 | |
| 13995 | |
| 13996 | /* |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 13997 | * Add a word to be banned. |
| 13998 | */ |
| 13999 | static void |
| 14000 | add_banned(su, word) |
| 14001 | suginfo_T *su; |
| 14002 | char_u *word; |
| 14003 | { |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 14004 | char_u *s; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14005 | hash_T hash; |
| 14006 | hashitem_T *hi; |
| 14007 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 14008 | hash = hash_hash(word); |
| 14009 | hi = hash_lookup(&su->su_banned, word, hash); |
| 14010 | if (HASHITEM_EMPTY(hi)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14011 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 14012 | s = vim_strsave(word); |
| 14013 | if (s != NULL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14014 | hash_add_item(&su->su_banned, hi, s, hash); |
| 14015 | } |
| 14016 | } |
| 14017 | |
| 14018 | /* |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14019 | * Recompute the score for all suggestions if sound-folding is possible. This |
| 14020 | * is slow, thus only done for the final results. |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14021 | */ |
| 14022 | static void |
| 14023 | rescore_suggestions(su) |
| 14024 | suginfo_T *su; |
| 14025 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14026 | int i; |
| 14027 | |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14028 | if (su->su_sallang != NULL) |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 14029 | for (i = 0; i < su->su_ga.ga_len; ++i) |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14030 | rescore_one(su, &SUG(su->su_ga, i)); |
| 14031 | } |
| 14032 | |
| 14033 | /* |
| 14034 | * Recompute the score for one suggestion if sound-folding is possible. |
| 14035 | */ |
| 14036 | static void |
| 14037 | rescore_one(su, stp) |
| Bram Moolenaar | 4effc80 | 2005-09-30 21:12:02 +0000 | [diff] [blame] | 14038 | suginfo_T *su; |
| 14039 | suggest_T *stp; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14040 | { |
| 14041 | slang_T *slang = stp->st_slang; |
| 14042 | char_u sal_badword[MAXWLEN]; |
| Bram Moolenaar | 4effc80 | 2005-09-30 21:12:02 +0000 | [diff] [blame] | 14043 | char_u *p; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14044 | |
| 14045 | /* Only rescore suggestions that have no sal score yet and do have a |
| 14046 | * language. */ |
| 14047 | if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus) |
| 14048 | { |
| 14049 | if (slang == su->su_sallang) |
| Bram Moolenaar | 4effc80 | 2005-09-30 21:12:02 +0000 | [diff] [blame] | 14050 | p = su->su_sal_badword; |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14051 | else |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 14052 | { |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14053 | spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword); |
| Bram Moolenaar | 4effc80 | 2005-09-30 21:12:02 +0000 | [diff] [blame] | 14054 | p = sal_badword; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14055 | } |
| Bram Moolenaar | 4effc80 | 2005-09-30 21:12:02 +0000 | [diff] [blame] | 14056 | |
| 14057 | stp->st_altscore = stp_sal_score(stp, su, slang, p); |
| Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 14058 | if (stp->st_altscore == SCORE_MAXMAX) |
| 14059 | stp->st_altscore = SCORE_BIG; |
| 14060 | stp->st_score = RESCORE(stp->st_score, stp->st_altscore); |
| 14061 | stp->st_had_bonus = TRUE; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14062 | } |
| 14063 | } |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14064 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14065 | static int |
| 14066 | #ifdef __BORLANDC__ |
| 14067 | _RTLENTRYF |
| 14068 | #endif |
| 14069 | sug_compare __ARGS((const void *s1, const void *s2)); |
| 14070 | |
| 14071 | /* |
| 14072 | * Function given to qsort() to sort the suggestions on st_score. |
| Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 14073 | * First on "st_score", then "st_altscore" then alphabetically. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14074 | */ |
| 14075 | static int |
| 14076 | #ifdef __BORLANDC__ |
| 14077 | _RTLENTRYF |
| 14078 | #endif |
| 14079 | sug_compare(s1, s2) |
| 14080 | const void *s1; |
| 14081 | const void *s2; |
| 14082 | { |
| 14083 | suggest_T *p1 = (suggest_T *)s1; |
| 14084 | suggest_T *p2 = (suggest_T *)s2; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14085 | int n = p1->st_score - p2->st_score; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14086 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14087 | if (n == 0) |
| Bram Moolenaar | 6b730e1 | 2005-09-16 21:47:57 +0000 | [diff] [blame] | 14088 | { |
| 14089 | n = p1->st_altscore - p2->st_altscore; |
| 14090 | if (n == 0) |
| 14091 | n = STRICMP(p1->st_word, p2->st_word); |
| 14092 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14093 | return n; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14094 | } |
| 14095 | |
| 14096 | /* |
| 14097 | * Cleanup the suggestions: |
| 14098 | * - Sort on score. |
| 14099 | * - Remove words that won't be displayed. |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14100 | * Returns the maximum score in the list or "maxscore" unmodified. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14101 | */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14102 | static int |
| 14103 | cleanup_suggestions(gap, maxscore, keep) |
| 14104 | garray_T *gap; |
| 14105 | int maxscore; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14106 | int keep; /* nr of suggestions to keep */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14107 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14108 | suggest_T *stp = &SUG(*gap, 0); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14109 | int i; |
| 14110 | |
| 14111 | /* Sort the list. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14112 | 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] | 14113 | |
| 14114 | /* Truncate the list to the number of suggestions that will be displayed. */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14115 | if (gap->ga_len > keep) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14116 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14117 | for (i = keep; i < gap->ga_len; ++i) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14118 | vim_free(stp[i].st_word); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14119 | gap->ga_len = keep; |
| 14120 | return stp[keep - 1].st_score; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14121 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14122 | return maxscore; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14123 | } |
| 14124 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14125 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 14126 | /* |
| 14127 | * Soundfold a string, for soundfold(). |
| 14128 | * Result is in allocated memory, NULL for an error. |
| 14129 | */ |
| 14130 | char_u * |
| 14131 | eval_soundfold(word) |
| 14132 | char_u *word; |
| 14133 | { |
| 14134 | langp_T *lp; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14135 | char_u sound[MAXWLEN]; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 14136 | int lpi; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14137 | |
| 14138 | if (curwin->w_p_spell && *curbuf->b_p_spl != NUL) |
| 14139 | /* Use the sound-folding of the first language that supports it. */ |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 14140 | for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 14141 | { |
| Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 14142 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14143 | if (lp->lp_slang->sl_sal.ga_len > 0) |
| 14144 | { |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14145 | /* soundfold the word */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14146 | spell_soundfold(lp->lp_slang, word, FALSE, sound); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14147 | return vim_strsave(sound); |
| 14148 | } |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 14149 | } |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14150 | |
| 14151 | /* No language with sound folding, return word as-is. */ |
| 14152 | return vim_strsave(word); |
| 14153 | } |
| 14154 | #endif |
| 14155 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14156 | /* |
| 14157 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 14158 | * |
| 14159 | * There are many ways to turn a word into a sound-a-like representation. The |
| 14160 | * oldest is Soundex (1918!). A nice overview can be found in "Approximate |
| 14161 | * swedish name matching - survey and test of different algorithms" by Klas |
| 14162 | * Erikson. |
| 14163 | * |
| 14164 | * We support two methods: |
| 14165 | * 1. SOFOFROM/SOFOTO do a simple character mapping. |
| 14166 | * 2. SAL items define a more advanced sound-folding (and much slower). |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14167 | */ |
| 14168 | static void |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14169 | spell_soundfold(slang, inword, folded, res) |
| 14170 | slang_T *slang; |
| 14171 | char_u *inword; |
| 14172 | int folded; /* "inword" is already case-folded */ |
| 14173 | char_u *res; |
| 14174 | { |
| 14175 | char_u fword[MAXWLEN]; |
| 14176 | char_u *word; |
| 14177 | |
| 14178 | if (slang->sl_sofo) |
| 14179 | /* SOFOFROM and SOFOTO used */ |
| 14180 | spell_soundfold_sofo(slang, inword, res); |
| 14181 | else |
| 14182 | { |
| 14183 | /* SAL items used. Requires the word to be case-folded. */ |
| 14184 | if (folded) |
| 14185 | word = inword; |
| 14186 | else |
| 14187 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 14188 | (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14189 | word = fword; |
| 14190 | } |
| 14191 | |
| 14192 | #ifdef FEAT_MBYTE |
| 14193 | if (has_mbyte) |
| 14194 | spell_soundfold_wsal(slang, word, res); |
| 14195 | else |
| 14196 | #endif |
| 14197 | spell_soundfold_sal(slang, word, res); |
| 14198 | } |
| 14199 | } |
| 14200 | |
| 14201 | /* |
| 14202 | * Perform sound folding of "inword" into "res" according to SOFOFROM and |
| 14203 | * SOFOTO lines. |
| 14204 | */ |
| 14205 | static void |
| 14206 | spell_soundfold_sofo(slang, inword, res) |
| 14207 | slang_T *slang; |
| 14208 | char_u *inword; |
| 14209 | char_u *res; |
| 14210 | { |
| 14211 | char_u *s; |
| 14212 | int ri = 0; |
| 14213 | int c; |
| 14214 | |
| 14215 | #ifdef FEAT_MBYTE |
| 14216 | if (has_mbyte) |
| 14217 | { |
| 14218 | int prevc = 0; |
| 14219 | int *ip; |
| 14220 | |
| 14221 | /* The sl_sal_first[] table contains the translation for chars up to |
| 14222 | * 255, sl_sal the rest. */ |
| 14223 | for (s = inword; *s != NUL; ) |
| 14224 | { |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 14225 | c = mb_cptr2char_adv(&s); |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14226 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 14227 | c = ' '; |
| 14228 | else if (c < 256) |
| 14229 | c = slang->sl_sal_first[c]; |
| 14230 | else |
| 14231 | { |
| 14232 | ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; |
| 14233 | if (ip == NULL) /* empty list, can't match */ |
| 14234 | c = NUL; |
| 14235 | else |
| 14236 | for (;;) /* find "c" in the list */ |
| 14237 | { |
| 14238 | if (*ip == 0) /* not found */ |
| 14239 | { |
| 14240 | c = NUL; |
| 14241 | break; |
| 14242 | } |
| 14243 | if (*ip == c) /* match! */ |
| 14244 | { |
| 14245 | c = ip[1]; |
| 14246 | break; |
| 14247 | } |
| 14248 | ip += 2; |
| 14249 | } |
| 14250 | } |
| 14251 | |
| 14252 | if (c != NUL && c != prevc) |
| 14253 | { |
| 14254 | ri += mb_char2bytes(c, res + ri); |
| 14255 | if (ri + MB_MAXBYTES > MAXWLEN) |
| 14256 | break; |
| 14257 | prevc = c; |
| 14258 | } |
| 14259 | } |
| 14260 | } |
| 14261 | else |
| 14262 | #endif |
| 14263 | { |
| 14264 | /* The sl_sal_first[] table contains the translation. */ |
| 14265 | for (s = inword; (c = *s) != NUL; ++s) |
| 14266 | { |
| 14267 | if (vim_iswhite(c)) |
| 14268 | c = ' '; |
| 14269 | else |
| 14270 | c = slang->sl_sal_first[c]; |
| 14271 | if (c != NUL && (ri == 0 || res[ri - 1] != c)) |
| 14272 | res[ri++] = c; |
| 14273 | } |
| 14274 | } |
| 14275 | |
| 14276 | res[ri] = NUL; |
| 14277 | } |
| 14278 | |
| 14279 | static void |
| 14280 | spell_soundfold_sal(slang, inword, res) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14281 | slang_T *slang; |
| 14282 | char_u *inword; |
| 14283 | char_u *res; |
| 14284 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14285 | salitem_T *smp; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14286 | char_u word[MAXWLEN]; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14287 | char_u *s = inword; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14288 | char_u *t; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14289 | char_u *pf; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14290 | int i, j, z; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14291 | int reslen; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14292 | int n, k = 0; |
| 14293 | int z0; |
| 14294 | int k0; |
| 14295 | int n0; |
| 14296 | int c; |
| 14297 | int pri; |
| 14298 | int p0 = -333; |
| 14299 | int c0; |
| 14300 | |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14301 | /* Remove accents, if wanted. We actually remove all non-word characters. |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14302 | * But keep white space. We need a copy, the word may be changed here. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14303 | if (slang->sl_rem_accents) |
| 14304 | { |
| 14305 | t = word; |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14306 | while (*s != NUL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14307 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14308 | if (vim_iswhite(*s)) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14309 | { |
| 14310 | *t++ = ' '; |
| 14311 | s = skipwhite(s); |
| 14312 | } |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14313 | else |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14314 | { |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14315 | if (spell_iswordp_nmw(s)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14316 | *t++ = *s; |
| 14317 | ++s; |
| 14318 | } |
| 14319 | } |
| 14320 | *t = NUL; |
| 14321 | } |
| 14322 | else |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14323 | STRCPY(word, s); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14324 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14325 | smp = (salitem_T *)slang->sl_sal.ga_data; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14326 | |
| 14327 | /* |
| 14328 | * This comes from Aspell phonet.cpp. Converted from C++ to C. |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14329 | * Changed to keep spaces. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14330 | */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14331 | i = reslen = z = 0; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14332 | while ((c = word[i]) != NUL) |
| 14333 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14334 | /* Start with the first rule that has the character in the word. */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14335 | n = slang->sl_sal_first[c]; |
| 14336 | z0 = 0; |
| 14337 | |
| 14338 | if (n >= 0) |
| 14339 | { |
| 14340 | /* check all rules for the same letter */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14341 | for (; (s = smp[n].sm_lead)[0] == c; ++n) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14342 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14343 | /* Quickly skip entries that don't match the word. Most |
| 14344 | * entries are less then three chars, optimize for that. */ |
| 14345 | k = smp[n].sm_leadlen; |
| 14346 | if (k > 1) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14347 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14348 | if (word[i + 1] != s[1]) |
| 14349 | continue; |
| 14350 | if (k > 2) |
| 14351 | { |
| 14352 | for (j = 2; j < k; ++j) |
| 14353 | if (word[i + j] != s[j]) |
| 14354 | break; |
| 14355 | if (j < k) |
| 14356 | continue; |
| 14357 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14358 | } |
| 14359 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14360 | if ((pf = smp[n].sm_oneof) != NULL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14361 | { |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14362 | /* Check for match with one of the chars in "sm_oneof". */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14363 | while (*pf != NUL && *pf != word[i + k]) |
| 14364 | ++pf; |
| 14365 | if (*pf == NUL) |
| 14366 | continue; |
| 14367 | ++k; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14368 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14369 | s = smp[n].sm_rules; |
| 14370 | pri = 5; /* default priority */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14371 | |
| 14372 | p0 = *s; |
| 14373 | k0 = k; |
| 14374 | while (*s == '-' && k > 1) |
| 14375 | { |
| 14376 | k--; |
| 14377 | s++; |
| 14378 | } |
| 14379 | if (*s == '<') |
| 14380 | s++; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14381 | if (VIM_ISDIGIT(*s)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14382 | { |
| 14383 | /* determine priority */ |
| 14384 | pri = *s - '0'; |
| 14385 | s++; |
| 14386 | } |
| 14387 | if (*s == '^' && *(s + 1) == '^') |
| 14388 | s++; |
| 14389 | |
| 14390 | if (*s == NUL |
| 14391 | || (*s == '^' |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14392 | && (i == 0 || !(word[i - 1] == ' ' |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14393 | || spell_iswordp(word + i - 1, curbuf))) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14394 | && (*(s + 1) != '$' |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14395 | || (!spell_iswordp(word + i + k0, curbuf)))) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14396 | || (*s == '$' && i > 0 |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14397 | && spell_iswordp(word + i - 1, curbuf) |
| 14398 | && (!spell_iswordp(word + i + k0, curbuf)))) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14399 | { |
| 14400 | /* search for followup rules, if: */ |
| 14401 | /* followup and k > 1 and NO '-' in searchstring */ |
| 14402 | c0 = word[i + k - 1]; |
| 14403 | n0 = slang->sl_sal_first[c0]; |
| 14404 | |
| 14405 | if (slang->sl_followup && k > 1 && n0 >= 0 |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14406 | && p0 != '-' && word[i + k] != NUL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14407 | { |
| 14408 | /* test follow-up rule for "word[i + k]" */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14409 | for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14410 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14411 | /* Quickly skip entries that don't match the word. |
| 14412 | * */ |
| 14413 | k0 = smp[n0].sm_leadlen; |
| 14414 | if (k0 > 1) |
| 14415 | { |
| 14416 | if (word[i + k] != s[1]) |
| 14417 | continue; |
| 14418 | if (k0 > 2) |
| 14419 | { |
| 14420 | pf = word + i + k + 1; |
| 14421 | for (j = 2; j < k0; ++j) |
| 14422 | if (*pf++ != s[j]) |
| 14423 | break; |
| 14424 | if (j < k0) |
| 14425 | continue; |
| 14426 | } |
| 14427 | } |
| 14428 | k0 += k - 1; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14429 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14430 | if ((pf = smp[n0].sm_oneof) != NULL) |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14431 | { |
| 14432 | /* Check for match with one of the chars in |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14433 | * "sm_oneof". */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14434 | while (*pf != NUL && *pf != word[i + k0]) |
| 14435 | ++pf; |
| 14436 | if (*pf == NUL) |
| 14437 | continue; |
| 14438 | ++k0; |
| 14439 | } |
| 14440 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14441 | p0 = 5; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14442 | s = smp[n0].sm_rules; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14443 | while (*s == '-') |
| 14444 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14445 | /* "k0" gets NOT reduced because |
| 14446 | * "if (k0 == k)" */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14447 | s++; |
| 14448 | } |
| 14449 | if (*s == '<') |
| 14450 | s++; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14451 | if (VIM_ISDIGIT(*s)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14452 | { |
| 14453 | p0 = *s - '0'; |
| 14454 | s++; |
| 14455 | } |
| 14456 | |
| 14457 | if (*s == NUL |
| 14458 | /* *s == '^' cuts */ |
| 14459 | || (*s == '$' |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14460 | && !spell_iswordp(word + i + k0, |
| 14461 | curbuf))) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14462 | { |
| 14463 | if (k0 == k) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14464 | /* this is just a piece of the string */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14465 | continue; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14466 | |
| 14467 | if (p0 < pri) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14468 | /* priority too low */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14469 | continue; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14470 | /* rule fits; stop search */ |
| 14471 | break; |
| 14472 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14473 | } |
| 14474 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14475 | if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14476 | continue; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14477 | } |
| 14478 | |
| 14479 | /* replace string */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14480 | s = smp[n].sm_to; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 14481 | if (s == NULL) |
| 14482 | s = (char_u *)""; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14483 | pf = smp[n].sm_rules; |
| 14484 | p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14485 | if (p0 == 1 && z == 0) |
| 14486 | { |
| 14487 | /* rule with '<' is used */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14488 | if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
| 14489 | || res[reslen - 1] == *s)) |
| 14490 | reslen--; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14491 | z0 = 1; |
| 14492 | z = 1; |
| 14493 | k0 = 0; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14494 | while (*s != NUL && word[i + k0] != NUL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14495 | { |
| 14496 | word[i + k0] = *s; |
| 14497 | k0++; |
| 14498 | s++; |
| 14499 | } |
| 14500 | if (k > k0) |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 14501 | STRMOVE(word + i + k0, word + i + k); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14502 | |
| 14503 | /* new "actual letter" */ |
| 14504 | c = word[i]; |
| 14505 | } |
| 14506 | else |
| 14507 | { |
| 14508 | /* no '<' rule used */ |
| 14509 | i += k - 1; |
| 14510 | z = 0; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14511 | while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14512 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14513 | if (reslen == 0 || res[reslen - 1] != *s) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14514 | res[reslen++] = *s; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14515 | s++; |
| 14516 | } |
| 14517 | /* new "actual letter" */ |
| 14518 | c = *s; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14519 | if (strstr((char *)pf, "^^") != NULL) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14520 | { |
| 14521 | if (c != NUL) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14522 | res[reslen++] = c; |
| Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 14523 | STRMOVE(word, word + i + 1); |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14524 | i = 0; |
| 14525 | z0 = 1; |
| 14526 | } |
| 14527 | } |
| 14528 | break; |
| 14529 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14530 | } |
| 14531 | } |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14532 | else if (vim_iswhite(c)) |
| 14533 | { |
| 14534 | c = ' '; |
| 14535 | k = 1; |
| 14536 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14537 | |
| 14538 | if (z0 == 0) |
| 14539 | { |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14540 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 14541 | && (!slang->sl_collapse || reslen == 0 |
| 14542 | || res[reslen - 1] != c)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14543 | /* condense only double letters */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14544 | res[reslen++] = c; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14545 | |
| 14546 | i++; |
| 14547 | z = 0; |
| 14548 | k = 0; |
| 14549 | } |
| 14550 | } |
| 14551 | |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14552 | res[reslen] = NUL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 14553 | } |
| 14554 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14555 | #ifdef FEAT_MBYTE |
| 14556 | /* |
| 14557 | * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". |
| 14558 | * Multi-byte version of spell_soundfold(). |
| 14559 | */ |
| 14560 | static void |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14561 | spell_soundfold_wsal(slang, inword, res) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14562 | slang_T *slang; |
| 14563 | char_u *inword; |
| 14564 | char_u *res; |
| 14565 | { |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14566 | salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14567 | int word[MAXWLEN]; |
| 14568 | int wres[MAXWLEN]; |
| 14569 | int l; |
| 14570 | char_u *s; |
| 14571 | int *ws; |
| 14572 | char_u *t; |
| 14573 | int *pf; |
| 14574 | int i, j, z; |
| 14575 | int reslen; |
| 14576 | int n, k = 0; |
| 14577 | int z0; |
| 14578 | int k0; |
| 14579 | int n0; |
| 14580 | int c; |
| 14581 | int pri; |
| 14582 | int p0 = -333; |
| 14583 | int c0; |
| 14584 | int did_white = FALSE; |
| 14585 | |
| 14586 | /* |
| 14587 | * Convert the multi-byte string to a wide-character string. |
| 14588 | * Remove accents, if wanted. We actually remove all non-word characters. |
| 14589 | * But keep white space. |
| 14590 | */ |
| 14591 | n = 0; |
| 14592 | for (s = inword; *s != NUL; ) |
| 14593 | { |
| 14594 | t = s; |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 14595 | c = mb_cptr2char_adv(&s); |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14596 | if (slang->sl_rem_accents) |
| 14597 | { |
| 14598 | if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c)) |
| 14599 | { |
| 14600 | if (did_white) |
| 14601 | continue; |
| 14602 | c = ' '; |
| 14603 | did_white = TRUE; |
| 14604 | } |
| 14605 | else |
| 14606 | { |
| 14607 | did_white = FALSE; |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14608 | if (!spell_iswordp_nmw(t)) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14609 | continue; |
| 14610 | } |
| 14611 | } |
| 14612 | word[n++] = c; |
| 14613 | } |
| 14614 | word[n] = NUL; |
| 14615 | |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14616 | /* |
| 14617 | * This comes from Aspell phonet.cpp. |
| 14618 | * Converted from C++ to C. Added support for multi-byte chars. |
| 14619 | * Changed to keep spaces. |
| 14620 | */ |
| 14621 | i = reslen = z = 0; |
| 14622 | while ((c = word[i]) != NUL) |
| 14623 | { |
| 14624 | /* Start with the first rule that has the character in the word. */ |
| 14625 | n = slang->sl_sal_first[c & 0xff]; |
| 14626 | z0 = 0; |
| 14627 | |
| 14628 | if (n >= 0) |
| 14629 | { |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14630 | /* check all rules for the same index byte */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14631 | for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n) |
| 14632 | { |
| 14633 | /* Quickly skip entries that don't match the word. Most |
| 14634 | * entries are less then three chars, optimize for that. */ |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14635 | if (c != ws[0]) |
| 14636 | continue; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14637 | k = smp[n].sm_leadlen; |
| 14638 | if (k > 1) |
| 14639 | { |
| 14640 | if (word[i + 1] != ws[1]) |
| 14641 | continue; |
| 14642 | if (k > 2) |
| 14643 | { |
| 14644 | for (j = 2; j < k; ++j) |
| 14645 | if (word[i + j] != ws[j]) |
| 14646 | break; |
| 14647 | if (j < k) |
| 14648 | continue; |
| 14649 | } |
| 14650 | } |
| 14651 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14652 | if ((pf = smp[n].sm_oneof_w) != NULL) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14653 | { |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14654 | /* Check for match with one of the chars in "sm_oneof". */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14655 | while (*pf != NUL && *pf != word[i + k]) |
| 14656 | ++pf; |
| 14657 | if (*pf == NUL) |
| 14658 | continue; |
| 14659 | ++k; |
| 14660 | } |
| 14661 | s = smp[n].sm_rules; |
| 14662 | pri = 5; /* default priority */ |
| 14663 | |
| 14664 | p0 = *s; |
| 14665 | k0 = k; |
| 14666 | while (*s == '-' && k > 1) |
| 14667 | { |
| 14668 | k--; |
| 14669 | s++; |
| 14670 | } |
| 14671 | if (*s == '<') |
| 14672 | s++; |
| 14673 | if (VIM_ISDIGIT(*s)) |
| 14674 | { |
| 14675 | /* determine priority */ |
| 14676 | pri = *s - '0'; |
| 14677 | s++; |
| 14678 | } |
| 14679 | if (*s == '^' && *(s + 1) == '^') |
| 14680 | s++; |
| 14681 | |
| 14682 | if (*s == NUL |
| 14683 | || (*s == '^' |
| 14684 | && (i == 0 || !(word[i - 1] == ' ' |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14685 | || spell_iswordp_w(word + i - 1, curbuf))) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14686 | && (*(s + 1) != '$' |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14687 | || (!spell_iswordp_w(word + i + k0, curbuf)))) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14688 | || (*s == '$' && i > 0 |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14689 | && spell_iswordp_w(word + i - 1, curbuf) |
| 14690 | && (!spell_iswordp_w(word + i + k0, curbuf)))) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14691 | { |
| 14692 | /* search for followup rules, if: */ |
| 14693 | /* followup and k > 1 and NO '-' in searchstring */ |
| 14694 | c0 = word[i + k - 1]; |
| 14695 | n0 = slang->sl_sal_first[c0 & 0xff]; |
| 14696 | |
| 14697 | if (slang->sl_followup && k > 1 && n0 >= 0 |
| 14698 | && p0 != '-' && word[i + k] != NUL) |
| 14699 | { |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14700 | /* Test follow-up rule for "word[i + k]"; loop over |
| 14701 | * all entries with the same index byte. */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14702 | for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
| 14703 | == (c0 & 0xff); ++n0) |
| 14704 | { |
| 14705 | /* Quickly skip entries that don't match the word. |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14706 | */ |
| 14707 | if (c0 != ws[0]) |
| 14708 | continue; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14709 | k0 = smp[n0].sm_leadlen; |
| 14710 | if (k0 > 1) |
| 14711 | { |
| 14712 | if (word[i + k] != ws[1]) |
| 14713 | continue; |
| 14714 | if (k0 > 2) |
| 14715 | { |
| 14716 | pf = word + i + k + 1; |
| 14717 | for (j = 2; j < k0; ++j) |
| 14718 | if (*pf++ != ws[j]) |
| 14719 | break; |
| 14720 | if (j < k0) |
| 14721 | continue; |
| 14722 | } |
| 14723 | } |
| 14724 | k0 += k - 1; |
| 14725 | |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14726 | if ((pf = smp[n0].sm_oneof_w) != NULL) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14727 | { |
| 14728 | /* Check for match with one of the chars in |
| Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 14729 | * "sm_oneof". */ |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14730 | while (*pf != NUL && *pf != word[i + k0]) |
| 14731 | ++pf; |
| 14732 | if (*pf == NUL) |
| 14733 | continue; |
| 14734 | ++k0; |
| 14735 | } |
| 14736 | |
| 14737 | p0 = 5; |
| 14738 | s = smp[n0].sm_rules; |
| 14739 | while (*s == '-') |
| 14740 | { |
| 14741 | /* "k0" gets NOT reduced because |
| 14742 | * "if (k0 == k)" */ |
| 14743 | s++; |
| 14744 | } |
| 14745 | if (*s == '<') |
| 14746 | s++; |
| 14747 | if (VIM_ISDIGIT(*s)) |
| 14748 | { |
| 14749 | p0 = *s - '0'; |
| 14750 | s++; |
| 14751 | } |
| 14752 | |
| 14753 | if (*s == NUL |
| 14754 | /* *s == '^' cuts */ |
| 14755 | || (*s == '$' |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 14756 | && !spell_iswordp_w(word + i + k0, |
| 14757 | curbuf))) |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14758 | { |
| 14759 | if (k0 == k) |
| 14760 | /* this is just a piece of the string */ |
| 14761 | continue; |
| 14762 | |
| 14763 | if (p0 < pri) |
| 14764 | /* priority too low */ |
| 14765 | continue; |
| 14766 | /* rule fits; stop search */ |
| 14767 | break; |
| 14768 | } |
| 14769 | } |
| 14770 | |
| 14771 | if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) |
| 14772 | == (c0 & 0xff)) |
| 14773 | continue; |
| 14774 | } |
| 14775 | |
| 14776 | /* replace string */ |
| 14777 | ws = smp[n].sm_to_w; |
| 14778 | s = smp[n].sm_rules; |
| 14779 | p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; |
| 14780 | if (p0 == 1 && z == 0) |
| 14781 | { |
| 14782 | /* rule with '<' is used */ |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 14783 | if (reslen > 0 && ws != NULL && *ws != NUL |
| 14784 | && (wres[reslen - 1] == c |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14785 | || wres[reslen - 1] == *ws)) |
| 14786 | reslen--; |
| 14787 | z0 = 1; |
| 14788 | z = 1; |
| 14789 | k0 = 0; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 14790 | if (ws != NULL) |
| 14791 | while (*ws != NUL && word[i + k0] != NUL) |
| 14792 | { |
| 14793 | word[i + k0] = *ws; |
| 14794 | k0++; |
| 14795 | ws++; |
| 14796 | } |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14797 | if (k > k0) |
| 14798 | mch_memmove(word + i + k0, word + i + k, |
| 14799 | sizeof(int) * (STRLEN(word + i + k) + 1)); |
| 14800 | |
| 14801 | /* new "actual letter" */ |
| 14802 | c = word[i]; |
| 14803 | } |
| 14804 | else |
| 14805 | { |
| 14806 | /* no '<' rule used */ |
| 14807 | i += k - 1; |
| 14808 | z = 0; |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 14809 | if (ws != NULL) |
| 14810 | while (*ws != NUL && ws[1] != NUL |
| 14811 | && reslen < MAXWLEN) |
| 14812 | { |
| 14813 | if (reslen == 0 || wres[reslen - 1] != *ws) |
| 14814 | wres[reslen++] = *ws; |
| 14815 | ws++; |
| 14816 | } |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14817 | /* new "actual letter" */ |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 14818 | if (ws == NULL) |
| 14819 | c = NUL; |
| 14820 | else |
| 14821 | c = *ws; |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 14822 | if (strstr((char *)s, "^^") != NULL) |
| 14823 | { |
| 14824 | if (c != NUL) |
| 14825 | wres[reslen++] = c; |
| 14826 | mch_memmove(word, word + i + 1, |
| 14827 | sizeof(int) * (STRLEN(word + i + 1) + 1)); |
| 14828 | i = 0; |
| 14829 | z0 = 1; |
| 14830 | } |
| 14831 | } |
| 14832 | break; |
| 14833 | } |
| 14834 | } |
| 14835 | } |
| 14836 | else if (vim_iswhite(c)) |
| 14837 | { |
| 14838 | c = ' '; |
| 14839 | k = 1; |
| 14840 | } |
| 14841 | |
| 14842 | if (z0 == 0) |
| 14843 | { |
| 14844 | if (k && !p0 && reslen < MAXWLEN && c != NUL |
| 14845 | && (!slang->sl_collapse || reslen == 0 |
| 14846 | || wres[reslen - 1] != c)) |
| 14847 | /* condense only double letters */ |
| 14848 | wres[reslen++] = c; |
| 14849 | |
| 14850 | i++; |
| 14851 | z = 0; |
| 14852 | k = 0; |
| 14853 | } |
| 14854 | } |
| 14855 | |
| 14856 | /* Convert wide characters in "wres" to a multi-byte string in "res". */ |
| 14857 | l = 0; |
| 14858 | for (n = 0; n < reslen; ++n) |
| 14859 | { |
| 14860 | l += mb_char2bytes(wres[n], res + l); |
| 14861 | if (l + MB_MAXBYTES > MAXWLEN) |
| 14862 | break; |
| 14863 | } |
| 14864 | res[l] = NUL; |
| 14865 | } |
| 14866 | #endif |
| 14867 | |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 14868 | /* |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14869 | * Compute a score for two sound-a-like words. |
| 14870 | * This permits up to two inserts/deletes/swaps/etc. to keep things fast. |
| 14871 | * Instead of a generic loop we write out the code. That keeps it fast by |
| 14872 | * avoiding checks that will not be possible. |
| 14873 | */ |
| 14874 | static int |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14875 | soundalike_score(goodstart, badstart) |
| 14876 | char_u *goodstart; /* sound-folded good word */ |
| 14877 | char_u *badstart; /* sound-folded bad word */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14878 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14879 | char_u *goodsound = goodstart; |
| 14880 | char_u *badsound = badstart; |
| 14881 | int goodlen; |
| 14882 | int badlen; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14883 | int n; |
| 14884 | char_u *pl, *ps; |
| 14885 | char_u *pl2, *ps2; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14886 | int score = 0; |
| 14887 | |
| 14888 | /* adding/inserting "*" at the start (word starts with vowel) shouldn't be |
| 14889 | * counted so much, vowels halfway the word aren't counted at all. */ |
| 14890 | if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) |
| 14891 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 14892 | if (badsound[1] == goodsound[1] |
| 14893 | || (badsound[1] != NUL |
| 14894 | && goodsound[1] != NUL |
| 14895 | && badsound[2] == goodsound[2])) |
| 14896 | { |
| 14897 | /* handle like a substitute */ |
| 14898 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14899 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 14900 | { |
| 14901 | score = 2 * SCORE_DEL / 3; |
| 14902 | if (*badsound == '*') |
| 14903 | ++badsound; |
| 14904 | else |
| 14905 | ++goodsound; |
| 14906 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14907 | } |
| 14908 | |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 14909 | goodlen = (int)STRLEN(goodsound); |
| 14910 | badlen = (int)STRLEN(badsound); |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14911 | |
| Bram Moolenaar | f711faf | 2007-05-10 16:48:19 +0000 | [diff] [blame] | 14912 | /* Return quickly if the lengths are too different to be fixed by two |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14913 | * changes. */ |
| 14914 | n = goodlen - badlen; |
| 14915 | if (n < -2 || n > 2) |
| 14916 | return SCORE_MAXMAX; |
| 14917 | |
| 14918 | if (n > 0) |
| 14919 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14920 | pl = goodsound; /* goodsound is longest */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14921 | ps = badsound; |
| 14922 | } |
| 14923 | else |
| 14924 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14925 | pl = badsound; /* badsound is longest */ |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14926 | ps = goodsound; |
| 14927 | } |
| 14928 | |
| 14929 | /* Skip over the identical part. */ |
| 14930 | while (*pl == *ps && *pl != NUL) |
| 14931 | { |
| 14932 | ++pl; |
| 14933 | ++ps; |
| 14934 | } |
| 14935 | |
| 14936 | switch (n) |
| 14937 | { |
| 14938 | case -2: |
| 14939 | case 2: |
| 14940 | /* |
| 14941 | * Must delete two characters from "pl". |
| 14942 | */ |
| 14943 | ++pl; /* first delete */ |
| 14944 | while (*pl == *ps) |
| 14945 | { |
| 14946 | ++pl; |
| 14947 | ++ps; |
| 14948 | } |
| 14949 | /* strings must be equal after second delete */ |
| 14950 | if (STRCMP(pl + 1, ps) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14951 | return score + SCORE_DEL * 2; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14952 | |
| 14953 | /* Failed to compare. */ |
| 14954 | break; |
| 14955 | |
| 14956 | case -1: |
| 14957 | case 1: |
| 14958 | /* |
| 14959 | * Minimal one delete from "pl" required. |
| 14960 | */ |
| 14961 | |
| 14962 | /* 1: delete */ |
| 14963 | pl2 = pl + 1; |
| 14964 | ps2 = ps; |
| 14965 | while (*pl2 == *ps2) |
| 14966 | { |
| 14967 | if (*pl2 == NUL) /* reached the end */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14968 | return score + SCORE_DEL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14969 | ++pl2; |
| 14970 | ++ps2; |
| 14971 | } |
| 14972 | |
| 14973 | /* 2: delete then swap, then rest must be equal */ |
| 14974 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 14975 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14976 | return score + SCORE_DEL + SCORE_SWAP; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14977 | |
| 14978 | /* 3: delete then substitute, then the rest must be equal */ |
| 14979 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14980 | return score + SCORE_DEL + SCORE_SUBST; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14981 | |
| 14982 | /* 4: first swap then delete */ |
| 14983 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 14984 | { |
| 14985 | pl2 = pl + 2; /* swap, skip two chars */ |
| 14986 | ps2 = ps + 2; |
| 14987 | while (*pl2 == *ps2) |
| 14988 | { |
| 14989 | ++pl2; |
| 14990 | ++ps2; |
| 14991 | } |
| 14992 | /* delete a char and then strings must be equal */ |
| 14993 | if (STRCMP(pl2 + 1, ps2) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 14994 | return score + SCORE_SWAP + SCORE_DEL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 14995 | } |
| 14996 | |
| 14997 | /* 5: first substitute then delete */ |
| 14998 | pl2 = pl + 1; /* substitute, skip one char */ |
| 14999 | ps2 = ps + 1; |
| 15000 | while (*pl2 == *ps2) |
| 15001 | { |
| 15002 | ++pl2; |
| 15003 | ++ps2; |
| 15004 | } |
| 15005 | /* delete a char and then strings must be equal */ |
| 15006 | if (STRCMP(pl2 + 1, ps2) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15007 | return score + SCORE_SUBST + SCORE_DEL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15008 | |
| 15009 | /* Failed to compare. */ |
| 15010 | break; |
| 15011 | |
| 15012 | case 0: |
| 15013 | /* |
| 15014 | * Lenghts are equal, thus changes must result in same length: An |
| 15015 | * insert is only possible in combination with a delete. |
| 15016 | * 1: check if for identical strings |
| 15017 | */ |
| 15018 | if (*pl == NUL) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15019 | return score; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15020 | |
| 15021 | /* 2: swap */ |
| 15022 | if (pl[0] == ps[1] && pl[1] == ps[0]) |
| 15023 | { |
| 15024 | pl2 = pl + 2; /* swap, skip two chars */ |
| 15025 | ps2 = ps + 2; |
| 15026 | while (*pl2 == *ps2) |
| 15027 | { |
| 15028 | if (*pl2 == NUL) /* reached the end */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15029 | return score + SCORE_SWAP; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15030 | ++pl2; |
| 15031 | ++ps2; |
| 15032 | } |
| 15033 | /* 3: swap and swap again */ |
| 15034 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 15035 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15036 | return score + SCORE_SWAP + SCORE_SWAP; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15037 | |
| 15038 | /* 4: swap and substitute */ |
| 15039 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15040 | return score + SCORE_SWAP + SCORE_SUBST; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15041 | } |
| 15042 | |
| 15043 | /* 5: substitute */ |
| 15044 | pl2 = pl + 1; |
| 15045 | ps2 = ps + 1; |
| 15046 | while (*pl2 == *ps2) |
| 15047 | { |
| 15048 | if (*pl2 == NUL) /* reached the end */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15049 | return score + SCORE_SUBST; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15050 | ++pl2; |
| 15051 | ++ps2; |
| 15052 | } |
| 15053 | |
| 15054 | /* 6: substitute and swap */ |
| 15055 | if (pl2[0] == ps2[1] && pl2[1] == ps2[0] |
| 15056 | && STRCMP(pl2 + 2, ps2 + 2) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15057 | return score + SCORE_SUBST + SCORE_SWAP; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15058 | |
| 15059 | /* 7: substitute and substitute */ |
| 15060 | if (STRCMP(pl2 + 1, ps2 + 1) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15061 | return score + SCORE_SUBST + SCORE_SUBST; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15062 | |
| 15063 | /* 8: insert then delete */ |
| 15064 | pl2 = pl; |
| 15065 | ps2 = ps + 1; |
| 15066 | while (*pl2 == *ps2) |
| 15067 | { |
| 15068 | ++pl2; |
| 15069 | ++ps2; |
| 15070 | } |
| 15071 | if (STRCMP(pl2 + 1, ps2) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15072 | return score + SCORE_INS + SCORE_DEL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15073 | |
| 15074 | /* 9: delete then insert */ |
| 15075 | pl2 = pl + 1; |
| 15076 | ps2 = ps; |
| 15077 | while (*pl2 == *ps2) |
| 15078 | { |
| 15079 | ++pl2; |
| 15080 | ++ps2; |
| 15081 | } |
| 15082 | if (STRCMP(pl2, ps2 + 1) == 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15083 | return score + SCORE_INS + SCORE_DEL; |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15084 | |
| 15085 | /* Failed to compare. */ |
| 15086 | break; |
| 15087 | } |
| 15088 | |
| 15089 | return SCORE_MAXMAX; |
| 15090 | } |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15091 | |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15092 | /* |
| 15093 | * Compute the "edit distance" to turn "badword" into "goodword". The less |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15094 | * deletes/inserts/substitutes/swaps are required the lower the score. |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15095 | * |
| Bram Moolenaar | d12a132 | 2005-08-21 22:08:24 +0000 | [diff] [blame] | 15096 | * The algorithm is described by Du and Chang, 1992. |
| 15097 | * The implementation of the algorithm comes from Aspell editdist.cpp, |
| 15098 | * edit_distance(). It has been converted from C++ to C and modified to |
| 15099 | * support multi-byte characters. |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15100 | */ |
| 15101 | static int |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15102 | spell_edit_score(slang, badword, goodword) |
| 15103 | slang_T *slang; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15104 | char_u *badword; |
| 15105 | char_u *goodword; |
| 15106 | { |
| 15107 | int *cnt; |
| Bram Moolenaar | f711faf | 2007-05-10 16:48:19 +0000 | [diff] [blame] | 15108 | int badlen, goodlen; /* lengths including NUL */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15109 | int j, i; |
| 15110 | int t; |
| 15111 | int bc, gc; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15112 | int pbc, pgc; |
| 15113 | #ifdef FEAT_MBYTE |
| 15114 | char_u *p; |
| 15115 | int wbadword[MAXWLEN]; |
| 15116 | int wgoodword[MAXWLEN]; |
| 15117 | |
| 15118 | if (has_mbyte) |
| 15119 | { |
| 15120 | /* Get the characters from the multi-byte strings and put them in an |
| 15121 | * int array for easy access. */ |
| 15122 | for (p = badword, badlen = 0; *p != NUL; ) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 15123 | wbadword[badlen++] = mb_cptr2char_adv(&p); |
| Bram Moolenaar | 97409f1 | 2005-07-08 22:17:29 +0000 | [diff] [blame] | 15124 | wbadword[badlen++] = 0; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15125 | for (p = goodword, goodlen = 0; *p != NUL; ) |
| Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 15126 | wgoodword[goodlen++] = mb_cptr2char_adv(&p); |
| Bram Moolenaar | 97409f1 | 2005-07-08 22:17:29 +0000 | [diff] [blame] | 15127 | wgoodword[goodlen++] = 0; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15128 | } |
| 15129 | else |
| 15130 | #endif |
| 15131 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 15132 | badlen = (int)STRLEN(badword) + 1; |
| 15133 | goodlen = (int)STRLEN(goodword) + 1; |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15134 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15135 | |
| 15136 | /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ |
| 15137 | #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15138 | cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), |
| 15139 | TRUE); |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15140 | if (cnt == NULL) |
| 15141 | return 0; /* out of memory */ |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15142 | |
| 15143 | CNT(0, 0) = 0; |
| 15144 | for (j = 1; j <= goodlen; ++j) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15145 | CNT(0, j) = CNT(0, j - 1) + SCORE_INS; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15146 | |
| 15147 | for (i = 1; i <= badlen; ++i) |
| 15148 | { |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15149 | CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15150 | for (j = 1; j <= goodlen; ++j) |
| 15151 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15152 | #ifdef FEAT_MBYTE |
| 15153 | if (has_mbyte) |
| 15154 | { |
| 15155 | bc = wbadword[i - 1]; |
| 15156 | gc = wgoodword[j - 1]; |
| 15157 | } |
| 15158 | else |
| 15159 | #endif |
| 15160 | { |
| 15161 | bc = badword[i - 1]; |
| 15162 | gc = goodword[j - 1]; |
| 15163 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15164 | if (bc == gc) |
| 15165 | CNT(i, j) = CNT(i - 1, j - 1); |
| 15166 | else |
| 15167 | { |
| 15168 | /* Use a better score when there is only a case difference. */ |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15169 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15170 | CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
| 15171 | else |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15172 | { |
| 15173 | /* For a similar character use SCORE_SIMILAR. */ |
| 15174 | if (slang != NULL |
| 15175 | && slang->sl_has_map |
| 15176 | && similar_chars(slang, gc, bc)) |
| 15177 | CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1); |
| 15178 | else |
| 15179 | CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); |
| 15180 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15181 | |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15182 | if (i > 1 && j > 1) |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15183 | { |
| Bram Moolenaar | 9f30f50 | 2005-06-14 22:01:04 +0000 | [diff] [blame] | 15184 | #ifdef FEAT_MBYTE |
| 15185 | if (has_mbyte) |
| 15186 | { |
| 15187 | pbc = wbadword[i - 2]; |
| 15188 | pgc = wgoodword[j - 2]; |
| 15189 | } |
| 15190 | else |
| 15191 | #endif |
| 15192 | { |
| 15193 | pbc = badword[i - 2]; |
| 15194 | pgc = goodword[j - 2]; |
| 15195 | } |
| 15196 | if (bc == pgc && pbc == gc) |
| 15197 | { |
| 15198 | t = SCORE_SWAP + CNT(i - 2, j - 2); |
| 15199 | if (t < CNT(i, j)) |
| 15200 | CNT(i, j) = t; |
| 15201 | } |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15202 | } |
| 15203 | t = SCORE_DEL + CNT(i - 1, j); |
| 15204 | if (t < CNT(i, j)) |
| 15205 | CNT(i, j) = t; |
| 15206 | t = SCORE_INS + CNT(i, j - 1); |
| 15207 | if (t < CNT(i, j)) |
| 15208 | CNT(i, j) = t; |
| 15209 | } |
| 15210 | } |
| 15211 | } |
| Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 15212 | |
| 15213 | i = CNT(badlen - 1, goodlen - 1); |
| 15214 | vim_free(cnt); |
| 15215 | return i; |
| Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 15216 | } |
| Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 15217 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15218 | typedef struct |
| 15219 | { |
| 15220 | int badi; |
| 15221 | int goodi; |
| 15222 | int score; |
| 15223 | } limitscore_T; |
| 15224 | |
| 15225 | /* |
| 15226 | * Like spell_edit_score(), but with a limit on the score to make it faster. |
| 15227 | * May return SCORE_MAXMAX when the score is higher than "limit". |
| 15228 | * |
| 15229 | * This uses a stack for the edits still to be tried. |
| 15230 | * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support |
| 15231 | * for multi-byte characters. |
| 15232 | */ |
| 15233 | static int |
| 15234 | spell_edit_score_limit(slang, badword, goodword, limit) |
| 15235 | slang_T *slang; |
| 15236 | char_u *badword; |
| 15237 | char_u *goodword; |
| 15238 | int limit; |
| 15239 | { |
| 15240 | limitscore_T stack[10]; /* allow for over 3 * 2 edits */ |
| 15241 | int stackidx; |
| 15242 | int bi, gi; |
| 15243 | int bi2, gi2; |
| 15244 | int bc, gc; |
| 15245 | int score; |
| 15246 | int score_off; |
| 15247 | int minscore; |
| 15248 | int round; |
| 15249 | |
| 15250 | #ifdef FEAT_MBYTE |
| 15251 | /* Multi-byte characters require a bit more work, use a different function |
| 15252 | * to avoid testing "has_mbyte" quite often. */ |
| 15253 | if (has_mbyte) |
| 15254 | return spell_edit_score_limit_w(slang, badword, goodword, limit); |
| 15255 | #endif |
| 15256 | |
| 15257 | /* |
| 15258 | * The idea is to go from start to end over the words. So long as |
| 15259 | * characters are equal just continue, this always gives the lowest score. |
| 15260 | * When there is a difference try several alternatives. Each alternative |
| 15261 | * increases "score" for the edit distance. Some of the alternatives are |
| 15262 | * pushed unto a stack and tried later, some are tried right away. At the |
| 15263 | * end of the word the score for one alternative is known. The lowest |
| 15264 | * possible score is stored in "minscore". |
| 15265 | */ |
| 15266 | stackidx = 0; |
| 15267 | bi = 0; |
| 15268 | gi = 0; |
| 15269 | score = 0; |
| 15270 | minscore = limit + 1; |
| 15271 | |
| 15272 | for (;;) |
| 15273 | { |
| 15274 | /* Skip over an equal part, score remains the same. */ |
| 15275 | for (;;) |
| 15276 | { |
| 15277 | bc = badword[bi]; |
| 15278 | gc = goodword[gi]; |
| 15279 | if (bc != gc) /* stop at a char that's different */ |
| 15280 | break; |
| 15281 | if (bc == NUL) /* both words end */ |
| 15282 | { |
| 15283 | if (score < minscore) |
| 15284 | minscore = score; |
| 15285 | goto pop; /* do next alternative */ |
| 15286 | } |
| 15287 | ++bi; |
| 15288 | ++gi; |
| 15289 | } |
| 15290 | |
| 15291 | if (gc == NUL) /* goodword ends, delete badword chars */ |
| 15292 | { |
| 15293 | do |
| 15294 | { |
| 15295 | if ((score += SCORE_DEL) >= minscore) |
| 15296 | goto pop; /* do next alternative */ |
| 15297 | } while (badword[++bi] != NUL); |
| 15298 | minscore = score; |
| 15299 | } |
| 15300 | else if (bc == NUL) /* badword ends, insert badword chars */ |
| 15301 | { |
| 15302 | do |
| 15303 | { |
| 15304 | if ((score += SCORE_INS) >= minscore) |
| 15305 | goto pop; /* do next alternative */ |
| 15306 | } while (goodword[++gi] != NUL); |
| 15307 | minscore = score; |
| 15308 | } |
| 15309 | else /* both words continue */ |
| 15310 | { |
| 15311 | /* If not close to the limit, perform a change. Only try changes |
| 15312 | * that may lead to a lower score than "minscore". |
| 15313 | * round 0: try deleting a char from badword |
| 15314 | * round 1: try inserting a char in badword */ |
| 15315 | for (round = 0; round <= 1; ++round) |
| 15316 | { |
| 15317 | score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS); |
| 15318 | if (score_off < minscore) |
| 15319 | { |
| 15320 | if (score_off + SCORE_EDIT_MIN >= minscore) |
| 15321 | { |
| 15322 | /* Near the limit, rest of the words must match. We |
| 15323 | * can check that right now, no need to push an item |
| 15324 | * onto the stack. */ |
| 15325 | bi2 = bi + 1 - round; |
| 15326 | gi2 = gi + round; |
| 15327 | while (goodword[gi2] == badword[bi2]) |
| 15328 | { |
| 15329 | if (goodword[gi2] == NUL) |
| 15330 | { |
| 15331 | minscore = score_off; |
| 15332 | break; |
| 15333 | } |
| 15334 | ++bi2; |
| 15335 | ++gi2; |
| 15336 | } |
| 15337 | } |
| 15338 | else |
| 15339 | { |
| 15340 | /* try deleting/inserting a character later */ |
| 15341 | stack[stackidx].badi = bi + 1 - round; |
| 15342 | stack[stackidx].goodi = gi + round; |
| 15343 | stack[stackidx].score = score_off; |
| 15344 | ++stackidx; |
| 15345 | } |
| 15346 | } |
| 15347 | } |
| 15348 | |
| 15349 | if (score + SCORE_SWAP < minscore) |
| 15350 | { |
| 15351 | /* If swapping two characters makes a match then the |
| 15352 | * substitution is more expensive, thus there is no need to |
| 15353 | * try both. */ |
| 15354 | if (gc == badword[bi + 1] && bc == goodword[gi + 1]) |
| 15355 | { |
| 15356 | /* Swap two characters, that is: skip them. */ |
| 15357 | gi += 2; |
| 15358 | bi += 2; |
| 15359 | score += SCORE_SWAP; |
| 15360 | continue; |
| 15361 | } |
| 15362 | } |
| 15363 | |
| 15364 | /* Substitute one character for another which is the same |
| 15365 | * thing as deleting a character from both goodword and badword. |
| 15366 | * Use a better score when there is only a case difference. */ |
| 15367 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
| 15368 | score += SCORE_ICASE; |
| 15369 | else |
| 15370 | { |
| 15371 | /* For a similar character use SCORE_SIMILAR. */ |
| 15372 | if (slang != NULL |
| 15373 | && slang->sl_has_map |
| 15374 | && similar_chars(slang, gc, bc)) |
| 15375 | score += SCORE_SIMILAR; |
| 15376 | else |
| 15377 | score += SCORE_SUBST; |
| 15378 | } |
| 15379 | |
| 15380 | if (score < minscore) |
| 15381 | { |
| 15382 | /* Do the substitution. */ |
| 15383 | ++gi; |
| 15384 | ++bi; |
| 15385 | continue; |
| 15386 | } |
| 15387 | } |
| 15388 | pop: |
| 15389 | /* |
| 15390 | * Get here to try the next alternative, pop it from the stack. |
| 15391 | */ |
| 15392 | if (stackidx == 0) /* stack is empty, finished */ |
| 15393 | break; |
| 15394 | |
| 15395 | /* pop an item from the stack */ |
| 15396 | --stackidx; |
| 15397 | gi = stack[stackidx].goodi; |
| 15398 | bi = stack[stackidx].badi; |
| 15399 | score = stack[stackidx].score; |
| 15400 | } |
| 15401 | |
| 15402 | /* When the score goes over "limit" it may actually be much higher. |
| 15403 | * Return a very large number to avoid going below the limit when giving a |
| 15404 | * bonus. */ |
| 15405 | if (minscore > limit) |
| 15406 | return SCORE_MAXMAX; |
| 15407 | return minscore; |
| 15408 | } |
| 15409 | |
| 15410 | #ifdef FEAT_MBYTE |
| 15411 | /* |
| 15412 | * Multi-byte version of spell_edit_score_limit(). |
| 15413 | * Keep it in sync with the above! |
| 15414 | */ |
| 15415 | static int |
| 15416 | spell_edit_score_limit_w(slang, badword, goodword, limit) |
| 15417 | slang_T *slang; |
| 15418 | char_u *badword; |
| 15419 | char_u *goodword; |
| 15420 | int limit; |
| 15421 | { |
| 15422 | limitscore_T stack[10]; /* allow for over 3 * 2 edits */ |
| 15423 | int stackidx; |
| 15424 | int bi, gi; |
| 15425 | int bi2, gi2; |
| 15426 | int bc, gc; |
| 15427 | int score; |
| 15428 | int score_off; |
| 15429 | int minscore; |
| 15430 | int round; |
| 15431 | char_u *p; |
| 15432 | int wbadword[MAXWLEN]; |
| 15433 | int wgoodword[MAXWLEN]; |
| 15434 | |
| 15435 | /* Get the characters from the multi-byte strings and put them in an |
| 15436 | * int array for easy access. */ |
| 15437 | bi = 0; |
| 15438 | for (p = badword; *p != NUL; ) |
| 15439 | wbadword[bi++] = mb_cptr2char_adv(&p); |
| 15440 | wbadword[bi++] = 0; |
| 15441 | gi = 0; |
| 15442 | for (p = goodword; *p != NUL; ) |
| 15443 | wgoodword[gi++] = mb_cptr2char_adv(&p); |
| 15444 | wgoodword[gi++] = 0; |
| 15445 | |
| 15446 | /* |
| 15447 | * The idea is to go from start to end over the words. So long as |
| 15448 | * characters are equal just continue, this always gives the lowest score. |
| 15449 | * When there is a difference try several alternatives. Each alternative |
| 15450 | * increases "score" for the edit distance. Some of the alternatives are |
| 15451 | * pushed unto a stack and tried later, some are tried right away. At the |
| 15452 | * end of the word the score for one alternative is known. The lowest |
| 15453 | * possible score is stored in "minscore". |
| 15454 | */ |
| 15455 | stackidx = 0; |
| 15456 | bi = 0; |
| 15457 | gi = 0; |
| 15458 | score = 0; |
| 15459 | minscore = limit + 1; |
| 15460 | |
| 15461 | for (;;) |
| 15462 | { |
| 15463 | /* Skip over an equal part, score remains the same. */ |
| 15464 | for (;;) |
| 15465 | { |
| 15466 | bc = wbadword[bi]; |
| 15467 | gc = wgoodword[gi]; |
| 15468 | |
| 15469 | if (bc != gc) /* stop at a char that's different */ |
| 15470 | break; |
| 15471 | if (bc == NUL) /* both words end */ |
| 15472 | { |
| 15473 | if (score < minscore) |
| 15474 | minscore = score; |
| 15475 | goto pop; /* do next alternative */ |
| 15476 | } |
| 15477 | ++bi; |
| 15478 | ++gi; |
| 15479 | } |
| 15480 | |
| 15481 | if (gc == NUL) /* goodword ends, delete badword chars */ |
| 15482 | { |
| 15483 | do |
| 15484 | { |
| 15485 | if ((score += SCORE_DEL) >= minscore) |
| 15486 | goto pop; /* do next alternative */ |
| 15487 | } while (wbadword[++bi] != NUL); |
| 15488 | minscore = score; |
| 15489 | } |
| 15490 | else if (bc == NUL) /* badword ends, insert badword chars */ |
| 15491 | { |
| 15492 | do |
| 15493 | { |
| 15494 | if ((score += SCORE_INS) >= minscore) |
| 15495 | goto pop; /* do next alternative */ |
| 15496 | } while (wgoodword[++gi] != NUL); |
| 15497 | minscore = score; |
| 15498 | } |
| 15499 | else /* both words continue */ |
| 15500 | { |
| 15501 | /* If not close to the limit, perform a change. Only try changes |
| 15502 | * that may lead to a lower score than "minscore". |
| 15503 | * round 0: try deleting a char from badword |
| 15504 | * round 1: try inserting a char in badword */ |
| 15505 | for (round = 0; round <= 1; ++round) |
| 15506 | { |
| 15507 | score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS); |
| 15508 | if (score_off < minscore) |
| 15509 | { |
| 15510 | if (score_off + SCORE_EDIT_MIN >= minscore) |
| 15511 | { |
| 15512 | /* Near the limit, rest of the words must match. We |
| 15513 | * can check that right now, no need to push an item |
| 15514 | * onto the stack. */ |
| 15515 | bi2 = bi + 1 - round; |
| 15516 | gi2 = gi + round; |
| 15517 | while (wgoodword[gi2] == wbadword[bi2]) |
| 15518 | { |
| 15519 | if (wgoodword[gi2] == NUL) |
| 15520 | { |
| 15521 | minscore = score_off; |
| 15522 | break; |
| 15523 | } |
| 15524 | ++bi2; |
| 15525 | ++gi2; |
| 15526 | } |
| 15527 | } |
| 15528 | else |
| 15529 | { |
| 15530 | /* try deleting a character from badword later */ |
| 15531 | stack[stackidx].badi = bi + 1 - round; |
| 15532 | stack[stackidx].goodi = gi + round; |
| 15533 | stack[stackidx].score = score_off; |
| 15534 | ++stackidx; |
| 15535 | } |
| 15536 | } |
| 15537 | } |
| 15538 | |
| 15539 | if (score + SCORE_SWAP < minscore) |
| 15540 | { |
| 15541 | /* If swapping two characters makes a match then the |
| 15542 | * substitution is more expensive, thus there is no need to |
| 15543 | * try both. */ |
| 15544 | if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1]) |
| 15545 | { |
| 15546 | /* Swap two characters, that is: skip them. */ |
| 15547 | gi += 2; |
| 15548 | bi += 2; |
| 15549 | score += SCORE_SWAP; |
| 15550 | continue; |
| 15551 | } |
| 15552 | } |
| 15553 | |
| 15554 | /* Substitute one character for another which is the same |
| 15555 | * thing as deleting a character from both goodword and badword. |
| 15556 | * Use a better score when there is only a case difference. */ |
| 15557 | if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
| 15558 | score += SCORE_ICASE; |
| 15559 | else |
| 15560 | { |
| 15561 | /* For a similar character use SCORE_SIMILAR. */ |
| 15562 | if (slang != NULL |
| 15563 | && slang->sl_has_map |
| 15564 | && similar_chars(slang, gc, bc)) |
| 15565 | score += SCORE_SIMILAR; |
| 15566 | else |
| 15567 | score += SCORE_SUBST; |
| 15568 | } |
| 15569 | |
| 15570 | if (score < minscore) |
| 15571 | { |
| 15572 | /* Do the substitution. */ |
| 15573 | ++gi; |
| 15574 | ++bi; |
| 15575 | continue; |
| 15576 | } |
| 15577 | } |
| 15578 | pop: |
| 15579 | /* |
| 15580 | * Get here to try the next alternative, pop it from the stack. |
| 15581 | */ |
| 15582 | if (stackidx == 0) /* stack is empty, finished */ |
| 15583 | break; |
| 15584 | |
| 15585 | /* pop an item from the stack */ |
| 15586 | --stackidx; |
| 15587 | gi = stack[stackidx].goodi; |
| 15588 | bi = stack[stackidx].badi; |
| 15589 | score = stack[stackidx].score; |
| 15590 | } |
| 15591 | |
| 15592 | /* When the score goes over "limit" it may actually be much higher. |
| 15593 | * Return a very large number to avoid going below the limit when giving a |
| 15594 | * bonus. */ |
| 15595 | if (minscore > limit) |
| 15596 | return SCORE_MAXMAX; |
| 15597 | return minscore; |
| 15598 | } |
| 15599 | #endif |
| 15600 | |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 15601 | /* |
| 15602 | * ":spellinfo" |
| 15603 | */ |
| 15604 | /*ARGSUSED*/ |
| 15605 | void |
| 15606 | ex_spellinfo(eap) |
| 15607 | exarg_T *eap; |
| 15608 | { |
| 15609 | int lpi; |
| 15610 | langp_T *lp; |
| 15611 | char_u *p; |
| 15612 | |
| 15613 | if (no_spell_checking(curwin)) |
| 15614 | return; |
| 15615 | |
| 15616 | msg_start(); |
| 15617 | for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi) |
| 15618 | { |
| 15619 | lp = LANGP_ENTRY(curbuf->b_langp, lpi); |
| 15620 | msg_puts((char_u *)"file: "); |
| 15621 | msg_puts(lp->lp_slang->sl_fname); |
| 15622 | msg_putchar('\n'); |
| 15623 | p = lp->lp_slang->sl_info; |
| 15624 | if (p != NULL) |
| 15625 | { |
| 15626 | msg_puts(p); |
| 15627 | msg_putchar('\n'); |
| 15628 | } |
| 15629 | } |
| 15630 | msg_end(); |
| 15631 | } |
| 15632 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15633 | #define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */ |
| 15634 | #define DUMPFLAG_COUNT 2 /* include word count */ |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15635 | #define DUMPFLAG_ICASE 4 /* ignore case when finding matches */ |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15636 | #define DUMPFLAG_ONECAP 8 /* pattern starts with capital */ |
| 15637 | #define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */ |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15638 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15639 | /* |
| 15640 | * ":spelldump" |
| 15641 | */ |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15642 | void |
| 15643 | ex_spelldump(eap) |
| 15644 | exarg_T *eap; |
| 15645 | { |
| 15646 | buf_T *buf = curbuf; |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15647 | |
| 15648 | if (no_spell_checking(curwin)) |
| 15649 | return; |
| 15650 | |
| 15651 | /* Create a new empty buffer by splitting the window. */ |
| 15652 | do_cmdline_cmd((char_u *)"new"); |
| 15653 | if (!bufempty() || !buf_valid(buf)) |
| 15654 | return; |
| 15655 | |
| 15656 | spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); |
| 15657 | |
| 15658 | /* Delete the empty line that we started with. */ |
| 15659 | if (curbuf->b_ml.ml_line_count > 1) |
| 15660 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 15661 | |
| 15662 | redraw_later(NOT_VALID); |
| 15663 | } |
| 15664 | |
| 15665 | /* |
| 15666 | * Go through all possible words and: |
| 15667 | * 1. When "pat" is NULL: dump a list of all words in the current buffer. |
| 15668 | * "ic" and "dir" are not used. |
| 15669 | * 2. When "pat" is not NULL: add matching words to insert mode completion. |
| 15670 | */ |
| 15671 | void |
| 15672 | spell_dump_compl(buf, pat, ic, dir, dumpflags_arg) |
| 15673 | buf_T *buf; /* buffer with spell checking */ |
| 15674 | char_u *pat; /* leading part of the word */ |
| 15675 | int ic; /* ignore case */ |
| 15676 | int *dir; /* direction for adding matches */ |
| 15677 | int dumpflags_arg; /* DUMPFLAG_* */ |
| 15678 | { |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15679 | langp_T *lp; |
| 15680 | slang_T *slang; |
| 15681 | idx_T arridx[MAXWLEN]; |
| 15682 | int curi[MAXWLEN]; |
| 15683 | char_u word[MAXWLEN]; |
| 15684 | int c; |
| 15685 | char_u *byts; |
| 15686 | idx_T *idxs; |
| 15687 | linenr_T lnum = 0; |
| 15688 | int round; |
| 15689 | int depth; |
| 15690 | int n; |
| 15691 | int flags; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15692 | char_u *region_names = NULL; /* region names being used */ |
| 15693 | int do_region = TRUE; /* dump region names and numbers */ |
| 15694 | char_u *p; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15695 | int lpi; |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15696 | int dumpflags = dumpflags_arg; |
| 15697 | int patlen; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15698 | |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15699 | /* When ignoring case or when the pattern starts with capital pass this on |
| 15700 | * to dump_word(). */ |
| 15701 | if (pat != NULL) |
| 15702 | { |
| 15703 | if (ic) |
| 15704 | dumpflags |= DUMPFLAG_ICASE; |
| 15705 | else |
| 15706 | { |
| 15707 | n = captype(pat, NULL); |
| 15708 | if (n == WF_ONECAP) |
| 15709 | dumpflags |= DUMPFLAG_ONECAP; |
| 15710 | else if (n == WF_ALLCAP |
| 15711 | #ifdef FEAT_MBYTE |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 15712 | && (int)STRLEN(pat) > mb_ptr2len(pat) |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15713 | #else |
| Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 15714 | && (int)STRLEN(pat) > 1 |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15715 | #endif |
| 15716 | ) |
| 15717 | dumpflags |= DUMPFLAG_ALLCAP; |
| 15718 | } |
| 15719 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15720 | |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15721 | /* Find out if we can support regions: All languages must support the same |
| 15722 | * regions or none at all. */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15723 | for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15724 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15725 | lp = LANGP_ENTRY(buf->b_langp, lpi); |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15726 | p = lp->lp_slang->sl_regions; |
| 15727 | if (p[0] != 0) |
| 15728 | { |
| 15729 | if (region_names == NULL) /* first language with regions */ |
| 15730 | region_names = p; |
| 15731 | else if (STRCMP(region_names, p) != 0) |
| 15732 | { |
| 15733 | do_region = FALSE; /* region names are different */ |
| 15734 | break; |
| 15735 | } |
| 15736 | } |
| 15737 | } |
| 15738 | |
| 15739 | if (do_region && region_names != NULL) |
| 15740 | { |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15741 | if (pat == NULL) |
| 15742 | { |
| 15743 | vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); |
| 15744 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 15745 | } |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15746 | } |
| 15747 | else |
| 15748 | do_region = FALSE; |
| 15749 | |
| 15750 | /* |
| 15751 | * Loop over all files loaded for the entries in 'spelllang'. |
| 15752 | */ |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15753 | for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15754 | { |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15755 | lp = LANGP_ENTRY(buf->b_langp, lpi); |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15756 | slang = lp->lp_slang; |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15757 | if (slang->sl_fbyts == NULL) /* reloading failed */ |
| 15758 | continue; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15759 | |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15760 | if (pat == NULL) |
| 15761 | { |
| 15762 | vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); |
| 15763 | ml_append(lnum++, IObuff, (colnr_T)0, FALSE); |
| 15764 | } |
| 15765 | |
| 15766 | /* When matching with a pattern and there are no prefixes only use |
| 15767 | * parts of the tree that match "pat". */ |
| 15768 | if (pat != NULL && slang->sl_pbyts == NULL) |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 15769 | patlen = (int)STRLEN(pat); |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15770 | else |
| Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 15771 | patlen = -1; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15772 | |
| 15773 | /* round 1: case-folded tree |
| 15774 | * round 2: keep-case tree */ |
| 15775 | for (round = 1; round <= 2; ++round) |
| 15776 | { |
| 15777 | if (round == 1) |
| 15778 | { |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15779 | dumpflags &= ~DUMPFLAG_KEEPCASE; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15780 | byts = slang->sl_fbyts; |
| 15781 | idxs = slang->sl_fidxs; |
| 15782 | } |
| 15783 | else |
| 15784 | { |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15785 | dumpflags |= DUMPFLAG_KEEPCASE; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15786 | byts = slang->sl_kbyts; |
| 15787 | idxs = slang->sl_kidxs; |
| 15788 | } |
| 15789 | if (byts == NULL) |
| 15790 | continue; /* array is empty */ |
| 15791 | |
| 15792 | depth = 0; |
| 15793 | arridx[0] = 0; |
| 15794 | curi[0] = 1; |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15795 | while (depth >= 0 && !got_int |
| 15796 | && (pat == NULL || !compl_interrupted)) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15797 | { |
| 15798 | if (curi[depth] > byts[arridx[depth]]) |
| 15799 | { |
| 15800 | /* Done all bytes at this node, go up one level. */ |
| 15801 | --depth; |
| 15802 | line_breakcheck(); |
| Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 15803 | ins_compl_check_keys(50); |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15804 | } |
| 15805 | else |
| 15806 | { |
| 15807 | /* Do one more byte at this node. */ |
| 15808 | n = arridx[depth] + curi[depth]; |
| 15809 | ++curi[depth]; |
| 15810 | c = byts[n]; |
| 15811 | if (c == 0) |
| 15812 | { |
| 15813 | /* End of word, deal with the word. |
| 15814 | * Don't use keep-case words in the fold-case tree, |
| 15815 | * they will appear in the keep-case tree. |
| 15816 | * Only use the word when the region matches. */ |
| 15817 | flags = (int)idxs[n]; |
| 15818 | if ((round == 2 || (flags & WF_KEEPCAP) == 0) |
| Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 15819 | && (flags & WF_NEEDCOMP) == 0 |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15820 | && (do_region |
| 15821 | || (flags & WF_REGION) == 0 |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 15822 | || (((unsigned)flags >> 16) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15823 | & lp->lp_region) != 0)) |
| 15824 | { |
| 15825 | word[depth] = NUL; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15826 | if (!do_region) |
| 15827 | flags &= ~WF_REGION; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 15828 | |
| 15829 | /* Dump the basic word if there is no prefix or |
| 15830 | * when it's the first one. */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 15831 | c = (unsigned)flags >> 24; |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 15832 | if (c == 0 || curi[depth] == 2) |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15833 | { |
| 15834 | dump_word(slang, word, pat, dir, |
| 15835 | dumpflags, flags, lnum); |
| 15836 | if (pat == NULL) |
| 15837 | ++lnum; |
| 15838 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15839 | |
| 15840 | /* Apply the prefix, if there is one. */ |
| Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 15841 | if (c != 0) |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15842 | lnum = dump_prefixes(slang, word, pat, dir, |
| 15843 | dumpflags, flags, lnum); |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15844 | } |
| 15845 | } |
| 15846 | else |
| 15847 | { |
| 15848 | /* Normal char, go one level deeper. */ |
| 15849 | word[depth++] = c; |
| 15850 | arridx[depth] = idxs[n]; |
| 15851 | curi[depth] = 1; |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15852 | |
| 15853 | /* Check if this characters matches with the pattern. |
| 15854 | * If not skip the whole tree below it. |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15855 | * Always ignore case here, dump_word() will check |
| 15856 | * proper case later. This isn't exactly right when |
| 15857 | * length changes for multi-byte characters with |
| 15858 | * ignore case... */ |
| 15859 | if (depth <= patlen |
| 15860 | && MB_STRNICMP(word, pat, depth) != 0) |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15861 | --depth; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15862 | } |
| 15863 | } |
| 15864 | } |
| 15865 | } |
| 15866 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15867 | } |
| 15868 | |
| 15869 | /* |
| 15870 | * Dump one word: apply case modifications and append a line to the buffer. |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15871 | * When "lnum" is zero add insert mode completion. |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15872 | */ |
| 15873 | static void |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15874 | dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15875 | slang_T *slang; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15876 | char_u *word; |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15877 | char_u *pat; |
| 15878 | int *dir; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15879 | int dumpflags; |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15880 | int wordflags; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15881 | linenr_T lnum; |
| 15882 | { |
| 15883 | int keepcap = FALSE; |
| 15884 | char_u *p; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15885 | char_u *tw; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15886 | char_u cword[MAXWLEN]; |
| Bram Moolenaar | 7887d88 | 2005-07-01 22:33:52 +0000 | [diff] [blame] | 15887 | char_u badword[MAXWLEN + 10]; |
| 15888 | int i; |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15889 | int flags = wordflags; |
| 15890 | |
| 15891 | if (dumpflags & DUMPFLAG_ONECAP) |
| 15892 | flags |= WF_ONECAP; |
| 15893 | if (dumpflags & DUMPFLAG_ALLCAP) |
| 15894 | flags |= WF_ALLCAP; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15895 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15896 | if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15897 | { |
| 15898 | /* Need to fix case according to "flags". */ |
| 15899 | make_case_word(word, cword, flags); |
| 15900 | p = cword; |
| 15901 | } |
| 15902 | else |
| 15903 | { |
| 15904 | p = word; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15905 | if ((dumpflags & DUMPFLAG_KEEPCASE) |
| 15906 | && ((captype(word, NULL) & WF_KEEPCAP) == 0 |
| Bram Moolenaar | 0dc065e | 2005-07-04 22:49:24 +0000 | [diff] [blame] | 15907 | || (flags & WF_FIXCAP) != 0)) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15908 | keepcap = TRUE; |
| 15909 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15910 | tw = p; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15911 | |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15912 | if (pat == NULL) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15913 | { |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15914 | /* Add flags and regions after a slash. */ |
| 15915 | if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15916 | { |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15917 | STRCPY(badword, p); |
| 15918 | STRCAT(badword, "/"); |
| 15919 | if (keepcap) |
| 15920 | STRCAT(badword, "="); |
| 15921 | if (flags & WF_BANNED) |
| 15922 | STRCAT(badword, "!"); |
| 15923 | else if (flags & WF_RARE) |
| 15924 | STRCAT(badword, "?"); |
| 15925 | if (flags & WF_REGION) |
| 15926 | for (i = 0; i < 7; ++i) |
| 15927 | if (flags & (0x10000 << i)) |
| 15928 | sprintf((char *)badword + STRLEN(badword), "%d", i + 1); |
| 15929 | p = badword; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15930 | } |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15931 | |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15932 | if (dumpflags & DUMPFLAG_COUNT) |
| 15933 | { |
| 15934 | hashitem_T *hi; |
| 15935 | |
| 15936 | /* Include the word count for ":spelldump!". */ |
| 15937 | hi = hash_find(&slang->sl_wordcount, tw); |
| 15938 | if (!HASHITEM_EMPTY(hi)) |
| 15939 | { |
| 15940 | vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d", |
| 15941 | tw, HI2WC(hi)->wc_count); |
| 15942 | p = IObuff; |
| 15943 | } |
| 15944 | } |
| 15945 | |
| 15946 | ml_append(lnum, p, (colnr_T)0, FALSE); |
| 15947 | } |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15948 | else if (((dumpflags & DUMPFLAG_ICASE) |
| 15949 | ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0 |
| 15950 | : STRNCMP(p, pat, STRLEN(pat)) == 0) |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15951 | && ins_compl_add_infercase(p, (int)STRLEN(p), |
| Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 15952 | p_ic, NULL, *dir, 0) == OK) |
| Bram Moolenaar | d0131a8 | 2006-03-04 21:46:13 +0000 | [diff] [blame] | 15953 | /* if dir was BACKWARD then honor it just once */ |
| 15954 | *dir = FORWARD; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15955 | } |
| 15956 | |
| 15957 | /* |
| Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 15958 | * For ":spelldump": Find matching prefixes for "word". Prepend each to |
| 15959 | * "word" and append a line to the buffer. |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15960 | * When "lnum" is zero add insert mode completion. |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15961 | * Return the updated line number. |
| 15962 | */ |
| 15963 | static linenr_T |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15964 | dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15965 | slang_T *slang; |
| 15966 | char_u *word; /* case-folded word */ |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 15967 | char_u *pat; |
| 15968 | int *dir; |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 15969 | int dumpflags; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15970 | int flags; /* flags with prefix ID */ |
| 15971 | linenr_T startlnum; |
| 15972 | { |
| 15973 | idx_T arridx[MAXWLEN]; |
| 15974 | int curi[MAXWLEN]; |
| 15975 | char_u prefix[MAXWLEN]; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 15976 | char_u word_up[MAXWLEN]; |
| 15977 | int has_word_up = FALSE; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15978 | int c; |
| 15979 | char_u *byts; |
| 15980 | idx_T *idxs; |
| 15981 | linenr_T lnum = startlnum; |
| 15982 | int depth; |
| 15983 | int n; |
| 15984 | int len; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15985 | int i; |
| 15986 | |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 15987 | /* If the word starts with a lower-case letter make the word with an |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 15988 | * upper-case letter in word_up[]. */ |
| 15989 | c = PTR2CHAR(word); |
| 15990 | if (SPELL_TOUPPER(c) != c) |
| 15991 | { |
| 15992 | onecap_copy(word, word_up, TRUE); |
| 15993 | has_word_up = TRUE; |
| 15994 | } |
| 15995 | |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 15996 | byts = slang->sl_pbyts; |
| 15997 | idxs = slang->sl_pidxs; |
| 15998 | if (byts != NULL) /* array not is empty */ |
| 15999 | { |
| 16000 | /* |
| 16001 | * Loop over all prefixes, building them byte-by-byte in prefix[]. |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 16002 | * When at the end of a prefix check that it supports "flags". |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 16003 | */ |
| 16004 | depth = 0; |
| 16005 | arridx[0] = 0; |
| 16006 | curi[0] = 1; |
| 16007 | while (depth >= 0 && !got_int) |
| 16008 | { |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 16009 | n = arridx[depth]; |
| 16010 | len = byts[n]; |
| 16011 | if (curi[depth] > len) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 16012 | { |
| 16013 | /* Done all bytes at this node, go up one level. */ |
| 16014 | --depth; |
| 16015 | line_breakcheck(); |
| 16016 | } |
| 16017 | else |
| 16018 | { |
| 16019 | /* Do one more byte at this node. */ |
| Bram Moolenaar | dfb9ac0 | 2005-07-05 21:36:03 +0000 | [diff] [blame] | 16020 | n += curi[depth]; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 16021 | ++curi[depth]; |
| 16022 | c = byts[n]; |
| 16023 | if (c == 0) |
| 16024 | { |
| 16025 | /* End of prefix, find out how many IDs there are. */ |
| 16026 | for (i = 1; i < len; ++i) |
| 16027 | if (byts[n + i] != 0) |
| 16028 | break; |
| 16029 | curi[depth] += i - 1; |
| 16030 | |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 16031 | c = valid_word_prefix(i, n, flags, word, slang, FALSE); |
| 16032 | if (c != 0) |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 16033 | { |
| Bram Moolenaar | 9c96f59 | 2005-06-30 21:52:39 +0000 | [diff] [blame] | 16034 | vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 16035 | dump_word(slang, prefix, pat, dir, dumpflags, |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 16036 | (c & WF_RAREPFX) ? (flags | WF_RARE) |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 16037 | : flags, lnum); |
| 16038 | if (lnum != 0) |
| 16039 | ++lnum; |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 16040 | } |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 16041 | |
| 16042 | /* Check for prefix that matches the word when the |
| 16043 | * first letter is upper-case, but only if the prefix has |
| 16044 | * a condition. */ |
| 16045 | if (has_word_up) |
| 16046 | { |
| 16047 | c = valid_word_prefix(i, n, flags, word_up, slang, |
| 16048 | TRUE); |
| 16049 | if (c != 0) |
| 16050 | { |
| 16051 | vim_strncpy(prefix + depth, word_up, |
| 16052 | MAXWLEN - depth - 1); |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 16053 | dump_word(slang, prefix, pat, dir, dumpflags, |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 16054 | (c & WF_RAREPFX) ? (flags | WF_RARE) |
| Bram Moolenaar | b475fb9 | 2006-03-02 22:40:52 +0000 | [diff] [blame] | 16055 | : flags, lnum); |
| 16056 | if (lnum != 0) |
| 16057 | ++lnum; |
| Bram Moolenaar | 53805d1 | 2005-08-01 07:08:33 +0000 | [diff] [blame] | 16058 | } |
| 16059 | } |
| Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 16060 | } |
| 16061 | else |
| 16062 | { |
| 16063 | /* Normal char, go one level deeper. */ |
| 16064 | prefix[depth++] = c; |
| 16065 | arridx[depth] = idxs[n]; |
| 16066 | curi[depth] = 1; |
| 16067 | } |
| 16068 | } |
| 16069 | } |
| 16070 | } |
| 16071 | |
| 16072 | return lnum; |
| 16073 | } |
| 16074 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 16075 | /* |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 16076 | * Move "p" to the end of word "start". |
| 16077 | * Uses the spell-checking word characters. |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 16078 | */ |
| 16079 | char_u * |
| 16080 | spell_to_word_end(start, buf) |
| 16081 | char_u *start; |
| 16082 | buf_T *buf; |
| 16083 | { |
| 16084 | char_u *p = start; |
| 16085 | |
| 16086 | while (*p != NUL && spell_iswordp(p, buf)) |
| 16087 | mb_ptr_adv(p); |
| 16088 | return p; |
| 16089 | } |
| 16090 | |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16091 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16092 | /* |
| Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 16093 | * For Insert mode completion CTRL-X s: |
| 16094 | * Find start of the word in front of column "startcol". |
| 16095 | * We don't check if it is badly spelled, with completion we can only change |
| 16096 | * the word in front of the cursor. |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16097 | * Returns the column number of the word. |
| 16098 | */ |
| 16099 | int |
| 16100 | spell_word_start(startcol) |
| 16101 | int startcol; |
| 16102 | { |
| 16103 | char_u *line; |
| 16104 | char_u *p; |
| 16105 | int col = 0; |
| 16106 | |
| Bram Moolenaar | 9552956 | 2005-08-25 21:21:38 +0000 | [diff] [blame] | 16107 | if (no_spell_checking(curwin)) |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16108 | return startcol; |
| 16109 | |
| 16110 | /* Find a word character before "startcol". */ |
| 16111 | line = ml_get_curline(); |
| 16112 | for (p = line + startcol; p > line; ) |
| 16113 | { |
| 16114 | mb_ptr_back(line, p); |
| 16115 | if (spell_iswordp_nmw(p)) |
| 16116 | break; |
| 16117 | } |
| 16118 | |
| 16119 | /* Go back to start of the word. */ |
| 16120 | while (p > line) |
| 16121 | { |
| Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 16122 | col = (int)(p - line); |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16123 | mb_ptr_back(line, p); |
| 16124 | if (!spell_iswordp(p, curbuf)) |
| 16125 | break; |
| 16126 | col = 0; |
| 16127 | } |
| 16128 | |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16129 | return col; |
| 16130 | } |
| 16131 | |
| 16132 | /* |
| Bram Moolenaar | 4effc80 | 2005-09-30 21:12:02 +0000 | [diff] [blame] | 16133 | * Need to check for 'spellcapcheck' now, the word is removed before |
| 16134 | * expand_spelling() is called. Therefore the ugly global variable. |
| 16135 | */ |
| 16136 | static int spell_expand_need_cap; |
| 16137 | |
| 16138 | void |
| 16139 | spell_expand_check_cap(col) |
| 16140 | colnr_T col; |
| 16141 | { |
| 16142 | spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col); |
| 16143 | } |
| 16144 | |
| 16145 | /* |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16146 | * Get list of spelling suggestions. |
| 16147 | * Used for Insert mode completion CTRL-X ?. |
| 16148 | * Returns the number of matches. The matches are in "matchp[]", array of |
| 16149 | * allocated strings. |
| 16150 | */ |
| 16151 | /*ARGSUSED*/ |
| 16152 | int |
| 16153 | expand_spelling(lnum, col, pat, matchp) |
| 16154 | linenr_T lnum; |
| 16155 | int col; |
| 16156 | char_u *pat; |
| 16157 | char_u ***matchp; |
| 16158 | { |
| 16159 | garray_T ga; |
| 16160 | |
| Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 16161 | spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE); |
| Bram Moolenaar | 8b59de9 | 2005-08-11 19:59:29 +0000 | [diff] [blame] | 16162 | *matchp = ga.ga_data; |
| 16163 | return ga.ga_len; |
| 16164 | } |
| 16165 | #endif |
| 16166 | |
| Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 16167 | #endif /* FEAT_SPELL */ |