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). |
| 16 | * A NUL byte is used where the word may end. |
| 17 | * |
| 18 | * There are two trees: one with case-folded words and one with words in |
| 19 | * original case. The second one is only used for keep-case words and is |
| 20 | * usually small. |
| 21 | * |
| 22 | * Thanks to Olaf Seibert for providing an example implementation of this tree |
| 23 | * and the compression mechanism. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 24 | * |
| 25 | * Matching involves checking the caps type: Onecap ALLCAP KeepCap. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 26 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 27 | * Why doesn't Vim use aspell/ispell/myspell/etc.? |
| 28 | * See ":help develop-spell". |
| 29 | */ |
| 30 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Vim spell file format: <HEADER> <SUGGEST> <LWORDTREE> <KWORDTREE> |
| 33 | * |
| 34 | * <HEADER>: <fileID> <regioncnt> <regionname> ... |
| 35 | * <charflagslen> <charflags> <fcharslen> <fchars> |
| 36 | * |
| 37 | * <fileID> 10 bytes "VIMspell05" |
| 38 | * <regioncnt> 1 byte number of regions following (8 supported) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 39 | * <regionname> 2 bytes Region name: ca, au, etc. Lower case. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 40 | * First <regionname> is region 1. |
| 41 | * |
| 42 | * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128). |
| 43 | * <charflags> N bytes List of flags (first one is for character 128): |
| 44 | * 0x01 word character |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 45 | * 0x02 upper-case character |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 46 | * <fcharslen> 2 bytes Number of bytes in <fchars>. |
| 47 | * <fchars> N bytes Folded characters, first one is for character 128. |
| 48 | * |
| 49 | * |
| 50 | * <SUGGEST> : <suggestlen> <more> ... |
| 51 | * |
| 52 | * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding |
| 53 | * <suggestlen>. MSB first. |
| 54 | * <more> To be defined. |
| 55 | * |
| 56 | * |
| 57 | * <LWORDTREE>: <wordtree> |
| 58 | * |
| 59 | * <wordtree>: <nodecount> <nodedata> ... |
| 60 | * |
| 61 | * <nodecount> 4 bytes Number of nodes following. MSB first. |
| 62 | * |
| 63 | * <nodedata>: <siblingcount> <sibling> ... |
| 64 | * |
| 65 | * <siblingcount> 1 byte Number of siblings in this node. The siblings |
| 66 | * follow in sorted order. |
| 67 | * |
| 68 | * <sibling>: <byte> [<nodeidx> <xbyte> | <flags> [<region>]] |
| 69 | * |
| 70 | * <byte> 1 byte Byte value of the sibling. Special cases: |
| 71 | * BY_NOFLAGS: End of word without flags and for all |
| 72 | * regions. |
| 73 | * BY_FLAGS: End of word, <flags> follow. |
| 74 | * BY_INDEX: Child of sibling is shared, <nodeidx> |
| 75 | * and <xbyte> follow. |
| 76 | * |
| 77 | * <nodeidx> 3 bytes Index of child for this sibling, MSB first. |
| 78 | * |
| 79 | * <xbyte> 1 byte byte value of the sibling. |
| 80 | * |
| 81 | * <flags> 1 byte bitmask of: |
| 82 | * WF_ALLCAP word must have only capitals |
| 83 | * WF_ONECAP first char of word must be capital |
| 84 | * WF_RARE rare word |
| 85 | * WF_REGION <region> follows |
| 86 | * |
| 87 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 88 | * omitted it's valid in all regions. |
| 89 | * Lowest bit is for region 1. |
| 90 | * |
| 91 | * <KWORDTREE>: <wordtree> |
| 92 | * |
| 93 | * |
| 94 | * All text characters are in 'encoding', but stored as single bytes. |
| 95 | * The region name is ASCII. |
| 96 | */ |
| 97 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 98 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 99 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 100 | #endif |
| 101 | |
| 102 | #include "vim.h" |
| 103 | |
| 104 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 105 | |
| 106 | #ifdef HAVE_FCNTL_H |
| 107 | # include <fcntl.h> |
| 108 | #endif |
| 109 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 110 | #define MAXWLEN 250 /* assume max. word len is this many bytes */ |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 111 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 112 | /* Flags used for a word. */ |
| 113 | #define WF_REGION 0x01 /* region byte follows */ |
| 114 | #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */ |
| 115 | #define WF_ALLCAP 0x04 /* word must be all capitals */ |
| 116 | #define WF_RARE 0x08 /* rare word */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 117 | #define WF_BANNED 0x10 /* bad word */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 118 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 119 | #define WF_KEEPCAP 0x100 /* keep-case word (not stored in file) */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 120 | |
| 121 | #define BY_NOFLAGS 0 /* end of word without flags or region */ |
| 122 | #define BY_FLAGS 1 /* end of word, flag byte follows */ |
| 123 | #define BY_INDEX 2 /* child is shared, index follows */ |
| 124 | #define BY_SPECIAL BY_INDEX /* hightest special byte value */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 125 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 126 | /* Info from "REP" entries in ".aff" file used in af_rep. |
| 127 | * TODO: This is not used yet. Either use it or remove it. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 128 | typedef struct repentry_S |
| 129 | { |
| 130 | char_u *re_from; |
| 131 | char_u *re_to; |
| 132 | } repentry_T; |
| 133 | |
| 134 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 135 | * Structure used to store words and other info for one language, loaded from |
| 136 | * a .spl file. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 137 | * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the |
| 138 | * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words. |
| 139 | * |
| 140 | * The "byts" array stores the possible bytes in each tree node, preceded by |
| 141 | * the number of possible bytes, sorted on byte value: |
| 142 | * <len> <byte1> <byte2> ... |
| 143 | * The "idxs" array stores the index of the child node corresponding to the |
| 144 | * byte in "byts". |
| 145 | * Exception: when the byte is zero, the word may end here and "idxs" holds |
| 146 | * the flags and region for the word. There may be several zeros in sequence |
| 147 | * for alternative flag/region combinations. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 148 | */ |
| 149 | typedef struct slang_S slang_T; |
| 150 | struct slang_S |
| 151 | { |
| 152 | slang_T *sl_next; /* next language */ |
| 153 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 154 | char_u *sl_fbyts; /* case-folded word bytes */ |
| 155 | int *sl_fidxs; /* case-folded word indexes */ |
| 156 | char_u *sl_kbyts; /* keep-case word bytes */ |
| 157 | int *sl_kidxs; /* keep-case word indexes */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 158 | char_u *sl_try; /* "TRY" from .aff file TODO: not used */ |
| 159 | garray_T sl_rep; /* list of repentry_T entries from REP lines |
| 160 | * TODO not used */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 161 | char_u sl_regions[17]; /* table with up to 8 region names plus NUL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 162 | int sl_error; /* error while loading */ |
| 163 | }; |
| 164 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 165 | /* First language that is loaded, start of the linked list of loaded |
| 166 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 167 | static slang_T *first_lang = NULL; |
| 168 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 169 | /* |
| 170 | * Structure used in "b_langp", filled from 'spelllang'. |
| 171 | */ |
| 172 | typedef struct langp_S |
| 173 | { |
| 174 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 175 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 176 | } langp_T; |
| 177 | |
| 178 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 179 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 180 | #define REGION_ALL 0xff /* word valid in all regions */ |
| 181 | |
| 182 | /* Result values. Lower number is accepted over higher one. */ |
| 183 | #define SP_BANNED -1 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 184 | #define SP_OK 0 |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 185 | #define SP_RARE 1 |
| 186 | #define SP_LOCAL 2 |
| 187 | #define SP_BAD 3 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 188 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 189 | #define VIMSPELLMAGIC "VIMspell05" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 190 | #define VIMSPELLMAGICL 10 |
| 191 | |
| 192 | /* |
| 193 | * Structure to store info for word matching. |
| 194 | */ |
| 195 | typedef struct matchinf_S |
| 196 | { |
| 197 | langp_T *mi_lp; /* info for language and region */ |
| 198 | slang_T *mi_slang; /* info for the language */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 199 | |
| 200 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 201 | char_u *mi_word; /* start of word being checked */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 202 | char_u *mi_end; /* end of matching word */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 203 | char_u *mi_fend; /* next char to be added to mi_fword */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 204 | char_u *mi_cend; /* char after what was used for |
| 205 | mi_capflags */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 206 | |
| 207 | /* case-folded text */ |
| 208 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 209 | int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 210 | |
| 211 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 212 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 213 | int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 214 | } matchinf_T; |
| 215 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 216 | /* |
| 217 | * The tables used for recognizing word characters according to spelling. |
| 218 | * These are only used for the first 256 characters of 'encoding'. |
| 219 | */ |
| 220 | typedef struct spelltab_S |
| 221 | { |
| 222 | char_u st_isw[256]; /* flags: is word char */ |
| 223 | char_u st_isu[256]; /* flags: is uppercase char */ |
| 224 | char_u st_fold[256]; /* chars: folded case */ |
| 225 | } spelltab_T; |
| 226 | |
| 227 | static spelltab_T spelltab; |
| 228 | static int did_set_spelltab; |
| 229 | |
| 230 | #define SPELL_ISWORD 1 |
| 231 | #define SPELL_ISUPPER 2 |
| 232 | |
| 233 | static void clear_spell_chartab __ARGS((spelltab_T *sp)); |
| 234 | static int set_spell_finish __ARGS((spelltab_T *new_st)); |
| 235 | |
| 236 | /* |
| 237 | * Return TRUE if "p" points to a word character or "c" is a word character |
| 238 | * for spelling. |
| 239 | * Checking for a word character is done very often, avoid the function call |
| 240 | * overhead. |
| 241 | */ |
| 242 | #ifdef FEAT_MBYTE |
| 243 | # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \ |
| 244 | ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)]) |
| 245 | #else |
| 246 | # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)]) |
| 247 | #endif |
| 248 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 249 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 250 | static void slang_free __ARGS((slang_T *lp)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 251 | static void find_word __ARGS((matchinf_T *mip, int keepcap)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 252 | static void spell_load_lang __ARGS((char_u *lang)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 253 | static void spell_load_file __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 254 | static int read_tree __ARGS((FILE *fd, char_u *byts, int *idxs, int maxidx, int startidx)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 255 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 256 | static int captype __ARGS((char_u *word, char_u *end)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 257 | static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp)); |
| 258 | #ifdef FEAT_MBYTE |
| 259 | static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp)); |
| 260 | static void write_spell_chartab __ARGS((FILE *fd)); |
| 261 | #endif |
| 262 | static int spell_isupper __ARGS((int c)); |
| 263 | static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen)); |
| 264 | |
| 265 | static char *e_format = N_("E759: Format error in spell file"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Main spell-checking function. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 269 | * "ptr" points to a character that could be the start of a word. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 270 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 271 | * or when it's OK it remains unchanged. |
| 272 | * This must only be called when 'spelllang' is not empty. |
| 273 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 274 | * caller can skip over the word. |
| 275 | */ |
| 276 | int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 277 | spell_check(wp, ptr, attrp) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 278 | win_T *wp; /* current window */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 279 | char_u *ptr; |
| 280 | int *attrp; |
| 281 | { |
| 282 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 283 | be passed to functions quickly. */ |
| 284 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 285 | /* A word never starts at a space or a control character. Return quickly |
| 286 | * then, skipping over the character. */ |
| 287 | if (*ptr <= ' ') |
| 288 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 289 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 290 | /* A word starting with a number is always OK. Also skip hexadecimal |
| 291 | * numbers 0xFF99 and 0X99FF. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 292 | if (*ptr >= '0' && *ptr <= '9') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 293 | { |
| 294 | if (*ptr == '0' && (ptr[1] == 'x' || ptr[2] == 'X')) |
| 295 | mi.mi_end = skiphex(ptr); |
| 296 | else |
| 297 | mi.mi_end = skipdigits(ptr); |
| 298 | } |
| 299 | else |
| 300 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 301 | /* Find the end of the word. */ |
| 302 | mi.mi_word = ptr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 303 | mi.mi_fend = ptr; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 304 | if (SPELL_ISWORDP(mi.mi_fend)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 305 | { |
| 306 | /* Make case-folded copy of the characters until the next non-word |
| 307 | * character. */ |
| 308 | do |
| 309 | { |
| 310 | mb_ptr_adv(mi.mi_fend); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 311 | } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 312 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 313 | /* Check the caps type of the word. */ |
| 314 | mi.mi_capflags = captype(ptr, mi.mi_fend); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 315 | } |
| 316 | else |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 317 | /* No word characters, caps type is always zero. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 318 | mi.mi_capflags = 0; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 319 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 320 | /* We always use the characters up to the next non-word character, |
| 321 | * also for bad words. */ |
| 322 | mi.mi_end = mi.mi_fend; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 323 | mi.mi_cend = mi.mi_fend; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 324 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 325 | /* Include one non-word character so that we can check for the |
| 326 | * word end. */ |
| 327 | if (*mi.mi_fend != NUL) |
| 328 | mb_ptr_adv(mi.mi_fend); |
| 329 | |
| 330 | (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, |
| 331 | MAXWLEN + 1); |
| 332 | mi.mi_fwordlen = STRLEN(mi.mi_fword); |
| 333 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 334 | /* The word is bad unless we recognize it. */ |
| 335 | mi.mi_result = SP_BAD; |
| 336 | |
| 337 | /* |
| 338 | * Loop over the languages specified in 'spelllang'. |
| 339 | * We check them all, because a matching word may be longer than an |
| 340 | * already found matching word. |
| 341 | */ |
| 342 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 343 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 344 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 345 | /* Check for a matching word in case-folded words. */ |
| 346 | find_word(&mi, FALSE); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 347 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 348 | /* Try keep-case words. */ |
| 349 | find_word(&mi, TRUE); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 352 | if (mi.mi_result != SP_OK) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 353 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 354 | /* When we are at a non-word character there is no error, just |
| 355 | * skip over the character (try looking for a word after it). */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 356 | if (!SPELL_ISWORDP(ptr)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 357 | { |
| 358 | #ifdef FEAT_MBYTE |
| 359 | if (has_mbyte) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 360 | return mb_ptr2len_check(ptr); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 361 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 362 | return 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 363 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 364 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 365 | if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 366 | *attrp = highlight_attr[HLF_SPB]; |
| 367 | else if (mi.mi_result == SP_RARE) |
| 368 | *attrp = highlight_attr[HLF_SPR]; |
| 369 | else |
| 370 | *attrp = highlight_attr[HLF_SPL]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 374 | return (int)(mi.mi_end - ptr); |
| 375 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 376 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 377 | /* |
| 378 | * Check if the word at "mip->mi_word" is in the tree. |
| 379 | * When "keepcap" is TRUE check in keep-case word tree. |
| 380 | * |
| 381 | * For a match mip->mi_result is updated. |
| 382 | */ |
| 383 | static void |
| 384 | find_word(mip, keepcap) |
| 385 | matchinf_T *mip; |
| 386 | int keepcap; |
| 387 | { |
| 388 | int arridx = 0; |
| 389 | int endlen[MAXWLEN]; /* length at possible word endings */ |
| 390 | int endidx[MAXWLEN]; /* possible word endings */ |
| 391 | int endidxcnt = 0; |
| 392 | int len; |
| 393 | int wlen = 0; |
| 394 | int flen; |
| 395 | int c; |
| 396 | char_u *ptr; |
| 397 | unsigned lo, hi, m; |
| 398 | #ifdef FEAT_MBYTE |
| 399 | char_u *s; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 400 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 401 | char_u *p; |
| 402 | int res = SP_BAD; |
| 403 | int valid; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 404 | slang_T *slang = mip->mi_lp->lp_slang; |
| 405 | unsigned flags; |
| 406 | char_u *byts; |
| 407 | int *idxs; |
| 408 | |
| 409 | if (keepcap) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 410 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 411 | /* Check for word with matching case in keep-case tree. */ |
| 412 | ptr = mip->mi_word; |
| 413 | flen = 9999; /* no case folding, always enough bytes */ |
| 414 | byts = slang->sl_kbyts; |
| 415 | idxs = slang->sl_kidxs; |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | /* Check for case-folded in case-folded tree. */ |
| 420 | ptr = mip->mi_fword; |
| 421 | flen = mip->mi_fwordlen; /* available case-folded bytes */ |
| 422 | byts = slang->sl_fbyts; |
| 423 | idxs = slang->sl_fidxs; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 426 | if (byts == NULL) |
| 427 | return; /* array is empty */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 428 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 429 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 430 | * Repeat advancing in the tree until: |
| 431 | * - there is a byte that doesn't match, |
| 432 | * - we reach the end of the tree, |
| 433 | * - or we reach the end of the line. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 434 | */ |
| 435 | for (;;) |
| 436 | { |
| 437 | if (flen == 0 && *mip->mi_fend != NUL) |
| 438 | { |
| 439 | /* Need to fold at least one more character. Do until next |
| 440 | * non-word character for efficiency. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 441 | p = mip->mi_fend; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 442 | do |
| 443 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 444 | mb_ptr_adv(mip->mi_fend); |
| 445 | } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 446 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 447 | /* Include the non-word character so that we can check for the |
| 448 | * word end. */ |
| 449 | if (*mip->mi_fend != NUL) |
| 450 | mb_ptr_adv(mip->mi_fend); |
| 451 | |
| 452 | (void)spell_casefold(p, (int)(mip->mi_fend - p), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 453 | mip->mi_fword + mip->mi_fwordlen, |
| 454 | MAXWLEN - mip->mi_fwordlen); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 455 | flen = STRLEN(mip->mi_fword + mip->mi_fwordlen); |
| 456 | mip->mi_fwordlen += flen; |
| 457 | } |
| 458 | |
| 459 | len = byts[arridx++]; |
| 460 | |
| 461 | /* If the first possible byte is a zero the word could end here. |
| 462 | * Remember this index, we first check for the longest word. */ |
| 463 | if (byts[arridx] == 0) |
| 464 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 465 | if (endidxcnt == MAXWLEN) |
| 466 | { |
| 467 | /* Must be a corrupted spell file. */ |
| 468 | EMSG(_(e_format)); |
| 469 | return; |
| 470 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 471 | endlen[endidxcnt] = wlen; |
| 472 | endidx[endidxcnt++] = arridx++; |
| 473 | --len; |
| 474 | |
| 475 | /* Skip over the zeros, there can be several flag/region |
| 476 | * combinations. */ |
| 477 | while (len > 0 && byts[arridx] == 0) |
| 478 | { |
| 479 | ++arridx; |
| 480 | --len; |
| 481 | } |
| 482 | if (len == 0) |
| 483 | break; /* no children, word must end here */ |
| 484 | } |
| 485 | |
| 486 | /* Stop looking at end of the line. */ |
| 487 | if (ptr[wlen] == NUL) |
| 488 | break; |
| 489 | |
| 490 | /* Perform a binary search in the list of accepted bytes. */ |
| 491 | c = ptr[wlen]; |
| 492 | lo = arridx; |
| 493 | hi = arridx + len - 1; |
| 494 | while (lo < hi) |
| 495 | { |
| 496 | m = (lo + hi) / 2; |
| 497 | if (byts[m] > c) |
| 498 | hi = m - 1; |
| 499 | else if (byts[m] < c) |
| 500 | lo = m + 1; |
| 501 | else |
| 502 | { |
| 503 | lo = hi = m; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* Stop if there is no matching byte. */ |
| 509 | if (hi < lo || byts[lo] != c) |
| 510 | break; |
| 511 | |
| 512 | /* Continue at the child (if there is one). */ |
| 513 | arridx = idxs[lo]; |
| 514 | ++wlen; |
| 515 | --flen; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Verify that one of the possible endings is valid. Try the longest |
| 520 | * first. |
| 521 | */ |
| 522 | while (endidxcnt > 0) |
| 523 | { |
| 524 | --endidxcnt; |
| 525 | arridx = endidx[endidxcnt]; |
| 526 | wlen = endlen[endidxcnt]; |
| 527 | |
| 528 | #ifdef FEAT_MBYTE |
| 529 | if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
| 530 | continue; /* not at first byte of character */ |
| 531 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 532 | if (SPELL_ISWORDP(ptr + wlen)) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 533 | continue; /* next char is a word character */ |
| 534 | |
| 535 | #ifdef FEAT_MBYTE |
| 536 | if (!keepcap && has_mbyte) |
| 537 | { |
| 538 | /* Compute byte length in original word, length may change |
| 539 | * when folding case. */ |
| 540 | p = mip->mi_word; |
| 541 | for (s = ptr; s < ptr + wlen; mb_ptr_adv(s)) |
| 542 | mb_ptr_adv(p); |
| 543 | wlen = p - mip->mi_word; |
| 544 | } |
| 545 | #endif |
| 546 | |
| 547 | /* Check flags and region. Repeat this if there are more |
| 548 | * flags/region alternatives until there is a match. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 549 | for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len) |
| 550 | { |
| 551 | flags = idxs[arridx]; |
| 552 | if (keepcap) |
| 553 | { |
| 554 | /* For "keepcap" tree the case is always right. */ |
| 555 | valid = TRUE; |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | /* Check that the word is in the required case. */ |
| 560 | if (mip->mi_cend != mip->mi_word + wlen) |
| 561 | { |
| 562 | /* mi_capflags was set for a different word |
| 563 | * length, need to do it again. */ |
| 564 | mip->mi_cend = mip->mi_word + wlen; |
| 565 | mip->mi_capflags = captype(mip->mi_word, |
| 566 | mip->mi_cend); |
| 567 | } |
| 568 | |
| 569 | valid = (mip->mi_capflags == WF_ALLCAP |
| 570 | || ((flags & WF_ALLCAP) == 0 |
| 571 | && ((flags & WF_ONECAP) == 0 |
| 572 | || mip->mi_capflags == WF_ONECAP))); |
| 573 | } |
| 574 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 575 | if (valid) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 576 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 577 | if (flags & WF_BANNED) |
| 578 | res = SP_BANNED; |
| 579 | else if (flags & WF_REGION) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 580 | { |
| 581 | /* Check region. */ |
| 582 | if ((mip->mi_lp->lp_region & (flags >> 8)) != 0) |
| 583 | res = SP_OK; |
| 584 | else |
| 585 | res = SP_LOCAL; |
| 586 | } |
| 587 | else if (flags & WF_RARE) |
| 588 | res = SP_RARE; |
| 589 | else |
| 590 | res = SP_OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 591 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 592 | /* Always use the longest match and the best result. */ |
| 593 | if (mip->mi_result > res) |
| 594 | { |
| 595 | mip->mi_result = res; |
| 596 | mip->mi_end = mip->mi_word + wlen; |
| 597 | } |
| 598 | else if (mip->mi_result == res |
| 599 | && mip->mi_end < mip->mi_word + wlen) |
| 600 | mip->mi_end = mip->mi_word + wlen; |
| 601 | |
| 602 | if (res == SP_OK) |
| 603 | break; |
| 604 | } |
| 605 | else |
| 606 | res = SP_BAD; |
| 607 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 608 | ++arridx; |
| 609 | } |
| 610 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 611 | if (res == SP_OK) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 612 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 613 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 616 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 617 | /* |
| 618 | * Move to next spell error. |
| 619 | * Return OK if found, FAIL otherwise. |
| 620 | */ |
| 621 | int |
| 622 | spell_move_to(dir, allwords) |
| 623 | int dir; /* FORWARD or BACKWARD */ |
| 624 | int allwords; /* TRUE for "[s" and "]s" */ |
| 625 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 626 | linenr_T lnum; |
| 627 | pos_T found_pos; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 628 | char_u *line; |
| 629 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 630 | int attr = 0; |
| 631 | int len; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 632 | int has_syntax = syntax_present(curbuf); |
| 633 | int col; |
| 634 | int can_spell; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 635 | |
| 636 | if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL) |
| 637 | { |
| 638 | EMSG(_("E756: Spell checking not enabled")); |
| 639 | return FAIL; |
| 640 | } |
| 641 | |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 642 | /* |
| 643 | * Start looking for bad word at the start of the line, because we can't |
| 644 | * start halfway a word, we don't know where it starts or ends. |
| 645 | * |
| 646 | * When searching backwards, we continue in the line to find the last |
| 647 | * bad word (in the cursor line: before the cursor). |
| 648 | */ |
| 649 | lnum = curwin->w_cursor.lnum; |
| 650 | found_pos.lnum = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 651 | |
| 652 | while (!got_int) |
| 653 | { |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 654 | line = ml_get(lnum); |
| 655 | p = line; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 656 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 657 | while (*p != NUL) |
| 658 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 659 | /* When searching backward don't search after the cursor. */ |
| 660 | if (dir == BACKWARD |
| 661 | && lnum == curwin->w_cursor.lnum |
| 662 | && (colnr_T)(p - line) >= curwin->w_cursor.col) |
| 663 | break; |
| 664 | |
| 665 | /* start of word */ |
| 666 | len = spell_check(curwin, p, &attr); |
| 667 | |
| 668 | if (attr != 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 669 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 670 | /* We found a bad word. Check the attribute. */ |
| 671 | /* TODO: check for syntax @Spell cluster. */ |
| 672 | if (allwords || attr == highlight_attr[HLF_SPB]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 673 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 674 | /* When searching forward only accept a bad word after |
| 675 | * the cursor. */ |
| 676 | if (dir == BACKWARD |
| 677 | || lnum > curwin->w_cursor.lnum |
| 678 | || (lnum == curwin->w_cursor.lnum |
| 679 | && (colnr_T)(p - line) |
| 680 | > curwin->w_cursor.col)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 681 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 682 | if (has_syntax) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 683 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 684 | col = p - line; |
| 685 | (void)syn_get_id(lnum, (colnr_T)col, |
| 686 | FALSE, &can_spell); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 687 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 688 | /* have to get the line again, a multi-line |
| 689 | * regexp may make it invalid */ |
| 690 | line = ml_get(lnum); |
| 691 | p = line + col; |
| 692 | } |
| 693 | else |
| 694 | can_spell = TRUE; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 695 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 696 | if (can_spell) |
| 697 | { |
| 698 | found_pos.lnum = lnum; |
| 699 | found_pos.col = p - line; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 700 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 701 | found_pos.coladd = 0; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 702 | #endif |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 703 | if (dir == FORWARD) |
| 704 | { |
| 705 | /* No need to search further. */ |
| 706 | curwin->w_cursor = found_pos; |
| 707 | return OK; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 710 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 711 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 712 | attr = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 715 | /* advance to character after the word */ |
| 716 | p += len; |
| 717 | if (*p == NUL) |
| 718 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* Advance to next line. */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 722 | if (dir == BACKWARD) |
| 723 | { |
| 724 | if (found_pos.lnum != 0) |
| 725 | { |
| 726 | /* Use the last match in the line. */ |
| 727 | curwin->w_cursor = found_pos; |
| 728 | return OK; |
| 729 | } |
| 730 | if (lnum == 1) |
| 731 | return FAIL; |
| 732 | --lnum; |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | if (lnum == curbuf->b_ml.ml_line_count) |
| 737 | return FAIL; |
| 738 | ++lnum; |
| 739 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 740 | |
| 741 | line_breakcheck(); |
| 742 | } |
| 743 | |
| 744 | return FAIL; /* interrupted */ |
| 745 | } |
| 746 | |
| 747 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 748 | * Load word list(s) for "lang" from Vim spell file(s). |
| 749 | * "lang" must be the language without the region: "en". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 750 | */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 751 | static void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 752 | spell_load_lang(lang) |
| 753 | char_u *lang; |
| 754 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 755 | char_u fname_enc[80]; |
| 756 | char_u *p; |
| 757 | int r; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 758 | char_u langcp[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 759 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 760 | /* Copy the language name to pass it to spell_load_file() as a cookie. |
| 761 | * It's truncated when an error is detected. */ |
| 762 | STRCPY(langcp, lang); |
| 763 | |
| 764 | /* Find all spell files for "lang" in 'runtimepath' and load them. |
| 765 | * Use 'encoding', except that we use "latin1" for "latin9". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 766 | #ifdef FEAT_MBYTE |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 767 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 768 | p = p_enc; |
| 769 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 770 | #endif |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 771 | p = (char_u *)"latin1"; |
| 772 | vim_snprintf((char *)fname_enc, sizeof(fname_enc), |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 773 | "spell/%s.%s.spl", lang, p); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 774 | r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 775 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 776 | if (r == FAIL && *langcp != NUL) |
| 777 | { |
| 778 | /* Try loading the ASCII version. */ |
| 779 | vim_snprintf((char *)fname_enc, sizeof(fname_enc), |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 780 | "spell/%s.ascii.spl", lang); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 781 | r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 784 | if (r == FAIL) |
| 785 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 786 | fname_enc + 6); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Allocate a new slang_T. |
| 791 | * Caller must fill "sl_next". |
| 792 | */ |
| 793 | static slang_T * |
| 794 | slang_alloc(lang) |
| 795 | char_u *lang; |
| 796 | { |
| 797 | slang_T *lp; |
| 798 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 799 | lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 800 | if (lp != NULL) |
| 801 | { |
| 802 | lp->sl_name = vim_strsave(lang); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 803 | ga_init2(&lp->sl_rep, sizeof(repentry_T), 4); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 804 | } |
| 805 | return lp; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Free the contents of an slang_T and the structure itself. |
| 810 | */ |
| 811 | static void |
| 812 | slang_free(lp) |
| 813 | slang_T *lp; |
| 814 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 815 | vim_free(lp->sl_name); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 816 | vim_free(lp->sl_fbyts); |
| 817 | vim_free(lp->sl_kbyts); |
| 818 | vim_free(lp->sl_fidxs); |
| 819 | vim_free(lp->sl_kidxs); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 820 | ga_clear(&lp->sl_rep); |
| 821 | vim_free(lp->sl_try); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 822 | vim_free(lp); |
| 823 | } |
| 824 | |
| 825 | /* |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 826 | * Load one spell file and store the info into a slang_T. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 827 | * Invoked through do_in_runtimepath(). |
| 828 | */ |
| 829 | static void |
| 830 | spell_load_file(fname, cookie) |
| 831 | char_u *fname; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 832 | void *cookie; /* points to the language name */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 833 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 834 | char_u *lang = cookie; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 835 | FILE *fd; |
| 836 | char_u buf[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 837 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 838 | int i; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 839 | int len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 840 | int round; |
| 841 | char_u *save_sourcing_name = sourcing_name; |
| 842 | linenr_T save_sourcing_lnum = sourcing_lnum; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 843 | int cnt, ccnt; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 844 | char_u *fol; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 845 | slang_T *lp = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 846 | |
| 847 | fd = fopen((char *)fname, "r"); |
| 848 | if (fd == NULL) |
| 849 | { |
| 850 | EMSG2(_(e_notopen), fname); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 851 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 854 | lp = slang_alloc(lang); |
| 855 | if (lp == NULL) |
| 856 | goto endFAIL; |
| 857 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 858 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 859 | sourcing_name = fname; |
| 860 | sourcing_lnum = 0; |
| 861 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 862 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
| 863 | * <charflagslen> <charflags> <fcharslen> <fchars> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 864 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 865 | buf[i] = getc(fd); /* <fileID> */ |
| 866 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 867 | { |
| 868 | EMSG(_("E757: Wrong file ID in spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 869 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | cnt = getc(fd); /* <regioncnt> */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 873 | if (cnt < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 874 | { |
| 875 | truncerr: |
| 876 | EMSG(_("E758: Truncated spell file")); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 877 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 878 | } |
| 879 | if (cnt > 8) |
| 880 | { |
| 881 | formerr: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 882 | EMSG(_(e_format)); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 883 | goto endFAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 884 | } |
| 885 | for (i = 0; i < cnt; ++i) |
| 886 | { |
| 887 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 888 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 889 | } |
| 890 | lp->sl_regions[cnt * 2] = NUL; |
| 891 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 892 | cnt = getc(fd); /* <charflagslen> */ |
| 893 | if (cnt > 0) |
| 894 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 895 | p = alloc((unsigned)cnt); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 896 | if (p == NULL) |
| 897 | goto endFAIL; |
| 898 | for (i = 0; i < cnt; ++i) |
| 899 | p[i] = getc(fd); /* <charflags> */ |
| 900 | |
| 901 | ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */ |
| 902 | if (ccnt <= 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 903 | { |
| 904 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 905 | goto formerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 906 | } |
| 907 | fol = alloc((unsigned)ccnt + 1); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 908 | if (fol == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 909 | { |
| 910 | vim_free(p); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 911 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 912 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 913 | for (i = 0; i < ccnt; ++i) |
| 914 | fol[i] = getc(fd); /* <fchars> */ |
| 915 | fol[i] = NUL; |
| 916 | |
| 917 | /* Set the word-char flags and fill spell_isupper() table. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 918 | i = set_spell_charflags(p, cnt, fol); |
| 919 | vim_free(p); |
| 920 | vim_free(fol); |
| 921 | if (i == FAIL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 922 | goto formerr; |
| 923 | } |
| 924 | else |
| 925 | { |
| 926 | /* When <charflagslen> is zero then <fcharlen> must also be zero. */ |
| 927 | cnt = (getc(fd) << 8) + getc(fd); |
| 928 | if (cnt != 0) |
| 929 | goto formerr; |
| 930 | } |
| 931 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 932 | /* <SUGGEST> : <suggestlen> <more> ... */ |
| 933 | /* TODO, just skip this for now */ |
| 934 | i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 935 | while (i-- > 0) |
| 936 | if (getc(fd) == EOF) /* <suggestlen> */ |
| 937 | goto truncerr; |
| 938 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 939 | /* round 1: <LWORDTREE> |
| 940 | * round 2: <KWORDTREE> */ |
| 941 | for (round = 1; round <= 2; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 942 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 943 | /* The tree size was computed when writing the file, so that we can |
| 944 | * allocate it as one long block. <nodecount> */ |
| 945 | len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 946 | if (len < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 947 | goto truncerr; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 948 | if (len > 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 949 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 950 | /* Allocate the byte array. */ |
| 951 | p = lalloc((long_u)len, TRUE); |
| 952 | if (p == NULL) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 953 | goto endFAIL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 954 | if (round == 1) |
| 955 | lp->sl_fbyts = p; |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 956 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 957 | lp->sl_kbyts = p; |
| 958 | |
| 959 | /* Allocate the index array. */ |
| 960 | p = lalloc_clear((long_u)(len * sizeof(int)), TRUE); |
| 961 | if (p == NULL) |
| 962 | goto endFAIL; |
| 963 | if (round == 1) |
| 964 | lp->sl_fidxs = (int *)p; |
| 965 | else |
| 966 | lp->sl_kidxs = (int *)p; |
| 967 | |
| 968 | |
| 969 | /* Read the tree and store it in the array. */ |
| 970 | i = read_tree(fd, |
| 971 | round == 1 ? lp->sl_fbyts : lp->sl_kbyts, |
| 972 | round == 1 ? lp->sl_fidxs : lp->sl_kidxs, |
| 973 | len, 0); |
| 974 | if (i == -1) |
| 975 | goto truncerr; |
| 976 | if (i < 0) |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 977 | goto formerr; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 978 | } |
| 979 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 980 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 981 | lp->sl_next = first_lang; |
| 982 | first_lang = lp; |
| 983 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 984 | goto endOK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 985 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 986 | endFAIL: |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 987 | /* truncating the name signals the error to spell_load_lang() */ |
| 988 | *lang = NUL; |
| 989 | if (lp != NULL) |
| 990 | slang_free(lp); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 991 | |
| 992 | endOK: |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 993 | if (fd != NULL) |
| 994 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 995 | sourcing_name = save_sourcing_name; |
| 996 | sourcing_lnum = save_sourcing_lnum; |
| 997 | } |
| 998 | |
| 999 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1000 | * Read one row of siblings from the spell file and store it in the byte array |
| 1001 | * "byts" and index array "idxs". Recursively read the children. |
| 1002 | * |
| 1003 | * NOTE: The code here must match put_tree(). |
| 1004 | * |
| 1005 | * Returns the index follosing the siblings. |
| 1006 | * Returns -1 if the file is shorter than expected. |
| 1007 | * Returns -2 if there is a format error. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1008 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1009 | static int |
| 1010 | read_tree(fd, byts, idxs, maxidx, startidx) |
| 1011 | FILE *fd; |
| 1012 | char_u *byts; |
| 1013 | int *idxs; |
| 1014 | int maxidx; /* size of arrays */ |
| 1015 | int startidx; /* current index in "byts" and "idxs" */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1016 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1017 | int len; |
| 1018 | int i; |
| 1019 | int n; |
| 1020 | int idx = startidx; |
| 1021 | int c; |
| 1022 | #define SHARED_MASK 0x8000000 |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1023 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1024 | len = getc(fd); /* <siblingcount> */ |
| 1025 | if (len <= 0) |
| 1026 | return -1; |
| 1027 | |
| 1028 | if (startidx + len >= maxidx) |
| 1029 | return -2; |
| 1030 | byts[idx++] = len; |
| 1031 | |
| 1032 | /* Read the byte values, flag/region bytes and shared indexes. */ |
| 1033 | for (i = 1; i <= len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1034 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1035 | c = getc(fd); /* <byte> */ |
| 1036 | if (c < 0) |
| 1037 | return -1; |
| 1038 | if (c <= BY_SPECIAL) |
| 1039 | { |
| 1040 | if (c == BY_NOFLAGS) |
| 1041 | { |
| 1042 | /* No flags, all regions. */ |
| 1043 | idxs[idx] = 0; |
| 1044 | c = 0; |
| 1045 | } |
| 1046 | else if (c == BY_FLAGS) |
| 1047 | { |
| 1048 | /* Read flags and option region. */ |
| 1049 | c = getc(fd); /* <flags> */ |
| 1050 | if (c & WF_REGION) |
| 1051 | c = (getc(fd) << 8) + c; /* <region> */ |
| 1052 | idxs[idx] = c; |
| 1053 | c = 0; |
| 1054 | } |
| 1055 | else /* c == BY_INDEX */ |
| 1056 | { |
| 1057 | /* <nodeidx> */ |
| 1058 | n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 1059 | if (n < 0 || n >= maxidx) |
| 1060 | return -2; |
| 1061 | idxs[idx] = n + SHARED_MASK; |
| 1062 | c = getc(fd); /* <xbyte> */ |
| 1063 | } |
| 1064 | } |
| 1065 | byts[idx++] = c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1068 | /* Recursively read the children for non-shared siblings. |
| 1069 | * Skip the end-of-word ones (zero byte value) and the shared ones (and |
| 1070 | * remove SHARED_MASK) */ |
| 1071 | for (i = 1; i <= len; ++i) |
| 1072 | if (byts[startidx + i] != 0) |
| 1073 | { |
| 1074 | if (idxs[startidx + i] & SHARED_MASK) |
| 1075 | idxs[startidx + i] &= ~SHARED_MASK; |
| 1076 | else |
| 1077 | { |
| 1078 | idxs[startidx + i] = idx; |
| 1079 | idx = read_tree(fd, byts, idxs, maxidx, idx); |
| 1080 | if (idx < 0) |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1084 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1085 | return idx; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Parse 'spelllang' and set buf->b_langp accordingly. |
| 1090 | * Returns an error message or NULL. |
| 1091 | */ |
| 1092 | char_u * |
| 1093 | did_set_spelllang(buf) |
| 1094 | buf_T *buf; |
| 1095 | { |
| 1096 | garray_T ga; |
| 1097 | char_u *lang; |
| 1098 | char_u *e; |
| 1099 | char_u *region; |
| 1100 | int region_mask; |
| 1101 | slang_T *lp; |
| 1102 | int c; |
| 1103 | char_u lbuf[MAXWLEN + 1]; |
| 1104 | |
| 1105 | ga_init2(&ga, sizeof(langp_T), 2); |
| 1106 | |
| 1107 | /* loop over comma separated languages. */ |
| 1108 | for (lang = buf->b_p_spl; *lang != NUL; lang = e) |
| 1109 | { |
| 1110 | e = vim_strchr(lang, ','); |
| 1111 | if (e == NULL) |
| 1112 | e = lang + STRLEN(lang); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1113 | region = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1114 | if (e > lang + 2) |
| 1115 | { |
| 1116 | if (e - lang >= MAXWLEN) |
| 1117 | { |
| 1118 | ga_clear(&ga); |
| 1119 | return e_invarg; |
| 1120 | } |
| 1121 | if (lang[2] == '_') |
| 1122 | region = lang + 3; |
| 1123 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1124 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1125 | /* Check if we loaded this language before. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1126 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 1127 | if (STRNICMP(lp->sl_name, lang, 2) == 0) |
| 1128 | break; |
| 1129 | |
| 1130 | if (lp == NULL) |
| 1131 | { |
| 1132 | /* Not found, load the language. */ |
| 1133 | STRNCPY(lbuf, lang, e - lang); |
| 1134 | lbuf[e - lang] = NUL; |
| 1135 | if (region != NULL) |
| 1136 | mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1137 | spell_load_lang(lbuf); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1140 | /* |
| 1141 | * Loop over the languages, there can be several files for each. |
| 1142 | */ |
| 1143 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 1144 | if (STRNICMP(lp->sl_name, lang, 2) == 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1145 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1146 | if (region == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1147 | region_mask = REGION_ALL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1148 | else |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1149 | { |
| 1150 | /* find region in sl_regions */ |
| 1151 | c = find_region(lp->sl_regions, region); |
| 1152 | if (c == REGION_ALL) |
| 1153 | { |
| 1154 | c = *e; |
| 1155 | *e = NUL; |
| 1156 | smsg((char_u *)_("Warning: region %s not supported"), |
| 1157 | lang); |
| 1158 | *e = c; |
| 1159 | region_mask = REGION_ALL; |
| 1160 | } |
| 1161 | else |
| 1162 | region_mask = 1 << c; |
| 1163 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1164 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1165 | if (ga_grow(&ga, 1) == FAIL) |
| 1166 | { |
| 1167 | ga_clear(&ga); |
| 1168 | return e_outofmem; |
| 1169 | } |
| 1170 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 1171 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 1172 | ++ga.ga_len; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1173 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1174 | |
| 1175 | if (*e == ',') |
| 1176 | ++e; |
| 1177 | } |
| 1178 | |
| 1179 | /* Add a NULL entry to mark the end of the list. */ |
| 1180 | if (ga_grow(&ga, 1) == FAIL) |
| 1181 | { |
| 1182 | ga_clear(&ga); |
| 1183 | return e_outofmem; |
| 1184 | } |
| 1185 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 1186 | ++ga.ga_len; |
| 1187 | |
| 1188 | /* Everything is fine, store the new b_langp value. */ |
| 1189 | ga_clear(&buf->b_langp); |
| 1190 | buf->b_langp = ga; |
| 1191 | |
| 1192 | return NULL; |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 1197 | * Each region is simply stored as the two characters of it's name. |
| 1198 | * Returns the index if found, REGION_ALL if not found. |
| 1199 | */ |
| 1200 | static int |
| 1201 | find_region(rp, region) |
| 1202 | char_u *rp; |
| 1203 | char_u *region; |
| 1204 | { |
| 1205 | int i; |
| 1206 | |
| 1207 | for (i = 0; ; i += 2) |
| 1208 | { |
| 1209 | if (rp[i] == NUL) |
| 1210 | return REGION_ALL; |
| 1211 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 1212 | break; |
| 1213 | } |
| 1214 | return i / 2; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Return type of word: |
| 1219 | * w word 0 |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1220 | * Word WF_ONECAP |
| 1221 | * W WORD WF_ALLCAP |
| 1222 | * WoRd wOrd WF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1223 | */ |
| 1224 | static int |
| 1225 | captype(word, end) |
| 1226 | char_u *word; |
| 1227 | char_u *end; |
| 1228 | { |
| 1229 | char_u *p; |
| 1230 | int c; |
| 1231 | int firstcap; |
| 1232 | int allcap; |
| 1233 | int past_second = FALSE; /* past second word char */ |
| 1234 | |
| 1235 | /* find first letter */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1236 | for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1237 | if (p >= end) |
| 1238 | return 0; /* only non-word characters, illegal word */ |
| 1239 | #ifdef FEAT_MBYTE |
| 1240 | c = mb_ptr2char_adv(&p); |
| 1241 | #else |
| 1242 | c = *p++; |
| 1243 | #endif |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1244 | firstcap = allcap = spell_isupper(c); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1245 | |
| 1246 | /* |
| 1247 | * Need to check all letters to find a word with mixed upper/lower. |
| 1248 | * But a word with an upper char only at start is a ONECAP. |
| 1249 | */ |
| 1250 | for ( ; p < end; mb_ptr_adv(p)) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1251 | if (SPELL_ISWORDP(p)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1252 | { |
| 1253 | #ifdef FEAT_MBYTE |
| 1254 | c = mb_ptr2char(p); |
| 1255 | #else |
| 1256 | c = *p; |
| 1257 | #endif |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1258 | if (!spell_isupper(c)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1259 | { |
| 1260 | /* UUl -> KEEPCAP */ |
| 1261 | if (past_second && allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1262 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1263 | allcap = FALSE; |
| 1264 | } |
| 1265 | else if (!allcap) |
| 1266 | /* UlU -> KEEPCAP */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1267 | return WF_KEEPCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1268 | past_second = TRUE; |
| 1269 | } |
| 1270 | |
| 1271 | if (allcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1272 | return WF_ALLCAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1273 | if (firstcap) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1274 | return WF_ONECAP; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 1279 | /* |
| 1280 | * Clear all spelling tables and reload them. |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1281 | * Used after 'encoding' is set and when ":mkspell" was used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1282 | */ |
| 1283 | void |
| 1284 | spell_reload() |
| 1285 | { |
| 1286 | buf_T *buf; |
| 1287 | slang_T *lp; |
| 1288 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1289 | /* Initialize the table for SPELL_ISWORDP(). */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1290 | init_spell_chartab(); |
| 1291 | |
| 1292 | /* Unload all allocated memory. */ |
| 1293 | while (first_lang != NULL) |
| 1294 | { |
| 1295 | lp = first_lang; |
| 1296 | first_lang = lp->sl_next; |
| 1297 | slang_free(lp); |
| 1298 | } |
| 1299 | |
| 1300 | /* Go through all buffers and handle 'spelllang'. */ |
| 1301 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 1302 | { |
| 1303 | ga_clear(&buf->b_langp); |
| 1304 | if (*buf->b_p_spl != NUL) |
| 1305 | did_set_spelllang(buf); |
| 1306 | } |
| 1307 | } |
| 1308 | # endif |
| 1309 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1310 | |
| 1311 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 1312 | /* |
| 1313 | * Functions for ":mkspell". |
| 1314 | * Only possible with the multi-byte feature. |
| 1315 | */ |
| 1316 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1317 | #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] | 1318 | and .dic file. */ |
| 1319 | /* |
| 1320 | * Main structure to store the contents of a ".aff" file. |
| 1321 | */ |
| 1322 | typedef struct afffile_S |
| 1323 | { |
| 1324 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
| 1325 | char_u *af_try; /* "TRY" line in "af_enc" encoding */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1326 | int af_rar; /* ID for rare word */ |
| 1327 | int af_huh; /* ID for keep-case word */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1328 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 1329 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
| 1330 | garray_T af_rep; /* list of repentry_T entries from REP lines */ |
| 1331 | } afffile_T; |
| 1332 | |
| 1333 | typedef struct affentry_S affentry_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1334 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 1335 | struct affentry_S |
| 1336 | { |
| 1337 | affentry_T *ae_next; /* next affix with same name/number */ |
| 1338 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 1339 | char_u *ae_add; /* text to add to basic word (can be NULL) */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1340 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 1341 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1342 | }; |
| 1343 | |
| 1344 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 1345 | typedef struct affheader_S |
| 1346 | { |
| 1347 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
| 1348 | int ah_combine; /* suffix may combine with prefix */ |
| 1349 | affentry_T *ah_first; /* first affix entry */ |
| 1350 | } affheader_T; |
| 1351 | |
| 1352 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 1353 | |
| 1354 | /* |
| 1355 | * Structure that is used to store the items in the word tree. This avoids |
| 1356 | * the need to keep track of each allocated thing, it's freed all at once |
| 1357 | * after ":mkspell" is done. |
| 1358 | */ |
| 1359 | #define SBLOCKSIZE 16000 /* size of sb_data */ |
| 1360 | typedef struct sblock_S sblock_T; |
| 1361 | struct sblock_S |
| 1362 | { |
| 1363 | sblock_T *sb_next; /* next block in list */ |
| 1364 | int sb_used; /* nr of bytes already in use */ |
| 1365 | char_u sb_data[1]; /* data, actually longer */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1366 | }; |
| 1367 | |
| 1368 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1369 | * A node in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1370 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1371 | typedef struct wordnode_S wordnode_T; |
| 1372 | struct wordnode_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1373 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1374 | char_u wn_hashkey[6]; /* room for the hash key */ |
| 1375 | wordnode_T *wn_next; /* next node with same hash key */ |
| 1376 | wordnode_T *wn_child; /* child (next byte in word) */ |
| 1377 | wordnode_T *wn_sibling; /* next sibling (alternate byte in word, |
| 1378 | always sorted) */ |
| 1379 | wordnode_T *wn_wnode; /* parent node that will write this node */ |
| 1380 | int wn_index; /* index in written nodes (valid after first |
| 1381 | round) */ |
| 1382 | char_u wn_byte; /* Byte for this node. NUL for word end */ |
| 1383 | char_u wn_flags; /* when wn_byte is NUL: WF_ flags */ |
| 1384 | char_u wn_region; /* when wn_byte is NUL: region mask */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1385 | }; |
| 1386 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1387 | #define HI2WN(hi) (wordnode_T *)((hi)->hi_key) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1388 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * Info used while reading the spell files. |
| 1391 | */ |
| 1392 | typedef struct spellinfo_S |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1393 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1394 | wordnode_T *si_foldroot; /* tree with case-folded words */ |
| 1395 | wordnode_T *si_keeproot; /* tree with keep-case words */ |
| 1396 | sblock_T *si_blocks; /* memory blocks used */ |
| 1397 | int si_ascii; /* handling only ASCII words */ |
| 1398 | int si_region; /* region mask */ |
| 1399 | vimconv_T si_conv; /* for conversion to 'encoding' */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 1400 | int si_memtot; /* runtime memory used */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1401 | } spellinfo_T; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1402 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1403 | static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1404 | static int has_non_ascii __ARGS((char_u *s)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1405 | static void spell_free_aff __ARGS((afffile_T *aff)); |
| 1406 | static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1407 | static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, hashtab_T *ht, hashtab_T *xht, int comb, int flags)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1408 | static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin)); |
| 1409 | static void *getroom __ARGS((sblock_T **blp, size_t len)); |
| 1410 | static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s)); |
| 1411 | static void free_blocks __ARGS((sblock_T *bl)); |
| 1412 | static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp)); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1413 | static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags)); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1414 | static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp)); |
| 1415 | static void wordtree_compress __ARGS((wordnode_T *root)); |
| 1416 | static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot)); |
| 1417 | static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2)); |
| 1418 | static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin, int regcount, char_u *regchars)); |
| 1419 | static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1420 | |
| 1421 | /* |
| 1422 | * Read an affix ".aff" file. |
| 1423 | * Returns an afffile_T, NULL for failure. |
| 1424 | */ |
| 1425 | static afffile_T * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1426 | spell_read_aff(fname, spin) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1427 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1428 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1429 | { |
| 1430 | FILE *fd; |
| 1431 | afffile_T *aff; |
| 1432 | char_u rline[MAXLINELEN]; |
| 1433 | char_u *line; |
| 1434 | char_u *pc = NULL; |
| 1435 | char_u *(items[6]); |
| 1436 | int itemcnt; |
| 1437 | char_u *p; |
| 1438 | int lnum = 0; |
| 1439 | affheader_T *cur_aff = NULL; |
| 1440 | int aff_todo = 0; |
| 1441 | hashtab_T *tp; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1442 | char_u *low = NULL; |
| 1443 | char_u *fol = NULL; |
| 1444 | char_u *upp = NULL; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1445 | static char *e_affname = N_("Affix name too long in %s line %d: %s"); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1446 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1447 | /* |
| 1448 | * Open the file. |
| 1449 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1450 | fd = fopen((char *)fname, "r"); |
| 1451 | if (fd == NULL) |
| 1452 | { |
| 1453 | EMSG2(_(e_notopen), fname); |
| 1454 | return NULL; |
| 1455 | } |
| 1456 | |
| 1457 | smsg((char_u *)_("Reading affix file %s..."), fname); |
| 1458 | out_flush(); |
| 1459 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1460 | /* |
| 1461 | * Allocate and init the afffile_T structure. |
| 1462 | */ |
| 1463 | aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1464 | if (aff == NULL) |
| 1465 | return NULL; |
| 1466 | hash_init(&aff->af_pref); |
| 1467 | hash_init(&aff->af_suff); |
| 1468 | ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20); |
| 1469 | |
| 1470 | /* |
| 1471 | * Read all the lines in the file one by one. |
| 1472 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1473 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1474 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1475 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1476 | ++lnum; |
| 1477 | |
| 1478 | /* Skip comment lines. */ |
| 1479 | if (*rline == '#') |
| 1480 | continue; |
| 1481 | |
| 1482 | /* Convert from "SET" to 'encoding' when needed. */ |
| 1483 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1484 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1485 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1486 | pc = string_convert(&spin->si_conv, rline, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1487 | if (pc == NULL) |
| 1488 | { |
| 1489 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 1490 | fname, lnum, rline); |
| 1491 | continue; |
| 1492 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1493 | line = pc; |
| 1494 | } |
| 1495 | else |
| 1496 | { |
| 1497 | pc = NULL; |
| 1498 | line = rline; |
| 1499 | } |
| 1500 | |
| 1501 | /* Split the line up in white separated items. Put a NUL after each |
| 1502 | * item. */ |
| 1503 | itemcnt = 0; |
| 1504 | for (p = line; ; ) |
| 1505 | { |
| 1506 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 1507 | ++p; |
| 1508 | if (*p == NUL) |
| 1509 | break; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1510 | if (itemcnt == 6) /* too many items */ |
| 1511 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1512 | items[itemcnt++] = p; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1513 | while (*p > ' ') /* skip until white space or CR/NL */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1514 | ++p; |
| 1515 | if (*p == NUL) |
| 1516 | break; |
| 1517 | *p++ = NUL; |
| 1518 | } |
| 1519 | |
| 1520 | /* Handle non-empty lines. */ |
| 1521 | if (itemcnt > 0) |
| 1522 | { |
| 1523 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 1524 | && aff->af_enc == NULL) |
| 1525 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1526 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 1527 | aff->af_enc = enc_canonize(items[1]); |
| 1528 | if (aff->af_enc != NULL && !spin->si_ascii |
| 1529 | && convert_setup(&spin->si_conv, aff->af_enc, |
| 1530 | p_enc) == FAIL) |
| 1531 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 1532 | fname, aff->af_enc, p_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1533 | } |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 1534 | else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1) |
| 1535 | { |
| 1536 | /* ignored */ |
| 1537 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1538 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2 |
| 1539 | && aff->af_try == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1540 | { |
| 1541 | aff->af_try = getroom_save(&spin->si_blocks, items[1]); |
| 1542 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1543 | else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2 |
| 1544 | && aff->af_rar == 0) |
| 1545 | { |
| 1546 | aff->af_rar = items[1][0]; |
| 1547 | if (items[1][1] != NUL) |
| 1548 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 1549 | } |
| 1550 | else if (STRCMP(items[0], "HUH") == 0 && itemcnt == 2 |
| 1551 | && aff->af_huh == 0) |
| 1552 | { |
| 1553 | aff->af_huh = items[1][0]; |
| 1554 | if (items[1][1] != NUL) |
| 1555 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
| 1556 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1557 | else if ((STRCMP(items[0], "PFX") == 0 |
| 1558 | || STRCMP(items[0], "SFX") == 0) |
| 1559 | && aff_todo == 0 |
| 1560 | && itemcnt == 4) |
| 1561 | { |
| 1562 | /* New affix letter. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1563 | cur_aff = (affheader_T *)getroom(&spin->si_blocks, |
| 1564 | sizeof(affheader_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1565 | if (cur_aff == NULL) |
| 1566 | break; |
| 1567 | cur_aff->ah_key[0] = *items[1]; |
| 1568 | cur_aff->ah_key[1] = NUL; |
| 1569 | if (items[1][1] != NUL) |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1570 | smsg((char_u *)_(e_affname), fname, lnum, items[1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1571 | if (*items[2] == 'Y') |
| 1572 | cur_aff->ah_combine = TRUE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1573 | else if (*items[2] != 'N') |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1574 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 1575 | fname, lnum, items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1576 | if (*items[0] == 'P') |
| 1577 | tp = &aff->af_pref; |
| 1578 | else |
| 1579 | tp = &aff->af_suff; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1580 | aff_todo = atoi((char *)items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1581 | if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key))) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1582 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1583 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 1584 | fname, lnum, items[1]); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1585 | aff_todo = 0; |
| 1586 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1587 | else |
| 1588 | hash_add(tp, cur_aff->ah_key); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1589 | } |
| 1590 | else if ((STRCMP(items[0], "PFX") == 0 |
| 1591 | || STRCMP(items[0], "SFX") == 0) |
| 1592 | && aff_todo > 0 |
| 1593 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
| 1594 | && itemcnt == 5) |
| 1595 | { |
| 1596 | affentry_T *aff_entry; |
| 1597 | |
| 1598 | /* New item for an affix letter. */ |
| 1599 | --aff_todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1600 | aff_entry = (affentry_T *)getroom(&spin->si_blocks, |
| 1601 | sizeof(affentry_T)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1602 | if (aff_entry == NULL) |
| 1603 | break; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1604 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1605 | if (STRCMP(items[2], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1606 | aff_entry->ae_chop = getroom_save(&spin->si_blocks, |
| 1607 | items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1608 | if (STRCMP(items[3], "0") != 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1609 | aff_entry->ae_add = getroom_save(&spin->si_blocks, |
| 1610 | items[3]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1611 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1612 | /* Don't use an affix entry with non-ASCII characters when |
| 1613 | * "spin->si_ascii" is TRUE. */ |
| 1614 | if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1615 | || has_non_ascii(aff_entry->ae_add))) |
| 1616 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1617 | aff_entry->ae_next = cur_aff->ah_first; |
| 1618 | cur_aff->ah_first = aff_entry; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1619 | |
| 1620 | if (STRCMP(items[4], ".") != 0) |
| 1621 | { |
| 1622 | char_u buf[MAXLINELEN]; |
| 1623 | |
| 1624 | aff_entry->ae_cond = getroom_save(&spin->si_blocks, |
| 1625 | items[4]); |
| 1626 | if (*items[0] == 'P') |
| 1627 | sprintf((char *)buf, "^%s", items[4]); |
| 1628 | else |
| 1629 | sprintf((char *)buf, "%s$", items[4]); |
| 1630 | aff_entry->ae_prog = vim_regcomp(buf, |
| 1631 | RE_MAGIC + RE_STRING); |
| 1632 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1633 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1634 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1635 | else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2) |
| 1636 | { |
| 1637 | if (fol != NULL) |
| 1638 | smsg((char_u *)_("Duplicate FOL in %s line %d"), |
| 1639 | fname, lnum); |
| 1640 | else |
| 1641 | fol = vim_strsave(items[1]); |
| 1642 | } |
| 1643 | else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2) |
| 1644 | { |
| 1645 | if (low != NULL) |
| 1646 | smsg((char_u *)_("Duplicate LOW in %s line %d"), |
| 1647 | fname, lnum); |
| 1648 | else |
| 1649 | low = vim_strsave(items[1]); |
| 1650 | } |
| 1651 | else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2) |
| 1652 | { |
| 1653 | if (upp != NULL) |
| 1654 | smsg((char_u *)_("Duplicate UPP in %s line %d"), |
| 1655 | fname, lnum); |
| 1656 | else |
| 1657 | upp = vim_strsave(items[1]); |
| 1658 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1659 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
| 1660 | /* Ignore REP count */; |
| 1661 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 1662 | { |
| 1663 | repentry_T *rp; |
| 1664 | |
| 1665 | /* REP item */ |
| 1666 | if (ga_grow(&aff->af_rep, 1) == FAIL) |
| 1667 | break; |
| 1668 | rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1669 | rp->re_from = getroom_save(&spin->si_blocks, items[1]); |
| 1670 | rp->re_to = getroom_save(&spin->si_blocks, items[2]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1671 | ++aff->af_rep.ga_len; |
| 1672 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1673 | else |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1674 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 1675 | fname, lnum, items[0]); |
| 1676 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1679 | if (fol != NULL || low != NULL || upp != NULL) |
| 1680 | { |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 1681 | /* Don't write a word table for an ASCII file, so that we don't check |
| 1682 | * for conflicts with a word table that matches 'encoding'. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1683 | if (!spin->si_ascii) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 1684 | { |
| 1685 | if (fol == NULL || low == NULL || upp == NULL) |
| 1686 | smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname); |
| 1687 | else |
| 1688 | set_spell_chartab(fol, low, upp); |
| 1689 | } |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1690 | |
| 1691 | vim_free(fol); |
| 1692 | vim_free(low); |
| 1693 | vim_free(upp); |
| 1694 | } |
| 1695 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1696 | vim_free(pc); |
| 1697 | fclose(fd); |
| 1698 | return aff; |
| 1699 | } |
| 1700 | |
| 1701 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1702 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 1703 | * When "s" is NULL FALSE is returned. |
| 1704 | */ |
| 1705 | static int |
| 1706 | has_non_ascii(s) |
| 1707 | char_u *s; |
| 1708 | { |
| 1709 | char_u *p; |
| 1710 | |
| 1711 | if (s != NULL) |
| 1712 | for (p = s; *p != NUL; ++p) |
| 1713 | if (*p >= 128) |
| 1714 | return TRUE; |
| 1715 | return FALSE; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1719 | * Free the structure filled by spell_read_aff(). |
| 1720 | */ |
| 1721 | static void |
| 1722 | spell_free_aff(aff) |
| 1723 | afffile_T *aff; |
| 1724 | { |
| 1725 | hashtab_T *ht; |
| 1726 | hashitem_T *hi; |
| 1727 | int todo; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1728 | affheader_T *ah; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1729 | affentry_T *ae; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1730 | |
| 1731 | vim_free(aff->af_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1732 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1733 | /* All this trouble to foree the "ae_prog" items... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1734 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 1735 | { |
| 1736 | todo = ht->ht_used; |
| 1737 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 1738 | { |
| 1739 | if (!HASHITEM_EMPTY(hi)) |
| 1740 | { |
| 1741 | --todo; |
| 1742 | ah = HI2AH(hi); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1743 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 1744 | vim_free(ae->ae_prog); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | if (ht == &aff->af_suff) |
| 1748 | break; |
| 1749 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1750 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1751 | hash_clear(&aff->af_pref); |
| 1752 | hash_clear(&aff->af_suff); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1753 | ga_clear(&aff->af_rep); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1757 | * Read dictionary file "fname". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1758 | * Returns OK or FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1759 | */ |
| 1760 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1761 | spell_read_dic(fname, spin, affile) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1762 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1763 | spellinfo_T *spin; |
| 1764 | afffile_T *affile; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1765 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1766 | hashtab_T ht; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1767 | char_u line[MAXLINELEN]; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1768 | char_u *afflist; |
| 1769 | char_u *dw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1770 | char_u *pc; |
| 1771 | char_u *w; |
| 1772 | int l; |
| 1773 | hash_T hash; |
| 1774 | hashitem_T *hi; |
| 1775 | FILE *fd; |
| 1776 | int lnum = 1; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1777 | int non_ascii = 0; |
| 1778 | int retval = OK; |
| 1779 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1780 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1781 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1782 | /* |
| 1783 | * Open the file. |
| 1784 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1785 | fd = fopen((char *)fname, "r"); |
| 1786 | if (fd == NULL) |
| 1787 | { |
| 1788 | EMSG2(_(e_notopen), fname); |
| 1789 | return FAIL; |
| 1790 | } |
| 1791 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1792 | /* The hashtable is only used to detect duplicated words. */ |
| 1793 | hash_init(&ht); |
| 1794 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1795 | smsg((char_u *)_("Reading dictionary file %s..."), fname); |
| 1796 | out_flush(); |
| 1797 | |
| 1798 | /* Read and ignore the first line: word count. */ |
| 1799 | (void)vim_fgets(line, MAXLINELEN, fd); |
| 1800 | if (!isdigit(*skipwhite(line))) |
| 1801 | EMSG2(_("E760: No word count in %s"), fname); |
| 1802 | |
| 1803 | /* |
| 1804 | * Read all the lines in the file one by one. |
| 1805 | * The words are converted to 'encoding' here, before being added to |
| 1806 | * the hashtable. |
| 1807 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1808 | while (!vim_fgets(line, MAXLINELEN, fd) && !got_int) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1809 | { |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1810 | line_breakcheck(); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1811 | ++lnum; |
| 1812 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1813 | /* Remove CR, LF and white space from the end. White space halfway |
| 1814 | * the word is kept to allow e.g., "et al.". */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1815 | l = STRLEN(line); |
| 1816 | while (l > 0 && line[l - 1] <= ' ') |
| 1817 | --l; |
| 1818 | if (l == 0) |
| 1819 | continue; /* empty line */ |
| 1820 | line[l] = NUL; |
| 1821 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1822 | /* This takes time, print a message now and then. */ |
| 1823 | if ((lnum & 0x3ff) == 0) |
| 1824 | { |
| 1825 | vim_snprintf((char *)message, sizeof(message), |
| 1826 | _("line %6d - %s"), lnum, line); |
| 1827 | msg_start(); |
| 1828 | msg_outtrans_attr(message, 0); |
| 1829 | msg_clr_eos(); |
| 1830 | msg_didout = FALSE; |
| 1831 | msg_col = 0; |
| 1832 | out_flush(); |
| 1833 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1834 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1835 | /* Find the optional affix names. */ |
| 1836 | afflist = vim_strchr(line, '/'); |
| 1837 | if (afflist != NULL) |
| 1838 | *afflist++ = NUL; |
| 1839 | |
| 1840 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 1841 | if (spin->si_ascii && has_non_ascii(line)) |
| 1842 | { |
| 1843 | ++non_ascii; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1844 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1845 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1846 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1847 | /* Convert from "SET" to 'encoding' when needed. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1848 | if (spin->si_conv.vc_type != CONV_NONE) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1849 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1850 | pc = string_convert(&spin->si_conv, line, NULL); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1851 | if (pc == NULL) |
| 1852 | { |
| 1853 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 1854 | fname, lnum, line); |
| 1855 | continue; |
| 1856 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1857 | w = pc; |
| 1858 | } |
| 1859 | else |
| 1860 | { |
| 1861 | pc = NULL; |
| 1862 | w = line; |
| 1863 | } |
| 1864 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1865 | /* Store the word in the hashtable to be able to find duplicates. */ |
| 1866 | dw = (char_u *)getroom_save(&spin->si_blocks, w); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1867 | if (dw == NULL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1868 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1869 | vim_free(pc); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1870 | if (retval == FAIL) |
| 1871 | break; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1872 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1873 | hash = hash_hash(dw); |
| 1874 | hi = hash_lookup(&ht, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1875 | if (!HASHITEM_EMPTY(hi)) |
| 1876 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1877 | fname, lnum, line); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1878 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1879 | hash_add_item(&ht, hi, dw, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1880 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1881 | flags = 0; |
| 1882 | if (afflist != NULL) |
| 1883 | { |
| 1884 | /* Check for affix name that stands for keep-case word and stands |
| 1885 | * for rare word (if defined). */ |
| 1886 | if (affile->af_huh != NUL |
| 1887 | && vim_strchr(afflist, affile->af_huh) != NULL) |
| 1888 | flags |= WF_KEEPCAP; |
| 1889 | if (affile->af_rar != NUL |
| 1890 | && vim_strchr(afflist, affile->af_rar) != NULL) |
| 1891 | flags |= WF_RARE; |
| 1892 | } |
| 1893 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1894 | /* Add the word to the word tree(s). */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1895 | if (store_word(dw, spin, flags) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1896 | retval = FAIL; |
| 1897 | |
| 1898 | if (afflist != NULL) |
| 1899 | { |
| 1900 | /* Find all matching suffixes and add the resulting words. |
| 1901 | * Additionally do matching prefixes that combine. */ |
| 1902 | if (store_aff_word(dw, spin, afflist, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1903 | &affile->af_suff, &affile->af_pref, |
| 1904 | FALSE, flags) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1905 | retval = FAIL; |
| 1906 | |
| 1907 | /* Find all matching prefixes and add the resulting words. */ |
| 1908 | if (store_aff_word(dw, spin, afflist, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1909 | &affile->af_pref, NULL, FALSE, flags) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1910 | retval = FAIL; |
| 1911 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1914 | if (spin->si_ascii && non_ascii > 0) |
| 1915 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 1916 | non_ascii); |
| 1917 | hash_clear(&ht); |
| 1918 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1919 | fclose(fd); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1920 | return retval; |
| 1921 | } |
| 1922 | |
| 1923 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1924 | * Apply affixes to a word and store the resulting words. |
| 1925 | * "ht" is the hashtable with affentry_T that need to be applied, either |
| 1926 | * prefixes or suffixes. |
| 1927 | * "xht", when not NULL, is the prefix hashtable, to be used additionally on |
| 1928 | * the resulting words for combining affixes. |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1929 | * |
| 1930 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1931 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1932 | static int |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1933 | store_aff_word(word, spin, afflist, ht, xht, comb, flags) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1934 | char_u *word; /* basic word start */ |
| 1935 | spellinfo_T *spin; /* spell info */ |
| 1936 | char_u *afflist; /* list of names of supported affixes */ |
| 1937 | hashtab_T *ht; |
| 1938 | hashtab_T *xht; |
| 1939 | int comb; /* only use affixes that combine */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1940 | int flags; /* flags for the word */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1941 | { |
| 1942 | int todo; |
| 1943 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1944 | affheader_T *ah; |
| 1945 | affentry_T *ae; |
| 1946 | regmatch_T regmatch; |
| 1947 | char_u newword[MAXWLEN]; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 1948 | int retval = OK; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1949 | int i; |
| 1950 | char_u *p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1951 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1952 | todo = ht->ht_used; |
| 1953 | for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1954 | { |
| 1955 | if (!HASHITEM_EMPTY(hi)) |
| 1956 | { |
| 1957 | --todo; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1958 | ah = HI2AH(hi); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1959 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1960 | /* Check that the affix combines, if required, and that the word |
| 1961 | * supports this affix. */ |
| 1962 | if ((!comb || ah->ah_combine) |
| 1963 | && vim_strchr(afflist, *ah->ah_key) != NULL) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1964 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1965 | /* Loop over all affix entries with this name. */ |
| 1966 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1967 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1968 | /* Check the condition. It's not logical to match case |
| 1969 | * here, but it is required for compatibility with |
| 1970 | * Myspell. */ |
| 1971 | regmatch.regprog = ae->ae_prog; |
| 1972 | regmatch.rm_ic = FALSE; |
| 1973 | if (ae->ae_prog == NULL |
| 1974 | || vim_regexec(®match, word, (colnr_T)0)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1975 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1976 | /* Match. Remove the chop and add the affix. */ |
| 1977 | if (xht == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1978 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 1979 | /* prefix: chop/add at the start of the word */ |
| 1980 | if (ae->ae_add == NULL) |
| 1981 | *newword = NUL; |
| 1982 | else |
| 1983 | STRCPY(newword, ae->ae_add); |
| 1984 | p = word; |
| 1985 | if (ae->ae_chop != NULL) |
| 1986 | /* Skip chop string. */ |
| 1987 | for (i = mb_charlen(ae->ae_chop); i > 0; --i) |
| 1988 | mb_ptr_adv(p); |
| 1989 | STRCAT(newword, p); |
| 1990 | } |
| 1991 | else |
| 1992 | { |
| 1993 | /* suffix: chop/add at the end of the word */ |
| 1994 | STRCPY(newword, word); |
| 1995 | if (ae->ae_chop != NULL) |
| 1996 | { |
| 1997 | /* Remove chop string. */ |
| 1998 | p = newword + STRLEN(newword); |
| 1999 | for (i = mb_charlen(ae->ae_chop); i > 0; --i) |
| 2000 | mb_ptr_back(newword, p); |
| 2001 | *p = NUL; |
| 2002 | } |
| 2003 | if (ae->ae_add != NULL) |
| 2004 | STRCAT(newword, ae->ae_add); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2007 | /* Store the modified word. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2008 | if (store_word(newword, spin, flags) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2009 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2010 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2011 | /* When added a suffix and combining is allowed also |
| 2012 | * try adding prefixes additionally. */ |
| 2013 | if (xht != NULL && ah->ah_combine) |
| 2014 | if (store_aff_word(newword, spin, afflist, |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2015 | xht, NULL, TRUE, flags) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2016 | retval = FAIL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2023 | return retval; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2027 | * Read a file with a list of words. |
| 2028 | */ |
| 2029 | static int |
| 2030 | spell_read_wordfile(fname, spin) |
| 2031 | char_u *fname; |
| 2032 | spellinfo_T *spin; |
| 2033 | { |
| 2034 | FILE *fd; |
| 2035 | long lnum = 0; |
| 2036 | char_u rline[MAXLINELEN]; |
| 2037 | char_u *line; |
| 2038 | char_u *pc = NULL; |
| 2039 | int l; |
| 2040 | int retval = OK; |
| 2041 | int did_word = FALSE; |
| 2042 | int non_ascii = 0; |
| 2043 | char_u *enc; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2044 | int flags; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2045 | |
| 2046 | /* |
| 2047 | * Open the file. |
| 2048 | */ |
| 2049 | fd = fopen((char *)fname, "r"); |
| 2050 | if (fd == NULL) |
| 2051 | { |
| 2052 | EMSG2(_(e_notopen), fname); |
| 2053 | return FAIL; |
| 2054 | } |
| 2055 | |
| 2056 | smsg((char_u *)_("Reading word file %s..."), fname); |
| 2057 | out_flush(); |
| 2058 | |
| 2059 | /* |
| 2060 | * Read all the lines in the file one by one. |
| 2061 | */ |
| 2062 | while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int) |
| 2063 | { |
| 2064 | line_breakcheck(); |
| 2065 | ++lnum; |
| 2066 | |
| 2067 | /* Skip comment lines. */ |
| 2068 | if (*rline == '#') |
| 2069 | continue; |
| 2070 | |
| 2071 | /* Remove CR, LF and white space from the end. */ |
| 2072 | l = STRLEN(rline); |
| 2073 | while (l > 0 && rline[l - 1] <= ' ') |
| 2074 | --l; |
| 2075 | if (l == 0) |
| 2076 | continue; /* empty or blank line */ |
| 2077 | rline[l] = NUL; |
| 2078 | |
| 2079 | /* Convert from "=encoding={encoding}" to 'encoding' when needed. */ |
| 2080 | vim_free(pc); |
| 2081 | if (spin->si_conv.vc_type != CONV_NONE) |
| 2082 | { |
| 2083 | pc = string_convert(&spin->si_conv, rline, NULL); |
| 2084 | if (pc == NULL) |
| 2085 | { |
| 2086 | smsg((char_u *)_("Conversion failure for word in %s line %d: %s"), |
| 2087 | fname, lnum, rline); |
| 2088 | continue; |
| 2089 | } |
| 2090 | line = pc; |
| 2091 | } |
| 2092 | else |
| 2093 | { |
| 2094 | pc = NULL; |
| 2095 | line = rline; |
| 2096 | } |
| 2097 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2098 | flags = 0; |
| 2099 | |
| 2100 | if (*line == '/') |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2101 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2102 | ++line; |
| 2103 | if (STRNCMP(line, "encoding=", 9) == 0) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2104 | { |
| 2105 | if (spin->si_conv.vc_type != CONV_NONE) |
| 2106 | smsg((char_u *)_("Duplicate =encoding= line ignored in %s line %d: %s"), |
| 2107 | fname, lnum, line); |
| 2108 | else if (did_word) |
| 2109 | smsg((char_u *)_("=encoding= line after word ignored in %s line %d: %s"), |
| 2110 | fname, lnum, line); |
| 2111 | else |
| 2112 | { |
| 2113 | /* Setup for conversion to 'encoding'. */ |
| 2114 | enc = enc_canonize(line + 10); |
| 2115 | if (enc != NULL && !spin->si_ascii |
| 2116 | && convert_setup(&spin->si_conv, enc, |
| 2117 | p_enc) == FAIL) |
| 2118 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 2119 | fname, line + 10, p_enc); |
| 2120 | vim_free(enc); |
| 2121 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2122 | continue; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2123 | } |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2124 | |
| 2125 | if (*line == '=') |
| 2126 | { |
| 2127 | /* keep-case word */ |
| 2128 | flags |= WF_KEEPCAP; |
| 2129 | ++line; |
| 2130 | } |
| 2131 | |
| 2132 | if (*line == '!') |
| 2133 | { |
| 2134 | /* Bad, bad, wicked word. */ |
| 2135 | flags |= WF_BANNED; |
| 2136 | ++line; |
| 2137 | } |
| 2138 | else if (*line == '?') |
| 2139 | { |
| 2140 | /* Rare word. */ |
| 2141 | flags |= WF_RARE; |
| 2142 | ++line; |
| 2143 | } |
| 2144 | |
| 2145 | if (flags == 0) |
| 2146 | { |
| 2147 | smsg((char_u *)_("/ line ignored in %s line %d: %s"), |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2148 | fname, lnum, line); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2149 | continue; |
| 2150 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */ |
| 2154 | if (spin->si_ascii && has_non_ascii(line)) |
| 2155 | { |
| 2156 | ++non_ascii; |
| 2157 | continue; |
| 2158 | } |
| 2159 | |
| 2160 | /* Normal word: store it. */ |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2161 | if (store_word(line, spin, flags) == FAIL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2162 | { |
| 2163 | retval = FAIL; |
| 2164 | break; |
| 2165 | } |
| 2166 | did_word = TRUE; |
| 2167 | } |
| 2168 | |
| 2169 | vim_free(pc); |
| 2170 | fclose(fd); |
| 2171 | |
| 2172 | if (spin->si_ascii && non_ascii > 0) |
| 2173 | smsg((char_u *)_("Ignored %d words with non-ASCII characters"), |
| 2174 | non_ascii); |
| 2175 | return retval; |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * Get part of an sblock_T, "len" bytes long. |
| 2180 | * This avoids calling free() for every little struct we use. |
| 2181 | * The memory is cleared to all zeros. |
| 2182 | * Returns NULL when out of memory. |
| 2183 | */ |
| 2184 | static void * |
| 2185 | getroom(blp, len) |
| 2186 | sblock_T **blp; |
| 2187 | size_t len; /* length needed */ |
| 2188 | { |
| 2189 | char_u *p; |
| 2190 | sblock_T *bl = *blp; |
| 2191 | |
| 2192 | if (bl == NULL || bl->sb_used + len > SBLOCKSIZE) |
| 2193 | { |
| 2194 | /* Allocate a block of memory. This is not freed until much later. */ |
| 2195 | bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 2196 | if (bl == NULL) |
| 2197 | return NULL; |
| 2198 | bl->sb_next = *blp; |
| 2199 | *blp = bl; |
| 2200 | bl->sb_used = 0; |
| 2201 | } |
| 2202 | |
| 2203 | p = bl->sb_data + bl->sb_used; |
| 2204 | bl->sb_used += len; |
| 2205 | |
| 2206 | return p; |
| 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Make a copy of a string into memory allocated with getroom(). |
| 2211 | */ |
| 2212 | static char_u * |
| 2213 | getroom_save(blp, s) |
| 2214 | sblock_T **blp; |
| 2215 | char_u *s; |
| 2216 | { |
| 2217 | char_u *sc; |
| 2218 | |
| 2219 | sc = (char_u *)getroom(blp, STRLEN(s) + 1); |
| 2220 | if (sc != NULL) |
| 2221 | STRCPY(sc, s); |
| 2222 | return sc; |
| 2223 | } |
| 2224 | |
| 2225 | |
| 2226 | /* |
| 2227 | * Free the list of allocated sblock_T. |
| 2228 | */ |
| 2229 | static void |
| 2230 | free_blocks(bl) |
| 2231 | sblock_T *bl; |
| 2232 | { |
| 2233 | sblock_T *next; |
| 2234 | |
| 2235 | while (bl != NULL) |
| 2236 | { |
| 2237 | next = bl->sb_next; |
| 2238 | vim_free(bl); |
| 2239 | bl = next; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | /* |
| 2244 | * Allocate the root of a word tree. |
| 2245 | */ |
| 2246 | static wordnode_T * |
| 2247 | wordtree_alloc(blp) |
| 2248 | sblock_T **blp; |
| 2249 | { |
| 2250 | return (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 2251 | } |
| 2252 | |
| 2253 | /* |
| 2254 | * Store a word in the tree(s). |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2255 | * Always store it in the case-folded tree. A keep-case word can also be used |
| 2256 | * with all caps. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2257 | * For a keep-case word also store it in the keep-case tree. |
| 2258 | */ |
| 2259 | static int |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2260 | store_word(word, spin, flags) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2261 | char_u *word; |
| 2262 | spellinfo_T *spin; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2263 | int flags; /* extra flags, WF_BANNED */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2264 | { |
| 2265 | int len = STRLEN(word); |
| 2266 | int ct = captype(word, word + len); |
| 2267 | char_u foldword[MAXWLEN]; |
| 2268 | int res; |
| 2269 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2270 | if (flags & WF_KEEPCAP) |
| 2271 | res = OK; /* keep-case specified, don't add as fold-case */ |
| 2272 | else |
| 2273 | { |
| 2274 | (void)spell_casefold(word, len, foldword, MAXWLEN); |
| 2275 | res = tree_add_word(foldword, spin->si_foldroot, |
| 2276 | (ct == WF_KEEPCAP ? WF_ALLCAP : ct) | flags, |
| 2277 | spin->si_region, &spin->si_blocks); |
| 2278 | } |
| 2279 | |
| 2280 | if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP)) |
| 2281 | res = tree_add_word(word, spin->si_keeproot, flags, |
| 2282 | spin->si_region, &spin->si_blocks); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2283 | return res; |
| 2284 | } |
| 2285 | |
| 2286 | /* |
| 2287 | * Add word "word" to a word tree at "root". |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2288 | * Returns FAIL when out of memory. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2289 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2290 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2291 | tree_add_word(word, root, flags, region, blp) |
| 2292 | char_u *word; |
| 2293 | wordnode_T *root; |
| 2294 | int flags; |
| 2295 | int region; |
| 2296 | sblock_T **blp; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2297 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2298 | wordnode_T *node = root; |
| 2299 | wordnode_T *np; |
| 2300 | wordnode_T **prev = NULL; |
| 2301 | int i; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2302 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2303 | /* Add each byte of the word to the tree, including the NUL at the end. */ |
| 2304 | for (i = 0; ; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2305 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2306 | /* Look for the sibling that has the same character. They are sorted |
| 2307 | * on byte value, thus stop searching when a sibling is found with a |
| 2308 | * higher byte value. For zero bytes (end of word) check that the |
| 2309 | * flags are equal, there is a separate zero byte for each flag value. |
| 2310 | */ |
| 2311 | while (node != NULL && (node->wn_byte < word[i] |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2312 | || (node->wn_byte == 0 && node->wn_flags != (flags & 0xff)))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2313 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2314 | prev = &node->wn_sibling; |
| 2315 | node = *prev; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2316 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2317 | if (node == NULL || node->wn_byte != word[i]) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2318 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2319 | /* Allocate a new node. */ |
| 2320 | np = (wordnode_T *)getroom(blp, sizeof(wordnode_T)); |
| 2321 | if (np == NULL) |
| 2322 | return FAIL; |
| 2323 | np->wn_byte = word[i]; |
| 2324 | *prev = np; |
| 2325 | np->wn_sibling = node; |
| 2326 | node = np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2327 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2328 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2329 | if (word[i] == NUL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2330 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2331 | node->wn_flags = flags; |
| 2332 | node->wn_region |= region; |
| 2333 | break; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2334 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2335 | prev = &node->wn_child; |
| 2336 | node = *prev; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | return OK; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2343 | * Compress a tree: find tails that are identical and can be shared. |
| 2344 | */ |
| 2345 | static void |
| 2346 | wordtree_compress(root) |
| 2347 | wordnode_T *root; |
| 2348 | { |
| 2349 | hashtab_T ht; |
| 2350 | int n; |
| 2351 | int tot = 0; |
| 2352 | |
| 2353 | if (root != NULL) |
| 2354 | { |
| 2355 | hash_init(&ht); |
| 2356 | n = node_compress(root, &ht, &tot); |
| 2357 | smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"), |
| 2358 | n, tot, (tot - n) * 100 / tot); |
| 2359 | hash_clear(&ht); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | /* |
| 2364 | * Compress a node, its siblings and its children, depth first. |
| 2365 | * Returns the number of compressed nodes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2366 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2367 | static int |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2368 | node_compress(node, ht, tot) |
| 2369 | wordnode_T *node; |
| 2370 | hashtab_T *ht; |
| 2371 | int *tot; /* total count of nodes before compressing, |
| 2372 | incremented while going through the tree */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2373 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2374 | wordnode_T *np; |
| 2375 | wordnode_T *tp; |
| 2376 | wordnode_T *child; |
| 2377 | hash_T hash; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2378 | hashitem_T *hi; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2379 | int len = 0; |
| 2380 | unsigned nr, n; |
| 2381 | int compressed = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2382 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2383 | /* |
| 2384 | * Go through the list of siblings. Compress each child and then try |
| 2385 | * finding an identical child to replace it. |
| 2386 | * Note that with "child" we mean not just the node that is pointed to, |
| 2387 | * but the whole list of siblings, of which the node is the first. |
| 2388 | */ |
| 2389 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2390 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2391 | ++len; |
| 2392 | if ((child = np->wn_child) != NULL) |
| 2393 | { |
| 2394 | /* Compress the child. This fills wn_hashkey. */ |
| 2395 | compressed += node_compress(child, ht, tot); |
| 2396 | |
| 2397 | /* Try to find an identical child. */ |
| 2398 | hash = hash_hash(child->wn_hashkey); |
| 2399 | hi = hash_lookup(ht, child->wn_hashkey, hash); |
| 2400 | tp = NULL; |
| 2401 | if (!HASHITEM_EMPTY(hi)) |
| 2402 | { |
| 2403 | /* There are children with an identical hash value. Now check |
| 2404 | * if there is one that is really identical. */ |
| 2405 | for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next) |
| 2406 | if (node_equal(child, tp)) |
| 2407 | { |
| 2408 | /* Found one! Now use that child in place of the |
| 2409 | * current one. This means the current child is |
| 2410 | * dropped from the tree. */ |
| 2411 | np->wn_child = tp; |
| 2412 | ++compressed; |
| 2413 | break; |
| 2414 | } |
| 2415 | if (tp == NULL) |
| 2416 | { |
| 2417 | /* No other child with this hash value equals the child of |
| 2418 | * the node, add it to the linked list after the first |
| 2419 | * item. */ |
| 2420 | tp = HI2WN(hi); |
| 2421 | child->wn_next = tp->wn_next; |
| 2422 | tp->wn_next = child; |
| 2423 | } |
| 2424 | } |
| 2425 | else |
| 2426 | /* No other child has this hash value, add it to the |
| 2427 | * hashtable. */ |
| 2428 | hash_add_item(ht, hi, child->wn_hashkey, hash); |
| 2429 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2430 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2431 | *tot += len; |
| 2432 | |
| 2433 | /* |
| 2434 | * Make a hash key for the node and its siblings, so that we can quickly |
| 2435 | * find a lookalike node. This must be done after compressing the sibling |
| 2436 | * list, otherwise the hash key would become invalid by the compression. |
| 2437 | */ |
| 2438 | node->wn_hashkey[0] = len; |
| 2439 | nr = 0; |
| 2440 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2441 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2442 | if (np->wn_byte == NUL) |
| 2443 | /* end node: only use wn_flags and wn_region */ |
| 2444 | n = np->wn_flags + (np->wn_region << 8); |
| 2445 | else |
| 2446 | /* byte node: use the byte value and the child pointer */ |
| 2447 | n = np->wn_byte + ((long_u)np->wn_child << 8); |
| 2448 | nr = nr * 101 + n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2449 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2450 | |
| 2451 | /* Avoid NUL bytes, it terminates the hash key. */ |
| 2452 | n = nr & 0xff; |
| 2453 | node->wn_hashkey[1] = n == 0 ? 1 : n; |
| 2454 | n = (nr >> 8) & 0xff; |
| 2455 | node->wn_hashkey[2] = n == 0 ? 1 : n; |
| 2456 | n = (nr >> 16) & 0xff; |
| 2457 | node->wn_hashkey[3] = n == 0 ? 1 : n; |
| 2458 | n = (nr >> 24) & 0xff; |
| 2459 | node->wn_hashkey[4] = n == 0 ? 1 : n; |
| 2460 | node->wn_hashkey[5] = NUL; |
| 2461 | |
| 2462 | return compressed; |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * Return TRUE when two nodes have identical siblings and children. |
| 2467 | */ |
| 2468 | static int |
| 2469 | node_equal(n1, n2) |
| 2470 | wordnode_T *n1; |
| 2471 | wordnode_T *n2; |
| 2472 | { |
| 2473 | wordnode_T *p1; |
| 2474 | wordnode_T *p2; |
| 2475 | |
| 2476 | for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL; |
| 2477 | p1 = p1->wn_sibling, p2 = p2->wn_sibling) |
| 2478 | if (p1->wn_byte != p2->wn_byte |
| 2479 | || (p1->wn_byte == NUL |
| 2480 | ? (p1->wn_flags != p2->wn_flags |
| 2481 | || p1->wn_region != p2->wn_region) |
| 2482 | : (p1->wn_child != p2->wn_child))) |
| 2483 | break; |
| 2484 | |
| 2485 | return p1 == NULL && p2 == NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | /* |
| 2489 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 2490 | */ |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2491 | void |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2492 | put_bytes(fd, nr, len) |
| 2493 | FILE *fd; |
| 2494 | long_u nr; |
| 2495 | int len; |
| 2496 | { |
| 2497 | int i; |
| 2498 | |
| 2499 | for (i = len - 1; i >= 0; --i) |
| 2500 | putc((int)(nr >> (i * 8)), fd); |
| 2501 | } |
| 2502 | |
| 2503 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2504 | * Write the Vim spell file "fname". |
| 2505 | */ |
| 2506 | static void |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2507 | write_vim_spell(fname, spin, regcount, regchars) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2508 | char_u *fname; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2509 | spellinfo_T *spin; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2510 | int regcount; /* number of regions */ |
| 2511 | char_u *regchars; /* region names */ |
| 2512 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2513 | FILE *fd; |
| 2514 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2515 | int round; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2516 | wordnode_T *tree; |
| 2517 | int nodecount; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2518 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2519 | fd = fopen((char *)fname, "w"); |
| 2520 | if (fd == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2521 | { |
| 2522 | EMSG2(_(e_notopen), fname); |
| 2523 | return; |
| 2524 | } |
| 2525 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2526 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... |
| 2527 | * <charflagslen> <charflags> <fcharslen> <fchars> */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* <fileID> */ |
| 2530 | if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1) |
| 2531 | EMSG(_(e_write)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2532 | |
| 2533 | /* write the region names if there is more than one */ |
| 2534 | if (regcount > 1) |
| 2535 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2536 | putc(regcount, fd); /* <regioncnt> <regionname> ... */ |
| 2537 | fwrite(regchars, (size_t)(regcount * 2), (size_t)1, fd); |
| 2538 | regionmask = (1 << regcount) - 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2539 | } |
| 2540 | else |
| 2541 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2542 | putc(0, fd); |
| 2543 | regionmask = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2546 | /* Write the table with character flags and table for case folding. |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2547 | * <charflagslen> <charflags> <fcharlen> <fchars> |
| 2548 | * Skip this for ASCII, the table may conflict with the one used for |
| 2549 | * 'encoding'. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2550 | if (spin->si_ascii) |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2551 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2552 | putc(0, fd); |
| 2553 | putc(0, fd); |
| 2554 | putc(0, fd); |
Bram Moolenaar | 6f3058f | 2005-04-24 21:58:05 +0000 | [diff] [blame] | 2555 | } |
| 2556 | else |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2557 | write_spell_chartab(fd); |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2558 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2559 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2560 | /* <SUGGEST> : <suggestlen> <more> ... |
| 2561 | * TODO. Only write a zero length for now. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2562 | put_bytes(fd, 0L, 4); /* <suggestlen> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2563 | |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2564 | spin->si_memtot = 0; |
| 2565 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2566 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2567 | * <LWORDTREE> <KWORDTREE> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2568 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2569 | for (round = 1; round <= 2; ++round) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2570 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2571 | tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2572 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2573 | /* Count the number of nodes. Needed to be able to allocate the |
| 2574 | * memory when reading the nodes. Also fills in the index for shared |
| 2575 | * nodes. */ |
| 2576 | nodecount = put_tree(NULL, tree, 0, regionmask); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2577 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2578 | /* number of nodes in 4 bytes */ |
| 2579 | put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */ |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2580 | spin->si_memtot += nodecount + nodecount * sizeof(int); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2581 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2582 | /* Write the nodes. */ |
| 2583 | (void)put_tree(fd, tree, 0, regionmask); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2586 | fclose(fd); |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2590 | * Dump a word tree at node "node". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2591 | * |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2592 | * This first writes the list of possible bytes (siblings). Then for each |
| 2593 | * byte recursively write the children. |
| 2594 | * |
| 2595 | * NOTE: The code here must match the code in read_tree(), since assumptions |
| 2596 | * are made about the indexes (so that we don't have to write them in the |
| 2597 | * file). |
| 2598 | * |
| 2599 | * Returns the number of nodes used. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2600 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2601 | static int |
| 2602 | put_tree(fd, node, index, regionmask) |
| 2603 | FILE *fd; /* NULL when only counting */ |
| 2604 | wordnode_T *node; |
| 2605 | int index; |
| 2606 | int regionmask; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2607 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2608 | int newindex = index; |
| 2609 | int siblingcount = 0; |
| 2610 | wordnode_T *np; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2611 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2612 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2613 | /* If "node" is zero the tree is empty. */ |
| 2614 | if (node == NULL) |
| 2615 | return 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2616 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2617 | /* Store the index where this node is written. */ |
| 2618 | node->wn_index = index; |
| 2619 | |
| 2620 | /* Count the number of siblings. */ |
| 2621 | for (np = node; np != NULL; np = np->wn_sibling) |
| 2622 | ++siblingcount; |
| 2623 | |
| 2624 | /* Write the sibling count. */ |
| 2625 | if (fd != NULL) |
| 2626 | putc(siblingcount, fd); /* <siblingcount> */ |
| 2627 | |
| 2628 | /* Write each sibling byte and optionally extra info. */ |
| 2629 | for (np = node; np != NULL; np = np->wn_sibling) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2630 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2631 | if (np->wn_byte == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2632 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2633 | if (fd != NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2634 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2635 | /* For a NUL byte (end of word) instead of the byte itself |
| 2636 | * we write the flag/region items. */ |
| 2637 | flags = np->wn_flags; |
| 2638 | if (regionmask != 0 && np->wn_region != regionmask) |
| 2639 | flags |= WF_REGION; |
| 2640 | if (flags == 0) |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2641 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2642 | /* word without flags or region */ |
| 2643 | putc(BY_NOFLAGS, fd); /* <byte> */ |
| 2644 | } |
| 2645 | else |
| 2646 | { |
| 2647 | putc(BY_FLAGS, fd); /* <byte> */ |
| 2648 | putc(flags, fd); /* <flags> */ |
| 2649 | if (flags & WF_REGION) |
| 2650 | putc(np->wn_region, fd); /* <regionmask> */ |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2651 | } |
| 2652 | } |
Bram Moolenaar | 2cf8b30 | 2005-04-20 19:37:22 +0000 | [diff] [blame] | 2653 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2654 | else |
| 2655 | { |
| 2656 | if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node) |
| 2657 | { |
| 2658 | /* The child is written elsewhere, write the reference. */ |
| 2659 | if (fd != NULL) |
| 2660 | { |
| 2661 | putc(BY_INDEX, fd); /* <byte> */ |
| 2662 | /* <nodeidx> */ |
| 2663 | put_bytes(fd, (long_u)np->wn_child->wn_index, 3); |
| 2664 | } |
| 2665 | } |
| 2666 | else if (np->wn_child->wn_wnode == NULL) |
| 2667 | /* We will write the child below and give it an index. */ |
| 2668 | np->wn_child->wn_wnode = node; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2669 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2670 | if (fd != NULL) |
| 2671 | if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */ |
| 2672 | { |
| 2673 | EMSG(_(e_write)); |
| 2674 | return 0; |
| 2675 | } |
| 2676 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2677 | } |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2678 | |
| 2679 | /* Space used in the array when reading: one for each sibling and one for |
| 2680 | * the count. */ |
| 2681 | newindex += siblingcount + 1; |
| 2682 | |
| 2683 | /* Recursively dump the children of each sibling. */ |
| 2684 | for (np = node; np != NULL; np = np->wn_sibling) |
| 2685 | if (np->wn_byte != 0 && np->wn_child->wn_wnode == node) |
| 2686 | newindex = put_tree(fd, np->wn_child, newindex, regionmask); |
| 2687 | |
| 2688 | return newindex; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | |
| 2692 | /* |
| 2693 | * ":mkspell outfile infile ..." |
| 2694 | */ |
| 2695 | void |
| 2696 | ex_mkspell(eap) |
| 2697 | exarg_T *eap; |
| 2698 | { |
| 2699 | int fcount; |
| 2700 | char_u **fnames; |
| 2701 | char_u fname[MAXPATHL]; |
| 2702 | char_u wfname[MAXPATHL]; |
| 2703 | afffile_T *(afile[8]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2704 | int i; |
| 2705 | int len; |
| 2706 | char_u region_name[16]; |
| 2707 | struct stat st; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2708 | char_u *arg = eap->arg; |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2709 | int error = FALSE; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2710 | spellinfo_T spin; |
| 2711 | |
| 2712 | vim_memset(&spin, 0, sizeof(spin)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2713 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2714 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 2715 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2716 | spin.si_ascii = TRUE; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2717 | arg = skipwhite(arg + 6); |
| 2718 | } |
| 2719 | |
| 2720 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 2721 | if (get_arglist_exp(arg, &fcount, &fnames) == FAIL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2722 | return; |
| 2723 | if (fcount < 2) |
| 2724 | EMSG(_(e_invarg)); /* need at least output and input names */ |
| 2725 | else if (fcount > 9) |
| 2726 | EMSG(_("E754: Only up to 8 regions supported")); |
| 2727 | else |
| 2728 | { |
| 2729 | /* Check for overwriting before doing things that may take a lot of |
| 2730 | * time. */ |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 2731 | vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0], |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2732 | spin.si_ascii ? (char_u *)"ascii" : p_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2733 | if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0) |
| 2734 | { |
| 2735 | EMSG(_(e_exists)); |
| 2736 | goto theend; |
| 2737 | } |
| 2738 | if (mch_isdir(fnames[0])) |
| 2739 | { |
| 2740 | EMSG2(_(e_isadir2), fnames[0]); |
| 2741 | goto theend; |
| 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * Init the aff and dic pointers. |
| 2746 | * Get the region names if there are more than 2 arguments. |
| 2747 | */ |
| 2748 | for (i = 1; i < fcount; ++i) |
| 2749 | { |
| 2750 | afile[i - 1] = NULL; |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2751 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2752 | if (fcount > 2) |
| 2753 | { |
| 2754 | len = STRLEN(fnames[i]); |
| 2755 | if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_') |
| 2756 | { |
| 2757 | EMSG2(_("E755: Invalid region in %s"), fnames[i]); |
| 2758 | goto theend; |
| 2759 | } |
| 2760 | else |
| 2761 | { |
| 2762 | region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]); |
| 2763 | region_name[(i - 1) * 2 + 1] = |
| 2764 | TOLOWER_ASC(fnames[i][len - 1]); |
| 2765 | } |
| 2766 | } |
| 2767 | } |
| 2768 | |
Bram Moolenaar | 8fef2ad | 2005-04-23 20:42:23 +0000 | [diff] [blame] | 2769 | /* Clear the char type tables, don't want to use any of the currently |
| 2770 | * used spell properties. */ |
| 2771 | init_spell_chartab(); |
| 2772 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2773 | spin.si_foldroot = wordtree_alloc(&spin.si_blocks); |
| 2774 | spin.si_keeproot = wordtree_alloc(&spin.si_blocks); |
| 2775 | if (spin.si_foldroot == NULL || spin.si_keeproot == NULL) |
| 2776 | { |
| 2777 | error = TRUE; |
| 2778 | goto theend; |
| 2779 | } |
| 2780 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2781 | /* |
| 2782 | * Read all the .aff and .dic files. |
| 2783 | * Text is converted to 'encoding'. |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2784 | * Words are stored in the case-folded and keep-case trees. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2785 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2786 | for (i = 1; i < fcount && !error; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2787 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2788 | spin.si_conv.vc_type = CONV_NONE; |
| 2789 | spin.si_region = 1 << (i - 1); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2790 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2791 | vim_snprintf((char *)fname, sizeof(fname), "%s.aff", fnames[i]); |
| 2792 | if (mch_stat((char *)fname, &st) >= 0) |
| 2793 | { |
| 2794 | /* Read the .aff file. Will init "spin->si_conv" based on the |
| 2795 | * "SET" line. */ |
| 2796 | afile[i - 1] = spell_read_aff(fname, &spin); |
| 2797 | if (afile[i - 1] == NULL) |
| 2798 | error = TRUE; |
| 2799 | else |
| 2800 | { |
| 2801 | /* Read the .dic file and store the words in the trees. */ |
| 2802 | vim_snprintf((char *)fname, sizeof(fname), "%s.dic", |
| 2803 | fnames[i]); |
| 2804 | if (spell_read_dic(fname, &spin, afile[i - 1]) == FAIL) |
| 2805 | error = TRUE; |
| 2806 | } |
| 2807 | } |
| 2808 | else |
| 2809 | { |
| 2810 | /* No .aff file, try reading the file as a word list. Store |
| 2811 | * the words in the trees. */ |
| 2812 | if (spell_read_wordfile(fnames[i], &spin) == FAIL) |
| 2813 | error = TRUE; |
| 2814 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2815 | |
| 2816 | /* Free any conversion stuff. */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2817 | convert_setup(&spin.si_conv, NULL, NULL); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2820 | if (!error) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2821 | { |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2822 | /* |
| 2823 | * Remove the dummy NUL from the start of the tree root. |
| 2824 | */ |
| 2825 | spin.si_foldroot = spin.si_foldroot->wn_sibling; |
| 2826 | spin.si_keeproot = spin.si_keeproot->wn_sibling; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2827 | |
| 2828 | /* |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2829 | * Combine tails in the tree. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2830 | */ |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2831 | MSG(_("Compressing word tree...")); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2832 | out_flush(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2833 | wordtree_compress(spin.si_foldroot); |
| 2834 | wordtree_compress(spin.si_keeproot); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2837 | if (!error) |
| 2838 | { |
| 2839 | /* |
| 2840 | * Write the info in the spell file. |
| 2841 | */ |
| 2842 | smsg((char_u *)_("Writing spell file %s..."), wfname); |
| 2843 | out_flush(); |
| 2844 | write_vim_spell(wfname, &spin, fcount - 1, region_name); |
| 2845 | MSG(_("Done!")); |
Bram Moolenaar | 50cde82 | 2005-06-05 21:54:54 +0000 | [diff] [blame] | 2846 | |
| 2847 | smsg((char_u *)_("Estimated runtime memory use: %d bytes"), |
| 2848 | spin.si_memtot); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2849 | out_flush(); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2850 | |
| 2851 | /* May need to reload spell dictionaries */ |
| 2852 | spell_reload(); |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | /* Free the allocated memory. */ |
| 2856 | free_blocks(spin.si_blocks); |
| 2857 | |
| 2858 | /* Free the .aff file structures. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2859 | for (i = 1; i < fcount; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2860 | if (afile[i - 1] != NULL) |
| 2861 | spell_free_aff(afile[i - 1]); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | theend: |
| 2865 | FreeWild(fcount, fnames); |
| 2866 | } |
| 2867 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2868 | #endif /* FEAT_MBYTE */ |
| 2869 | |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 2870 | |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 2871 | /* |
| 2872 | * Init the chartab used for spelling for ASCII. |
| 2873 | * EBCDIC is not supported! |
| 2874 | */ |
| 2875 | static void |
| 2876 | clear_spell_chartab(sp) |
| 2877 | spelltab_T *sp; |
| 2878 | { |
| 2879 | int i; |
| 2880 | |
| 2881 | /* Init everything to FALSE. */ |
| 2882 | vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); |
| 2883 | vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); |
| 2884 | for (i = 0; i < 256; ++i) |
| 2885 | sp->st_fold[i] = i; |
| 2886 | |
| 2887 | /* We include digits. A word shouldn't start with a digit, but handling |
| 2888 | * that is done separately. */ |
| 2889 | for (i = '0'; i <= '9'; ++i) |
| 2890 | sp->st_isw[i] = TRUE; |
| 2891 | for (i = 'A'; i <= 'Z'; ++i) |
| 2892 | { |
| 2893 | sp->st_isw[i] = TRUE; |
| 2894 | sp->st_isu[i] = TRUE; |
| 2895 | sp->st_fold[i] = i + 0x20; |
| 2896 | } |
| 2897 | for (i = 'a'; i <= 'z'; ++i) |
| 2898 | sp->st_isw[i] = TRUE; |
| 2899 | } |
| 2900 | |
| 2901 | /* |
| 2902 | * Init the chartab used for spelling. Only depends on 'encoding'. |
| 2903 | * Called once while starting up and when 'encoding' changes. |
| 2904 | * The default is to use isalpha(), but the spell file should define the word |
| 2905 | * characters to make it possible that 'encoding' differs from the current |
| 2906 | * locale. |
| 2907 | */ |
| 2908 | void |
| 2909 | init_spell_chartab() |
| 2910 | { |
| 2911 | int i; |
| 2912 | |
| 2913 | did_set_spelltab = FALSE; |
| 2914 | clear_spell_chartab(&spelltab); |
| 2915 | |
| 2916 | #ifdef FEAT_MBYTE |
| 2917 | if (enc_dbcs) |
| 2918 | { |
| 2919 | /* DBCS: assume double-wide characters are word characters. */ |
| 2920 | for (i = 128; i <= 255; ++i) |
| 2921 | if (MB_BYTE2LEN(i) == 2) |
| 2922 | spelltab.st_isw[i] = TRUE; |
| 2923 | } |
| 2924 | else |
| 2925 | #endif |
| 2926 | { |
| 2927 | /* Rough guess: use isalpha() and isupper() for characters above 128. */ |
| 2928 | for (i = 128; i < 256; ++i) |
| 2929 | { |
| 2930 | spelltab.st_isw[i] = MB_ISUPPER(i) || MB_ISLOWER(i); |
| 2931 | if (MB_ISUPPER(i)) |
| 2932 | { |
| 2933 | spelltab.st_isu[i] = TRUE; |
| 2934 | spelltab.st_fold[i] = MB_TOLOWER(i); |
| 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | } |
| 2939 | |
| 2940 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2941 | static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP"); |
| 2942 | static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range"); |
| 2943 | |
| 2944 | /* |
| 2945 | * Set the spell character tables from strings in the affix file. |
| 2946 | */ |
| 2947 | static int |
| 2948 | set_spell_chartab(fol, low, upp) |
| 2949 | char_u *fol; |
| 2950 | char_u *low; |
| 2951 | char_u *upp; |
| 2952 | { |
| 2953 | /* We build the new tables here first, so that we can compare with the |
| 2954 | * previous one. */ |
| 2955 | spelltab_T new_st; |
| 2956 | char_u *pf = fol, *pl = low, *pu = upp; |
| 2957 | int f, l, u; |
| 2958 | |
| 2959 | clear_spell_chartab(&new_st); |
| 2960 | |
| 2961 | while (*pf != NUL) |
| 2962 | { |
| 2963 | if (*pl == NUL || *pu == NUL) |
| 2964 | { |
| 2965 | EMSG(_(e_affform)); |
| 2966 | return FAIL; |
| 2967 | } |
| 2968 | #ifdef FEAT_MBYTE |
| 2969 | f = mb_ptr2char_adv(&pf); |
| 2970 | l = mb_ptr2char_adv(&pl); |
| 2971 | u = mb_ptr2char_adv(&pu); |
| 2972 | #else |
| 2973 | f = *pf++; |
| 2974 | l = *pl++; |
| 2975 | u = *pu++; |
| 2976 | #endif |
| 2977 | /* Every character that appears is a word character. */ |
| 2978 | if (f < 256) |
| 2979 | new_st.st_isw[f] = TRUE; |
| 2980 | if (l < 256) |
| 2981 | new_st.st_isw[l] = TRUE; |
| 2982 | if (u < 256) |
| 2983 | new_st.st_isw[u] = TRUE; |
| 2984 | |
| 2985 | /* if "LOW" and "FOL" are not the same the "LOW" char needs |
| 2986 | * case-folding */ |
| 2987 | if (l < 256 && l != f) |
| 2988 | { |
| 2989 | if (f >= 256) |
| 2990 | { |
| 2991 | EMSG(_(e_affrange)); |
| 2992 | return FAIL; |
| 2993 | } |
| 2994 | new_st.st_fold[l] = f; |
| 2995 | } |
| 2996 | |
| 2997 | /* if "UPP" and "FOL" are not the same the "UPP" char needs |
| 2998 | * case-folding and it's upper case. */ |
| 2999 | if (u < 256 && u != f) |
| 3000 | { |
| 3001 | if (f >= 256) |
| 3002 | { |
| 3003 | EMSG(_(e_affrange)); |
| 3004 | return FAIL; |
| 3005 | } |
| 3006 | new_st.st_fold[u] = f; |
| 3007 | new_st.st_isu[u] = TRUE; |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | if (*pl != NUL || *pu != NUL) |
| 3012 | { |
| 3013 | EMSG(_(e_affform)); |
| 3014 | return FAIL; |
| 3015 | } |
| 3016 | |
| 3017 | return set_spell_finish(&new_st); |
| 3018 | } |
| 3019 | #endif |
| 3020 | |
| 3021 | /* |
| 3022 | * Set the spell character tables from strings in the .spl file. |
| 3023 | */ |
| 3024 | static int |
| 3025 | set_spell_charflags(flags, cnt, upp) |
| 3026 | char_u *flags; |
| 3027 | int cnt; |
| 3028 | char_u *upp; |
| 3029 | { |
| 3030 | /* We build the new tables here first, so that we can compare with the |
| 3031 | * previous one. */ |
| 3032 | spelltab_T new_st; |
| 3033 | int i; |
| 3034 | char_u *p = upp; |
| 3035 | |
| 3036 | clear_spell_chartab(&new_st); |
| 3037 | |
| 3038 | for (i = 0; i < cnt; ++i) |
| 3039 | { |
| 3040 | new_st.st_isw[i + 128] = (flags[i] & SPELL_ISWORD) != 0; |
| 3041 | new_st.st_isu[i + 128] = (flags[i] & SPELL_ISUPPER) != 0; |
| 3042 | |
| 3043 | if (*p == NUL) |
| 3044 | return FAIL; |
| 3045 | #ifdef FEAT_MBYTE |
| 3046 | new_st.st_fold[i + 128] = mb_ptr2char_adv(&p); |
| 3047 | #else |
| 3048 | new_st.st_fold[i + 128] = *p++; |
| 3049 | #endif |
| 3050 | } |
| 3051 | |
| 3052 | return set_spell_finish(&new_st); |
| 3053 | } |
| 3054 | |
| 3055 | static int |
| 3056 | set_spell_finish(new_st) |
| 3057 | spelltab_T *new_st; |
| 3058 | { |
| 3059 | int i; |
| 3060 | |
| 3061 | if (did_set_spelltab) |
| 3062 | { |
| 3063 | /* check that it's the same table */ |
| 3064 | for (i = 0; i < 256; ++i) |
| 3065 | { |
| 3066 | if (spelltab.st_isw[i] != new_st->st_isw[i] |
| 3067 | || spelltab.st_isu[i] != new_st->st_isu[i] |
| 3068 | || spelltab.st_fold[i] != new_st->st_fold[i]) |
| 3069 | { |
| 3070 | EMSG(_("E763: Word characters differ between spell files")); |
| 3071 | return FAIL; |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | else |
| 3076 | { |
| 3077 | /* copy the new spelltab into the one being used */ |
| 3078 | spelltab = *new_st; |
| 3079 | did_set_spelltab = TRUE; |
| 3080 | } |
| 3081 | |
| 3082 | return OK; |
| 3083 | } |
| 3084 | |
| 3085 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 3086 | /* |
| 3087 | * Write the current tables into the .spl file. |
| 3088 | * This makes sure the same characters are recognized as word characters when |
| 3089 | * generating an when using a spell file. |
| 3090 | */ |
| 3091 | static void |
| 3092 | write_spell_chartab(fd) |
| 3093 | FILE *fd; |
| 3094 | { |
| 3095 | char_u charbuf[256 * 4]; |
| 3096 | int len = 0; |
| 3097 | int flags; |
| 3098 | int i; |
| 3099 | |
| 3100 | fputc(128, fd); /* <charflagslen> */ |
| 3101 | for (i = 128; i < 256; ++i) |
| 3102 | { |
| 3103 | flags = 0; |
| 3104 | if (spelltab.st_isw[i]) |
| 3105 | flags |= SPELL_ISWORD; |
| 3106 | if (spelltab.st_isu[i]) |
| 3107 | flags |= SPELL_ISUPPER; |
| 3108 | fputc(flags, fd); /* <charflags> */ |
| 3109 | |
| 3110 | len += mb_char2bytes(spelltab.st_fold[i], charbuf + len); |
| 3111 | } |
| 3112 | |
| 3113 | put_bytes(fd, (long_u)len, 2); /* <fcharlen> */ |
| 3114 | fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */ |
| 3115 | } |
| 3116 | #endif |
| 3117 | |
| 3118 | /* |
| 3119 | * Return TRUE if "c" is an upper-case character for spelling. |
| 3120 | */ |
| 3121 | static int |
| 3122 | spell_isupper(c) |
| 3123 | int c; |
| 3124 | { |
| 3125 | # ifdef FEAT_MBYTE |
| 3126 | if (enc_utf8) |
| 3127 | { |
| 3128 | /* For Unicode we can call utf_isupper(), but don't do that for ASCII, |
| 3129 | * because we don't want to use 'casemap' here. */ |
| 3130 | if (c >= 128) |
| 3131 | return utf_isupper(c); |
| 3132 | } |
| 3133 | else if (has_mbyte && c > 256) |
| 3134 | { |
| 3135 | /* For characters above 255 we don't have something specfied. |
| 3136 | * Fall back to locale-dependent iswupper(). If not available |
| 3137 | * simply return FALSE. */ |
| 3138 | # ifdef HAVE_ISWUPPER |
| 3139 | return iswupper(c); |
| 3140 | # else |
| 3141 | return FALSE; |
| 3142 | # endif |
| 3143 | } |
| 3144 | # endif |
| 3145 | return spelltab.st_isu[c]; |
| 3146 | } |
| 3147 | |
| 3148 | /* |
| 3149 | * Case-fold "p[len]" into "buf[buflen]". Used for spell checking. |
| 3150 | * When using a multi-byte 'encoding' the length may change! |
| 3151 | * Returns FAIL when something wrong. |
| 3152 | */ |
| 3153 | static int |
| 3154 | spell_casefold(p, len, buf, buflen) |
| 3155 | char_u *p; |
| 3156 | int len; |
| 3157 | char_u *buf; |
| 3158 | int buflen; |
| 3159 | { |
| 3160 | int i; |
| 3161 | |
| 3162 | if (len >= buflen) |
| 3163 | { |
| 3164 | buf[0] = NUL; |
| 3165 | return FAIL; /* result will not fit */ |
| 3166 | } |
| 3167 | |
| 3168 | #ifdef FEAT_MBYTE |
| 3169 | if (has_mbyte) |
| 3170 | { |
| 3171 | int c; |
| 3172 | int outi = 0; |
| 3173 | |
| 3174 | /* Fold one character at a time. */ |
| 3175 | for (i = 0; i < len; i += mb_ptr2len_check(p + i)) |
| 3176 | { |
| 3177 | c = mb_ptr2char(p + i); |
| 3178 | if (enc_utf8) |
| 3179 | /* For Unicode case folding is always the same, no need to use |
| 3180 | * the table from the spell file. */ |
| 3181 | c = utf_fold(c); |
| 3182 | else if (c < 256) |
| 3183 | /* Use the table from the spell file. */ |
| 3184 | c = spelltab.st_fold[c]; |
| 3185 | # ifdef HAVE_TOWLOWER |
| 3186 | else |
| 3187 | /* We don't know what to do, fall back to towlower(), it |
| 3188 | * depends on the current locale. */ |
| 3189 | c = towlower(c); |
| 3190 | # endif |
| 3191 | if (outi + MB_MAXBYTES > buflen) |
| 3192 | { |
| 3193 | buf[outi] = NUL; |
| 3194 | return FAIL; |
| 3195 | } |
| 3196 | outi += mb_char2bytes(c, buf + outi); |
| 3197 | } |
| 3198 | buf[outi] = NUL; |
| 3199 | } |
| 3200 | else |
| 3201 | #endif |
| 3202 | { |
| 3203 | /* Be quick for non-multibyte encodings. */ |
| 3204 | for (i = 0; i < len; ++i) |
| 3205 | buf[i] = spelltab.st_fold[p[i]]; |
| 3206 | buf[i] = NUL; |
| 3207 | } |
| 3208 | |
| 3209 | return OK; |
| 3210 | } |
| 3211 | |
| 3212 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3213 | #endif /* FEAT_SYN_HL */ |