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 | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 13 | * The basic spell checking mechanism is: |
| 14 | * 1. Isolate a word, up to the next non-word character. |
| 15 | * 2. Find the word in the hashtable of basic words. |
| 16 | * 3. If not found, look in the hashtable with "prewords". These are prefixes |
| 17 | * with a non-word character following a word character, e.g., "de-". |
| 18 | * 4. If still not found, for each matching a prefix try if the word matches |
| 19 | * without the prefix (and with the "chop" string added back). |
| 20 | * 5. If still still not found, for each matching suffix try if the word |
| 21 | * matches without the suffix (and with the "chop" string added back). |
| 22 | * |
| 23 | * Matching involves checking the caps type: Onecap ALLCAP KeepCap. |
| 24 | * After finding a matching word check for a leadstring (non-word characters |
| 25 | * before the word) and addstring (more text following, starting with a |
| 26 | * non-word character). |
| 27 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 28 | * Why doesn't Vim use aspell/ispell/myspell/etc.? |
| 29 | * See ":help develop-spell". |
| 30 | */ |
| 31 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 32 | #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64) |
| 33 | # include <io.h> /* for lseek(), must be before vim.h */ |
| 34 | #endif |
| 35 | |
| 36 | #include "vim.h" |
| 37 | |
| 38 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 39 | |
| 40 | #ifdef HAVE_FCNTL_H |
| 41 | # include <fcntl.h> |
| 42 | #endif |
| 43 | |
Bram Moolenaar | fc73515 | 2005-03-22 22:54:12 +0000 | [diff] [blame] | 44 | #define MAXWLEN 100 /* assume max. word len is this many bytes */ |
| 45 | |
Bram Moolenaar | e19defe | 2005-03-21 08:23:33 +0000 | [diff] [blame] | 46 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 47 | * Structure that is used to store the structures and strings from the |
| 48 | * language file. This avoids the need to allocate space for each individual |
| 49 | * word. It's allocated in big chunks for speed. It's freed all at once when |
| 50 | * 'encoding' changes. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 51 | */ |
| 52 | #define SBLOCKSIZE 4096 /* default size of sb_data */ |
| 53 | typedef struct sblock_S sblock_T; |
| 54 | struct sblock_S |
| 55 | { |
| 56 | sblock_T *sb_next; /* next block in list */ |
| 57 | char_u sb_data[1]; /* data, actually longer */ |
| 58 | }; |
| 59 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 60 | /* Info from "REP" entries in ".aff" file used in af_rep. |
| 61 | * TODO: This is not used yet. Either use it or remove it. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 62 | typedef struct repentry_S |
| 63 | { |
| 64 | char_u *re_from; |
| 65 | char_u *re_to; |
| 66 | } repentry_T; |
| 67 | |
| 68 | /* |
| 69 | * Structure to store affix info. |
| 70 | */ |
| 71 | typedef struct affitem_S affitem_T; |
| 72 | struct affitem_S |
| 73 | { |
| 74 | affitem_T *ai_next; /* next affix with same ai_add[] or NULL */ |
| 75 | short_u ai_nr; /* affix number */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 76 | char_u ai_flags; /* AFF_ flags */ |
| 77 | char_u ai_choplen; /* length of chop string in bytes */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 78 | char_u ai_addlen; /* length of ai_add in bytes */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 79 | char_u ai_leadlen; /* for AFF_PREWORD: length of lead string */ |
| 80 | char_u ai_taillen; /* for AFF_PREWORD: length of tail string */ |
| 81 | char_u ai_add[1]; /* Text added to basic word. This stores: |
| 82 | * 0: word for AFF_PREWORD or whole addition |
| 83 | * ai_addlen + 1: chop string |
| 84 | * + ai_choplen + 1: lead string for AFF_PREWORD |
| 85 | * + ai_leadlen + 1: trail string f. AFF_PREWORD |
| 86 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* Get affitem_T pointer from hashitem that uses ai_add */ |
| 90 | static affitem_T dumai; |
| 91 | #define HI2AI(hi) ((affitem_T *)((hi)->hi_key - (dumai.ai_add - (char_u *)&dumai))) |
| 92 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 93 | /* ai_flags: Affix item flags */ |
| 94 | #define AFF_COMBINE 0x01 /* prefix combines with suffix */ |
| 95 | #define AFF_PREWORD 0x02 /* prefix includes word */ |
| 96 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 97 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 98 | * Structure used to store words and other info for one language, loaded from |
| 99 | * a .spl file. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 100 | */ |
| 101 | typedef struct slang_S slang_T; |
| 102 | struct slang_S |
| 103 | { |
| 104 | slang_T *sl_next; /* next language */ |
| 105 | char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */ |
| 106 | hashtab_T sl_words; /* main word table, fword_T */ |
| 107 | int sl_prefcnt; /* number of prefix NRs */ |
| 108 | garray_T sl_preftab; /* list of hashtables to lookup prefixes */ |
| 109 | affitem_T *sl_prefzero; /* list of prefixes with zero add length */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 110 | hashtab_T sl_prewords; /* prefixes that include a word */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 111 | int sl_suffcnt; /* number of suffix NRs */ |
| 112 | garray_T sl_sufftab; /* list of hashtables to lookup suffixes */ |
| 113 | affitem_T *sl_suffzero; /* list of suffixes with zero add length */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 114 | char_u *sl_try; /* "TRY" from .aff file TODO: not used */ |
| 115 | garray_T sl_rep; /* list of repentry_T entries from REP lines |
| 116 | * TODO not used */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 117 | char_u sl_regions[17]; /* table with up to 8 region names plus NUL */ |
| 118 | sblock_T *sl_block; /* list with allocated memory blocks */ |
| 119 | int sl_error; /* error while loading */ |
| 120 | }; |
| 121 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 122 | /* First language that is loaded, start of the linked list of loaded |
| 123 | * languages. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 124 | static slang_T *first_lang = NULL; |
| 125 | |
| 126 | /* |
| 127 | * Structure to store an addition to a basic word. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 128 | * There are many of these, keep it small! |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 129 | */ |
| 130 | typedef struct addword_S addword_T; |
| 131 | struct addword_S |
| 132 | { |
| 133 | addword_T *aw_next; /* next addition */ |
| 134 | char_u aw_flags; /* ADD_ flags */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 135 | char_u aw_region; /* region for word with this addition */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 136 | char_u aw_leadlen; /* byte length of lead in aw_word */ |
| 137 | char_u aw_wordlen; /* byte length of first word in aw_word */ |
| 138 | char_u aw_saveb; /* saved byte where aw_word[] is truncated at |
| 139 | end of hashtable key; NUL when not using |
| 140 | hashtable */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 141 | char_u aw_word[1]; /* text, actually longer: case-folded addition |
| 142 | plus, with ADD_KEEPCAP: keep-case addition */ |
| 143 | }; |
| 144 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 145 | /* Get addword_T pointer from hashitem that uses aw_word */ |
| 146 | static addword_T dumaw; |
| 147 | #define HI2ADDWORD(hi) ((addword_T *)((hi)->hi_key - (dumaw.aw_word - (char_u *)&dumaw))) |
| 148 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 149 | /* |
| 150 | * Structure to store a basic word. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 151 | * There are many of these, keep it small! |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 152 | */ |
| 153 | typedef struct fword_S fword_T; |
| 154 | struct fword_S |
| 155 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 156 | fword_T *fw_next; /* same basic word with different caps and/or |
| 157 | * affixes */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 158 | addword_T *fw_adds; /* first addword_T entry */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 159 | short_u fw_flags; /* BWF_ flags */ |
| 160 | char_u fw_region; /* region bits */ |
| 161 | char_u fw_prefixcnt; /* number of prefix NRs */ |
| 162 | char_u fw_suffixcnt; /* number of suffix NRs */ |
| 163 | char_u fw_word[1]; /* actually longer: |
| 164 | * 0: case folded word or keep-case word when |
| 165 | * (flags & BWF_KEEPCAP) |
| 166 | * + word length + 1: list of prefix NRs |
| 167 | * + fw_prefixcnt [* 2]: list of suffix NRs |
| 168 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /* Get fword_T pointer from hashitem that uses fw_word */ |
| 172 | static fword_T dumfw; |
| 173 | #define HI2FWORD(hi) ((fword_T *)((hi)->hi_key - (dumfw.fw_word - (char_u *)&dumfw))) |
| 174 | |
| 175 | #define REGION_ALL 0xff |
| 176 | |
| 177 | |
| 178 | /* |
| 179 | * Structure used in "b_langp", filled from 'spelllang'. |
| 180 | */ |
| 181 | typedef struct langp_S |
| 182 | { |
| 183 | slang_T *lp_slang; /* info for this language (NULL for last one) */ |
| 184 | int lp_region; /* bitmask for region or REGION_ALL */ |
| 185 | } langp_T; |
| 186 | |
| 187 | #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i)) |
| 188 | |
| 189 | #define SP_OK 0 |
| 190 | #define SP_BAD 1 |
| 191 | #define SP_RARE 2 |
| 192 | #define SP_LOCAL 3 |
| 193 | |
| 194 | /* flags used for basic words in the spell file */ |
| 195 | #define BWF_VALID 0x01 /* word is valid without additions */ |
| 196 | #define BWF_REGION 0x02 /* region byte follows */ |
| 197 | #define BWF_ONECAP 0x04 /* first letter must be capital */ |
| 198 | #define BWF_SUFFIX 0x08 /* has suffix NR list */ |
| 199 | #define BWF_SECOND 0x10 /* second flags byte follows */ |
| 200 | |
| 201 | #define BWF_ADDS 0x0100 /* there are additions */ |
| 202 | #define BWF_PREFIX 0x0200 /* has prefix NR list */ |
| 203 | #define BWF_ALLCAP 0x0400 /* all letters must be capital (not used |
| 204 | for single-letter words) */ |
| 205 | #define BWF_KEEPCAP 0x0800 /* Keep case as-is */ |
| 206 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 207 | #define BWF_ADDHASH 0x8000 /* Internal: use hashtab for additions */ |
| 208 | |
| 209 | #define NOWC_KEY (char_u *)"x" /* hashtab key used for additions without |
| 210 | any word character */ |
| 211 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 212 | /* flags used for addition in the spell file */ |
| 213 | #define ADD_REGION 0x02 /* region byte follows */ |
| 214 | #define ADD_ONECAP 0x04 /* first letter must be capital */ |
| 215 | #define ADD_ALLCAP 0x40 /* all letters must be capital (not used |
| 216 | for single-letter words) */ |
| 217 | #define ADD_KEEPCAP 0x80 /* fixed case */ |
| 218 | |
| 219 | /* Translate ADD_ flags to BWF_ flags. |
| 220 | * (Needed to keep ADD_ flags in one byte.) */ |
| 221 | #define ADD2BWF(x) (((x) & 0x0f) | (((x) & 0xf0) << 4)) |
| 222 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 223 | #define VIMSPELLMAGIC "VIMspell02" /* string at start of Vim spell file */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 224 | #define VIMSPELLMAGICL 10 |
| 225 | |
| 226 | /* |
| 227 | * Structure to store info for word matching. |
| 228 | */ |
| 229 | typedef struct matchinf_S |
| 230 | { |
| 231 | langp_T *mi_lp; /* info for language and region */ |
| 232 | slang_T *mi_slang; /* info for the language */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 233 | |
| 234 | /* pointers to original text to be checked */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 235 | char_u *mi_line; /* start of line containing word */ |
| 236 | char_u *mi_word; /* start of word being checked */ |
| 237 | char_u *mi_end; /* first non-word char after mi_word */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 238 | char_u *mi_wend; /* end of matching word (is mi_end |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 239 | * or further) */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 240 | char_u *mi_fend; /* next char to be added to mi_fword */ |
| 241 | |
| 242 | /* case-folded text */ |
| 243 | char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ |
| 244 | int mi_fendlen; /* byte length of first word in |
| 245 | mi_fword */ |
| 246 | int mi_faddlen; /* byte length of text in mi_fword |
| 247 | after first word */ |
| 248 | char_u *mi_cword; /* word to check, points in mi_fword */ |
| 249 | char_u *mi_awend; /* after next word, to check for |
| 250 | addition (NULL when not done yet) */ |
| 251 | int mi_did_awend; /* did compute mi_awend */ |
| 252 | |
| 253 | /* others */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 254 | int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
| 255 | int mi_capflags; /* BWF_ONECAP BWF_ALLCAP BWF_KEEPCAP */ |
| 256 | } matchinf_T; |
| 257 | |
| 258 | static int word_match __ARGS((matchinf_T *mip)); |
| 259 | static int check_adds __ARGS((matchinf_T *mip, fword_T *fw, int req_pref, int req_suf)); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 260 | static void fill_awend __ARGS((matchinf_T *mip)); |
| 261 | static void fold_addchars __ARGS((matchinf_T *mip, int addlen)); |
| 262 | static int supports_affix __ARGS((int cnt, char_u *afflist, int afflistlen, int nr)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 263 | static int prefix_match __ARGS((matchinf_T *mip)); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 264 | static int noprefix_match __ARGS((matchinf_T *mip, char_u *pword, char_u *cstart, affitem_T *ai)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 265 | static int suffix_match __ARGS((matchinf_T *mip)); |
| 266 | static int match_caps __ARGS((int flags, char_u *caseword, matchinf_T *mip, char_u *cword, char_u *end)); |
| 267 | static slang_T *slang_alloc __ARGS((char_u *lang)); |
| 268 | static void slang_free __ARGS((slang_T *lp)); |
| 269 | static slang_T *spell_load_lang __ARGS((char_u *lang)); |
| 270 | static void spell_load_file __ARGS((char_u *fname, void *cookie)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 271 | static void *getroom __ARGS((slang_T *lp, int *bl_used, int len)); |
| 272 | static int find_region __ARGS((char_u *rp, char_u *region)); |
| 273 | static int captype __ARGS((char_u *word, char_u *end)); |
| 274 | |
| 275 | /* |
| 276 | * Main spell-checking function. |
| 277 | * "ptr" points to the start of a word. |
| 278 | * "*attrp" is set to the attributes for a badly spelled word. For a non-word |
| 279 | * or when it's OK it remains unchanged. |
| 280 | * This must only be called when 'spelllang' is not empty. |
| 281 | * Returns the length of the word in bytes, also when it's OK, so that the |
| 282 | * caller can skip over the word. |
| 283 | */ |
| 284 | int |
| 285 | spell_check(wp, line, ptr, attrp) |
| 286 | win_T *wp; /* current window */ |
| 287 | char_u *line; /* start of line where "ptr" points into */ |
| 288 | char_u *ptr; |
| 289 | int *attrp; |
| 290 | { |
| 291 | matchinf_T mi; /* Most things are put in "mi" so that it can |
| 292 | be passed to functions quickly. */ |
| 293 | |
| 294 | /* Find the end of the word. We already know that *ptr is a word char. */ |
| 295 | mi.mi_word = ptr; |
| 296 | mi.mi_end = ptr; |
| 297 | do |
| 298 | { |
| 299 | mb_ptr_adv(mi.mi_end); |
| 300 | } while (*mi.mi_end != NUL && spell_iswordc(mi.mi_end)); |
| 301 | |
| 302 | /* A word starting with a number is always OK. */ |
| 303 | if (*ptr >= '0' && *ptr <= '9') |
| 304 | return (int)(mi.mi_end - ptr); |
| 305 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 306 | /* Make case-folded copy of the word. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 307 | (void)str_foldcase(ptr, mi.mi_end - ptr, mi.mi_fword, MAXWLEN + 1); |
| 308 | mi.mi_cword = mi.mi_fword; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 309 | mi.mi_fendlen = STRLEN(mi.mi_fword); |
| 310 | mi.mi_faddlen = 0; |
| 311 | mi.mi_fend = mi.mi_end; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 312 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 313 | /* Check the caps type of the word. */ |
| 314 | mi.mi_capflags = captype(ptr, mi.mi_end); |
| 315 | |
| 316 | /* The word is bad unless we recognize it. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 317 | mi.mi_result = SP_BAD; |
| 318 | mi.mi_wend = mi.mi_end; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 319 | |
| 320 | mi.mi_awend = NULL; |
| 321 | mi.mi_did_awend = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 322 | mi.mi_line = line; |
| 323 | |
| 324 | /* |
| 325 | * Loop over the languages specified in 'spelllang'. |
| 326 | * We check them all, because a matching word may have additions that are |
| 327 | * longer than an already found matching word. |
| 328 | */ |
| 329 | for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0); |
| 330 | mi.mi_lp->lp_slang != NULL; ++mi.mi_lp) |
| 331 | { |
| 332 | /* |
| 333 | * Check for a matching word. |
| 334 | * If not found or wrong region try removing prefixes (and then |
| 335 | * suffixes). |
| 336 | * If still not found or wrong region try removing suffixes. |
| 337 | */ |
| 338 | mi.mi_slang = mi.mi_lp->lp_slang; |
| 339 | if (!word_match(&mi) || mi.mi_result != SP_OK) |
| 340 | if (!prefix_match(&mi) || mi.mi_result != SP_OK) |
| 341 | suffix_match(&mi); |
| 342 | } |
| 343 | |
| 344 | if (mi.mi_result != SP_OK) |
| 345 | { |
| 346 | if (mi.mi_result == SP_BAD) |
| 347 | *attrp = highlight_attr[HLF_SPB]; |
| 348 | else if (mi.mi_result == SP_RARE) |
| 349 | *attrp = highlight_attr[HLF_SPR]; |
| 350 | else |
| 351 | *attrp = highlight_attr[HLF_SPL]; |
| 352 | } |
| 353 | |
| 354 | return (int)(mi.mi_wend - ptr); |
| 355 | } |
| 356 | |
| 357 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 358 | * Check if the word "mip->mi_word" matches. |
| 359 | * "mip->mi_fword" is the same word case-folded; |
| 360 | * |
| 361 | * This checks the word as a whole and for prefixes that include a word. |
| 362 | * |
| 363 | * Note that when called mi_fword only contains the word up to mip->mi_end, |
| 364 | * but when checking additions it gets longer. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 365 | */ |
| 366 | static int |
| 367 | word_match(mip) |
| 368 | matchinf_T *mip; |
| 369 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 370 | hash_T fhash = hash_hash(mip->mi_fword); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 371 | hashitem_T *hi; |
| 372 | fword_T *fw; |
| 373 | int valid = FALSE; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 374 | char_u *p; |
| 375 | char_u pword[MAXWLEN + 1]; |
| 376 | int charlen; |
| 377 | int capflags_save; |
| 378 | affitem_T *ai; |
| 379 | char_u *cstart; |
| 380 | int addlen; |
| 381 | int n; |
| 382 | char_u *save_end; |
| 383 | int cc; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 384 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 385 | hi = hash_lookup(&mip->mi_slang->sl_words, mip->mi_fword, fhash); |
| 386 | if (!HASHITEM_EMPTY(hi)) |
| 387 | { |
| 388 | /* |
| 389 | * Find a basic word for which the case of "mi_word" is correct. |
| 390 | * If it is, check additions and use the longest one. |
| 391 | */ |
| 392 | for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next) |
| 393 | if (match_caps(fw->fw_flags, fw->fw_word, mip, |
| 394 | mip->mi_word, mip->mi_end)) |
| 395 | valid |= check_adds(mip, fw, -1, -1); |
| 396 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 397 | |
| 398 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 399 | * Try finding a matching preword for "mip->mi_word". These are |
| 400 | * prefixes that have a non-word character after a word character: |
| 401 | * "d'", "de-", "'s-", "l'de-". But not "'s". |
| 402 | * Also need to do this when a matching word was already found, because we |
| 403 | * might find a longer match this way (French: "qu" and "qu'a-t-elle"). |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 404 | */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 405 | cc = mip->mi_fword[mip->mi_fendlen]; |
| 406 | mip->mi_fword[mip->mi_fendlen] = NUL; |
| 407 | hi = hash_lookup(&mip->mi_slang->sl_prewords, mip->mi_fword, fhash); |
| 408 | mip->mi_fword[mip->mi_fendlen] = cc; |
| 409 | if (!HASHITEM_EMPTY(hi)) |
| 410 | { |
| 411 | capflags_save = mip->mi_capflags; |
| 412 | |
| 413 | /* Go through the list of matching prewords. */ |
| 414 | for (ai = HI2AI(hi); ai != NULL; ai = ai->ai_next) |
| 415 | { |
| 416 | /* Check that the lead string matches before the word. */ |
| 417 | p = ai->ai_add + ai->ai_addlen + ai->ai_choplen + 2; |
| 418 | if (ai->ai_leadlen > 0) |
| 419 | { |
| 420 | if (mip->mi_word - mip->mi_line < ai->ai_leadlen |
| 421 | || STRNCMP(mip->mi_word - ai->ai_leadlen, p, |
| 422 | ai->ai_leadlen) != 0) |
| 423 | continue; |
| 424 | p += ai->ai_leadlen + 1; /* advance "p" to tail */ |
| 425 | } |
| 426 | else |
| 427 | ++p; /* advance "p" to tail */ |
| 428 | |
| 429 | /* Check that the tail string matches after the word. Need |
| 430 | * to fold case first. */ |
| 431 | if (ai->ai_taillen > 0) |
| 432 | { |
| 433 | if (ai->ai_taillen >= mip->mi_faddlen) |
| 434 | { |
| 435 | fold_addchars(mip, ai->ai_taillen); |
| 436 | if (ai->ai_taillen > mip->mi_faddlen) |
| 437 | continue; /* not enough chars, can't match */ |
| 438 | } |
| 439 | if (STRNCMP(mip->mi_fword + mip->mi_fendlen, |
| 440 | p, ai->ai_taillen) != 0) |
| 441 | continue; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * This preword matches. Remove the preword and check that |
| 446 | * the resulting word exits. |
| 447 | */ |
| 448 | |
| 449 | /* Find the place in the original word where the tail ends, |
| 450 | * needed for case checks. */ |
| 451 | #ifdef FEAT_MBYTE |
| 452 | charlen = mb_charlen(p); |
| 453 | #else |
| 454 | charlen = ai->ai_taillen; |
| 455 | #endif |
| 456 | cstart = mip->mi_end; |
| 457 | for (n = 0; n < charlen; ++n) |
| 458 | mb_ptr_adv(cstart); |
| 459 | |
| 460 | /* The new word starts with the chop. Then add up to the next |
| 461 | * non-word char. */ |
| 462 | mch_memmove(pword, ai->ai_add + ai->ai_addlen + 1, |
| 463 | ai->ai_choplen); |
| 464 | p = mip->mi_fword + mip->mi_fendlen + ai->ai_taillen; |
| 465 | addlen = ai->ai_taillen; |
| 466 | while (spell_iswordc(p)) |
| 467 | { |
| 468 | ++charlen; |
| 469 | #ifdef FEAT_MBYTE |
| 470 | addlen += (*mb_ptr2len_check)(p); |
| 471 | #else |
| 472 | ++addlen; |
| 473 | #endif |
| 474 | mb_ptr_adv(p); |
| 475 | if (addlen >= mip->mi_faddlen) |
| 476 | { |
| 477 | /* Get more folded characters in mip->mi_fword. */ |
| 478 | fold_addchars(mip, addlen); |
| 479 | if (addlen >= mip->mi_faddlen) |
| 480 | break; /* not enough chars, can't match */ |
| 481 | } |
| 482 | } |
| 483 | mch_memmove(pword + ai->ai_choplen, |
| 484 | mip->mi_fword + mip->mi_fendlen + ai->ai_taillen, |
| 485 | addlen - ai->ai_taillen); |
| 486 | pword[ai->ai_choplen + addlen - ai->ai_taillen] = NUL; |
| 487 | |
| 488 | /* Need to set mi_end to find additions. Also set mi_fendlen |
| 489 | * and mi_faddlen. */ |
| 490 | save_end = mip->mi_end; |
| 491 | while (--charlen >= 0) |
| 492 | mb_ptr_adv(mip->mi_end); |
| 493 | mip->mi_fendlen += addlen; |
| 494 | mip->mi_faddlen -= addlen; |
| 495 | |
| 496 | /* Find the word "pword", caseword "cstart". */ |
| 497 | n = noprefix_match(mip, pword, cstart, ai); |
| 498 | mip->mi_end = save_end; |
| 499 | mip->mi_fendlen -= addlen; |
| 500 | mip->mi_faddlen += addlen; |
| 501 | if (n) |
| 502 | valid = TRUE; |
| 503 | |
| 504 | /* If we found a valid word, we still need to try other |
| 505 | * suffixes, because it may have an addition that's longer. */ |
| 506 | } |
| 507 | |
| 508 | mip->mi_capflags = capflags_save; |
| 509 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 510 | |
| 511 | return valid; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Check a matching basic word for additions. |
| 516 | * Return TRUE if we have a valid match. |
| 517 | */ |
| 518 | static int |
| 519 | check_adds(mip, fw, req_pref, req_suf) |
| 520 | matchinf_T *mip; |
| 521 | fword_T *fw; |
| 522 | int req_pref; /* required prefix nr, -1 if none */ |
| 523 | int req_suf; /* required suffix nr, -1 if none */ |
| 524 | { |
| 525 | int valid = FALSE; |
| 526 | addword_T *aw; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 527 | addword_T *naw = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 528 | char_u *p; |
| 529 | int addlen; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 530 | int cc; |
| 531 | hashitem_T *hi; |
| 532 | char_u *cp = NULL; |
| 533 | int n; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 534 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 535 | /* Check if required prefixes and suffixes are supported. These are on |
| 536 | * the basic word, not on each addition. */ |
| 537 | if (req_pref >= 0 || req_suf >= 0) |
| 538 | { |
| 539 | /* Prefix NRs are stored just after the word in fw_word. */ |
| 540 | cp = fw->fw_word + STRLEN(fw->fw_word) + 1; |
| 541 | if (req_pref >= 0 && !supports_affix(mip->mi_slang->sl_prefcnt, |
| 542 | cp, fw->fw_prefixcnt, req_pref)) |
| 543 | return FALSE; |
| 544 | if (req_suf >= 0) |
| 545 | { |
| 546 | /* Suffix NRs are stored just after the Prefix NRs. */ |
| 547 | if (fw->fw_prefixcnt > 0) |
| 548 | { |
| 549 | if (mip->mi_slang->sl_prefcnt > 256) |
| 550 | cp += fw->fw_prefixcnt * 2; |
| 551 | else |
| 552 | cp += fw->fw_prefixcnt; |
| 553 | } |
| 554 | if (!supports_affix(mip->mi_slang->sl_suffcnt, |
| 555 | cp, fw->fw_suffixcnt, req_suf)) |
| 556 | return FALSE; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* A word may be valid without an addition. */ |
| 561 | if (fw->fw_flags & BWF_VALID) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 562 | { |
| 563 | valid = TRUE; |
| 564 | if (mip->mi_result != SP_OK) |
| 565 | { |
| 566 | if ((fw->fw_region & mip->mi_lp->lp_region) == 0) |
| 567 | mip->mi_result = SP_LOCAL; |
| 568 | else |
| 569 | mip->mi_result = SP_OK; |
| 570 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 571 | /* Set word end, required when matching a word after a preword. */ |
| 572 | if (mip->mi_wend < mip->mi_end) |
| 573 | mip->mi_wend = mip->mi_end; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Check additions, both before and after the word. |
| 578 | * This may make the word longer, thus we also need to check |
| 579 | * when we already found a matching word. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 580 | * When the BWF_ADDHASH flag is present then fw_adds points to a hashtable |
| 581 | * for quick lookup. Otherwise it points to the list of all possible |
| 582 | * additions. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 583 | */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 584 | if (fw->fw_flags & BWF_ADDHASH) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 585 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 586 | /* Locate the text up to the next end-of-word. */ |
| 587 | if (!mip->mi_did_awend) |
| 588 | fill_awend(mip); |
| 589 | if (mip->mi_awend == NULL) |
| 590 | return valid; /* there is no next word */ |
| 591 | |
| 592 | cc = *mip->mi_awend; |
| 593 | *mip->mi_awend = NUL; |
| 594 | hi = hash_find((hashtab_T *)fw->fw_adds, |
| 595 | mip->mi_fword + mip->mi_fendlen); |
| 596 | *mip->mi_awend = cc; |
| 597 | if (HASHITEM_EMPTY(hi)) |
| 598 | return valid; /* no matching addition */ |
| 599 | aw = HI2ADDWORD(hi); |
| 600 | |
| 601 | /* Also check additions without word characters. If they are there, |
| 602 | * skip the first dummy entry. */ |
| 603 | hi = hash_find((hashtab_T *)fw->fw_adds, NOWC_KEY); |
| 604 | if (!HASHITEM_EMPTY(hi)) |
| 605 | naw = HI2ADDWORD(hi)->aw_next; |
| 606 | } |
| 607 | else |
| 608 | aw = fw->fw_adds; |
| 609 | |
| 610 | for ( ; ; aw = aw->aw_next) |
| 611 | { |
| 612 | if (aw == NULL) |
| 613 | { |
| 614 | /* At end of list: may also try additions without word chars. */ |
| 615 | if (naw == NULL) |
| 616 | break; |
| 617 | aw = naw; |
| 618 | naw = NULL; |
| 619 | } |
| 620 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 621 | if (aw->aw_leadlen > 0) |
| 622 | { |
| 623 | /* There is a leader, verify that it matches. */ |
| 624 | if (aw->aw_leadlen > mip->mi_word - mip->mi_line |
| 625 | || STRNCMP(mip->mi_word - aw->aw_leadlen, |
| 626 | aw->aw_word, aw->aw_leadlen) != 0) |
| 627 | continue; |
| 628 | if (mip->mi_word - aw->aw_leadlen > mip->mi_line) |
| 629 | { |
| 630 | /* There must not be a word character just before the |
| 631 | * leader. */ |
| 632 | p = mip->mi_word - aw->aw_leadlen; |
| 633 | mb_ptr_back(mip->mi_line, p); |
| 634 | if (spell_iswordc(p)) |
| 635 | continue; |
| 636 | } |
| 637 | /* Leader matches. Addition is rest of "aw_word". */ |
| 638 | p = aw->aw_word + aw->aw_leadlen; |
| 639 | } |
| 640 | else |
| 641 | /* No leader, use whole of "aw_word" for addition. */ |
| 642 | p = aw->aw_word; |
| 643 | |
| 644 | addlen = aw->aw_wordlen - aw->aw_leadlen; |
| 645 | if (addlen > 0) |
| 646 | { |
| 647 | /* Check for matching addition and no word character after it. |
| 648 | * First make sure we have enough case-folded chars to compare |
| 649 | * with. */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 650 | if (addlen >= mip->mi_faddlen) |
| 651 | fold_addchars(mip, addlen); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 652 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 653 | /* Put back the saved char, if needed. */ |
| 654 | if (aw->aw_saveb != NUL) |
| 655 | { |
| 656 | cp = p + STRLEN(p); |
| 657 | *cp = aw->aw_saveb; |
| 658 | } |
| 659 | n = STRNCMP(mip->mi_fword + mip->mi_fendlen, p, addlen); |
| 660 | if (aw->aw_saveb != NUL) |
| 661 | *cp = NUL; |
| 662 | |
| 663 | if (n != 0 || (mip->mi_fword[mip->mi_fendlen + addlen] != NUL |
| 664 | && spell_iswordc(mip->mi_fword + mip->mi_fendlen + addlen))) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 665 | continue; |
| 666 | |
| 667 | /* Compute the length in the original word, before case folding. */ |
| 668 | #ifdef FEAT_MBYTE |
| 669 | if (has_mbyte) |
| 670 | { |
| 671 | int l; |
| 672 | |
| 673 | p = mip->mi_end; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 674 | for (l = 0; l < addlen; l += (*mb_ptr2len_check)(mip->mi_fword |
| 675 | + mip->mi_fendlen + l)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 676 | mb_ptr_adv(p); |
| 677 | addlen = p - mip->mi_end; |
| 678 | } |
| 679 | #endif |
| 680 | |
| 681 | /* Check case of the addition. */ |
| 682 | if (!match_caps(ADD2BWF(aw->aw_flags), |
| 683 | aw->aw_word + aw->aw_wordlen + 1, mip, |
| 684 | mip->mi_end, mip->mi_end + addlen)) |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | /* Match! Use the new length if it's longer. */ |
| 689 | if (mip->mi_wend < mip->mi_end + addlen) |
| 690 | mip->mi_wend = mip->mi_end + addlen; |
| 691 | |
| 692 | valid = TRUE; |
| 693 | if (mip->mi_result != SP_OK) |
| 694 | { |
| 695 | if ((aw->aw_region & mip->mi_lp->lp_region) == 0) |
| 696 | mip->mi_result = SP_LOCAL; |
| 697 | else |
| 698 | mip->mi_result = SP_OK; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return valid; |
| 703 | } |
| 704 | |
| 705 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 706 | * Locate the text up to the next end-of-word after mip->mi_end. |
| 707 | */ |
| 708 | static void |
| 709 | fill_awend(mip) |
| 710 | matchinf_T *mip; |
| 711 | { |
| 712 | char_u *p = mip->mi_end; |
| 713 | int addlen = 0; |
| 714 | int find_word = TRUE; |
| 715 | |
| 716 | mip->mi_did_awend = TRUE; |
| 717 | if (mip->mi_faddlen == 0) |
| 718 | fold_addchars(mip, 0); /* need to fold first char */ |
| 719 | |
| 720 | /* 1: find_word == TRUE: skip over non-word characters after mi_end. |
| 721 | * 2: find_word == FALSE: skip over following word characters. */ |
| 722 | for (p = mip->mi_fword + mip->mi_fendlen; *p != NUL; mb_ptr_adv(p)) |
| 723 | { |
| 724 | if (spell_iswordc(p) == find_word) |
| 725 | { |
| 726 | if (!find_word) |
| 727 | break; /* done */ |
| 728 | find_word = !find_word; |
| 729 | } |
| 730 | #ifdef FEAT_MBYTE |
| 731 | addlen += (*mb_ptr2len_check)(p); |
| 732 | #else |
| 733 | ++addlen; |
| 734 | #endif |
| 735 | if (addlen >= mip->mi_faddlen) |
| 736 | fold_addchars(mip, addlen); /* need to fold more chars */ |
| 737 | } |
| 738 | |
| 739 | /* If there are extra chars store the result. */ |
| 740 | if (addlen != 0) |
| 741 | mip->mi_awend = p; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Fold enough characters of the checked text to be able to compare with an |
| 746 | * addition of length "addlen" plus one character (to be able to check the |
| 747 | * next character to be a non-word char). |
| 748 | * When there are not enough characters (end of line) mip->mi_faddlen will be |
| 749 | * smaller than "addlen". |
| 750 | */ |
| 751 | static void |
| 752 | fold_addchars(mip, addlen) |
| 753 | matchinf_T *mip; |
| 754 | int addlen; |
| 755 | { |
| 756 | int l; |
| 757 | char_u *p = mip->mi_fword + mip->mi_fendlen; |
| 758 | |
| 759 | while (mip->mi_faddlen <= addlen) |
| 760 | { |
| 761 | if (*mip->mi_fend == NUL) /* end of the line */ |
| 762 | { |
| 763 | p[mip->mi_faddlen] = NUL; |
| 764 | break; |
| 765 | } |
| 766 | #ifdef FEAT_MBYTE |
| 767 | if (has_mbyte) |
| 768 | l = (*mb_ptr2len_check)(mip->mi_fend); |
| 769 | else |
| 770 | #endif |
| 771 | l = 1; |
| 772 | (void)str_foldcase(mip->mi_fend, l, p + mip->mi_faddlen, |
| 773 | MAXWLEN - mip->mi_fendlen - mip->mi_faddlen); |
| 774 | mip->mi_fend += l; |
| 775 | mip->mi_faddlen += STRLEN(p + mip->mi_faddlen); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Return TRUE if affix "nr" appears in affix list "afflist[afflistlen]". |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 781 | */ |
| 782 | static int |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 783 | supports_affix(cnt, afflist, afflistlen, nr) |
| 784 | int cnt; /* total affix NR count */ |
| 785 | char_u *afflist; |
| 786 | int afflistlen; /* affix count in "afflist" */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 787 | int nr; |
| 788 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 789 | char_u *pc = afflist; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 790 | int i; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 791 | int nr_msb, nr_lsb; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 792 | |
| 793 | if (cnt <= 256) |
| 794 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 795 | /* one byte affix numbers */ |
| 796 | for (i = afflistlen; --i >= 0; ) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 797 | if (*pc++ == nr) |
| 798 | return TRUE; |
| 799 | } |
| 800 | else |
| 801 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 802 | /* two byte affix numbers, MSB first */ |
| 803 | nr_msb = (unsigned)nr >> 8; |
| 804 | nr_lsb = nr & 0xff; |
| 805 | for (i = afflistlen; --i >= 0; ) |
| 806 | { |
| 807 | if (*pc++ == nr_msb && *pc == nr_lsb) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 808 | return TRUE; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 809 | ++pc; |
| 810 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 811 | } |
| 812 | return FALSE; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Try finding a match for "mip->mi_cword" by removing prefixes. |
| 817 | */ |
| 818 | static int |
| 819 | prefix_match(mip) |
| 820 | matchinf_T *mip; |
| 821 | { |
| 822 | int len = 0; |
| 823 | int charlen = 0; |
| 824 | int cc; |
| 825 | affitem_T *ai; |
| 826 | char_u pword[MAXWLEN + 1]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 827 | hashtab_T *ht; |
| 828 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 829 | int found_valid = FALSE; |
| 830 | int cstart_charlen = 0; |
| 831 | char_u *cstart = mip->mi_word; |
| 832 | int capflags_save = mip->mi_capflags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * Check for prefixes with different character lengths. |
| 836 | * Start with zero length (only chop off). |
| 837 | */ |
| 838 | for (charlen = 0; charlen <= mip->mi_slang->sl_preftab.ga_len; ++charlen) |
| 839 | { |
| 840 | if (charlen > 0) |
| 841 | { |
| 842 | #ifdef FEAT_MBYTE |
| 843 | if (has_mbyte) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 844 | len += (*mb_ptr2len_check)(mip->mi_cword + len); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 845 | else |
| 846 | #endif |
| 847 | len += 1; |
| 848 | } |
| 849 | if (mip->mi_cword[len] == NUL) /* end of word, no prefix possible */ |
| 850 | break; |
| 851 | |
| 852 | if (charlen == 0) |
| 853 | ai = mip->mi_slang->sl_prefzero; |
| 854 | else |
| 855 | { |
| 856 | /* Get pointer to hashtab for prefix of this many chars. */ |
| 857 | ht = ((hashtab_T *)mip->mi_slang->sl_preftab.ga_data) + charlen - 1; |
| 858 | if (ht->ht_used == 0) |
| 859 | continue; |
| 860 | |
| 861 | cc = mip->mi_cword[len]; |
| 862 | mip->mi_cword[len] = NUL; |
| 863 | hi = hash_find(ht, mip->mi_cword); |
| 864 | mip->mi_cword[len] = cc; |
| 865 | |
| 866 | if (HASHITEM_EMPTY(hi)) |
| 867 | ai = NULL; |
| 868 | else |
| 869 | ai = HI2AI(hi); |
| 870 | } |
| 871 | |
| 872 | /* Loop over all matching prefixes. */ |
| 873 | for ( ; ai != NULL; ai = ai->ai_next) |
| 874 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 875 | /* Create the basic word from the chop string and the word after |
| 876 | * the matching add string. */ |
| 877 | mch_memmove(pword, ai->ai_add + ai->ai_addlen + 1, ai->ai_choplen); |
| 878 | mch_memmove(pword + ai->ai_choplen, mip->mi_cword + ai->ai_addlen, |
| 879 | mip->mi_fendlen - ai->ai_addlen); |
| 880 | pword[mip->mi_fendlen - ai->ai_addlen] = NUL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 881 | |
| 882 | /* Adjust the word start for case checks, we only check the |
| 883 | * part after the prefix. */ |
| 884 | while (cstart_charlen < charlen) |
| 885 | { |
| 886 | mb_ptr_adv(cstart); |
| 887 | ++cstart_charlen; |
| 888 | } |
| 889 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 890 | /* Find the word "pword", caseword "cstart". */ |
| 891 | found_valid |= noprefix_match(mip, pword, cstart, ai); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 892 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 893 | if (found_valid && mip->mi_result == SP_OK) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 894 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 895 | /* Found a valid word, no need to try other suffixes. */ |
| 896 | mip->mi_capflags = capflags_save; |
| 897 | return TRUE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | mip->mi_capflags = capflags_save; |
| 903 | return FALSE; |
| 904 | } |
| 905 | |
| 906 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 907 | * Check for matching word after removing a prefix. |
| 908 | * Return TRUE if found. |
| 909 | */ |
| 910 | static int |
| 911 | noprefix_match(mip, pword, cstart, ai) |
| 912 | matchinf_T *mip; |
| 913 | char_u *pword; /* case-folded word */ |
| 914 | char_u *cstart; /* original word after removed prefix */ |
| 915 | affitem_T *ai; /* the prefix item */ |
| 916 | { |
| 917 | hashitem_T *hi; |
| 918 | fword_T *fw; |
| 919 | int found_valid = FALSE; |
| 920 | char_u *word; |
| 921 | int i; |
| 922 | int fendlen; |
| 923 | |
| 924 | /* Removing the prefix may change the caps, e.g. for |
| 925 | * "deAlf" removing "de" makes it ONECAP. */ |
| 926 | mip->mi_capflags = captype(cstart, mip->mi_end); |
| 927 | |
| 928 | /* Find the basic word. */ |
| 929 | hi = hash_find(&mip->mi_slang->sl_words, pword); |
| 930 | if (!HASHITEM_EMPTY(hi)) |
| 931 | { |
| 932 | /* Check if the word supports this prefix. */ |
| 933 | for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next) |
| 934 | if (match_caps(fw->fw_flags, fw->fw_word, mip, |
| 935 | cstart, mip->mi_end)) |
| 936 | found_valid |= check_adds(mip, fw, ai->ai_nr, -1); |
| 937 | |
| 938 | if (found_valid && mip->mi_result == SP_OK) |
| 939 | /* Found a valid word, no need to try other suffixes. */ |
| 940 | return TRUE; |
| 941 | } |
| 942 | |
| 943 | /* No matching basic word without prefix. When combining is |
| 944 | * allowed try with suffixes. */ |
| 945 | if (ai->ai_flags & AFF_COMBINE) |
| 946 | { |
| 947 | /* Pass the word with prefix removed to suffix_match(). */ |
| 948 | mip->mi_cword = pword; |
| 949 | word = mip->mi_word; |
| 950 | mip->mi_word = cstart; |
| 951 | fendlen = mip->mi_fendlen; |
| 952 | mip->mi_fendlen = STRLEN(pword); |
| 953 | i = suffix_match(mip); |
| 954 | mip->mi_cword = mip->mi_fword; |
| 955 | mip->mi_word = word; |
| 956 | mip->mi_fendlen = fendlen; |
| 957 | if (i) |
| 958 | return TRUE; |
| 959 | } |
| 960 | |
| 961 | return FALSE; |
| 962 | } |
| 963 | |
| 964 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 965 | * Try finding a match for "mip->mi_cword" by removing suffixes. |
| 966 | */ |
| 967 | static int |
| 968 | suffix_match(mip) |
| 969 | matchinf_T *mip; |
| 970 | { |
| 971 | char_u *sufp; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 972 | char_u *endw = mip->mi_cword + mip->mi_fendlen; |
| 973 | int endw_c = *endw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 974 | int charlen; |
| 975 | affitem_T *ai; |
| 976 | char_u pword[MAXWLEN + 1]; |
| 977 | fword_T *fw; |
| 978 | hashtab_T *ht; |
| 979 | hashitem_T *hi; |
| 980 | int tlen; |
| 981 | int cend_charlen = 0; |
| 982 | char_u *cend = mip->mi_end; |
| 983 | int found_valid = FALSE; |
| 984 | int capflags_save = mip->mi_capflags; |
| 985 | |
| 986 | /* |
| 987 | * Try suffixes of different length, starting with an empty suffix (chop |
| 988 | * only, thus adds something). |
| 989 | * Stop checking if there are no suffixes with so many characters. |
| 990 | */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 991 | sufp = endw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 992 | for (charlen = 0; charlen <= mip->mi_slang->sl_sufftab.ga_len; ++charlen) |
| 993 | { |
| 994 | /* Move the pointer to the possible suffix back one character, unless |
| 995 | * doing the first round (empty suffix). */ |
| 996 | if (charlen > 0) |
| 997 | { |
| 998 | mb_ptr_back(mip->mi_cword, sufp); |
| 999 | if (sufp <= mip->mi_cword) /* start of word, no suffix possible */ |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | if (charlen == 0) |
| 1004 | ai = mip->mi_slang->sl_suffzero; |
| 1005 | else |
| 1006 | { |
| 1007 | /* Get pointer to hashtab for suffix of this many chars. */ |
| 1008 | ht = ((hashtab_T *)mip->mi_slang->sl_sufftab.ga_data) + charlen - 1; |
| 1009 | if (ht->ht_used == 0) |
| 1010 | continue; |
| 1011 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1012 | *endw = NUL; /* truncate after possible suffix */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1013 | hi = hash_find(ht, sufp); |
| 1014 | if (HASHITEM_EMPTY(hi)) |
| 1015 | ai = NULL; |
| 1016 | else |
| 1017 | ai = HI2AI(hi); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1018 | *endw = endw_c; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | if (ai != NULL) |
| 1022 | { |
| 1023 | /* Found a list of matching suffixes. Now check that there is one |
| 1024 | * we can use. */ |
| 1025 | tlen = sufp - mip->mi_cword; /* length of word without suffix */ |
| 1026 | mch_memmove(pword, mip->mi_cword, tlen); |
| 1027 | |
| 1028 | for ( ; ai != NULL; ai = ai->ai_next) |
| 1029 | { |
| 1030 | /* Found a matching suffix. Create the basic word by removing |
| 1031 | * the suffix and adding the chop string. */ |
| 1032 | if (ai->ai_choplen == 0) |
| 1033 | pword[tlen] = NUL; |
| 1034 | else |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1035 | mch_memmove(pword + tlen, ai->ai_add + ai->ai_addlen + 1, |
| 1036 | ai->ai_choplen + 1); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1037 | |
| 1038 | /* Find the basic word. */ |
| 1039 | hi = hash_find(&mip->mi_slang->sl_words, pword); |
| 1040 | if (!HASHITEM_EMPTY(hi)) |
| 1041 | { |
| 1042 | /* Adjust the end for case checks, we only check the part |
| 1043 | * before the suffix. */ |
| 1044 | while (cend_charlen < charlen) |
| 1045 | { |
| 1046 | mb_ptr_back(mip->mi_word, cend); |
| 1047 | ++cend_charlen; |
| 1048 | } |
| 1049 | |
| 1050 | /* Removing the suffix may change the caps, e.g. for |
| 1051 | * "UFOs" removing 's' makes it ALLCAP. */ |
| 1052 | mip->mi_capflags = captype(mip->mi_word, cend); |
| 1053 | |
| 1054 | /* Check if the word supports this suffix. */ |
| 1055 | for (fw = HI2FWORD(hi); fw != NULL; fw = fw->fw_next) |
| 1056 | if (match_caps(fw->fw_flags, fw->fw_word, mip, |
| 1057 | mip->mi_word, cend)) |
| 1058 | found_valid |= check_adds(mip, fw, -1, ai->ai_nr); |
| 1059 | |
| 1060 | if (found_valid && mip->mi_result == SP_OK) |
| 1061 | { |
| 1062 | /* Found a valid word, no need to try other suffixes. */ |
| 1063 | mip->mi_capflags = capflags_save; |
| 1064 | return TRUE; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | mip->mi_capflags = capflags_save; |
| 1072 | return FALSE; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Return TRUE if case of "cword" meets the requirements of case flags |
| 1077 | * "flags". |
| 1078 | */ |
| 1079 | static int |
| 1080 | match_caps(flags, caseword, mip, cword, end) |
| 1081 | int flags; /* flags required by basic word or addition */ |
| 1082 | char_u *caseword; /* word with case as required */ |
| 1083 | matchinf_T *mip; |
| 1084 | char_u *cword; /* word to compare against "caseword" */ |
| 1085 | char_u *end; /* end of "cword" */ |
| 1086 | { |
| 1087 | char_u *p; |
| 1088 | int c; |
| 1089 | int len; |
| 1090 | int capflags = mip->mi_capflags; /* flags of checked word */ |
| 1091 | int past_second; |
| 1092 | |
| 1093 | if ((capflags & BWF_KEEPCAP) == 0 && end > mip->mi_end) |
| 1094 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1095 | /* If "end" is past "mip->mi_end" we need to adjust the caps type for |
| 1096 | * characters after the basic word. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1097 | #ifdef FEAT_MBYTE |
| 1098 | past_second = (mip->mi_word + (*mb_ptr2len_check)(mip->mi_word) |
| 1099 | < mip->mi_end); |
| 1100 | #else |
| 1101 | past_second = mip->mi_word + 1 < mip->mi_end; |
| 1102 | #endif |
| 1103 | for (p = mip->mi_end; p < end; ) |
| 1104 | { |
| 1105 | if (!spell_iswordc(p)) |
| 1106 | mb_ptr_adv(p); |
| 1107 | else |
| 1108 | { |
| 1109 | #ifdef FEAT_MBYTE |
| 1110 | if (has_mbyte) |
| 1111 | c = mb_ptr2char_adv(&p); |
| 1112 | else |
| 1113 | #endif |
| 1114 | c = *p++; |
| 1115 | if (MB_ISUPPER(c)) |
| 1116 | { |
| 1117 | if (capflags == 0 || (capflags & BWF_ONECAP)) |
| 1118 | { |
| 1119 | capflags = BWF_KEEPCAP; /* lU or UlU */ |
| 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | else |
| 1124 | { |
| 1125 | if (capflags & BWF_ALLCAP) |
| 1126 | { |
| 1127 | if (past_second) |
| 1128 | { |
| 1129 | capflags = BWF_KEEPCAP; /* UUl */ |
| 1130 | break; |
| 1131 | } |
| 1132 | capflags = BWF_ONECAP; /* Uu */ |
| 1133 | } |
| 1134 | } |
| 1135 | past_second = TRUE; |
| 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | if (capflags == BWF_ALLCAP) |
| 1141 | return TRUE; /* All caps is always OK. */ |
| 1142 | |
| 1143 | if (flags & BWF_KEEPCAP) |
| 1144 | { |
| 1145 | len = STRLEN(caseword); |
| 1146 | return (len == end - cword && STRNCMP(caseword, cword, len) == 0); |
| 1147 | } |
| 1148 | |
| 1149 | if (flags & BWF_ALLCAP) |
| 1150 | return FALSE; /* need ALLCAP, already checked above */ |
| 1151 | |
| 1152 | if (flags & BWF_ONECAP) |
| 1153 | return capflags == BWF_ONECAP; |
| 1154 | |
| 1155 | return capflags != BWF_KEEPCAP; /* no case check, only KEEPCAP is bad */ |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Move to next spell error. |
| 1160 | * Return OK if found, FAIL otherwise. |
| 1161 | */ |
| 1162 | int |
| 1163 | spell_move_to(dir, allwords) |
| 1164 | int dir; /* FORWARD or BACKWARD */ |
| 1165 | int allwords; /* TRUE for "[s" and "]s" */ |
| 1166 | { |
| 1167 | pos_T pos; |
| 1168 | char_u *line; |
| 1169 | char_u *p; |
| 1170 | int wc; |
| 1171 | int nwc; |
| 1172 | int attr = 0; |
| 1173 | int len; |
| 1174 | |
| 1175 | if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL) |
| 1176 | { |
| 1177 | EMSG(_("E756: Spell checking not enabled")); |
| 1178 | return FAIL; |
| 1179 | } |
| 1180 | |
| 1181 | /* TODO: moving backwards */ |
| 1182 | |
| 1183 | /* Start looking for bad word at the start of the line, because we can't |
| 1184 | * start halfway a word and know where it ends. */ |
| 1185 | pos = curwin->w_cursor; |
| 1186 | pos.col = 0; |
| 1187 | wc = FALSE; |
| 1188 | |
| 1189 | while (!got_int) |
| 1190 | { |
| 1191 | line = ml_get(pos.lnum); |
| 1192 | p = line + pos.col; |
| 1193 | while (*p != NUL) |
| 1194 | { |
| 1195 | nwc = spell_iswordc(p); |
| 1196 | if (!wc && nwc) |
| 1197 | { |
| 1198 | /* start of word */ |
| 1199 | /* TODO: check for bad word attr */ |
| 1200 | len = spell_check(curwin, line, p, &attr); |
| 1201 | if (attr != 0) |
| 1202 | { |
| 1203 | if (curwin->w_cursor.lnum < pos.lnum |
| 1204 | || (curwin->w_cursor.lnum == pos.lnum |
| 1205 | && curwin->w_cursor.col < (colnr_T)(p - line))) |
| 1206 | { |
| 1207 | curwin->w_cursor.lnum = pos.lnum; |
| 1208 | curwin->w_cursor.col = p - line; |
| 1209 | return OK; |
| 1210 | } |
| 1211 | attr = 0; /* bad word is before or at cursor */ |
| 1212 | } |
| 1213 | p += len; |
| 1214 | if (*p == NUL) |
| 1215 | break; |
| 1216 | nwc = FALSE; |
| 1217 | } |
| 1218 | |
| 1219 | /* advance to next character */ |
| 1220 | mb_ptr_adv(p); |
| 1221 | wc = nwc; |
| 1222 | } |
| 1223 | |
| 1224 | /* Advance to next line. */ |
| 1225 | if (pos.lnum == curbuf->b_ml.ml_line_count) |
| 1226 | return FAIL; |
| 1227 | ++pos.lnum; |
| 1228 | pos.col = 0; |
| 1229 | wc = FALSE; |
| 1230 | |
| 1231 | line_breakcheck(); |
| 1232 | } |
| 1233 | |
| 1234 | return FAIL; /* interrupted */ |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * Load word list for "lang" from a Vim spell file. |
| 1239 | * "lang" must be the language without the region: "en" or "en-rare". |
| 1240 | */ |
| 1241 | static slang_T * |
| 1242 | spell_load_lang(lang) |
| 1243 | char_u *lang; |
| 1244 | { |
| 1245 | slang_T *lp; |
| 1246 | char_u fname_enc[80]; |
| 1247 | char_u *p; |
| 1248 | int r; |
| 1249 | |
| 1250 | lp = slang_alloc(lang); |
| 1251 | if (lp != NULL) |
| 1252 | { |
| 1253 | /* Find all spell files for "lang" in 'runtimepath' and load them. |
| 1254 | * Use 'encoding', except that we use "latin1" for "latin9". */ |
| 1255 | #ifdef FEAT_MBYTE |
| 1256 | if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) |
| 1257 | p = p_enc; |
| 1258 | else |
| 1259 | #endif |
| 1260 | p = (char_u *)"latin1"; |
| 1261 | sprintf((char *)fname_enc, "spell/%s.%s.spl", lang, p); |
| 1262 | |
| 1263 | r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1264 | if (r == FAIL && !lp->sl_error) |
| 1265 | { |
| 1266 | /* Try loading the ASCII version. */ |
| 1267 | sprintf((char *)fname_enc, "spell/%s.ascii.spl", lang); |
| 1268 | |
| 1269 | r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, lp); |
| 1270 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1271 | if (r == FAIL || lp->sl_error) |
| 1272 | { |
| 1273 | slang_free(lp); |
| 1274 | lp = NULL; |
| 1275 | if (r == FAIL) |
| 1276 | smsg((char_u *)_("Warning: Cannot find word list \"%s\""), |
| 1277 | fname_enc + 6); |
| 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | lp->sl_next = first_lang; |
| 1282 | first_lang = lp; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return lp; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Allocate a new slang_T. |
| 1291 | * Caller must fill "sl_next". |
| 1292 | */ |
| 1293 | static slang_T * |
| 1294 | slang_alloc(lang) |
| 1295 | char_u *lang; |
| 1296 | { |
| 1297 | slang_T *lp; |
| 1298 | |
| 1299 | lp = (slang_T *)alloc(sizeof(slang_T)); |
| 1300 | if (lp != NULL) |
| 1301 | { |
| 1302 | lp->sl_name = vim_strsave(lang); |
| 1303 | hash_init(&lp->sl_words); |
| 1304 | ga_init2(&lp->sl_preftab, sizeof(hashtab_T), 4); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1305 | hash_init(&lp->sl_prewords); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1306 | ga_init2(&lp->sl_sufftab, sizeof(hashtab_T), 4); |
| 1307 | lp->sl_prefzero = NULL; |
| 1308 | lp->sl_suffzero = NULL; |
| 1309 | lp->sl_try = NULL; |
| 1310 | ga_init2(&lp->sl_rep, sizeof(repentry_T), 4); |
| 1311 | lp->sl_regions[0] = NUL; |
| 1312 | lp->sl_block = NULL; |
| 1313 | lp->sl_error = FALSE; |
| 1314 | } |
| 1315 | return lp; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Free the contents of an slang_T and the structure itself. |
| 1320 | */ |
| 1321 | static void |
| 1322 | slang_free(lp) |
| 1323 | slang_T *lp; |
| 1324 | { |
| 1325 | sblock_T *sp; |
| 1326 | int i; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1327 | fword_T *fw; |
| 1328 | int todo; |
| 1329 | hashitem_T *hi; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1330 | |
| 1331 | vim_free(lp->sl_name); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1332 | |
| 1333 | /* The words themselves are in memory blocks referenced by "sl_block". |
| 1334 | * Only the hashtables for additions need to be cleared. */ |
| 1335 | todo = lp->sl_words.ht_used; |
| 1336 | for (hi = lp->sl_words.ht_array; todo > 0; ++hi) |
| 1337 | { |
| 1338 | if (!HASHITEM_EMPTY(hi)) |
| 1339 | { |
| 1340 | --todo; |
| 1341 | fw = HI2FWORD(hi); |
| 1342 | if (fw->fw_flags & BWF_ADDHASH) |
| 1343 | hash_clear((hashtab_T *)fw->fw_adds); |
| 1344 | } |
| 1345 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1346 | hash_clear(&lp->sl_words); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1347 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1348 | for (i = 0; i < lp->sl_preftab.ga_len; ++i) |
| 1349 | hash_clear(((hashtab_T *)lp->sl_preftab.ga_data) + i); |
| 1350 | ga_clear(&lp->sl_preftab); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1351 | hash_clear(&lp->sl_prewords); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1352 | for (i = 0; i < lp->sl_sufftab.ga_len; ++i) |
| 1353 | hash_clear(((hashtab_T *)lp->sl_sufftab.ga_data) + i); |
| 1354 | ga_clear(&lp->sl_sufftab); |
| 1355 | ga_clear(&lp->sl_rep); |
| 1356 | vim_free(lp->sl_try); |
| 1357 | while (lp->sl_block != NULL) |
| 1358 | { |
| 1359 | sp = lp->sl_block; |
| 1360 | lp->sl_block = sp->sb_next; |
| 1361 | vim_free(sp); |
| 1362 | } |
| 1363 | vim_free(lp); |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * Load one spell file into an slang_T. |
| 1368 | * Invoked through do_in_runtimepath(). |
| 1369 | */ |
| 1370 | static void |
| 1371 | spell_load_file(fname, cookie) |
| 1372 | char_u *fname; |
| 1373 | void *cookie; /* points to the slang_T to be filled */ |
| 1374 | { |
| 1375 | slang_T *lp = cookie; |
| 1376 | FILE *fd; |
| 1377 | char_u buf[MAXWLEN + 1]; |
| 1378 | char_u cbuf[MAXWLEN + 1]; |
| 1379 | char_u fbuf[MAXWLEN + 1]; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1380 | char_u affixbuf[256 * 2 * 2]; /* max 2 * 256 affix nrs of 2 bytes */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1381 | char_u *p; |
| 1382 | int itm; |
| 1383 | int i; |
| 1384 | int affcount; |
| 1385 | int affnr; |
| 1386 | int affflags; |
| 1387 | int affitemcnt; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1388 | int prefixcnt, suffixcnt; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1389 | int bl_used = SBLOCKSIZE; |
| 1390 | int widx; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1391 | int prefm = 0; /* 1 if <= 256 prefixes, sizeof(short_u) otherw. */ |
| 1392 | int suffm = 0; /* 1 if <= 256 suffixes, sizeof(short_u) otherw. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1393 | int wlen; |
| 1394 | int flags; |
| 1395 | affitem_T *ai, *ai2, **aip; |
| 1396 | int round; |
| 1397 | char_u *save_sourcing_name = sourcing_name; |
| 1398 | linenr_T save_sourcing_lnum = sourcing_lnum; |
| 1399 | int cnt; |
| 1400 | int choplen; |
| 1401 | int addlen; |
| 1402 | int leadlen; |
| 1403 | int wordcount; |
| 1404 | fword_T *fw, *fw2; |
| 1405 | garray_T *gap; |
| 1406 | hashtab_T *ht; |
| 1407 | hashitem_T *hi; |
| 1408 | hash_T hash; |
| 1409 | int adds; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1410 | addword_T *aw, *naw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1411 | int flen; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1412 | int xlen; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1413 | |
| 1414 | fd = fopen((char *)fname, "r"); |
| 1415 | if (fd == NULL) |
| 1416 | { |
| 1417 | EMSG2(_(e_notopen), fname); |
| 1418 | goto errorend; |
| 1419 | } |
| 1420 | |
| 1421 | /* Set sourcing_name, so that error messages mention the file name. */ |
| 1422 | sourcing_name = fname; |
| 1423 | sourcing_lnum = 0; |
| 1424 | |
| 1425 | /* <HEADER>: <fileID> <regioncnt> <regionname> ... */ |
| 1426 | for (i = 0; i < VIMSPELLMAGICL; ++i) |
| 1427 | buf[i] = getc(fd); /* <fileID> */ |
| 1428 | if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0) |
| 1429 | { |
| 1430 | EMSG(_("E757: Wrong file ID in spell file")); |
| 1431 | goto errorend; |
| 1432 | } |
| 1433 | |
| 1434 | cnt = getc(fd); /* <regioncnt> */ |
| 1435 | if (cnt == EOF) |
| 1436 | { |
| 1437 | truncerr: |
| 1438 | EMSG(_("E758: Truncated spell file")); |
| 1439 | goto errorend; |
| 1440 | } |
| 1441 | if (cnt > 8) |
| 1442 | { |
| 1443 | formerr: |
| 1444 | EMSG(_("E759: Format error in spell file")); |
| 1445 | goto errorend; |
| 1446 | } |
| 1447 | for (i = 0; i < cnt; ++i) |
| 1448 | { |
| 1449 | lp->sl_regions[i * 2] = getc(fd); /* <regionname> */ |
| 1450 | lp->sl_regions[i * 2 + 1] = getc(fd); |
| 1451 | } |
| 1452 | lp->sl_regions[cnt * 2] = NUL; |
| 1453 | |
| 1454 | /* round 1: <PREFIXLIST>: <affcount> <afftotcnt> <affix> ... |
| 1455 | * round 2: <SUFFIXLIST>: <affcount> <afftotcnt> <affix> ... */ |
| 1456 | for (round = 1; round <= 2; ++round) |
| 1457 | { |
| 1458 | affcount = (getc(fd) << 8) + getc(fd); /* <affcount> */ |
| 1459 | if (affcount < 0) |
| 1460 | goto truncerr; |
| 1461 | if (round == 1) |
| 1462 | { |
| 1463 | gap = &lp->sl_preftab; |
| 1464 | aip = &lp->sl_prefzero; |
| 1465 | lp->sl_prefcnt = affcount; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1466 | prefm = affcount > 256 ? 2 : 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1467 | } |
| 1468 | else |
| 1469 | { |
| 1470 | gap = &lp->sl_sufftab; |
| 1471 | aip = &lp->sl_suffzero; |
| 1472 | lp->sl_suffcnt = affcount; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1473 | suffm = affcount > 256 ? 2 : 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | i = (getc(fd) << 8) + getc(fd); /* <afftotcnt> */ |
| 1477 | /* afftotcnt is not used */ |
| 1478 | |
| 1479 | /* |
| 1480 | * For each affix NR there can be several affixes. |
| 1481 | */ |
| 1482 | for (affnr = 0; affnr < affcount; ++affnr) |
| 1483 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1484 | /* <affix>: <affitemcnt> <affitem> ... */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1485 | affitemcnt = (getc(fd) << 8) + getc(fd); /* <affitemcnt> */ |
| 1486 | if (affitemcnt < 0) |
| 1487 | goto truncerr; |
| 1488 | for (itm = 0; itm < affitemcnt; ++itm) |
| 1489 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1490 | /* <affitem>: <affflags> <affchoplen> <affchop> |
| 1491 | * <affaddlen> <affadd> */ |
| 1492 | affflags = getc(fd); /* <affflags> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1493 | choplen = getc(fd); /* <affchoplen> */ |
| 1494 | if (choplen == EOF) |
| 1495 | goto truncerr; |
| 1496 | if (choplen >= MAXWLEN) |
| 1497 | goto formerr; |
| 1498 | for (i = 0; i < choplen; ++i) /* <affchop> */ |
| 1499 | buf[i] = getc(fd); |
| 1500 | buf[i] = NUL; |
| 1501 | addlen = getc(fd); /* <affaddlen> */ |
| 1502 | if (addlen == EOF) |
| 1503 | goto truncerr; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1504 | if (affflags & AFF_PREWORD) |
| 1505 | xlen = addlen + 2; /* space for lead and trail string */ |
| 1506 | else |
| 1507 | xlen = 0; |
| 1508 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1509 | /* Get room to store the affitem_T, chop and add strings. */ |
| 1510 | p = (char_u *)getroom(lp, &bl_used, |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1511 | sizeof(affitem_T) + addlen + choplen + 1 + xlen); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1512 | if (p == NULL) |
| 1513 | goto errorend; |
| 1514 | |
| 1515 | ai = (affitem_T *)p; |
| 1516 | ai->ai_nr = affnr; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1517 | ai->ai_flags = affflags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1518 | ai->ai_choplen = choplen; |
| 1519 | ai->ai_addlen = addlen; |
| 1520 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1521 | /* Chop string is at ai_add[ai_addlen + 1]. */ |
| 1522 | p = ai->ai_add + addlen + 1; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1523 | STRCPY(p, buf); |
| 1524 | |
| 1525 | p = ai->ai_add; |
| 1526 | for (i = 0; i < addlen; ++i) /* <affadd> */ |
| 1527 | p[i] = getc(fd); |
| 1528 | p[i] = NUL; |
| 1529 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1530 | if (affflags & AFF_PREWORD) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1531 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1532 | int l, leadoff, trailoff; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1533 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1534 | /* |
| 1535 | * Separate lead and trail string, put word at ai_add, so |
| 1536 | * that it can be used as hashtable key. |
| 1537 | */ |
| 1538 | /* lead string: up to first word char */ |
| 1539 | while (*p != NUL && !spell_iswordc(p)) |
| 1540 | mb_ptr_adv(p); |
| 1541 | ai->ai_leadlen = p - ai->ai_add; |
| 1542 | leadoff = addlen + choplen + 2; |
| 1543 | mch_memmove(ai->ai_add + leadoff, ai->ai_add, |
| 1544 | ai->ai_leadlen); |
| 1545 | ai->ai_add[leadoff + ai->ai_leadlen] = NUL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1546 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1547 | /* trail string: after last word char */ |
| 1548 | while (*p != NUL && spell_iswordc(p)) |
| 1549 | mb_ptr_adv(p); |
| 1550 | trailoff = leadoff + ai->ai_leadlen + 1; |
| 1551 | STRCPY(ai->ai_add + trailoff, p); |
| 1552 | ai->ai_taillen = STRLEN(p); |
| 1553 | |
| 1554 | /* word itself */ |
| 1555 | l = (p - ai->ai_add) - ai->ai_leadlen; |
| 1556 | mch_memmove(ai->ai_add, ai->ai_add + ai->ai_leadlen, l); |
| 1557 | ai->ai_add[l] = NUL; |
| 1558 | hash = hash_hash(ai->ai_add); |
| 1559 | hi = hash_lookup(&lp->sl_prewords, ai->ai_add, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1560 | if (HASHITEM_EMPTY(hi)) |
| 1561 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1562 | /* First affix with this word, add to hashtable. */ |
| 1563 | hash_add_item(&lp->sl_prewords, hi, ai->ai_add, hash); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1564 | ai->ai_next = NULL; |
| 1565 | } |
| 1566 | else |
| 1567 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1568 | /* There already is an affix with this word, link in |
| 1569 | * the list. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1570 | ai2 = HI2AI(hi); |
| 1571 | ai->ai_next = ai2->ai_next; |
| 1572 | ai2->ai_next = ai; |
| 1573 | } |
| 1574 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1575 | else |
| 1576 | { |
| 1577 | /* |
| 1578 | * Add the affix to a hashtable. Which one depends on the |
| 1579 | * length of the added string in characters. |
| 1580 | */ |
| 1581 | #ifdef FEAT_MBYTE |
| 1582 | /* Change "addlen" from length in bytes to length in |
| 1583 | * chars. */ |
| 1584 | if (has_mbyte) |
| 1585 | addlen = mb_charlen(p); |
| 1586 | #endif |
| 1587 | if (addlen == 0) |
| 1588 | { |
| 1589 | /* Link in list of zero length affixes. */ |
| 1590 | ai->ai_next = *aip; |
| 1591 | *aip = ai; |
| 1592 | } |
| 1593 | else |
| 1594 | { |
| 1595 | if (gap->ga_len < addlen) |
| 1596 | { |
| 1597 | /* Longer affix, need more hashtables. */ |
| 1598 | if (ga_grow(gap, addlen - gap->ga_len) == FAIL) |
| 1599 | goto errorend; |
| 1600 | |
| 1601 | /* Re-allocating ga_data means that an ht_array |
| 1602 | * pointing to ht_smallarray becomes invalid. We |
| 1603 | * can recognize this: ht_mask is at its init |
| 1604 | * value. */ |
| 1605 | for (i = 0; i < gap->ga_len; ++i) |
| 1606 | { |
| 1607 | ht = ((hashtab_T *)gap->ga_data) + i; |
| 1608 | if (ht->ht_mask == HT_INIT_SIZE - 1) |
| 1609 | ht->ht_array = ht->ht_smallarray; |
| 1610 | } |
| 1611 | |
| 1612 | /* Init the newly used hashtable(s). */ |
| 1613 | while (gap->ga_len < addlen) |
| 1614 | { |
| 1615 | hash_init(((hashtab_T *)gap->ga_data) |
| 1616 | + gap->ga_len); |
| 1617 | ++gap->ga_len; |
| 1618 | } |
| 1619 | } |
| 1620 | ht = ((hashtab_T *)gap->ga_data) + addlen - 1; |
| 1621 | hash = hash_hash(p); |
| 1622 | hi = hash_lookup(ht, p, hash); |
| 1623 | if (HASHITEM_EMPTY(hi)) |
| 1624 | { |
| 1625 | /* First affix with this "ai_add", add to |
| 1626 | * hashtable. */ |
| 1627 | hash_add_item(ht, hi, p, hash); |
| 1628 | ai->ai_next = NULL; |
| 1629 | } |
| 1630 | else |
| 1631 | { |
| 1632 | /* There already is an affix with this "ai_add", |
| 1633 | * link in the list. */ |
| 1634 | ai2 = HI2AI(hi); |
| 1635 | ai->ai_next = ai2->ai_next; |
| 1636 | ai2->ai_next = ai; |
| 1637 | } |
| 1638 | } |
| 1639 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /* <SUGGEST> : <suggestlen> <more> ... */ |
| 1645 | /* TODO, just skip this for now */ |
| 1646 | i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd); |
| 1647 | while (i-- > 0) |
| 1648 | if (getc(fd) == EOF) /* <suggestlen> */ |
| 1649 | goto truncerr; |
| 1650 | |
| 1651 | /* <WORDLIST>: <wordcount> <worditem> ... */ /* <wordcount> */ |
| 1652 | wordcount = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) |
| 1653 | + getc(fd); |
| 1654 | if (wordcount < 0) |
| 1655 | goto truncerr; |
| 1656 | |
| 1657 | /* Init hashtable for this number of words, so that it doesn't need to |
| 1658 | * reallocate the table halfway. */ |
| 1659 | hash_lock_size(&lp->sl_words, wordcount); |
| 1660 | |
| 1661 | for (widx = 0; ; ++widx) |
| 1662 | { |
| 1663 | /* <worditem>: <nr> <string> <flags> [<flags2>] |
| 1664 | * [<caselen> <caseword>] |
| 1665 | * [<affixcnt> <affixNR> ...] (prefixes) |
| 1666 | * [<affixcnt> <affixNR> ...] (suffixes) |
| 1667 | * [<region>] |
| 1668 | * [<addcnt> <add> ...] |
| 1669 | */ |
| 1670 | /* Use <nr> bytes from the previous word. */ |
| 1671 | wlen = getc(fd); /* <nr> */ |
| 1672 | if (wlen == EOF) |
| 1673 | { |
| 1674 | if (widx >= wordcount) /* normal way to end the file */ |
| 1675 | break; |
| 1676 | goto truncerr; |
| 1677 | } |
| 1678 | |
| 1679 | /* Read further word bytes until one below 0x20, that must be the |
| 1680 | * flags. Keep this fast! */ |
| 1681 | for (;;) |
| 1682 | { |
| 1683 | if ((buf[wlen] = getc(fd)) < 0x20) /* <string> */ |
| 1684 | break; |
| 1685 | if (++wlen == MAXWLEN) |
| 1686 | goto formerr; |
| 1687 | } |
| 1688 | flags = buf[wlen]; /* <flags> */ |
| 1689 | buf[wlen] = NUL; |
| 1690 | |
| 1691 | /* Get more flags if they're there. */ |
| 1692 | if (flags & BWF_SECOND) |
| 1693 | flags += getc(fd) << 8; /* <flags2> */ |
| 1694 | |
| 1695 | if (flags & BWF_KEEPCAP) |
| 1696 | { |
| 1697 | /* Read <caselen> and <caseword> first, its length may differ from |
| 1698 | * the case-folded word. Note: this should only happen after the |
| 1699 | * basic word! */ |
| 1700 | wlen = getc(fd); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1701 | if (wlen < 0) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1702 | goto truncerr; |
| 1703 | for (i = 0; i < wlen; ++i) |
| 1704 | cbuf[i] = getc(fd); |
| 1705 | cbuf[i] = NUL; |
| 1706 | } |
| 1707 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1708 | /* Optional prefixes */ |
| 1709 | p = affixbuf; |
| 1710 | if (flags & BWF_PREFIX) |
| 1711 | { |
| 1712 | cnt = getc(fd); /* <affixcnt> */ |
| 1713 | if (cnt < 0) |
| 1714 | goto truncerr; |
| 1715 | prefixcnt = cnt; |
| 1716 | for (i = cnt * prefm; --i >= 0; ) /* <affixNR> */ |
| 1717 | *p++ = getc(fd); |
| 1718 | } |
| 1719 | else |
| 1720 | prefixcnt = 0; |
| 1721 | |
| 1722 | /* Optional suffixes */ |
| 1723 | if (flags & BWF_SUFFIX) |
| 1724 | { |
| 1725 | cnt = getc(fd); /* <affixcnt> */ |
| 1726 | if (cnt < 0) |
| 1727 | goto truncerr; |
| 1728 | suffixcnt = cnt; |
| 1729 | for (i = cnt * suffm; --i >= 0; ) /* <affixNR> */ |
| 1730 | *p++ = getc(fd); |
| 1731 | } |
| 1732 | else |
| 1733 | suffixcnt = 0; |
| 1734 | |
| 1735 | /* Find room to store the word in an fword_T. */ |
| 1736 | fw = (fword_T *)getroom(lp, &bl_used, (int)sizeof(fword_T) + wlen |
| 1737 | + (p - affixbuf)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1738 | if (fw == NULL) |
| 1739 | goto errorend; |
| 1740 | mch_memmove(fw->fw_word, (flags & BWF_KEEPCAP) ? cbuf : buf, wlen + 1); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1741 | |
| 1742 | /* Put the affix NRs just after the word, if any. */ |
| 1743 | if (p > affixbuf) |
| 1744 | mch_memmove(fw->fw_word + wlen + 1, affixbuf, p - affixbuf); |
| 1745 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1746 | fw->fw_flags = flags; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1747 | fw->fw_prefixcnt = prefixcnt; |
| 1748 | fw->fw_suffixcnt = suffixcnt; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1749 | |
| 1750 | hash = hash_hash(buf); |
| 1751 | hi = hash_lookup(&lp->sl_words, buf, hash); |
| 1752 | if (HASHITEM_EMPTY(hi)) |
| 1753 | { |
| 1754 | if (hash_add_item(&lp->sl_words, hi, fw->fw_word, hash) == FAIL) |
| 1755 | goto errorend; |
| 1756 | fw->fw_next = NULL; |
| 1757 | } |
| 1758 | else |
| 1759 | { |
| 1760 | /* Already have this basic word in the hashtable, this one will |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1761 | * have different case flags and/or affixes. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1762 | fw2 = HI2FWORD(hi); |
| 1763 | fw->fw_next = fw2->fw_next; |
| 1764 | fw2->fw_next = fw; |
| 1765 | --widx; /* don't count this one */ |
| 1766 | } |
| 1767 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1768 | if (flags & BWF_REGION) |
| 1769 | fw->fw_region = getc(fd); /* <region> */ |
| 1770 | else |
| 1771 | fw->fw_region = REGION_ALL; |
| 1772 | |
| 1773 | fw->fw_adds = NULL; |
| 1774 | if (flags & BWF_ADDS) |
| 1775 | { |
| 1776 | adds = (getc(fd) << 8) + getc(fd); /* <addcnt> */ |
| 1777 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1778 | if (adds > 30) |
| 1779 | { |
| 1780 | /* Use a hashtable to loopup the part until the next word end. |
| 1781 | * This uses more memory and involves some overhead, thus only |
| 1782 | * do it when there are many additions (e.g., for French). */ |
| 1783 | ht = (hashtab_T *)getroom(lp, &bl_used, sizeof(hashtab_T)); |
| 1784 | if (ht == NULL) |
| 1785 | goto errorend; |
| 1786 | hash_init(ht); |
| 1787 | fw->fw_adds = (addword_T *)ht; |
| 1788 | fw->fw_flags |= BWF_ADDHASH; |
| 1789 | |
| 1790 | /* Preset the size of the hashtable. It's never unlocked. */ |
| 1791 | hash_lock_size(ht, adds + 1); |
| 1792 | } |
| 1793 | else |
| 1794 | ht = NULL; |
| 1795 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1796 | while (--adds >= 0) |
| 1797 | { |
| 1798 | /* <add>: <addflags> <addlen> [<leadlen> <addstring>] |
| 1799 | * [<region>] */ |
| 1800 | flags = getc(fd); /* <addflags> */ |
| 1801 | addlen = getc(fd); /* <addlen> */ |
| 1802 | if (addlen == EOF) |
| 1803 | goto truncerr; |
| 1804 | if (addlen >= MAXWLEN) |
| 1805 | goto formerr; |
| 1806 | |
| 1807 | if (addlen > 0) |
| 1808 | { |
| 1809 | leadlen = getc(fd); /* <leadlen> */ |
| 1810 | for (i = 0; i < addlen; ++i) /* <addstring> */ |
| 1811 | cbuf[i] = getc(fd); |
| 1812 | cbuf[i] = NUL; |
| 1813 | } |
| 1814 | else |
| 1815 | leadlen = 0; |
| 1816 | |
| 1817 | if (flags & ADD_KEEPCAP) |
| 1818 | { |
| 1819 | /* <addstring> is in original case, need to get |
| 1820 | * case-folded word too. */ |
| 1821 | (void)str_foldcase(cbuf, addlen, fbuf, MAXWLEN); |
| 1822 | flen = addlen - leadlen + 1; |
| 1823 | addlen = STRLEN(fbuf); |
| 1824 | } |
| 1825 | else |
| 1826 | flen = 0; |
| 1827 | |
| 1828 | aw = (addword_T *)getroom(lp, &bl_used, |
| 1829 | sizeof(addword_T) + addlen + flen); |
| 1830 | if (aw == NULL) |
| 1831 | goto errorend; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1832 | |
| 1833 | if (flags & ADD_KEEPCAP) |
| 1834 | { |
| 1835 | /* Put the addition in original case after the case-folded |
| 1836 | * string. */ |
| 1837 | STRCPY(aw->aw_word, fbuf); |
| 1838 | STRCPY(aw->aw_word + addlen + 1, cbuf + leadlen); |
| 1839 | } |
| 1840 | else |
| 1841 | STRCPY(aw->aw_word, cbuf); |
| 1842 | |
| 1843 | aw->aw_flags = flags; |
| 1844 | aw->aw_wordlen = addlen; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1845 | aw->aw_leadlen = leadlen; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1846 | |
| 1847 | if (flags & ADD_REGION) |
| 1848 | aw->aw_region = getc(fd); /* <region> */ |
| 1849 | else |
| 1850 | aw->aw_region = REGION_ALL; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (ht == NULL) |
| 1853 | { |
| 1854 | /* Using simple linked list, put it in front. */ |
| 1855 | aw->aw_next = fw->fw_adds; |
| 1856 | fw->fw_adds = aw; |
| 1857 | aw->aw_saveb = NUL; |
| 1858 | } |
| 1859 | else |
| 1860 | { |
| 1861 | /* Put addition in hashtable. For key we use the part up |
| 1862 | * to the next end-of-word. */ |
| 1863 | if (leadlen == 0) |
| 1864 | { |
| 1865 | p = aw->aw_word; |
| 1866 | while (*p != NUL && !spell_iswordc(p)) |
| 1867 | mb_ptr_adv(p); |
| 1868 | } |
| 1869 | |
| 1870 | if (leadlen != 0 || *p == NUL) |
| 1871 | { |
| 1872 | /* Only non-word characters in addition, add it to the |
| 1873 | * list with the special key NOWC_KEY. Also do this |
| 1874 | * when there is a leadstring, it would get too |
| 1875 | * complicated. */ |
| 1876 | hash = hash_hash(NOWC_KEY); |
| 1877 | hi = hash_lookup(ht, NOWC_KEY, hash); |
| 1878 | if (HASHITEM_EMPTY(hi)) |
| 1879 | { |
| 1880 | /* we use a dummy item as the list header */ |
| 1881 | naw = (addword_T *)getroom(lp, &bl_used, |
| 1882 | sizeof(addword_T) + STRLEN(NOWC_KEY)); |
| 1883 | if (naw == NULL) |
| 1884 | goto errorend; |
| 1885 | STRCPY(naw->aw_word, NOWC_KEY); |
| 1886 | hash_add_item(ht, hi, naw->aw_word, hash); |
| 1887 | naw->aw_next = aw; |
| 1888 | aw->aw_next = NULL; |
| 1889 | } |
| 1890 | else |
| 1891 | { |
| 1892 | naw = HI2ADDWORD(hi); |
| 1893 | aw->aw_next = naw->aw_next; |
| 1894 | naw->aw_next = aw; |
| 1895 | } |
| 1896 | aw->aw_saveb = NUL; |
| 1897 | } |
| 1898 | else |
| 1899 | { |
| 1900 | /* Truncate at next non-word character, store that |
| 1901 | * byte in "aw_saveb". */ |
| 1902 | while (*p != NUL && spell_iswordc(p)) |
| 1903 | mb_ptr_adv(p); |
| 1904 | aw->aw_saveb = *p; |
| 1905 | *p = NUL; |
| 1906 | hash = hash_hash(aw->aw_word); |
| 1907 | hi = hash_lookup(ht, aw->aw_word, hash); |
| 1908 | if (HASHITEM_EMPTY(hi)) |
| 1909 | { |
| 1910 | hash_add_item(ht, hi, aw->aw_word, hash); |
| 1911 | aw->aw_next = NULL; |
| 1912 | } |
| 1913 | else |
| 1914 | { |
| 1915 | naw = HI2ADDWORD(hi); |
| 1916 | aw->aw_next = naw->aw_next; |
| 1917 | naw->aw_next = aw; |
| 1918 | } |
| 1919 | } |
| 1920 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | goto end_OK; |
| 1925 | |
| 1926 | errorend: |
| 1927 | lp->sl_error = TRUE; |
| 1928 | end_OK: |
| 1929 | if (fd != NULL) |
| 1930 | fclose(fd); |
| 1931 | hash_unlock(&lp->sl_words); |
| 1932 | sourcing_name = save_sourcing_name; |
| 1933 | sourcing_lnum = save_sourcing_lnum; |
| 1934 | } |
| 1935 | |
| 1936 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1937 | * Get part of an sblock_T, at least "len" bytes long. |
| 1938 | * Returns NULL when out of memory. |
| 1939 | */ |
| 1940 | static void * |
| 1941 | getroom(lp, bl_used, len) |
| 1942 | slang_T *lp; /* lp->sl_block is current block or NULL */ |
| 1943 | int *bl_used; /* used up from current block */ |
| 1944 | int len; /* length needed */ |
| 1945 | { |
| 1946 | char_u *p; |
| 1947 | sblock_T *bl = lp->sl_block; |
| 1948 | |
| 1949 | if (bl == NULL || *bl_used + len > SBLOCKSIZE) |
| 1950 | { |
| 1951 | /* Allocate a block of memory. This is not freed until spell_reload() |
| 1952 | * is called. */ |
| 1953 | bl = (sblock_T *)alloc((unsigned)(sizeof(sblock_T) + SBLOCKSIZE)); |
| 1954 | if (bl == NULL) |
| 1955 | return NULL; |
| 1956 | bl->sb_next = lp->sl_block; |
| 1957 | lp->sl_block = bl; |
| 1958 | *bl_used = 0; |
| 1959 | } |
| 1960 | |
| 1961 | p = bl->sb_data + *bl_used; |
| 1962 | *bl_used += len; |
| 1963 | |
| 1964 | return p; |
| 1965 | } |
| 1966 | |
| 1967 | /* |
| 1968 | * Parse 'spelllang' and set buf->b_langp accordingly. |
| 1969 | * Returns an error message or NULL. |
| 1970 | */ |
| 1971 | char_u * |
| 1972 | did_set_spelllang(buf) |
| 1973 | buf_T *buf; |
| 1974 | { |
| 1975 | garray_T ga; |
| 1976 | char_u *lang; |
| 1977 | char_u *e; |
| 1978 | char_u *region; |
| 1979 | int region_mask; |
| 1980 | slang_T *lp; |
| 1981 | int c; |
| 1982 | char_u lbuf[MAXWLEN + 1]; |
| 1983 | |
| 1984 | ga_init2(&ga, sizeof(langp_T), 2); |
| 1985 | |
| 1986 | /* loop over comma separated languages. */ |
| 1987 | for (lang = buf->b_p_spl; *lang != NUL; lang = e) |
| 1988 | { |
| 1989 | e = vim_strchr(lang, ','); |
| 1990 | if (e == NULL) |
| 1991 | e = lang + STRLEN(lang); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 1992 | region = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1993 | if (e > lang + 2) |
| 1994 | { |
| 1995 | if (e - lang >= MAXWLEN) |
| 1996 | { |
| 1997 | ga_clear(&ga); |
| 1998 | return e_invarg; |
| 1999 | } |
| 2000 | if (lang[2] == '_') |
| 2001 | region = lang + 3; |
| 2002 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2003 | |
| 2004 | for (lp = first_lang; lp != NULL; lp = lp->sl_next) |
| 2005 | if (STRNICMP(lp->sl_name, lang, 2) == 0) |
| 2006 | break; |
| 2007 | |
| 2008 | if (lp == NULL) |
| 2009 | { |
| 2010 | /* Not found, load the language. */ |
| 2011 | STRNCPY(lbuf, lang, e - lang); |
| 2012 | lbuf[e - lang] = NUL; |
| 2013 | if (region != NULL) |
| 2014 | mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4); |
| 2015 | lp = spell_load_lang(lbuf); |
| 2016 | } |
| 2017 | |
| 2018 | if (lp != NULL) |
| 2019 | { |
| 2020 | if (region == NULL) |
| 2021 | region_mask = REGION_ALL; |
| 2022 | else |
| 2023 | { |
| 2024 | /* find region in sl_regions */ |
| 2025 | c = find_region(lp->sl_regions, region); |
| 2026 | if (c == REGION_ALL) |
| 2027 | { |
| 2028 | c = *e; |
| 2029 | *e = NUL; |
| 2030 | smsg((char_u *)_("Warning: region %s not supported"), lang); |
| 2031 | *e = c; |
| 2032 | region_mask = REGION_ALL; |
| 2033 | } |
| 2034 | else |
| 2035 | region_mask = 1 << c; |
| 2036 | } |
| 2037 | |
| 2038 | if (ga_grow(&ga, 1) == FAIL) |
| 2039 | { |
| 2040 | ga_clear(&ga); |
| 2041 | return e_outofmem; |
| 2042 | } |
| 2043 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp; |
| 2044 | LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
| 2045 | ++ga.ga_len; |
| 2046 | } |
| 2047 | |
| 2048 | if (*e == ',') |
| 2049 | ++e; |
| 2050 | } |
| 2051 | |
| 2052 | /* Add a NULL entry to mark the end of the list. */ |
| 2053 | if (ga_grow(&ga, 1) == FAIL) |
| 2054 | { |
| 2055 | ga_clear(&ga); |
| 2056 | return e_outofmem; |
| 2057 | } |
| 2058 | LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL; |
| 2059 | ++ga.ga_len; |
| 2060 | |
| 2061 | /* Everything is fine, store the new b_langp value. */ |
| 2062 | ga_clear(&buf->b_langp); |
| 2063 | buf->b_langp = ga; |
| 2064 | |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * Find the region "region[2]" in "rp" (points to "sl_regions"). |
| 2070 | * Each region is simply stored as the two characters of it's name. |
| 2071 | * Returns the index if found, REGION_ALL if not found. |
| 2072 | */ |
| 2073 | static int |
| 2074 | find_region(rp, region) |
| 2075 | char_u *rp; |
| 2076 | char_u *region; |
| 2077 | { |
| 2078 | int i; |
| 2079 | |
| 2080 | for (i = 0; ; i += 2) |
| 2081 | { |
| 2082 | if (rp[i] == NUL) |
| 2083 | return REGION_ALL; |
| 2084 | if (rp[i] == region[0] && rp[i + 1] == region[1]) |
| 2085 | break; |
| 2086 | } |
| 2087 | return i / 2; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Return type of word: |
| 2092 | * w word 0 |
| 2093 | * Word BWF_ONECAP |
| 2094 | * W WORD BWF_ALLCAP |
| 2095 | * WoRd wOrd BWF_KEEPCAP |
| 2096 | */ |
| 2097 | static int |
| 2098 | captype(word, end) |
| 2099 | char_u *word; |
| 2100 | char_u *end; |
| 2101 | { |
| 2102 | char_u *p; |
| 2103 | int c; |
| 2104 | int firstcap; |
| 2105 | int allcap; |
| 2106 | int past_second = FALSE; /* past second word char */ |
| 2107 | |
| 2108 | /* find first letter */ |
| 2109 | for (p = word; !spell_iswordc(p); mb_ptr_adv(p)) |
| 2110 | if (p >= end) |
| 2111 | return 0; /* only non-word characters, illegal word */ |
| 2112 | #ifdef FEAT_MBYTE |
| 2113 | c = mb_ptr2char_adv(&p); |
| 2114 | #else |
| 2115 | c = *p++; |
| 2116 | #endif |
| 2117 | firstcap = allcap = MB_ISUPPER(c); |
| 2118 | |
| 2119 | /* |
| 2120 | * Need to check all letters to find a word with mixed upper/lower. |
| 2121 | * But a word with an upper char only at start is a ONECAP. |
| 2122 | */ |
| 2123 | for ( ; p < end; mb_ptr_adv(p)) |
| 2124 | if (spell_iswordc(p)) |
| 2125 | { |
| 2126 | #ifdef FEAT_MBYTE |
| 2127 | c = mb_ptr2char(p); |
| 2128 | #else |
| 2129 | c = *p; |
| 2130 | #endif |
| 2131 | if (!MB_ISUPPER(c)) |
| 2132 | { |
| 2133 | /* UUl -> KEEPCAP */ |
| 2134 | if (past_second && allcap) |
| 2135 | return BWF_KEEPCAP; |
| 2136 | allcap = FALSE; |
| 2137 | } |
| 2138 | else if (!allcap) |
| 2139 | /* UlU -> KEEPCAP */ |
| 2140 | return BWF_KEEPCAP; |
| 2141 | past_second = TRUE; |
| 2142 | } |
| 2143 | |
| 2144 | if (allcap) |
| 2145 | return BWF_ALLCAP; |
| 2146 | if (firstcap) |
| 2147 | return BWF_ONECAP; |
| 2148 | return 0; |
| 2149 | } |
| 2150 | |
| 2151 | # if defined(FEAT_MBYTE) || defined(PROTO) |
| 2152 | /* |
| 2153 | * Clear all spelling tables and reload them. |
| 2154 | * Used after 'encoding' is set. |
| 2155 | */ |
| 2156 | void |
| 2157 | spell_reload() |
| 2158 | { |
| 2159 | buf_T *buf; |
| 2160 | slang_T *lp; |
| 2161 | |
| 2162 | /* Initialize the table for spell_iswordc(). */ |
| 2163 | init_spell_chartab(); |
| 2164 | |
| 2165 | /* Unload all allocated memory. */ |
| 2166 | while (first_lang != NULL) |
| 2167 | { |
| 2168 | lp = first_lang; |
| 2169 | first_lang = lp->sl_next; |
| 2170 | slang_free(lp); |
| 2171 | } |
| 2172 | |
| 2173 | /* Go through all buffers and handle 'spelllang'. */ |
| 2174 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 2175 | { |
| 2176 | ga_clear(&buf->b_langp); |
| 2177 | if (*buf->b_p_spl != NUL) |
| 2178 | did_set_spelllang(buf); |
| 2179 | } |
| 2180 | } |
| 2181 | # endif |
| 2182 | |
| 2183 | /* |
| 2184 | * Recognizing words uses a two-step mechanism: |
| 2185 | * 1. Locate a basic word, made out of word characters only and separated by |
| 2186 | * non-word characters. |
| 2187 | * 2. When a basic word is found, check if (possibly required) additions |
| 2188 | * before and after the word are present. |
| 2189 | * |
| 2190 | * Both mechanisms use affixes (prefixes and suffixes) to reduce the number of |
| 2191 | * words. When no matching word was found in the hashtable the start of the |
| 2192 | * word is checked for matching prefixes and the end of the word for matching |
| 2193 | * suffixes. All matching affixes are removed and then the resulting word is |
| 2194 | * searched for. If found it is checked if it supports the used affix. |
| 2195 | */ |
| 2196 | |
| 2197 | |
| 2198 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2199 | /* |
| 2200 | * Functions for ":mkspell". |
| 2201 | * Only possible with the multi-byte feature. |
| 2202 | */ |
| 2203 | |
| 2204 | #define MAXLINELEN 300 /* Maximum length in bytes of a line in a .aff |
| 2205 | and .dic file. */ |
| 2206 | /* |
| 2207 | * Main structure to store the contents of a ".aff" file. |
| 2208 | */ |
| 2209 | typedef struct afffile_S |
| 2210 | { |
| 2211 | char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */ |
| 2212 | char_u *af_try; /* "TRY" line in "af_enc" encoding */ |
| 2213 | hashtab_T af_pref; /* hashtable for prefixes, affheader_T */ |
| 2214 | hashtab_T af_suff; /* hashtable for suffixes, affheader_T */ |
| 2215 | garray_T af_rep; /* list of repentry_T entries from REP lines */ |
| 2216 | } afffile_T; |
| 2217 | |
| 2218 | typedef struct affentry_S affentry_T; |
| 2219 | |
| 2220 | /* Affix header from ".aff" file. Used for af_pref and af_suff. */ |
| 2221 | typedef struct affheader_S |
| 2222 | { |
| 2223 | char_u ah_key[2]; /* key for hashtable == name of affix entry */ |
| 2224 | int ah_combine; |
| 2225 | affentry_T *ah_first; /* first affix entry */ |
| 2226 | short_u ah_affnr; /* used in get_new_aff() */ |
| 2227 | } affheader_T; |
| 2228 | |
| 2229 | #define HI2AH(hi) ((affheader_T *)(hi)->hi_key) |
| 2230 | |
| 2231 | /* Affix entry from ".aff" file. Used for prefixes and suffixes. */ |
| 2232 | struct affentry_S |
| 2233 | { |
| 2234 | affentry_T *ae_next; /* next affix with same name/number */ |
| 2235 | char_u *ae_chop; /* text to chop off basic word (can be NULL) */ |
| 2236 | char_u *ae_add; /* text to add to basic word (can be NULL) */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2237 | char_u *ae_add_nw; /* For a suffix: first non-word char in |
| 2238 | * "ae_add"; for a prefix with only non-word |
| 2239 | * chars: equal to "ae_add", for a prefix with |
| 2240 | * word and non-word chars: first non-word |
| 2241 | * char after word char. NULL otherwise. */ |
| 2242 | char_u *ae_add_pw; /* For a prefix with both word and non-word |
| 2243 | * chars: first word char. NULL otherwise. */ |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2244 | char_u ae_preword; /* TRUE for a prefix with one word */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2245 | char_u *ae_cond; /* condition (NULL for ".") */ |
| 2246 | regprog_T *ae_prog; /* regexp program for ae_cond or NULL */ |
| 2247 | short_u ae_affnr; /* for old affix: new affix number */ |
| 2248 | }; |
| 2249 | |
| 2250 | /* |
| 2251 | * Structure to store a word from a ".dic" file. |
| 2252 | */ |
| 2253 | typedef struct dicword_S |
| 2254 | { |
| 2255 | char_u *dw_affnm; /* original affix names */ |
| 2256 | char_u dw_word[1]; /* actually longer: the word in 'encoding' */ |
| 2257 | } dicword_T; |
| 2258 | |
| 2259 | static dicword_T dumdw; |
| 2260 | #define HI2DW(hi) ((dicword_T *)((hi)->hi_key - (dumdw.dw_word - (char_u *)&dumdw))) |
| 2261 | |
| 2262 | /* |
| 2263 | * Structure to store a basic word for the spell file. |
| 2264 | * This is used for ":mkspell", not for spell checking. |
| 2265 | */ |
| 2266 | typedef struct basicword_S basicword_T; |
| 2267 | struct basicword_S |
| 2268 | { |
| 2269 | basicword_T *bw_next; /* next word with same basic word */ |
| 2270 | basicword_T *bw_cnext; /* next word with same caps */ |
| 2271 | int bw_flags; /* BWF_ flags */ |
| 2272 | garray_T bw_prefix; /* table with prefix numbers */ |
| 2273 | garray_T bw_suffix; /* table with suffix numbers */ |
| 2274 | int bw_region; /* region bits */ |
| 2275 | char_u *bw_caseword; /* keep-case word */ |
| 2276 | char_u *bw_leadstring; /* must come before bw_word */ |
| 2277 | char_u *bw_addstring; /* must come after bw_word */ |
| 2278 | char_u bw_word[1]; /* actually longer: word case folded */ |
| 2279 | }; |
| 2280 | |
| 2281 | static basicword_T dumbw; |
| 2282 | #define KEY2BW(p) ((basicword_T *)((p) - (dumbw.bw_word - (char_u *)&dumbw))) |
| 2283 | #define HI2BW(hi) KEY2BW((hi)->hi_key) |
| 2284 | |
| 2285 | /* Store the affix number related with a certain string. */ |
| 2286 | typedef struct affhash_S |
| 2287 | { |
| 2288 | short_u as_nr; /* the affix nr */ |
| 2289 | char_u as_word[1]; /* actually longer */ |
| 2290 | } affhash_T; |
| 2291 | |
| 2292 | static affhash_T dumas; |
| 2293 | #define HI2AS(hi) ((affhash_T *)((hi)->hi_key - (dumas.as_word - (char_u *)&dumas))) |
| 2294 | |
| 2295 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2296 | static afffile_T *spell_read_aff __ARGS((char_u *fname, vimconv_T *conv, int ascii)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2297 | static void spell_free_aff __ARGS((afffile_T *aff)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2298 | static int has_non_ascii __ARGS((char_u *s)); |
| 2299 | static int spell_read_dic __ARGS((hashtab_T *ht, char_u *fname, vimconv_T *conv, int ascii)); |
| 2300 | static int get_new_aff __ARGS((hashtab_T *oldaff, garray_T *gap, int prefix)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2301 | static void spell_free_dic __ARGS((hashtab_T *dic)); |
| 2302 | static int same_affentries __ARGS((affheader_T *ah1, affheader_T *ah2)); |
| 2303 | static void add_affhash __ARGS((hashtab_T *ht, char_u *key, int newnr)); |
| 2304 | static void clear_affhash __ARGS((hashtab_T *ht)); |
| 2305 | static void trans_affixes __ARGS((dicword_T *dw, basicword_T *bw, afffile_T *oldaff, hashtab_T *newwords)); |
| 2306 | static int build_wordlist __ARGS((hashtab_T *newwords, hashtab_T *oldwords, afffile_T *oldaff, int regionmask)); |
| 2307 | static void combine_regions __ARGS((hashtab_T *newwords)); |
| 2308 | static int same_affixes __ARGS((basicword_T *bw, basicword_T *nbw)); |
| 2309 | static void expand_affixes __ARGS((hashtab_T *newwords, garray_T *prefgap, garray_T *suffgap)); |
| 2310 | static void expand_one_aff __ARGS((basicword_T *bw, garray_T *add_words, affentry_T *pae, affentry_T *sae)); |
| 2311 | static void add_to_wordlist __ARGS((hashtab_T *newwords, basicword_T *bw)); |
| 2312 | static void put_bytes __ARGS((FILE *fd, long_u nr, int len)); |
| 2313 | static void write_affix __ARGS((FILE *fd, affheader_T *ah)); |
| 2314 | static void write_affixlist __ARGS((FILE *fd, garray_T *aff, int bytes)); |
| 2315 | static void write_vim_spell __ARGS((char_u *fname, garray_T *prefga, garray_T *suffga, hashtab_T *newwords, int regcount, char_u *regchars)); |
| 2316 | static void write_bword __ARGS((FILE *fd, basicword_T *bw, int lowcap, basicword_T **prevbw, int regionmask, int prefm, int suffm)); |
| 2317 | static void free_wordtable __ARGS((hashtab_T *ht)); |
| 2318 | static void free_basicword __ARGS((basicword_T *bw)); |
| 2319 | static void free_affixentries __ARGS((affentry_T *first)); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2320 | static void free_affix_entry __ARGS((affentry_T *ap)); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2321 | |
| 2322 | /* |
| 2323 | * Read an affix ".aff" file. |
| 2324 | * Returns an afffile_T, NULL for failure. |
| 2325 | */ |
| 2326 | static afffile_T * |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2327 | spell_read_aff(fname, conv, ascii) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2328 | char_u *fname; |
| 2329 | vimconv_T *conv; /* info for encoding conversion */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2330 | int ascii; /* Only accept ASCII characters */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2331 | { |
| 2332 | FILE *fd; |
| 2333 | afffile_T *aff; |
| 2334 | char_u rline[MAXLINELEN]; |
| 2335 | char_u *line; |
| 2336 | char_u *pc = NULL; |
| 2337 | char_u *(items[6]); |
| 2338 | int itemcnt; |
| 2339 | char_u *p; |
| 2340 | int lnum = 0; |
| 2341 | affheader_T *cur_aff = NULL; |
| 2342 | int aff_todo = 0; |
| 2343 | hashtab_T *tp; |
| 2344 | |
| 2345 | fd = fopen((char *)fname, "r"); |
| 2346 | if (fd == NULL) |
| 2347 | { |
| 2348 | EMSG2(_(e_notopen), fname); |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | |
| 2352 | smsg((char_u *)_("Reading affix file %s..."), fname); |
| 2353 | out_flush(); |
| 2354 | |
| 2355 | aff = (afffile_T *)alloc_clear((unsigned)sizeof(afffile_T)); |
| 2356 | if (aff == NULL) |
| 2357 | return NULL; |
| 2358 | hash_init(&aff->af_pref); |
| 2359 | hash_init(&aff->af_suff); |
| 2360 | ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20); |
| 2361 | |
| 2362 | /* |
| 2363 | * Read all the lines in the file one by one. |
| 2364 | */ |
| 2365 | while (!vim_fgets(rline, MAXLINELEN, fd)) |
| 2366 | { |
| 2367 | ++lnum; |
| 2368 | |
| 2369 | /* Skip comment lines. */ |
| 2370 | if (*rline == '#') |
| 2371 | continue; |
| 2372 | |
| 2373 | /* Convert from "SET" to 'encoding' when needed. */ |
| 2374 | vim_free(pc); |
| 2375 | if (conv->vc_type != CONV_NONE) |
| 2376 | { |
| 2377 | pc = string_convert(conv, rline, NULL); |
| 2378 | line = pc; |
| 2379 | } |
| 2380 | else |
| 2381 | { |
| 2382 | pc = NULL; |
| 2383 | line = rline; |
| 2384 | } |
| 2385 | |
| 2386 | /* Split the line up in white separated items. Put a NUL after each |
| 2387 | * item. */ |
| 2388 | itemcnt = 0; |
| 2389 | for (p = line; ; ) |
| 2390 | { |
| 2391 | while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */ |
| 2392 | ++p; |
| 2393 | if (*p == NUL) |
| 2394 | break; |
| 2395 | items[itemcnt++] = p; |
| 2396 | while (*p > ' ') /* skip until white space or CR/NL */ |
| 2397 | ++p; |
| 2398 | if (*p == NUL) |
| 2399 | break; |
| 2400 | *p++ = NUL; |
| 2401 | } |
| 2402 | |
| 2403 | /* Handle non-empty lines. */ |
| 2404 | if (itemcnt > 0) |
| 2405 | { |
| 2406 | if (STRCMP(items[0], "SET") == 0 && itemcnt == 2 |
| 2407 | && aff->af_enc == NULL) |
| 2408 | { |
| 2409 | if (aff->af_enc != NULL) |
| 2410 | smsg((char_u *)_("Duplicate SET line ignored in %s line %d: %s"), |
| 2411 | fname, lnum, line); |
| 2412 | else |
| 2413 | { |
| 2414 | /* Setup for conversion from "ENC" to 'encoding'. */ |
| 2415 | aff->af_enc = enc_canonize(items[1]); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2416 | if (aff->af_enc != NULL && !ascii |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2417 | && convert_setup(conv, aff->af_enc, p_enc) == FAIL) |
| 2418 | smsg((char_u *)_("Conversion in %s not supported: from %s to %s"), |
| 2419 | fname, aff->af_enc, p_enc); |
| 2420 | } |
| 2421 | } |
| 2422 | else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2 |
| 2423 | && aff->af_try == NULL) |
| 2424 | aff->af_try = vim_strsave(items[1]); |
| 2425 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2426 | || STRCMP(items[0], "SFX") == 0) |
| 2427 | && aff_todo == 0 |
| 2428 | && itemcnt == 4) |
| 2429 | { |
| 2430 | /* New affix letter. */ |
| 2431 | cur_aff = (affheader_T *)alloc((unsigned)sizeof(affheader_T)); |
| 2432 | if (cur_aff == NULL) |
| 2433 | break; |
| 2434 | cur_aff->ah_key[0] = *items[1]; |
| 2435 | cur_aff->ah_key[1] = NUL; |
| 2436 | if (items[1][1] != NUL) |
| 2437 | smsg((char_u *)_("Affix name too long in %s line %d: %s"), |
| 2438 | fname, lnum, items[1]); |
| 2439 | if (*items[2] == 'Y') |
| 2440 | cur_aff->ah_combine = TRUE; |
| 2441 | else if (*items[2] == 'N') |
| 2442 | cur_aff->ah_combine = FALSE; |
| 2443 | else if (p_verbose > 0) |
| 2444 | smsg((char_u *)_("Expected Y or N in %s line %d: %s"), |
| 2445 | fname, lnum, items[2]); |
| 2446 | cur_aff->ah_first = NULL; |
| 2447 | if (*items[0] == 'P') |
| 2448 | tp = &aff->af_pref; |
| 2449 | else |
| 2450 | tp = &aff->af_suff; |
| 2451 | if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key))) |
| 2452 | smsg((char_u *)_("Duplicate affix in %s line %d: %s"), |
| 2453 | fname, lnum, items[1]); |
| 2454 | else |
| 2455 | hash_add(tp, cur_aff->ah_key); |
| 2456 | |
| 2457 | aff_todo = atoi((char *)items[3]); |
| 2458 | } |
| 2459 | else if ((STRCMP(items[0], "PFX") == 0 |
| 2460 | || STRCMP(items[0], "SFX") == 0) |
| 2461 | && aff_todo > 0 |
| 2462 | && STRCMP(cur_aff->ah_key, items[1]) == 0 |
| 2463 | && itemcnt == 5) |
| 2464 | { |
| 2465 | affentry_T *aff_entry; |
| 2466 | |
| 2467 | /* New item for an affix letter. */ |
| 2468 | --aff_todo; |
| 2469 | aff_entry = (affentry_T *)alloc_clear( |
| 2470 | (unsigned)sizeof(affentry_T)); |
| 2471 | if (aff_entry == NULL) |
| 2472 | break; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2473 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2474 | if (STRCMP(items[2], "0") != 0) |
| 2475 | aff_entry->ae_chop = vim_strsave(items[2]); |
| 2476 | if (STRCMP(items[3], "0") != 0) |
| 2477 | aff_entry->ae_add = vim_strsave(items[3]); |
| 2478 | if (STRCMP(items[4], ".") != 0) |
| 2479 | { |
| 2480 | char_u buf[MAXLINELEN]; |
| 2481 | |
| 2482 | aff_entry->ae_cond = vim_strsave(items[4]); |
| 2483 | if (*items[0] == 'P') |
| 2484 | sprintf((char *)buf, "^%s", items[4]); |
| 2485 | else |
| 2486 | sprintf((char *)buf, "%s$", items[4]); |
| 2487 | aff_entry->ae_prog = vim_regcomp(buf, RE_MAGIC + RE_STRING); |
| 2488 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2489 | |
| 2490 | if (ascii && (has_non_ascii(aff_entry->ae_chop) |
| 2491 | || has_non_ascii(aff_entry->ae_add))) |
| 2492 | { |
| 2493 | /* Don't use an affix entry with non-ASCII characters when |
| 2494 | * "ascii" is TRUE. */ |
| 2495 | free_affix_entry(aff_entry); |
| 2496 | } |
| 2497 | else |
| 2498 | { |
| 2499 | aff_entry->ae_next = cur_aff->ah_first; |
| 2500 | cur_aff->ah_first = aff_entry; |
| 2501 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2502 | } |
| 2503 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2) |
| 2504 | /* Ignore REP count */; |
| 2505 | else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3) |
| 2506 | { |
| 2507 | repentry_T *rp; |
| 2508 | |
| 2509 | /* REP item */ |
| 2510 | if (ga_grow(&aff->af_rep, 1) == FAIL) |
| 2511 | break; |
| 2512 | rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len; |
| 2513 | rp->re_from = vim_strsave(items[1]); |
| 2514 | rp->re_to = vim_strsave(items[2]); |
| 2515 | ++aff->af_rep.ga_len; |
| 2516 | } |
| 2517 | else if (p_verbose > 0) |
| 2518 | smsg((char_u *)_("Unrecognized item in %s line %d: %s"), |
| 2519 | fname, lnum, items[0]); |
| 2520 | } |
| 2521 | |
| 2522 | } |
| 2523 | |
| 2524 | vim_free(pc); |
| 2525 | fclose(fd); |
| 2526 | return aff; |
| 2527 | } |
| 2528 | |
| 2529 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2530 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 2531 | * When "s" is NULL FALSE is returned. |
| 2532 | */ |
| 2533 | static int |
| 2534 | has_non_ascii(s) |
| 2535 | char_u *s; |
| 2536 | { |
| 2537 | char_u *p; |
| 2538 | |
| 2539 | if (s != NULL) |
| 2540 | for (p = s; *p != NUL; ++p) |
| 2541 | if (*p >= 128) |
| 2542 | return TRUE; |
| 2543 | return FALSE; |
| 2544 | } |
| 2545 | |
| 2546 | /* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2547 | * Free the structure filled by spell_read_aff(). |
| 2548 | */ |
| 2549 | static void |
| 2550 | spell_free_aff(aff) |
| 2551 | afffile_T *aff; |
| 2552 | { |
| 2553 | hashtab_T *ht; |
| 2554 | hashitem_T *hi; |
| 2555 | int todo; |
| 2556 | int i; |
| 2557 | repentry_T *rp; |
| 2558 | affheader_T *ah; |
| 2559 | |
| 2560 | vim_free(aff->af_enc); |
| 2561 | vim_free(aff->af_try); |
| 2562 | |
| 2563 | for (ht = &aff->af_pref; ; ht = &aff->af_suff) |
| 2564 | { |
| 2565 | todo = ht->ht_used; |
| 2566 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 2567 | { |
| 2568 | if (!HASHITEM_EMPTY(hi)) |
| 2569 | { |
| 2570 | --todo; |
| 2571 | ah = HI2AH(hi); |
| 2572 | free_affixentries(ah->ah_first); |
| 2573 | vim_free(ah); |
| 2574 | } |
| 2575 | } |
| 2576 | if (ht == &aff->af_suff) |
| 2577 | break; |
| 2578 | } |
| 2579 | hash_clear(&aff->af_pref); |
| 2580 | hash_clear(&aff->af_suff); |
| 2581 | |
| 2582 | for (i = 0; i < aff->af_rep.ga_len; ++i) |
| 2583 | { |
| 2584 | rp = ((repentry_T *)aff->af_rep.ga_data) + i; |
| 2585 | vim_free(rp->re_from); |
| 2586 | vim_free(rp->re_to); |
| 2587 | } |
| 2588 | ga_clear(&aff->af_rep); |
| 2589 | |
| 2590 | vim_free(aff); |
| 2591 | } |
| 2592 | |
| 2593 | /* |
| 2594 | * Read a dictionary ".dic" file. |
| 2595 | * Returns OK or FAIL; |
| 2596 | * Each entry in the hashtab_T is a dicword_T. |
| 2597 | */ |
| 2598 | static int |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2599 | spell_read_dic(ht, fname, conv, ascii) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2600 | hashtab_T *ht; |
| 2601 | char_u *fname; |
| 2602 | vimconv_T *conv; /* info for encoding conversion */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2603 | int ascii; /* only accept ASCII words */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2604 | { |
| 2605 | char_u line[MAXLINELEN]; |
| 2606 | char_u *p; |
| 2607 | dicword_T *dw; |
| 2608 | char_u *pc; |
| 2609 | char_u *w; |
| 2610 | int l; |
| 2611 | hash_T hash; |
| 2612 | hashitem_T *hi; |
| 2613 | FILE *fd; |
| 2614 | int lnum = 1; |
| 2615 | |
| 2616 | fd = fopen((char *)fname, "r"); |
| 2617 | if (fd == NULL) |
| 2618 | { |
| 2619 | EMSG2(_(e_notopen), fname); |
| 2620 | return FAIL; |
| 2621 | } |
| 2622 | |
| 2623 | smsg((char_u *)_("Reading dictionary file %s..."), fname); |
| 2624 | out_flush(); |
| 2625 | |
| 2626 | /* Read and ignore the first line: word count. */ |
| 2627 | (void)vim_fgets(line, MAXLINELEN, fd); |
| 2628 | if (!isdigit(*skipwhite(line))) |
| 2629 | EMSG2(_("E760: No word count in %s"), fname); |
| 2630 | |
| 2631 | /* |
| 2632 | * Read all the lines in the file one by one. |
| 2633 | * The words are converted to 'encoding' here, before being added to |
| 2634 | * the hashtable. |
| 2635 | */ |
| 2636 | while (!vim_fgets(line, MAXLINELEN, fd)) |
| 2637 | { |
| 2638 | ++lnum; |
| 2639 | |
| 2640 | /* Remove CR, LF and white space from end. */ |
| 2641 | l = STRLEN(line); |
| 2642 | while (l > 0 && line[l - 1] <= ' ') |
| 2643 | --l; |
| 2644 | if (l == 0) |
| 2645 | continue; /* empty line */ |
| 2646 | line[l] = NUL; |
| 2647 | |
| 2648 | /* Find the optional affix names. */ |
| 2649 | p = vim_strchr(line, '/'); |
| 2650 | if (p != NULL) |
| 2651 | *p++ = NUL; |
| 2652 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2653 | /* Skip non-ASCII words when "ascii" is TRUE. */ |
| 2654 | if (ascii && has_non_ascii(line)) |
| 2655 | continue; |
| 2656 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2657 | /* Convert from "SET" to 'encoding' when needed. */ |
| 2658 | if (conv->vc_type != CONV_NONE) |
| 2659 | { |
| 2660 | pc = string_convert(conv, line, NULL); |
| 2661 | w = pc; |
| 2662 | } |
| 2663 | else |
| 2664 | { |
| 2665 | pc = NULL; |
| 2666 | w = line; |
| 2667 | } |
| 2668 | |
| 2669 | dw = (dicword_T *)alloc_clear((unsigned)sizeof(dicword_T) |
| 2670 | + STRLEN(w)); |
| 2671 | if (dw == NULL) |
| 2672 | break; |
| 2673 | STRCPY(dw->dw_word, w); |
| 2674 | vim_free(pc); |
| 2675 | |
| 2676 | hash = hash_hash(dw->dw_word); |
| 2677 | hi = hash_lookup(ht, dw->dw_word, hash); |
| 2678 | if (!HASHITEM_EMPTY(hi)) |
| 2679 | smsg((char_u *)_("Duplicate word in %s line %d: %s"), |
| 2680 | fname, lnum, line); |
| 2681 | else |
| 2682 | hash_add_item(ht, hi, dw->dw_word, hash); |
| 2683 | |
| 2684 | if (p != NULL) |
| 2685 | dw->dw_affnm = vim_strsave(p); |
| 2686 | } |
| 2687 | |
| 2688 | fclose(fd); |
| 2689 | return OK; |
| 2690 | } |
| 2691 | |
| 2692 | /* |
| 2693 | * Free the structure filled by spell_read_dic(). |
| 2694 | */ |
| 2695 | static void |
| 2696 | spell_free_dic(dic) |
| 2697 | hashtab_T *dic; |
| 2698 | { |
| 2699 | int todo; |
| 2700 | dicword_T *dw; |
| 2701 | hashitem_T *hi; |
| 2702 | |
| 2703 | todo = dic->ht_used; |
| 2704 | for (hi = dic->ht_array; todo > 0; ++hi) |
| 2705 | { |
| 2706 | if (!HASHITEM_EMPTY(hi)) |
| 2707 | { |
| 2708 | --todo; |
| 2709 | dw = HI2DW(hi); |
| 2710 | vim_free(dw->dw_affnm); |
| 2711 | vim_free(dw); |
| 2712 | } |
| 2713 | } |
| 2714 | hash_clear(dic); |
| 2715 | } |
| 2716 | |
| 2717 | /* |
| 2718 | * Take the affixes read by spell_read_aff() and add them to the new list. |
| 2719 | * Attempts to re-use the same number for identical affixes (ignoring the |
| 2720 | * condition, since we remove that). That is especially important when using |
| 2721 | * multiple regions. |
| 2722 | * Returns OK or FAIL; |
| 2723 | */ |
| 2724 | static int |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2725 | get_new_aff(oldaff, gap, prefix) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2726 | hashtab_T *oldaff; /* hashtable with affheader_T */ |
| 2727 | garray_T *gap; /* table with new affixes */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2728 | int prefix; /* TRUE when doing prefixes, FALSE for |
| 2729 | suffixes */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2730 | { |
| 2731 | int oldtodo; |
| 2732 | affheader_T *oldah, *newah, *gapah; |
| 2733 | affentry_T *oldae, *newae; |
| 2734 | hashitem_T *oldhi; |
| 2735 | hashitem_T *hi; |
| 2736 | hashtab_T condht; /* conditions already found */ |
| 2737 | char_u condkey[MAXLINELEN]; |
| 2738 | int newnr; |
| 2739 | int gapnr; |
| 2740 | int retval = OK; |
| 2741 | char_u *p; |
| 2742 | garray_T tga; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2743 | int preword; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2744 | |
| 2745 | /* |
| 2746 | * Loop over all the old affix names. |
| 2747 | */ |
| 2748 | oldtodo = oldaff->ht_used; |
| 2749 | for (oldhi = oldaff->ht_array; oldtodo > 0 && retval == OK; ++oldhi) |
| 2750 | { |
| 2751 | if (!HASHITEM_EMPTY(oldhi)) |
| 2752 | { |
| 2753 | --oldtodo; |
| 2754 | oldah = (affheader_T *)oldhi->hi_key; |
| 2755 | |
| 2756 | /* Put entries with the same condition under the same new affix |
| 2757 | * nr in "tga". Use hashtable "condht" to find them. */ |
| 2758 | ga_init2(&tga, sizeof(affheader_T), 10); |
| 2759 | hash_init(&condht); |
| 2760 | |
| 2761 | /* |
| 2762 | * Loop over all affixes with the same name. |
| 2763 | * The affixes with the same condition will get the same number, |
| 2764 | * since they can be used with the same words. |
| 2765 | * 1. build the lists of new affentry_T, with the headers in "tga". |
| 2766 | * 2. Check if some of the lists already exist in "gap", re-use |
| 2767 | * their number. |
| 2768 | * 3. Assign the new numbers to the old affixes. |
| 2769 | */ |
| 2770 | |
| 2771 | /* 1. build the lists of new affentry_T. */ |
| 2772 | for (oldae = oldah->ah_first; oldae != NULL && retval == OK; |
| 2773 | oldae = oldae->ae_next) |
| 2774 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2775 | preword = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2776 | oldae->ae_add_nw = NULL; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2777 | oldae->ae_add_pw = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2778 | if (oldae->ae_add != NULL) |
| 2779 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2780 | /* Check for non-word characters in the affix. If there |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2781 | * is one a suffix will be turned into an addition, a |
| 2782 | * prefix may be turned into a leadstring. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2783 | * This is stored with the old affix, that is where |
| 2784 | * trans_affixes() will check. */ |
| 2785 | for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p)) |
| 2786 | if (!spell_iswordc(p)) |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2787 | { |
| 2788 | oldae->ae_add_nw = p; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2789 | break; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | if (prefix && oldae->ae_add_nw != NULL) |
| 2793 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2794 | /* If a prefix has non-word characters special |
| 2795 | * treatment is necessary: |
| 2796 | * - If it has only non-word characters it becomes a |
| 2797 | * leadstring. |
| 2798 | * - If it has a sequence of word characters followed |
| 2799 | * by a non-word char it becomes a "preword": "d'", |
| 2800 | * "de-", "d'ai", etc. |
| 2801 | * - if it has another mix of word and non-word |
| 2802 | * characters the part before the last word char |
| 2803 | * becomes a leadstring: "'d", etc. |
| 2804 | */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2805 | for (p = oldae->ae_add; *p != NUL; mb_ptr_adv(p)) |
| 2806 | if (spell_iswordc(p)) |
| 2807 | { |
| 2808 | oldae->ae_add_pw = p; |
| 2809 | break; |
| 2810 | } |
| 2811 | if (oldae->ae_add_pw != NULL) |
| 2812 | { |
| 2813 | /* Mixed prefix, set ae_add_nw to first non-word |
| 2814 | * char after ae_add_pw (if there is one). */ |
| 2815 | oldae->ae_add_nw = NULL; |
| 2816 | for ( ; *p != NUL; mb_ptr_adv(p)) |
| 2817 | if (!spell_iswordc(p)) |
| 2818 | { |
| 2819 | oldae->ae_add_nw = p; |
| 2820 | break; |
| 2821 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2822 | if (oldae->ae_add_nw != NULL) |
| 2823 | { |
| 2824 | preword = TRUE; |
| 2825 | oldae->ae_add_pw = NULL; |
| 2826 | oldae->ae_add_nw = NULL; |
| 2827 | } |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 2828 | } |
| 2829 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | if (oldae->ae_cond == NULL) |
| 2833 | /* hashtable requires a non-empty key */ |
| 2834 | STRCPY(condkey, "---"); |
| 2835 | else |
| 2836 | STRCPY(condkey, oldae->ae_cond); |
| 2837 | |
| 2838 | /* Look for an existing list with this name and condition. */ |
| 2839 | hi = hash_find(&condht, condkey); |
| 2840 | if (!HASHITEM_EMPTY(hi)) |
| 2841 | /* Match with existing affix, use that one. */ |
| 2842 | newnr = HI2AS(hi)->as_nr; |
| 2843 | else |
| 2844 | { |
| 2845 | /* Add a new affix number. */ |
| 2846 | newnr = tga.ga_len; |
| 2847 | if (ga_grow(&tga, 1) == FAIL) |
| 2848 | retval = FAIL; |
| 2849 | else |
| 2850 | { |
| 2851 | newah = ((affheader_T *)tga.ga_data) + newnr; |
| 2852 | newah->ah_combine = oldah->ah_combine; |
| 2853 | newah->ah_first = NULL; |
| 2854 | ++tga.ga_len; |
| 2855 | |
| 2856 | /* Add the new list to the condht hashtable. */ |
| 2857 | add_affhash(&condht, condkey, newnr); |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | /* Add the new affentry_T to the list. */ |
| 2862 | newah = ((affheader_T *)tga.ga_data) + newnr; |
| 2863 | newae = (affentry_T *)alloc_clear((unsigned)sizeof(affentry_T)); |
| 2864 | if (newae == NULL) |
| 2865 | retval = FAIL; |
| 2866 | else |
| 2867 | { |
| 2868 | newae->ae_next = newah->ah_first; |
| 2869 | newah->ah_first = newae; |
| 2870 | if (oldae->ae_chop == NULL) |
| 2871 | newae->ae_chop = NULL; |
| 2872 | else |
| 2873 | newae->ae_chop = vim_strsave(oldae->ae_chop); |
| 2874 | if (oldae->ae_add == NULL) |
| 2875 | newae->ae_add = NULL; |
| 2876 | else |
| 2877 | newae->ae_add = vim_strsave(oldae->ae_add); |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2878 | newae->ae_preword = preword; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2879 | |
| 2880 | /* The condition is not copied, since the new affix is |
| 2881 | * only used for words where the condition matches. */ |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | /* 2. Check if some of the lists already exist, re-use their |
| 2886 | * number. Otherwise add the list to "gap". */ |
| 2887 | for (newnr = 0; newnr < tga.ga_len; ++newnr) |
| 2888 | { |
| 2889 | newah = ((affheader_T *)tga.ga_data) + newnr; |
| 2890 | for (gapnr = 0; gapnr < gap->ga_len; ++gapnr) |
| 2891 | { |
| 2892 | gapah = ((affheader_T *)gap->ga_data) + gapnr; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 2893 | if (newah->ah_combine == gapah->ah_combine |
| 2894 | && same_affentries(newah, gapah)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 2895 | /* Found an existing affheader_T entry with same |
| 2896 | * affentry_T list, use its number. */ |
| 2897 | break; |
| 2898 | } |
| 2899 | |
| 2900 | newah->ah_affnr = gapnr; |
| 2901 | if (gapnr == gap->ga_len) |
| 2902 | { |
| 2903 | /* This is a new affentry_T list, add it. */ |
| 2904 | if (ga_grow(gap, 1) == FAIL) |
| 2905 | retval = FAIL; |
| 2906 | else |
| 2907 | { |
| 2908 | *(((affheader_T *)gap->ga_data) + gap->ga_len) = *newah; |
| 2909 | ++gap->ga_len; |
| 2910 | } |
| 2911 | } |
| 2912 | else |
| 2913 | { |
| 2914 | /* free unused affentry_T list */ |
| 2915 | free_affixentries(newah->ah_first); |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | /* 3. Assign the new affix numbers to the old affixes. */ |
| 2920 | for (oldae = oldah->ah_first; oldae != NULL && retval == OK; |
| 2921 | oldae = oldae->ae_next) |
| 2922 | { |
| 2923 | if (oldae->ae_cond == NULL) |
| 2924 | /* hashtable requires a non-empty key */ |
| 2925 | STRCPY(condkey, "---"); |
| 2926 | else |
| 2927 | STRCPY(condkey, oldae->ae_cond); |
| 2928 | |
| 2929 | /* Look for an existing affix with this name and condition. */ |
| 2930 | hi = hash_find(&condht, condkey); |
| 2931 | if (!HASHITEM_EMPTY(hi)) |
| 2932 | /* Match with existing affix, use that one. */ |
| 2933 | newnr = HI2AS(hi)->as_nr; |
| 2934 | else |
| 2935 | { |
| 2936 | EMSG(_(e_internal)); |
| 2937 | retval = FAIL; |
| 2938 | } |
| 2939 | newah = ((affheader_T *)tga.ga_data) + newnr; |
| 2940 | oldae->ae_affnr = newah->ah_affnr; |
| 2941 | } |
| 2942 | |
| 2943 | ga_clear(&tga); |
| 2944 | clear_affhash(&condht); |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | return retval; |
| 2949 | } |
| 2950 | |
| 2951 | /* |
| 2952 | * Return TRUE if the affentry_T lists for "ah1" and "ah2" contain the same |
| 2953 | * items, ignoring the order. |
| 2954 | * Only compares the chop and add strings, not the condition. |
| 2955 | */ |
| 2956 | static int |
| 2957 | same_affentries(ah1, ah2) |
| 2958 | affheader_T *ah1; |
| 2959 | affheader_T *ah2; |
| 2960 | { |
| 2961 | affentry_T *ae1, *ae2; |
| 2962 | |
| 2963 | /* Check the length of the lists first. */ |
| 2964 | ae2 = ah2->ah_first; |
| 2965 | for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next) |
| 2966 | { |
| 2967 | if (ae2 == NULL) |
| 2968 | return FALSE; /* "ah1" list is longer */ |
| 2969 | ae2 = ae2->ae_next; |
| 2970 | } |
| 2971 | if (ae2 != NULL) |
| 2972 | return FALSE; /* "ah2" list is longer */ |
| 2973 | |
| 2974 | /* Check that each entry in "ah1" appears in "ah2". */ |
| 2975 | for (ae1 = ah1->ah_first; ae1 != NULL; ae1 = ae1->ae_next) |
| 2976 | { |
| 2977 | for (ae2 = ah2->ah_first; ae2 != NULL; ae2 = ae2->ae_next) |
| 2978 | { |
| 2979 | if ((ae1->ae_chop == NULL) == (ae2->ae_chop == NULL) |
| 2980 | && (ae1->ae_add == NULL) == (ae2->ae_add == NULL) |
| 2981 | && (ae1->ae_chop == NULL |
| 2982 | || STRCMP(ae1->ae_chop, ae2->ae_chop) == 0) |
| 2983 | && (ae1->ae_add == NULL |
| 2984 | || STRCMP(ae1->ae_add, ae2->ae_add) == 0)) |
| 2985 | break; |
| 2986 | } |
| 2987 | if (ae2 == NULL) |
| 2988 | return FALSE; |
| 2989 | } |
| 2990 | |
| 2991 | return TRUE; |
| 2992 | } |
| 2993 | |
| 2994 | /* |
| 2995 | * Add a chop/add or cond hashtable entry. |
| 2996 | */ |
| 2997 | static void |
| 2998 | add_affhash(ht, key, newnr) |
| 2999 | hashtab_T *ht; |
| 3000 | char_u *key; |
| 3001 | int newnr; |
| 3002 | { |
| 3003 | affhash_T *as; |
| 3004 | |
| 3005 | as = (affhash_T *)alloc((unsigned)sizeof(affhash_T) + STRLEN(key)); |
| 3006 | if (as != NULL) |
| 3007 | { |
| 3008 | as->as_nr = newnr; |
| 3009 | STRCPY(as->as_word, key); |
| 3010 | hash_add(ht, as->as_word); |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | /* |
| 3015 | * Clear the chop/add hashtable used to detect identical affixes. |
| 3016 | */ |
| 3017 | static void |
| 3018 | clear_affhash(ht) |
| 3019 | hashtab_T *ht; |
| 3020 | { |
| 3021 | int todo; |
| 3022 | hashitem_T *hi; |
| 3023 | |
| 3024 | todo = ht->ht_used; |
| 3025 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 3026 | { |
| 3027 | if (!HASHITEM_EMPTY(hi)) |
| 3028 | { |
| 3029 | --todo; |
| 3030 | vim_free(HI2AS(hi)); |
| 3031 | } |
| 3032 | } |
| 3033 | hash_clear(ht); |
| 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * Translate list of affix names for an old word to affix numbers in a new |
| 3038 | * basic word. |
| 3039 | * This checks if the conditions match with the old word. The result is that |
| 3040 | * the new affix does not need to store the condition. |
| 3041 | */ |
| 3042 | static void |
| 3043 | trans_affixes(dw, bw, oldaff, newwords) |
| 3044 | dicword_T *dw; /* old word */ |
| 3045 | basicword_T *bw; /* basic word */ |
| 3046 | afffile_T *oldaff; /* affixes for "oldwords" */ |
| 3047 | hashtab_T *newwords; /* table with words */ |
| 3048 | { |
| 3049 | char_u key[2]; |
| 3050 | char_u *p; |
| 3051 | char_u *affnm; |
| 3052 | garray_T *gap; |
| 3053 | hashitem_T *aff_hi; |
| 3054 | affheader_T *ah; |
| 3055 | affentry_T *ae; |
| 3056 | regmatch_T regmatch; |
| 3057 | int i; |
| 3058 | basicword_T *nbw; |
| 3059 | int alen; |
| 3060 | int wlen; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3061 | garray_T suffixga; /* list of words with non-word suffixes */ |
| 3062 | garray_T prefixga; /* list of words with non-word prefixes */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3063 | char_u nword[MAXWLEN]; |
| 3064 | int flags; |
| 3065 | int n; |
| 3066 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3067 | ga_init2(&suffixga, (int)sizeof(basicword_T *), 5); |
| 3068 | ga_init2(&prefixga, (int)sizeof(basicword_T *), 5); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3069 | |
| 3070 | /* Loop over all the affix names of the old word. */ |
| 3071 | key[1] = NUL; |
| 3072 | for (affnm = dw->dw_affnm; *affnm != NUL; ++affnm) |
| 3073 | { |
| 3074 | key[0] = *affnm; |
| 3075 | aff_hi = hash_find(&oldaff->af_pref, key); |
| 3076 | if (!HASHITEM_EMPTY(aff_hi)) |
| 3077 | gap = &bw->bw_prefix; /* found a prefix */ |
| 3078 | else |
| 3079 | { |
| 3080 | gap = &bw->bw_suffix; /* must be a suffix */ |
| 3081 | aff_hi = hash_find(&oldaff->af_suff, key); |
| 3082 | if (HASHITEM_EMPTY(aff_hi)) |
| 3083 | { |
| 3084 | smsg((char_u *)_("No affix entry '%s' for word %s"), |
| 3085 | key, dw->dw_word); |
| 3086 | continue; |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | /* Loop over all the affix entries for this affix name. */ |
| 3091 | ah = HI2AH(aff_hi); |
| 3092 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3093 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3094 | /* Setup for regexp matching. Note that we don't ignore case. |
| 3095 | * This is weird, because he rules in an .aff file don't care |
| 3096 | * about case, but it's necessary for compatibility with Myspell. |
| 3097 | */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3098 | regmatch.regprog = ae->ae_prog; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3099 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3100 | if (ae->ae_prog == NULL |
| 3101 | || vim_regexec(®match, dw->dw_word, (colnr_T)0)) |
| 3102 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3103 | if ((ae->ae_add_nw != NULL || ae->ae_add_pw != NULL) |
| 3104 | && (gap != &bw->bw_suffix || bw->bw_addstring == NULL)) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3105 | { |
| 3106 | /* Affix has a non-word character and isn't prepended to |
| 3107 | * leader or appended to addition. Need to use another |
| 3108 | * word with an addition. It's a copy of the basicword_T |
| 3109 | * "bw". */ |
| 3110 | if (gap == &bw->bw_suffix) |
| 3111 | { |
| 3112 | alen = ae->ae_add_nw - ae->ae_add; |
| 3113 | nbw = (basicword_T *)alloc((unsigned)( |
| 3114 | sizeof(basicword_T) + STRLEN(bw->bw_word) |
| 3115 | + alen + 1)); |
| 3116 | if (nbw != NULL) |
| 3117 | { |
| 3118 | *nbw = *bw; |
| 3119 | ga_init2(&nbw->bw_prefix, sizeof(short_u), 1); |
| 3120 | ga_init2(&nbw->bw_suffix, sizeof(short_u), 1); |
| 3121 | |
| 3122 | /* Adding the suffix may change the caps. */ |
| 3123 | STRCPY(nword, dw->dw_word); |
| 3124 | if (ae->ae_chop != NULL) |
| 3125 | { |
| 3126 | /* Remove chop string. */ |
| 3127 | p = nword + STRLEN(nword); |
| 3128 | for (i = mb_charlen(ae->ae_chop); i > 0; --i) |
| 3129 | mb_ptr_back(nword, p); |
| 3130 | *p = NUL; |
| 3131 | } |
| 3132 | STRCAT(nword, ae->ae_add); |
| 3133 | flags = captype(nword, nword + STRLEN(nword)); |
| 3134 | if (flags & BWF_KEEPCAP) |
| 3135 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3136 | /* "caseword" excludes the addition */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3137 | nword[STRLEN(dw->dw_word) + alen] = NUL; |
| 3138 | nbw->bw_caseword = vim_strsave(nword); |
| 3139 | } |
| 3140 | nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP |
| 3141 | | BWF_KEEPCAP); |
| 3142 | nbw->bw_flags |= flags; |
| 3143 | |
| 3144 | if (bw->bw_leadstring != NULL) |
| 3145 | nbw->bw_leadstring = |
| 3146 | vim_strsave(bw->bw_leadstring); |
| 3147 | nbw->bw_addstring = vim_strsave(ae->ae_add_nw); |
| 3148 | |
| 3149 | STRCPY(nbw->bw_word, bw->bw_word); |
| 3150 | if (alen > 0 || ae->ae_chop != NULL) |
| 3151 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3152 | /* Suffix starts with word character and/or |
| 3153 | * chop off something. Append it to the word. |
| 3154 | * Add new word entry. */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3155 | wlen = STRLEN(nbw->bw_word); |
| 3156 | if (ae->ae_chop != NULL) |
| 3157 | wlen -= STRLEN(ae->ae_chop); |
| 3158 | mch_memmove(nbw->bw_word + wlen, ae->ae_add, |
| 3159 | alen); |
| 3160 | nbw->bw_word[wlen + alen] = NUL; |
| 3161 | add_to_wordlist(newwords, nbw); |
| 3162 | } |
| 3163 | else |
| 3164 | /* Basic word is the same, link "nbw" after |
| 3165 | * "bw". */ |
| 3166 | bw->bw_next = nbw; |
| 3167 | |
| 3168 | /* Remember this word, we need to set bw_prefix |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3169 | * and bw_prefix later. */ |
| 3170 | if (ga_grow(&suffixga, 1) == OK) |
| 3171 | ((basicword_T **)suffixga.ga_data) |
| 3172 | [suffixga.ga_len++] = nbw; |
| 3173 | } |
| 3174 | } |
| 3175 | else if (ae->ae_add_nw == NULL) |
| 3176 | { |
| 3177 | /* Prefix that starts with non-word char(s) and may be |
| 3178 | * followed by word chars: Make a leadstring and |
| 3179 | * prepend word chars before the word. */ |
| 3180 | alen = STRLEN(ae->ae_add_pw); |
| 3181 | nbw = (basicword_T *)alloc((unsigned)( |
| 3182 | sizeof(basicword_T) + STRLEN(bw->bw_word) |
| 3183 | + alen + 1)); |
| 3184 | if (nbw != NULL) |
| 3185 | { |
| 3186 | *nbw = *bw; |
| 3187 | ga_init2(&nbw->bw_prefix, sizeof(short_u), 1); |
| 3188 | ga_init2(&nbw->bw_suffix, sizeof(short_u), 1); |
| 3189 | |
| 3190 | /* Adding the prefix may change the caps. */ |
| 3191 | STRCPY(nword, ae->ae_add); |
| 3192 | p = dw->dw_word; |
| 3193 | if (ae->ae_chop != NULL) |
| 3194 | /* Skip chop string. */ |
| 3195 | for (i = mb_charlen(ae->ae_chop); i > 0; --i) |
| 3196 | mb_ptr_adv( p); |
| 3197 | STRCAT(nword, p); |
| 3198 | |
| 3199 | flags = captype(nword, nword + STRLEN(nword)); |
| 3200 | if (flags & BWF_KEEPCAP) |
| 3201 | /* "caseword" excludes the addition */ |
| 3202 | nbw->bw_caseword = vim_strsave(nword |
| 3203 | + (ae->ae_add_pw - ae->ae_add)); |
| 3204 | else |
| 3205 | nbw->bw_caseword = NULL; |
| 3206 | nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP |
| 3207 | | BWF_KEEPCAP); |
| 3208 | nbw->bw_flags |= flags; |
| 3209 | |
| 3210 | if (bw->bw_addstring != NULL) |
| 3211 | nbw->bw_addstring = |
| 3212 | vim_strsave(bw->bw_addstring); |
| 3213 | else |
| 3214 | nbw->bw_addstring = NULL; |
| 3215 | nbw->bw_leadstring = vim_strnsave(ae->ae_add, |
| 3216 | ae->ae_add_pw - ae->ae_add); |
| 3217 | |
| 3218 | if (alen > 0 || ae->ae_chop != NULL) |
| 3219 | { |
| 3220 | /* Prefix ends in word character and/or chop |
| 3221 | * off something. Prepend it to the word. |
| 3222 | * Add new word entry. */ |
| 3223 | STRCPY(nbw->bw_word, ae->ae_add_pw); |
| 3224 | p = bw->bw_word; |
| 3225 | if (ae->ae_chop != NULL) |
| 3226 | p += STRLEN(ae->ae_chop); |
| 3227 | STRCAT(nbw->bw_word, p); |
| 3228 | add_to_wordlist(newwords, nbw); |
| 3229 | } |
| 3230 | else |
| 3231 | { |
| 3232 | /* Basic word is the same, link "nbw" after |
| 3233 | * "bw". */ |
| 3234 | STRCPY(nbw->bw_word, bw->bw_word); |
| 3235 | bw->bw_next = nbw; |
| 3236 | } |
| 3237 | |
| 3238 | /* Remember this word, we need to set bw_suffix |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3239 | * and bw_suffix later. */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3240 | if (ga_grow(&prefixga, 1) == OK) |
| 3241 | ((basicword_T **)prefixga.ga_data) |
| 3242 | [prefixga.ga_len++] = nbw; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3243 | } |
| 3244 | } |
| 3245 | else |
| 3246 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3247 | /* Prefix with both non-word and word characters: Turn |
| 3248 | * prefix into basic word, original word becomes an |
| 3249 | * addstring. */ |
| 3250 | |
| 3251 | /* Fold-case the word characters in the prefix into |
| 3252 | * nword[]. */ |
| 3253 | alen = 0; |
| 3254 | for (p = ae->ae_add_pw; p < ae->ae_add_nw; p += n) |
| 3255 | { |
| 3256 | #ifdef FEAT_MBYTE |
| 3257 | n = (*mb_ptr2len_check)(p); |
| 3258 | #else |
| 3259 | n = 1; |
| 3260 | #endif |
| 3261 | (void)str_foldcase(p, n, nword + alen, |
| 3262 | MAXWLEN - alen); |
| 3263 | alen += STRLEN(nword + alen); |
| 3264 | } |
| 3265 | |
| 3266 | /* Allocate a new word entry. */ |
| 3267 | nbw = (basicword_T *)alloc((unsigned)( |
| 3268 | sizeof(basicword_T) + alen + 1)); |
| 3269 | if (nbw != NULL) |
| 3270 | { |
| 3271 | *nbw = *bw; |
| 3272 | ga_init2(&nbw->bw_prefix, sizeof(short_u), 1); |
| 3273 | ga_init2(&nbw->bw_suffix, sizeof(short_u), 1); |
| 3274 | |
| 3275 | mch_memmove(nbw->bw_word, nword, alen); |
| 3276 | nbw->bw_word[alen] = NUL; |
| 3277 | |
| 3278 | /* Use the cap type of the prefix. */ |
| 3279 | alen = ae->ae_add_nw - ae->ae_add_pw; |
| 3280 | mch_memmove(nword, ae->ae_add_pw, alen); |
| 3281 | nword[alen] = NUL; |
| 3282 | flags = captype(nword, nword + STRLEN(nword)); |
| 3283 | if (flags & BWF_KEEPCAP) |
| 3284 | nbw->bw_caseword = vim_strsave(nword); |
| 3285 | else |
| 3286 | nbw->bw_caseword = NULL; |
| 3287 | nbw->bw_flags &= ~(BWF_ONECAP | BWF_ALLCAP |
| 3288 | | BWF_KEEPCAP); |
| 3289 | nbw->bw_flags |= flags; |
| 3290 | |
| 3291 | /* The addstring is the prefix after the word |
| 3292 | * characters, the original word excluding "chop", |
| 3293 | * plus any addition. */ |
| 3294 | STRCPY(nword, ae->ae_add_nw); |
| 3295 | p = bw->bw_word; |
| 3296 | if (ae->ae_chop != NULL) |
| 3297 | p += STRLEN(ae->ae_chop); |
| 3298 | STRCAT(nword, p); |
| 3299 | if (bw->bw_addstring != NULL) |
| 3300 | STRCAT(nword, bw->bw_addstring); |
| 3301 | nbw->bw_addstring = vim_strsave(nword); |
| 3302 | |
| 3303 | if (ae->ae_add_pw > ae->ae_add) |
| 3304 | nbw->bw_leadstring = vim_strnsave(ae->ae_add, |
| 3305 | ae->ae_add_pw - ae->ae_add); |
| 3306 | else |
| 3307 | nbw->bw_leadstring = NULL; |
| 3308 | |
| 3309 | add_to_wordlist(newwords, nbw); |
| 3310 | |
| 3311 | /* Remember this word, we need to set bw_suffix |
| 3312 | * and bw_suffix later. */ |
| 3313 | if (ga_grow(&prefixga, 1) == OK) |
| 3314 | ((basicword_T **)prefixga.ga_data) |
| 3315 | [prefixga.ga_len++] = nbw; |
| 3316 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | else |
| 3320 | { |
| 3321 | /* Affix applies to this word, add the related affix |
| 3322 | * number. But only if it's not there yet. And keep the |
| 3323 | * list sorted, so that we can compare it later. */ |
| 3324 | for (i = 0; i < gap->ga_len; ++i) |
| 3325 | { |
| 3326 | n = ((short_u *)gap->ga_data)[i]; |
| 3327 | if (n >= ae->ae_affnr) |
| 3328 | { |
| 3329 | if (n == ae->ae_affnr) |
| 3330 | i = -1; |
| 3331 | break; |
| 3332 | } |
| 3333 | } |
| 3334 | if (i >= 0 && ga_grow(gap, 1) == OK) |
| 3335 | { |
| 3336 | if (i < gap->ga_len) |
| 3337 | mch_memmove(((short_u *)gap->ga_data) + i + 1, |
| 3338 | ((short_u *)gap->ga_data) + i, |
| 3339 | sizeof(short_u) * (gap->ga_len - i)); |
| 3340 | ((short_u *)gap->ga_data)[i] = ae->ae_affnr; |
| 3341 | ++gap->ga_len; |
| 3342 | } |
| 3343 | } |
| 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | /* |
| 3349 | * For the words that we added for suffixes with non-word characters: Use |
| 3350 | * the prefix list of the main word. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3351 | */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3352 | for (i = 0; i < suffixga.ga_len; ++i) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3353 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3354 | nbw = ((basicword_T **)suffixga.ga_data)[i]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3355 | if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK) |
| 3356 | { |
| 3357 | mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data, |
| 3358 | bw->bw_prefix.ga_len * sizeof(short_u)); |
| 3359 | nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len; |
| 3360 | } |
| 3361 | } |
| 3362 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3363 | /* |
| 3364 | * For the words that we added for prefixes with non-word characters: Use |
| 3365 | * the suffix list of the main word. |
| 3366 | */ |
| 3367 | for (i = 0; i < prefixga.ga_len; ++i) |
| 3368 | { |
| 3369 | nbw = ((basicword_T **)prefixga.ga_data)[i]; |
| 3370 | if (ga_grow(&nbw->bw_suffix, bw->bw_suffix.ga_len) == OK) |
| 3371 | { |
| 3372 | mch_memmove(nbw->bw_suffix.ga_data, bw->bw_suffix.ga_data, |
| 3373 | bw->bw_suffix.ga_len * sizeof(short_u)); |
| 3374 | nbw->bw_suffix.ga_len = bw->bw_suffix.ga_len; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | ga_clear(&suffixga); |
| 3379 | ga_clear(&prefixga); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | /* |
| 3383 | * Go over all words in "oldwords" and change the old affix names to the new |
| 3384 | * affix numbers, check the conditions, fold case, extract the basic word and |
| 3385 | * additions. |
| 3386 | */ |
| 3387 | static int |
| 3388 | build_wordlist(newwords, oldwords, oldaff, regionmask) |
| 3389 | hashtab_T *newwords; /* basicword_T entries */ |
| 3390 | hashtab_T *oldwords; /* dicword_T entries */ |
| 3391 | afffile_T *oldaff; /* affixes for "oldwords" */ |
| 3392 | int regionmask; /* value for bw_region */ |
| 3393 | { |
| 3394 | int todo; |
| 3395 | hashitem_T *old_hi; |
| 3396 | dicword_T *dw; |
| 3397 | basicword_T *bw; |
| 3398 | char_u foldword[MAXLINELEN]; |
| 3399 | int leadlen; |
| 3400 | char_u leadstring[MAXLINELEN]; |
| 3401 | int addlen; |
| 3402 | char_u addstring[MAXLINELEN]; |
| 3403 | int dwlen; |
| 3404 | char_u *p; |
| 3405 | int clen; |
| 3406 | int flags; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3407 | char_u *cp = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3408 | int l; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3409 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3410 | |
| 3411 | todo = oldwords->ht_used; |
| 3412 | for (old_hi = oldwords->ht_array; todo > 0; ++old_hi) |
| 3413 | { |
| 3414 | if (!HASHITEM_EMPTY(old_hi)) |
| 3415 | { |
| 3416 | --todo; |
| 3417 | dw = HI2DW(old_hi); |
| 3418 | |
| 3419 | /* This takes time, print a message now and then. */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3420 | if ((todo & 0x3ff) == 0 || todo == (int)oldwords->ht_used - 1) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3421 | { |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3422 | sprintf((char *)message, _("%6d todo - %s"), |
| 3423 | todo, dw->dw_word); |
| 3424 | msg_start(); |
| 3425 | msg_outtrans_attr(message, 0); |
| 3426 | msg_clr_eos(); |
| 3427 | msg_didout = FALSE; |
| 3428 | msg_col = 0; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3429 | out_flush(); |
| 3430 | ui_breakcheck(); |
| 3431 | if (got_int) |
| 3432 | break; |
| 3433 | } |
| 3434 | |
| 3435 | /* The basic words are always stored with folded case. */ |
| 3436 | dwlen = STRLEN(dw->dw_word); |
| 3437 | (void)str_foldcase(dw->dw_word, dwlen, foldword, MAXLINELEN); |
| 3438 | flags = captype(dw->dw_word, dw->dw_word + dwlen); |
| 3439 | |
| 3440 | /* Check for non-word characters before the word. */ |
| 3441 | clen = 0; |
| 3442 | leadlen = 0; |
| 3443 | if (!spell_iswordc(foldword)) |
| 3444 | { |
| 3445 | p = foldword; |
| 3446 | for (;;) |
| 3447 | { |
| 3448 | mb_ptr_adv(p); |
| 3449 | ++clen; |
| 3450 | if (*p == NUL) /* Only non-word chars (bad word!) */ |
| 3451 | { |
| 3452 | if (p_verbose > 0) |
| 3453 | smsg((char_u *)_("Warning: word without word characters: \"%s\""), |
| 3454 | foldword); |
| 3455 | break; |
| 3456 | } |
| 3457 | if (spell_iswordc(p)) |
| 3458 | { |
| 3459 | /* Move the leader to "leadstring" and remove it from |
| 3460 | * "foldword". */ |
| 3461 | leadlen = p - foldword; |
| 3462 | mch_memmove(leadstring, foldword, leadlen); |
| 3463 | leadstring[leadlen] = NUL; |
| 3464 | mch_memmove(foldword, p, STRLEN(p) + 1); |
| 3465 | break; |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | /* Check for non-word characters after word characters. */ |
| 3471 | addlen = 0; |
| 3472 | for (p = foldword; spell_iswordc(p); mb_ptr_adv(p)) |
| 3473 | { |
| 3474 | if (*p == NUL) |
| 3475 | break; |
| 3476 | ++clen; |
| 3477 | } |
| 3478 | if (*p != NUL) |
| 3479 | { |
| 3480 | /* Move the addition to "addstring" and truncate "foldword". */ |
| 3481 | if (flags & BWF_KEEPCAP) |
| 3482 | { |
| 3483 | /* Preserve caps, need to skip the right number of |
| 3484 | * characters in the original word (case folding may |
| 3485 | * change the byte count). */ |
| 3486 | l = 0; |
| 3487 | for (cp = dw->dw_word; l < clen; mb_ptr_adv(cp)) |
| 3488 | ++l; |
| 3489 | addlen = STRLEN(cp); |
| 3490 | mch_memmove(addstring, cp, addlen + 1); |
| 3491 | } |
| 3492 | else |
| 3493 | { |
| 3494 | addlen = STRLEN(p); |
| 3495 | mch_memmove(addstring, p, addlen + 1); |
| 3496 | } |
| 3497 | *p = NUL; |
| 3498 | } |
| 3499 | |
| 3500 | bw = (basicword_T *)alloc_clear((unsigned)sizeof(basicword_T) |
| 3501 | + STRLEN(foldword)); |
| 3502 | if (bw == NULL) |
| 3503 | break; |
| 3504 | STRCPY(bw->bw_word, foldword); |
| 3505 | bw->bw_region = regionmask; |
| 3506 | |
| 3507 | if (leadlen > 0) |
| 3508 | bw->bw_leadstring = vim_strsave(leadstring); |
| 3509 | else |
| 3510 | bw->bw_leadstring = NULL; |
| 3511 | if (addlen > 0) |
| 3512 | bw->bw_addstring = vim_strsave(addstring); |
| 3513 | else |
| 3514 | bw->bw_addstring = NULL; |
| 3515 | |
| 3516 | add_to_wordlist(newwords, bw); |
| 3517 | |
| 3518 | if (flags & BWF_KEEPCAP) |
| 3519 | { |
| 3520 | if (addlen == 0) |
| 3521 | /* use the whole word */ |
| 3522 | bw->bw_caseword = vim_strsave(dw->dw_word + leadlen); |
| 3523 | else |
| 3524 | /* use only up to the addition */ |
| 3525 | bw->bw_caseword = vim_strnsave(dw->dw_word + leadlen, |
| 3526 | cp - dw->dw_word - leadlen); |
| 3527 | if (bw->bw_caseword == NULL) /* out of memory */ |
| 3528 | flags &= ~BWF_KEEPCAP; |
| 3529 | } |
| 3530 | bw->bw_flags = flags; |
| 3531 | |
| 3532 | /* Deal with any affix names on the old word, translate them |
| 3533 | * into affix numbers. */ |
| 3534 | ga_init2(&bw->bw_prefix, sizeof(short_u), 10); |
| 3535 | ga_init2(&bw->bw_suffix, sizeof(short_u), 10); |
| 3536 | if (dw->dw_affnm != NULL) |
| 3537 | trans_affixes(dw, bw, oldaff, newwords); |
| 3538 | } |
| 3539 | } |
| 3540 | if (todo > 0) |
| 3541 | return FAIL; |
| 3542 | return OK; |
| 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * Go through the list of words and combine the ones that are identical except |
| 3547 | * for the region. |
| 3548 | */ |
| 3549 | static void |
| 3550 | combine_regions(newwords) |
| 3551 | hashtab_T *newwords; |
| 3552 | { |
| 3553 | int todo; |
| 3554 | hashitem_T *hi; |
| 3555 | basicword_T *bw, *nbw, *pbw; |
| 3556 | |
| 3557 | /* Loop over all basic words in the words table. */ |
| 3558 | todo = newwords->ht_used; |
| 3559 | for (hi = newwords->ht_array; todo > 0; ++hi) |
| 3560 | { |
| 3561 | if (!HASHITEM_EMPTY(hi)) |
| 3562 | { |
| 3563 | --todo; |
| 3564 | |
| 3565 | /* Loop over the list of words for this basic word. Compare with |
| 3566 | * each following word in the same list. */ |
| 3567 | for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next) |
| 3568 | { |
| 3569 | pbw = bw; |
| 3570 | for (nbw = pbw->bw_next; nbw != NULL; nbw = pbw->bw_next) |
| 3571 | { |
| 3572 | if (bw->bw_flags == nbw->bw_flags |
| 3573 | && (bw->bw_leadstring == NULL) |
| 3574 | == (nbw->bw_leadstring == NULL) |
| 3575 | && (bw->bw_addstring == NULL) |
| 3576 | == (nbw->bw_addstring == NULL) |
| 3577 | && ((bw->bw_flags & BWF_KEEPCAP) == 0 |
| 3578 | || (STRCMP(bw->bw_caseword, |
| 3579 | nbw->bw_caseword) == 0)) |
| 3580 | && (bw->bw_leadstring == NULL |
| 3581 | || (STRCMP(bw->bw_leadstring, |
| 3582 | nbw->bw_leadstring) == 0)) |
| 3583 | && (bw->bw_addstring == NULL |
| 3584 | || (STRCMP(bw->bw_addstring, |
| 3585 | nbw->bw_addstring) == 0)) |
| 3586 | && same_affixes(bw, nbw) |
| 3587 | ) |
| 3588 | { |
| 3589 | /* Match, combine regions and delete "nbw". */ |
| 3590 | pbw->bw_next = nbw->bw_next; |
| 3591 | bw->bw_region |= nbw->bw_region; |
| 3592 | free_basicword(nbw); |
| 3593 | } |
| 3594 | else |
| 3595 | /* No match, continue with next one. */ |
| 3596 | pbw = nbw; |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | /* |
| 3604 | * Return TRUE when the prefixes and suffixes for "bw" and "nbw" are equal. |
| 3605 | */ |
| 3606 | static int |
| 3607 | same_affixes(bw, nbw) |
| 3608 | basicword_T *bw; |
| 3609 | basicword_T *nbw; |
| 3610 | { |
| 3611 | return (bw->bw_prefix.ga_len == nbw->bw_prefix.ga_len |
| 3612 | && bw->bw_suffix.ga_len == nbw->bw_suffix.ga_len |
| 3613 | && (bw->bw_prefix.ga_len == 0 |
| 3614 | || vim_memcmp(bw->bw_prefix.ga_data, |
| 3615 | nbw->bw_prefix.ga_data, |
| 3616 | bw->bw_prefix.ga_len * sizeof(short_u)) == 0) |
| 3617 | && (bw->bw_suffix.ga_len == 0 |
| 3618 | || vim_memcmp(bw->bw_suffix.ga_data, |
| 3619 | nbw->bw_suffix.ga_data, |
| 3620 | bw->bw_suffix.ga_len * sizeof(short_u)) == 0)); |
| 3621 | } |
| 3622 | |
| 3623 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3624 | * For each basic word with additions turn the suffixes into other additions |
| 3625 | * and/or new basic words. For each basic word with a leadstring turn the |
| 3626 | * prefixes into other leadstrings and/or new basic words. |
| 3627 | * The result is that no affixes apply to the additions or leadstring of a |
| 3628 | * word. |
| 3629 | * This is also needed when a word with an addition has a prefix and the word |
| 3630 | * with prefix also exists. E.g., "blurp's/D" (D is prefix "de") and |
| 3631 | * "deblurp". "deblurp" would match and no prefix would be tried. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3632 | */ |
| 3633 | static void |
| 3634 | expand_affixes(newwords, prefgap, suffgap) |
| 3635 | hashtab_T *newwords; |
| 3636 | garray_T *prefgap; |
| 3637 | garray_T *suffgap; |
| 3638 | { |
| 3639 | int todo; |
| 3640 | hashitem_T *hi; |
| 3641 | basicword_T *bw; |
| 3642 | int pi, si; |
| 3643 | affentry_T *pae, *sae; |
| 3644 | garray_T add_words; |
| 3645 | int n; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3646 | char_u message[MAXLINELEN + MAXWLEN]; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3647 | |
| 3648 | ga_init2(&add_words, sizeof(basicword_T *), 10); |
| 3649 | |
| 3650 | todo = newwords->ht_used; |
| 3651 | for (hi = newwords->ht_array; todo > 0; ++hi) |
| 3652 | { |
| 3653 | if (!HASHITEM_EMPTY(hi)) |
| 3654 | { |
| 3655 | --todo; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 3656 | |
| 3657 | /* This takes time, print a message now and then. */ |
| 3658 | if ((todo & 0x3ff) == 0 || todo == (int)newwords->ht_used - 1) |
| 3659 | { |
| 3660 | sprintf((char *)message, _("%6d todo - %s"), |
| 3661 | todo, HI2BW(hi)->bw_word); |
| 3662 | msg_start(); |
| 3663 | msg_outtrans_attr(message, 0); |
| 3664 | msg_clr_eos(); |
| 3665 | msg_didout = FALSE; |
| 3666 | msg_col = 0; |
| 3667 | out_flush(); |
| 3668 | ui_breakcheck(); |
| 3669 | if (got_int) |
| 3670 | break; |
| 3671 | } |
| 3672 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3673 | for (bw = HI2BW(hi); bw != NULL; bw = bw->bw_next) |
| 3674 | { |
| 3675 | /* |
| 3676 | * Need to fix affixes if there is a leader or addition and |
| 3677 | * there are prefixes or suffixes. |
| 3678 | */ |
| 3679 | if ((bw->bw_leadstring != NULL || bw->bw_addstring != NULL) |
| 3680 | && (bw->bw_prefix.ga_len != 0 |
| 3681 | || bw->bw_suffix.ga_len != 0)) |
| 3682 | { |
| 3683 | /* Loop over all prefix numbers, but first without a |
| 3684 | * prefix. */ |
| 3685 | for (pi = -1; pi < bw->bw_prefix.ga_len; ++pi) |
| 3686 | { |
| 3687 | pae = NULL; |
| 3688 | if (pi >= 0) |
| 3689 | { |
| 3690 | n = ((short_u *)bw->bw_prefix.ga_data)[pi]; |
| 3691 | pae = ((affheader_T *)prefgap->ga_data + n) |
| 3692 | ->ah_first; |
| 3693 | } |
| 3694 | |
| 3695 | /* Loop over all entries for prefix "pi". Do it once |
| 3696 | * when there is no prefix (pi == -1). */ |
| 3697 | do |
| 3698 | { |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3699 | /* Skip prewords, they don't need to be expanded. */ |
| 3700 | if (pae == NULL || !pae->ae_preword) |
| 3701 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3702 | /* Loop over all suffix numbers. Do without a |
| 3703 | * suffix first when there is a prefix. */ |
| 3704 | for (si = (pi == -1 ? 0 : -1); |
| 3705 | si < bw->bw_suffix.ga_len; ++si) |
| 3706 | { |
| 3707 | sae = NULL; |
| 3708 | if (si >= 0) |
| 3709 | { |
| 3710 | n = ((short_u *)bw->bw_suffix.ga_data)[si]; |
| 3711 | sae = ((affheader_T *)suffgap->ga_data + n) |
| 3712 | ->ah_first; |
| 3713 | } |
| 3714 | |
| 3715 | /* Loop over all entries for suffix "si". Do |
| 3716 | * it once when there is no suffix (si == -1). |
| 3717 | */ |
| 3718 | do |
| 3719 | { |
| 3720 | /* Expand the word for this combination of |
| 3721 | * prefixes and affixes. */ |
| 3722 | expand_one_aff(bw, &add_words, pae, sae); |
| 3723 | |
| 3724 | /* Advance to next suffix entry, if there |
| 3725 | * is one. */ |
| 3726 | if (sae != NULL) |
| 3727 | sae = sae->ae_next; |
| 3728 | } while (sae != NULL); |
| 3729 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3730 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3731 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3732 | /* Advance to next prefix entry, if there is one. */ |
| 3733 | if (pae != NULL) |
| 3734 | pae = pae->ae_next; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3735 | } while (pae != NULL); |
| 3736 | } |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 | |
| 3742 | /* |
| 3743 | * Add the new words afterwards, can't change "newwords" while going over |
| 3744 | * all its items. |
| 3745 | */ |
| 3746 | for (pi = 0; pi < add_words.ga_len; ++pi) |
| 3747 | add_to_wordlist(newwords, ((basicword_T **)add_words.ga_data)[pi]); |
| 3748 | |
| 3749 | ga_clear(&add_words); |
| 3750 | } |
| 3751 | |
| 3752 | /* |
| 3753 | * Add one word to "add_words" for basic word "bw" with additions, adding |
| 3754 | * prefix "pae" and suffix "sae". Either "pae" or "sae" can be NULL. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3755 | * Don't do this when not necessary: |
| 3756 | * - no leadstring and adding prefix doesn't result in existing word. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3757 | */ |
| 3758 | static void |
| 3759 | expand_one_aff(bw, add_words, pae, sae) |
| 3760 | basicword_T *bw; |
| 3761 | garray_T *add_words; |
| 3762 | affentry_T *pae; |
| 3763 | affentry_T *sae; |
| 3764 | { |
| 3765 | char_u word[MAXWLEN + 1]; |
| 3766 | char_u caseword[MAXWLEN + 1]; |
| 3767 | int l = 0; |
| 3768 | int choplen = 0; |
| 3769 | int ll; |
| 3770 | basicword_T *nbw; |
| 3771 | |
| 3772 | /* Prepend prefix to the basic word if there is a prefix and there is no |
| 3773 | * leadstring. */ |
| 3774 | if (pae != NULL && bw->bw_leadstring == NULL) |
| 3775 | { |
| 3776 | if (pae->ae_add != NULL) |
| 3777 | { |
| 3778 | l = STRLEN(pae->ae_add); |
| 3779 | mch_memmove(word, pae->ae_add, l); |
| 3780 | } |
| 3781 | if (pae->ae_chop != NULL) |
| 3782 | choplen = STRLEN(pae->ae_chop); |
| 3783 | } |
| 3784 | |
| 3785 | /* Copy the body of the word. */ |
| 3786 | STRCPY(word + l, bw->bw_word + choplen); |
| 3787 | |
| 3788 | /* Do the same for bw_caseword, if it's there. */ |
| 3789 | if (bw->bw_flags & BWF_KEEPCAP) |
| 3790 | { |
| 3791 | if (l > 0) |
| 3792 | mch_memmove(caseword, pae->ae_add, l); |
| 3793 | STRCPY(caseword + l, bw->bw_caseword + choplen); |
| 3794 | } |
| 3795 | |
| 3796 | /* Append suffix to the basic word if there is a suffix and there is no |
| 3797 | * addstring. */ |
| 3798 | if (sae != 0 && bw->bw_addstring == NULL) |
| 3799 | { |
| 3800 | l = STRLEN(word); |
| 3801 | if (sae->ae_chop != NULL) |
| 3802 | l -= STRLEN(sae->ae_chop); |
| 3803 | if (sae->ae_add == NULL) |
| 3804 | word[l] = NUL; |
| 3805 | else |
| 3806 | STRCPY(word + l, sae->ae_add); |
| 3807 | |
| 3808 | if (bw->bw_flags & BWF_KEEPCAP) |
| 3809 | { |
| 3810 | /* Do the same for the caseword. */ |
| 3811 | l = STRLEN(caseword); |
| 3812 | if (sae->ae_chop != NULL) |
| 3813 | l -= STRLEN(sae->ae_chop); |
| 3814 | if (sae->ae_add == NULL) |
| 3815 | caseword[l] = NUL; |
| 3816 | else |
| 3817 | STRCPY(caseword + l, sae->ae_add); |
| 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | nbw = (basicword_T *)alloc_clear((unsigned) |
| 3822 | sizeof(basicword_T) + STRLEN(word)); |
| 3823 | if (nbw != NULL) |
| 3824 | { |
| 3825 | /* Add the new word to the list of words to be added later. */ |
| 3826 | if (ga_grow(add_words, 1) == FAIL) |
| 3827 | { |
| 3828 | vim_free(nbw); |
| 3829 | return; |
| 3830 | } |
| 3831 | ((basicword_T **)add_words->ga_data)[add_words->ga_len++] = nbw; |
| 3832 | |
| 3833 | /* Copy the (modified) basic word, flags and region. */ |
| 3834 | STRCPY(nbw->bw_word, word); |
| 3835 | nbw->bw_flags = bw->bw_flags; |
| 3836 | nbw->bw_region = bw->bw_region; |
| 3837 | |
| 3838 | /* Set the (modified) caseword. */ |
| 3839 | if (bw->bw_flags & BWF_KEEPCAP) |
| 3840 | if ((nbw->bw_caseword = vim_strsave(caseword)) == NULL) |
| 3841 | nbw->bw_flags &= ~BWF_KEEPCAP; |
| 3842 | |
| 3843 | if (bw->bw_leadstring != NULL) |
| 3844 | { |
| 3845 | if (pae != NULL) |
| 3846 | { |
| 3847 | /* Prepend prefix to leadstring. */ |
| 3848 | ll = STRLEN(bw->bw_leadstring); |
| 3849 | l = choplen = 0; |
| 3850 | if (pae->ae_add != NULL) |
| 3851 | l = STRLEN(pae->ae_add); |
| 3852 | if (pae->ae_chop != NULL) |
| 3853 | { |
| 3854 | choplen = STRLEN(pae->ae_chop); |
| 3855 | if (choplen > ll) /* TODO: error? */ |
| 3856 | choplen = ll; |
| 3857 | } |
| 3858 | nbw->bw_leadstring = alloc((unsigned)(ll + l - choplen + 1)); |
| 3859 | if (nbw->bw_leadstring != NULL) |
| 3860 | { |
| 3861 | if (l > 0) |
| 3862 | mch_memmove(nbw->bw_leadstring, pae->ae_add, l); |
| 3863 | STRCPY(nbw->bw_leadstring + l, bw->bw_leadstring + choplen); |
| 3864 | } |
| 3865 | } |
| 3866 | else |
| 3867 | nbw->bw_leadstring = vim_strsave(bw->bw_leadstring); |
| 3868 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3869 | else if (bw->bw_prefix.ga_len > 0) |
| 3870 | { |
| 3871 | /* There is no leadstring, copy the list of possible prefixes. */ |
| 3872 | ga_init2(&nbw->bw_prefix, sizeof(short_u), 1); |
| 3873 | if (ga_grow(&nbw->bw_prefix, bw->bw_prefix.ga_len) == OK) |
| 3874 | { |
| 3875 | mch_memmove(nbw->bw_prefix.ga_data, bw->bw_prefix.ga_data, |
| 3876 | bw->bw_prefix.ga_len * sizeof(short_u)); |
| 3877 | nbw->bw_prefix.ga_len = bw->bw_prefix.ga_len; |
| 3878 | } |
| 3879 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3880 | |
| 3881 | if (bw->bw_addstring != NULL) |
| 3882 | { |
| 3883 | if (sae != NULL) |
| 3884 | { |
| 3885 | /* Append suffix to addstring. */ |
| 3886 | l = STRLEN(bw->bw_addstring); |
| 3887 | if (sae->ae_chop != NULL) |
| 3888 | { |
| 3889 | l -= STRLEN(sae->ae_chop); |
| 3890 | if (l < 0) /* TODO: error? */ |
| 3891 | l = 0; |
| 3892 | } |
| 3893 | if (sae->ae_add == NULL) |
| 3894 | ll = 0; |
| 3895 | else |
| 3896 | ll = STRLEN(sae->ae_add); |
| 3897 | nbw->bw_addstring = alloc((unsigned)(ll + l - choplen + 1)); |
| 3898 | if (nbw->bw_addstring != NULL) |
| 3899 | { |
| 3900 | STRCPY(nbw->bw_addstring, bw->bw_addstring); |
| 3901 | if (sae->ae_add == NULL) |
| 3902 | nbw->bw_addstring[l] = NUL; |
| 3903 | else |
| 3904 | STRCPY(nbw->bw_addstring + l, sae->ae_add); |
| 3905 | } |
| 3906 | } |
| 3907 | else |
| 3908 | nbw->bw_addstring = vim_strsave(bw->bw_addstring); |
| 3909 | } |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /* |
| 3914 | * Add basicword_T "*bw" to wordlist "newwords". |
| 3915 | */ |
| 3916 | static void |
| 3917 | add_to_wordlist(newwords, bw) |
| 3918 | hashtab_T *newwords; |
| 3919 | basicword_T *bw; |
| 3920 | { |
| 3921 | hashitem_T *hi; |
| 3922 | basicword_T *bw2; |
| 3923 | |
| 3924 | hi = hash_find(newwords, bw->bw_word); |
| 3925 | if (HASHITEM_EMPTY(hi)) |
| 3926 | { |
| 3927 | /* New entry, add to hashlist. */ |
| 3928 | hash_add(newwords, bw->bw_word); |
| 3929 | bw->bw_next = NULL; |
| 3930 | } |
| 3931 | else |
| 3932 | { |
| 3933 | /* Existing entry, append to list of basic words. */ |
| 3934 | bw2 = HI2BW(hi); |
| 3935 | bw->bw_next = bw2->bw_next; |
| 3936 | bw2->bw_next = bw; |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | /* |
| 3941 | * Write a number to file "fd", MSB first, in "len" bytes. |
| 3942 | */ |
| 3943 | static void |
| 3944 | put_bytes(fd, nr, len) |
| 3945 | FILE *fd; |
| 3946 | long_u nr; |
| 3947 | int len; |
| 3948 | { |
| 3949 | int i; |
| 3950 | |
| 3951 | for (i = len - 1; i >= 0; --i) |
| 3952 | putc((int)(nr >> (i * 8)), fd); |
| 3953 | } |
| 3954 | |
| 3955 | /* |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3956 | * Write affix info. <affitemcnt> <affitem> ... |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3957 | */ |
| 3958 | static void |
| 3959 | write_affix(fd, ah) |
| 3960 | FILE *fd; |
| 3961 | affheader_T *ah; |
| 3962 | { |
| 3963 | int i = 0; |
| 3964 | affentry_T *ae; |
| 3965 | char_u *p; |
| 3966 | int round; |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3967 | int flags; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3968 | |
| 3969 | /* Count the number of entries. */ |
| 3970 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
| 3971 | ++i; |
| 3972 | put_bytes(fd, (long_u)i, 2); /* <affitemcnt> */ |
| 3973 | |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3974 | /* <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> */ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3975 | for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next) |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3976 | { |
| 3977 | flags = ah->ah_combine ? AFF_COMBINE : 0; |
| 3978 | if (ae->ae_preword) |
| 3979 | flags |= AFF_PREWORD; |
| 3980 | fputc(flags, fd); /* <affflags> */ |
| 3981 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3982 | for (round = 1; round <= 2; ++round) |
| 3983 | { |
| 3984 | p = round == 1 ? ae->ae_chop : ae->ae_add; |
| 3985 | if (p == NULL) |
| 3986 | putc(0, fd); /* <affchoplen> / <affaddlen> */ |
| 3987 | else |
| 3988 | { |
| 3989 | putc(STRLEN(p), fd); /* <affchoplen> / <affaddlen> */ |
| 3990 | /* <affchop> / <affadd> */ |
| 3991 | fwrite(p, STRLEN(p), (size_t)1, fd); |
| 3992 | } |
| 3993 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 3994 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | /* |
| 3998 | * Write list of affix NRs: <affixcnt> <affixNR> ... |
| 3999 | */ |
| 4000 | static void |
| 4001 | write_affixlist(fd, aff, bytes) |
| 4002 | FILE *fd; |
| 4003 | garray_T *aff; |
| 4004 | int bytes; |
| 4005 | { |
| 4006 | int i; |
| 4007 | |
| 4008 | if (aff->ga_len > 0) |
| 4009 | { |
| 4010 | putc(aff->ga_len, fd); /* <affixcnt> */ |
| 4011 | for (i = 0; i < aff->ga_len; ++i) |
| 4012 | put_bytes(fd, (long_u )((short_u *)aff->ga_data)[i], bytes); |
| 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | /* |
| 4017 | * Vim spell file format: <HEADER> <PREFIXLIST> <SUFFIXLIST> |
| 4018 | * <SUGGEST> <WORDLIST> |
| 4019 | * |
| 4020 | * <HEADER>: <fileID> <regioncnt> <regionname> ... |
| 4021 | * |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4022 | * <fileID> 10 bytes "VIMspell02" |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4023 | * <regioncnt> 1 byte number of regions following (8 supported) |
| 4024 | * <regionname> 2 bytes Region name: ca, au, etc. |
| 4025 | * First <regionname> is region 1. |
| 4026 | * |
| 4027 | * |
| 4028 | * <PREFIXLIST>: <affcount> <afftotcnt> <affix> ... |
| 4029 | * <SUFFIXLIST>: <affcount> <afftotcnt> <affix> ... |
| 4030 | * list of possible affixes: prefixes and suffixes. |
| 4031 | * |
| 4032 | * <affcount> 2 bytes Number of affixes (MSB comes first). |
| 4033 | * When more than 256 an affixNR is 2 bytes. |
| 4034 | * This is separate for prefixes and suffixes! |
| 4035 | * First affixNR is 0. |
| 4036 | * <afftotcnt> 2 bytes Total number of affix items (MSB comes first). |
| 4037 | * |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4038 | * <affix>: <affitemcnt> <affitem> ... |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4039 | * |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4040 | * <affitemcnt> 2 bytes Number of affixes with this affixNR (MSB first). |
| 4041 | * |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4042 | * <affitem>: <affflags> <affchoplen> <affchop> <affaddlen> <affadd> |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4043 | * |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4044 | * <affflags> 1 byte 0x01: prefix combines with suffix, AFF_COMBINE |
| 4045 | * 0x02: prefix includes word, AFF_PREWORD |
| 4046 | * 0x04-0x80: unset |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4047 | * <affchoplen> 1 byte Length of <affchop> in bytes. |
| 4048 | * <affchop> N bytes To be removed from basic word. |
| 4049 | * <affaddlen> 1 byte Length of <affadd> in bytes. |
| 4050 | * <affadd> N bytes To be added to basic word. |
| 4051 | * |
| 4052 | * |
| 4053 | * <SUGGEST> : <suggestlen> <more> ... |
| 4054 | * |
| 4055 | * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding |
| 4056 | * <suggestlen>. MSB first. |
| 4057 | * <more> To be defined. |
| 4058 | * |
| 4059 | * |
| 4060 | * <WORDLIST>: <wordcount> <worditem> ... |
| 4061 | * |
| 4062 | * <wordcount> 4 bytes Number of <worditem> following. MSB first. |
| 4063 | * |
| 4064 | * <worditem>: <nr> <string> <flags> [<flags2>] |
| 4065 | * [<caselen> <caseword>] |
| 4066 | * [<affixcnt> <affixNR> ...] (prefixes) |
| 4067 | * [<affixcnt> <affixNR> ...] (suffixes) |
| 4068 | * [<region>] |
| 4069 | * [<addcnt> <add> ...] |
| 4070 | * |
| 4071 | * <nr> i 1 byte Number of bytes copied from previous word. |
| 4072 | * <string> N bytes Additional bytes for word, up to byte smaller than |
| 4073 | * 0x20 (space). |
| 4074 | * Must only contain case-folded word characters. |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4075 | * <flags> 1 byte 0x01: word is valid without addition, BWF_VALID |
| 4076 | * 0x02: has region byte, BWF_REGION |
| 4077 | * 0x04: first letter must be upper-case, BWF_ONECAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4078 | * 0x08: has suffixes, <affixcnt> and <affixNR> follow |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4079 | * BWF_SUFFIX |
| 4080 | * 0x10: more flags, <flags2> follows next, BWF_SECOND |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4081 | * 0x20-0x80: can't be used, unset |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4082 | * <flags2> 1 byte 0x01: has additions, <addcnt> and <add> follow, |
| 4083 | * BWF_ADDS |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4084 | * 0x02: has prefixes, <affixcnt> and <affixNR> follow |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4085 | * BWF_PREFIX |
| 4086 | * 0x04: all letters must be upper-case, BWF_ALLCAP |
| 4087 | * 0x08: case must match, BWF_KEEPCAP |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4088 | * 0x10-0x80: unset |
| 4089 | * <caselen> 1 byte Length of <caseword>. |
| 4090 | * <caseword> N bytes Word with matching case. |
| 4091 | * <affixcnt> 1 byte Number of affix NRs following. |
| 4092 | * <affixNR> 1 or 2 byte Number of possible affix for this word. |
| 4093 | * When using 2 bytes MSB comes first. |
| 4094 | * <region> 1 byte Bitmask for regions in which word is valid. When |
| 4095 | * omitted it's valid in all regions. |
| 4096 | * Lowest bit is for region 1. |
| 4097 | * <addcnt> 2 bytes Number of <add> items following. |
| 4098 | * |
| 4099 | * <add>: <addflags> <addlen> [<leadlen> <addstring>] [<region>] |
| 4100 | * |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4101 | * <addflags> 1 byte 0x01: unset |
| 4102 | * 0x02: has region byte, ADD_REGION |
| 4103 | * 0x04: first letter must be upper-case, ADD_ONECAP |
| 4104 | * 0x08-0x20: unset |
| 4105 | * 0x40: all letters must be upper-case, ADD_ALLCAP |
| 4106 | * 0x80: fixed case, <addstring> is the whole word |
| 4107 | * with matching case, ADD_KEEPCAP. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4108 | * <addlen> 1 byte Length of <addstring> in bytes. |
| 4109 | * <leadlen> 1 byte Number of bytes at start of <addstring> that must |
| 4110 | * come before the start of the basic word. |
| 4111 | * <addstring> N bytes Word characters, before/in/after the word. |
| 4112 | * |
| 4113 | * All text characters are in 'encoding': <affchop>, <affadd>, <string>, |
| 4114 | * <caseword>> and <addstring>. |
| 4115 | * All other fields are ASCII: <regionname> |
| 4116 | * <string> is always case-folded. |
| 4117 | */ |
| 4118 | |
| 4119 | /* |
| 4120 | * Write the Vim spell file "fname". |
| 4121 | */ |
| 4122 | static void |
| 4123 | write_vim_spell(fname, prefga, suffga, newwords, regcount, regchars) |
| 4124 | char_u *fname; |
| 4125 | garray_T *prefga; /* prefixes, affheader_T entries */ |
| 4126 | garray_T *suffga; /* suffixes, affheader_T entries */ |
| 4127 | hashtab_T *newwords; /* basic words, basicword_T entries */ |
| 4128 | int regcount; /* number of regions */ |
| 4129 | char_u *regchars; /* region names */ |
| 4130 | { |
| 4131 | FILE *fd; |
| 4132 | garray_T *gap; |
| 4133 | hashitem_T *hi; |
| 4134 | char_u **wtab; |
| 4135 | int todo; |
| 4136 | int flags, aflags; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4137 | basicword_T *bw, *bwf, *bw2 = NULL, *prevbw = NULL; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4138 | int regionmask; /* mask for all relevant region bits */ |
| 4139 | int i; |
| 4140 | int cnt; |
| 4141 | affentry_T *ae; |
| 4142 | int round; |
| 4143 | int prefm, suffm; |
| 4144 | garray_T bwga; |
| 4145 | |
| 4146 | fd = fopen((char *)fname, "w"); |
| 4147 | if (fd == NULL) |
| 4148 | { |
| 4149 | EMSG2(_(e_notopen), fname); |
| 4150 | return; |
| 4151 | } |
| 4152 | |
| 4153 | fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd); |
| 4154 | |
| 4155 | /* write the region names if there is more than one */ |
| 4156 | if (regcount > 1) |
| 4157 | { |
| 4158 | putc(regcount, fd); |
| 4159 | fwrite(regchars, (size_t)(regcount * 2), (size_t)1, fd); |
| 4160 | regionmask = (1 << regcount) - 1; |
| 4161 | } |
| 4162 | else |
| 4163 | { |
| 4164 | putc(0, fd); |
| 4165 | regionmask = 0; |
| 4166 | } |
| 4167 | |
| 4168 | /* Write the prefix and suffix lists. */ |
| 4169 | for (round = 1; round <= 2; ++round) |
| 4170 | { |
| 4171 | gap = round == 1 ? prefga : suffga; |
| 4172 | put_bytes(fd, (long_u)gap->ga_len, 2); /* <affcount> */ |
| 4173 | |
| 4174 | /* Count the total number of affix items. */ |
| 4175 | cnt = 0; |
| 4176 | for (i = 0; i < gap->ga_len; ++i) |
| 4177 | for (ae = ((affheader_T *)gap->ga_data + i)->ah_first; |
| 4178 | ae != NULL; ae = ae->ae_next) |
| 4179 | ++cnt; |
| 4180 | put_bytes(fd, (long_u)cnt, 2); /* <afftotcnt> */ |
| 4181 | |
| 4182 | for (i = 0; i < gap->ga_len; ++i) |
| 4183 | write_affix(fd, (affheader_T *)gap->ga_data + i); |
| 4184 | } |
| 4185 | |
| 4186 | /* Number of bytes used for affix NR depends on affix count. */ |
| 4187 | prefm = (prefga->ga_len > 256) ? 2 : 1; |
| 4188 | suffm = (suffga->ga_len > 256) ? 2 : 1; |
| 4189 | |
| 4190 | /* Write the suggest info. TODO */ |
| 4191 | put_bytes(fd, 0L, 4); |
| 4192 | |
| 4193 | /* |
| 4194 | * Write the word list. <wordcount> <worditem> ... |
| 4195 | */ |
| 4196 | /* number of basic words in 4 bytes */ |
| 4197 | put_bytes(fd, newwords->ht_used, 4); /* <wordcount> */ |
| 4198 | |
| 4199 | /* |
| 4200 | * Sort the word list, so that we can reuse as many bytes as possible. |
| 4201 | */ |
| 4202 | wtab = (char_u **)alloc((unsigned)(sizeof(char_u *) * newwords->ht_used)); |
| 4203 | if (wtab != NULL) |
| 4204 | { |
| 4205 | /* Make a table with pointers to each word. */ |
| 4206 | todo = newwords->ht_used; |
| 4207 | for (hi = newwords->ht_array; todo > 0; ++hi) |
| 4208 | if (!HASHITEM_EMPTY(hi)) |
| 4209 | wtab[--todo] = hi->hi_key; |
| 4210 | |
| 4211 | /* Sort. */ |
| 4212 | sort_strings(wtab, (int)newwords->ht_used); |
| 4213 | |
| 4214 | /* Now write each basic word to the spell file. */ |
| 4215 | ga_init2(&bwga, sizeof(basicword_T *), 10); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4216 | for (todo = 0; (long_u)todo < newwords->ht_used; ++todo) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4217 | { |
| 4218 | bwf = KEY2BW(wtab[todo]); |
| 4219 | |
| 4220 | /* |
| 4221 | * Reorder the list of basicword_T words: make a list for words |
| 4222 | * with the same case-folded word. Put them together for same |
| 4223 | * caps (ONECAP, ALLCAP and various KEEPCAP words) and same |
| 4224 | * affixes. Each list will then be put as a basic word with |
| 4225 | * additions. |
| 4226 | * This won't take much space, since the basic word is the same |
| 4227 | * every time, only its length is written. |
| 4228 | */ |
| 4229 | bwga.ga_len = 0; |
| 4230 | for (bw = bwf; bw != NULL; bw = bw->bw_next) |
| 4231 | { |
| 4232 | flags = bw->bw_flags & (BWF_ONECAP | BWF_KEEPCAP | BWF_ALLCAP); |
| 4233 | |
| 4234 | /* Go through the lists we found so far. Break when the case |
| 4235 | * matches. */ |
| 4236 | for (i = 0; i < bwga.ga_len; ++i) |
| 4237 | { |
| 4238 | bw2 = ((basicword_T **)bwga.ga_data)[i]; |
| 4239 | aflags = bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP |
| 4240 | | BWF_ALLCAP); |
| 4241 | if (flags == aflags |
| 4242 | && ((flags & BWF_KEEPCAP) == 0 |
| 4243 | || (STRCMP(bw->bw_caseword, |
| 4244 | bw2->bw_caseword) == 0)) |
| 4245 | && same_affixes(bw, bw2)) |
| 4246 | break; |
| 4247 | } |
| 4248 | if (i == bwga.ga_len) |
| 4249 | { |
| 4250 | /* No word with similar caps, make a new list. */ |
| 4251 | if (ga_grow(&bwga, 1) == FAIL) |
| 4252 | break; |
| 4253 | ((basicword_T **)bwga.ga_data)[i] = bw; |
| 4254 | bw->bw_cnext = NULL; |
| 4255 | ++bwga.ga_len; |
| 4256 | } |
| 4257 | else |
| 4258 | { |
| 4259 | /* Add to list of words with similar caps. */ |
| 4260 | bw->bw_cnext = bw2->bw_cnext; |
| 4261 | bw2->bw_cnext = bw; |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | /* Prefer the word with no caps to use as the first basic word. |
| 4266 | * At least one without KEEPCAP. */ |
| 4267 | bw = NULL; |
| 4268 | for (i = 0; i < bwga.ga_len; ++i) |
| 4269 | { |
| 4270 | bw2 = ((basicword_T **)bwga.ga_data)[i]; |
| 4271 | if (bw == NULL |
| 4272 | || (bw2->bw_flags & (BWF_ONECAP | BWF_KEEPCAP |
| 4273 | | BWF_ALLCAP)) == 0 |
| 4274 | || (bw->bw_flags & BWF_KEEPCAP)) |
| 4275 | bw = bw2; |
| 4276 | } |
| 4277 | |
| 4278 | /* Write first basic word. If it's KEEPCAP then we need a word |
| 4279 | * without VALID flag first (makes it easier to read the list back |
| 4280 | * in). */ |
| 4281 | if (bw->bw_flags & BWF_KEEPCAP) |
| 4282 | write_bword(fd, bw, TRUE, &prevbw, regionmask, prefm, suffm); |
| 4283 | write_bword(fd, bw, FALSE, &prevbw, regionmask, prefm, suffm); |
| 4284 | |
| 4285 | /* Write other basic words, with different caps. */ |
| 4286 | for (i = 0; i < bwga.ga_len; ++i) |
| 4287 | { |
| 4288 | bw2 = ((basicword_T **)bwga.ga_data)[i]; |
| 4289 | if (bw2 != bw) |
| 4290 | write_bword(fd, bw2, FALSE, &prevbw, regionmask, |
| 4291 | prefm, suffm); |
| 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | ga_clear(&bwga); |
| 4296 | } |
| 4297 | |
| 4298 | fclose(fd); |
| 4299 | } |
| 4300 | |
| 4301 | /* |
| 4302 | * Write basic word, followed by any additions. |
| 4303 | * |
| 4304 | * <worditem>: <nr> <string> <flags> [<flags2>] |
| 4305 | * [<caselen> <caseword>] |
| 4306 | * [<affixcnt> <affixNR> ...] (prefixes) |
| 4307 | * [<affixcnt> <affixNR> ...] (suffixes) |
| 4308 | * [<region>] |
| 4309 | * [<addcnt> <add> ...] |
| 4310 | */ |
| 4311 | static void |
| 4312 | write_bword(fd, bwf, lowcap, prevbw, regionmask, prefm, suffm) |
| 4313 | FILE *fd; |
| 4314 | basicword_T *bwf; |
| 4315 | int lowcap; /* write KEEPKAP word as not-valid */ |
| 4316 | basicword_T **prevbw; /* last written basic word */ |
| 4317 | int regionmask; /* mask that includes all possible regions */ |
| 4318 | int prefm; |
| 4319 | int suffm; |
| 4320 | { |
| 4321 | int flags; |
| 4322 | int aflags; |
| 4323 | int len; |
| 4324 | int leadlen, addlen; |
| 4325 | int clen; |
| 4326 | int adds = 0; |
| 4327 | int i; |
| 4328 | basicword_T *bw, *bw2; |
| 4329 | |
| 4330 | /* Check how many bytes can be copied from the previous word. */ |
| 4331 | len = STRLEN(bwf->bw_word); |
| 4332 | if (*prevbw == NULL) |
| 4333 | clen = 0; |
| 4334 | else |
| 4335 | for (clen = 0; clen < len |
| 4336 | && (*prevbw)->bw_word[clen] == bwf->bw_word[clen]; ++clen) |
| 4337 | ; |
| 4338 | putc(clen, fd); /* <nr> */ |
| 4339 | *prevbw = bwf; |
| 4340 | /* <string> */ |
| 4341 | if (len > clen) |
| 4342 | fwrite(bwf->bw_word + clen, (size_t)(len - clen), (size_t)1, fd); |
| 4343 | |
| 4344 | /* Try to find a word without additions to use first. */ |
| 4345 | bw = bwf; |
| 4346 | for (bw2 = bwf; bw2 != NULL; bw2 = bw2->bw_cnext) |
| 4347 | { |
| 4348 | if (bw2->bw_addstring != NULL || bw2->bw_leadstring != NULL) |
| 4349 | ++adds; |
| 4350 | else |
| 4351 | bw = bw2; |
| 4352 | } |
| 4353 | |
| 4354 | /* Flags: If there is no leadstring and no addstring the basic word is |
| 4355 | * valid, may have prefixes, suffixes and region. */ |
| 4356 | flags = bw->bw_flags; |
| 4357 | if (bw->bw_addstring == NULL && bw->bw_leadstring == NULL) |
| 4358 | { |
| 4359 | flags |= BWF_VALID; |
| 4360 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4361 | /* Flags: add the region byte if the word isn't valid in all |
| 4362 | * regions. */ |
| 4363 | if (regionmask != 0 && (bw->bw_region & regionmask) != regionmask) |
| 4364 | flags |= BWF_REGION; |
| 4365 | } |
Bram Moolenaar | 63d5a1e | 2005-04-19 21:30:25 +0000 | [diff] [blame] | 4366 | /* Add the prefix/suffix list if there are prefixes/suffixes. */ |
| 4367 | if (bw->bw_leadstring == NULL && bw->bw_prefix.ga_len > 0) |
| 4368 | flags |= BWF_PREFIX; |
| 4369 | if (bw->bw_addstring == NULL && bw->bw_suffix.ga_len > 0) |
| 4370 | flags |= BWF_SUFFIX; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4371 | |
| 4372 | /* Flags: may have additions. */ |
| 4373 | if (adds > 0) |
| 4374 | flags |= BWF_ADDS; |
| 4375 | |
| 4376 | /* The dummy word before a KEEPCAP word doesn't have any flags, they are |
| 4377 | * in the actual word that follows. */ |
| 4378 | if (lowcap) |
| 4379 | flags = 0; |
| 4380 | |
| 4381 | /* Flags: when the upper byte is not used we only write one flags |
| 4382 | * byte, if it's used then set an extra flag in the first byte and |
| 4383 | * also write the second byte. */ |
| 4384 | if ((flags & 0xff00) == 0) |
| 4385 | putc(flags, fd); /* <flags> */ |
| 4386 | else |
| 4387 | { |
| 4388 | putc(flags | BWF_SECOND, fd); /* <flags> */ |
| 4389 | putc((int)((unsigned)flags >> 8), fd); /* <flags2> */ |
| 4390 | } |
| 4391 | |
| 4392 | /* First dummy word doesn't need anything but flags. */ |
| 4393 | if (lowcap) |
| 4394 | return; |
| 4395 | |
| 4396 | if (flags & BWF_KEEPCAP) |
| 4397 | { |
| 4398 | len = STRLEN(bw->bw_caseword); |
| 4399 | putc(len, fd); /* <caselen> */ |
| 4400 | for (i = 0; i < len; ++i) |
| 4401 | putc(bw->bw_caseword[i], fd); /* <caseword> */ |
| 4402 | } |
| 4403 | |
| 4404 | /* write prefix and suffix lists: <affixcnt> <affixNR> ... */ |
| 4405 | if (flags & BWF_PREFIX) |
| 4406 | write_affixlist(fd, &bw->bw_prefix, prefm); |
| 4407 | if (flags & BWF_SUFFIX) |
| 4408 | write_affixlist(fd, &bw->bw_suffix, suffm); |
| 4409 | |
| 4410 | if (flags & BWF_REGION) |
| 4411 | putc(bw->bw_region, fd); /* <region> */ |
| 4412 | |
| 4413 | /* |
| 4414 | * Additions. |
| 4415 | */ |
| 4416 | if (adds > 0) |
| 4417 | { |
| 4418 | put_bytes(fd, (long_u)adds, 2); /* <addcnt> */ |
| 4419 | |
| 4420 | for (bw = bwf; bw != NULL; bw = bw->bw_cnext) |
| 4421 | if (bw->bw_leadstring != NULL || bw->bw_addstring != NULL) |
| 4422 | { |
| 4423 | /* <add>: <addflags> <addlen> [<leadlen> <addstring>] |
| 4424 | * [<region>] */ |
| 4425 | aflags = 0; |
| 4426 | if (bw->bw_flags & BWF_ONECAP) |
| 4427 | aflags |= ADD_ONECAP; |
| 4428 | if (bw->bw_flags & BWF_ALLCAP) |
| 4429 | aflags |= ADD_ALLCAP; |
| 4430 | if (bw->bw_flags & BWF_KEEPCAP) |
| 4431 | aflags |= ADD_KEEPCAP; |
| 4432 | if (regionmask != 0 |
| 4433 | && (bw->bw_region & regionmask) != regionmask) |
| 4434 | aflags |= ADD_REGION; |
| 4435 | putc(aflags, fd); /* <addflags> */ |
| 4436 | |
| 4437 | if (bw->bw_leadstring == NULL) |
| 4438 | leadlen = 0; |
| 4439 | else |
| 4440 | leadlen = STRLEN(bw->bw_leadstring); |
| 4441 | if (bw->bw_addstring == NULL) |
| 4442 | addlen = 0; |
| 4443 | else |
| 4444 | addlen = STRLEN(bw->bw_addstring); |
| 4445 | putc(leadlen + addlen, fd); /* <addlen> */ |
| 4446 | putc(leadlen, fd); /* <leadlen> */ |
| 4447 | /* <addstring> */ |
| 4448 | if (bw->bw_leadstring != NULL) |
| 4449 | fwrite(bw->bw_leadstring, (size_t)leadlen, (size_t)1, fd); |
| 4450 | if (bw->bw_addstring != NULL) |
| 4451 | fwrite(bw->bw_addstring, (size_t)addlen, (size_t)1, fd); |
| 4452 | |
| 4453 | if (aflags & ADD_REGION) |
| 4454 | putc(bw->bw_region, fd); /* <region> */ |
| 4455 | } |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | |
| 4460 | /* |
| 4461 | * ":mkspell outfile infile ..." |
| 4462 | */ |
| 4463 | void |
| 4464 | ex_mkspell(eap) |
| 4465 | exarg_T *eap; |
| 4466 | { |
| 4467 | int fcount; |
| 4468 | char_u **fnames; |
| 4469 | char_u fname[MAXPATHL]; |
| 4470 | char_u wfname[MAXPATHL]; |
| 4471 | afffile_T *(afile[8]); |
| 4472 | hashtab_T dfile[8]; |
| 4473 | int i; |
| 4474 | int len; |
| 4475 | char_u region_name[16]; |
| 4476 | struct stat st; |
| 4477 | int round; |
| 4478 | vimconv_T conv; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4479 | int ascii = FALSE; |
| 4480 | char_u *arg = eap->arg; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4481 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4482 | if (STRNCMP(arg, "-ascii", 6) == 0) |
| 4483 | { |
| 4484 | ascii = TRUE; |
| 4485 | arg = skipwhite(arg + 6); |
| 4486 | } |
| 4487 | |
| 4488 | /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */ |
| 4489 | if (get_arglist_exp(arg, &fcount, &fnames) == FAIL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4490 | return; |
| 4491 | if (fcount < 2) |
| 4492 | EMSG(_(e_invarg)); /* need at least output and input names */ |
| 4493 | else if (fcount > 9) |
| 4494 | EMSG(_("E754: Only up to 8 regions supported")); |
| 4495 | else |
| 4496 | { |
| 4497 | /* Check for overwriting before doing things that may take a lot of |
| 4498 | * time. */ |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4499 | sprintf((char *)wfname, "%s.%s.spl", fnames[0], |
| 4500 | ascii ? (char_u *)"ascii" : p_enc); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4501 | if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0) |
| 4502 | { |
| 4503 | EMSG(_(e_exists)); |
| 4504 | goto theend; |
| 4505 | } |
| 4506 | if (mch_isdir(fnames[0])) |
| 4507 | { |
| 4508 | EMSG2(_(e_isadir2), fnames[0]); |
| 4509 | goto theend; |
| 4510 | } |
| 4511 | |
| 4512 | /* |
| 4513 | * Init the aff and dic pointers. |
| 4514 | * Get the region names if there are more than 2 arguments. |
| 4515 | */ |
| 4516 | for (i = 1; i < fcount; ++i) |
| 4517 | { |
| 4518 | afile[i - 1] = NULL; |
| 4519 | hash_init(&dfile[i - 1]); |
| 4520 | if (fcount > 2) |
| 4521 | { |
| 4522 | len = STRLEN(fnames[i]); |
| 4523 | if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_') |
| 4524 | { |
| 4525 | EMSG2(_("E755: Invalid region in %s"), fnames[i]); |
| 4526 | goto theend; |
| 4527 | } |
| 4528 | else |
| 4529 | { |
| 4530 | region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]); |
| 4531 | region_name[(i - 1) * 2 + 1] = |
| 4532 | TOLOWER_ASC(fnames[i][len - 1]); |
| 4533 | } |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | /* |
| 4538 | * Read all the .aff and .dic files. |
| 4539 | * Text is converted to 'encoding'. |
| 4540 | */ |
| 4541 | for (i = 1; i < fcount; ++i) |
| 4542 | { |
| 4543 | /* Read the .aff file. Will init "conv" based on the "SET" line. */ |
| 4544 | conv.vc_type = CONV_NONE; |
| 4545 | sprintf((char *)fname, "%s.aff", fnames[i]); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4546 | if ((afile[i - 1] = spell_read_aff(fname, &conv, ascii)) == NULL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4547 | break; |
| 4548 | |
| 4549 | /* Read the .dic file. */ |
| 4550 | sprintf((char *)fname, "%s.dic", fnames[i]); |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4551 | if (spell_read_dic(&dfile[i - 1], fname, &conv, ascii) == FAIL) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4552 | break; |
| 4553 | |
| 4554 | /* Free any conversion stuff. */ |
| 4555 | convert_setup(&conv, NULL, NULL); |
| 4556 | } |
| 4557 | |
| 4558 | /* Process the data when all the files could be read. */ |
| 4559 | if (i == fcount) |
| 4560 | { |
| 4561 | garray_T prefga; |
| 4562 | garray_T suffga; |
| 4563 | garray_T *gap; |
| 4564 | hashtab_T newwords; |
| 4565 | |
| 4566 | /* |
| 4567 | * Combine all the affixes into one new affix list. This is done |
| 4568 | * for prefixes and suffixes separately. |
| 4569 | * We need to do this for each region, try to re-use the same |
| 4570 | * affixes. |
| 4571 | * Since we number the new affix entries, a growarray will do. In |
| 4572 | * the affheader_T the ah_key is unused. |
| 4573 | */ |
| 4574 | MSG(_("Combining affixes...")); |
| 4575 | out_flush(); |
| 4576 | for (round = 1; round <= 2; ++round) |
| 4577 | { |
| 4578 | gap = round == 1 ? &prefga : &suffga; |
| 4579 | ga_init2(gap, sizeof(affheader_T), 50); |
| 4580 | for (i = 1; i < fcount; ++i) |
| 4581 | get_new_aff(round == 1 ? &afile[i - 1]->af_pref |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4582 | : &afile[i - 1]->af_suff, |
| 4583 | gap, round == 1); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | /* |
| 4587 | * Go over all words and: |
| 4588 | * - change the old affix names to the new affix numbers |
| 4589 | * - check the conditions |
| 4590 | * - fold case |
| 4591 | * - extract the basic word and additions. |
| 4592 | * Do this for each region. |
| 4593 | */ |
| 4594 | MSG(_("Building word list...")); |
| 4595 | out_flush(); |
| 4596 | hash_init(&newwords); |
| 4597 | |
| 4598 | for (i = 1; i < fcount; ++i) |
| 4599 | build_wordlist(&newwords, &dfile[i - 1], afile[i - 1], |
| 4600 | 1 << (i - 1)); |
| 4601 | |
| 4602 | if (fcount > 2) |
| 4603 | { |
| 4604 | /* Combine words for the different regions into one. */ |
| 4605 | MSG(_("Combining regions...")); |
| 4606 | out_flush(); |
| 4607 | combine_regions(&newwords); |
| 4608 | } |
| 4609 | |
| 4610 | /* |
| 4611 | * Affixes on a word with additions are clumsy, would require |
| 4612 | * inefficient searching. Turn the affixes into additions and/or |
| 4613 | * the expanded word. |
| 4614 | */ |
| 4615 | MSG(_("Processing words...")); |
| 4616 | out_flush(); |
| 4617 | expand_affixes(&newwords, &prefga, &suffga); |
| 4618 | |
| 4619 | /* Write the info in the spell file. */ |
| 4620 | smsg((char_u *)_("Writing spell file %s..."), wfname); |
| 4621 | out_flush(); |
| 4622 | write_vim_spell(wfname, &prefga, &suffga, &newwords, |
| 4623 | fcount - 1, region_name); |
| 4624 | MSG(_("Done!")); |
| 4625 | out_flush(); |
| 4626 | |
| 4627 | /* Free the allocated stuff. */ |
| 4628 | free_wordtable(&newwords); |
| 4629 | for (round = 1; round <= 2; ++round) |
| 4630 | { |
| 4631 | gap = round == 1 ? &prefga: &suffga; |
| 4632 | for (i = 0; i < gap->ga_len; ++i) |
| 4633 | free_affixentries(((affheader_T *)gap->ga_data + i) |
| 4634 | ->ah_first); |
| 4635 | ga_clear(gap); |
| 4636 | } |
| 4637 | } |
| 4638 | |
| 4639 | /* Free the .aff and .dic file structures. */ |
| 4640 | for (i = 1; i < fcount; ++i) |
| 4641 | { |
| 4642 | if (afile[i - 1] != NULL) |
| 4643 | spell_free_aff(afile[i - 1]); |
| 4644 | spell_free_dic(&dfile[i - 1]); |
| 4645 | } |
| 4646 | } |
| 4647 | |
| 4648 | theend: |
| 4649 | FreeWild(fcount, fnames); |
| 4650 | } |
| 4651 | |
| 4652 | static void |
| 4653 | free_wordtable(ht) |
| 4654 | hashtab_T *ht; |
| 4655 | { |
| 4656 | int todo; |
| 4657 | basicword_T *bw, *nbw; |
| 4658 | hashitem_T *hi; |
| 4659 | |
| 4660 | todo = ht->ht_used; |
| 4661 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 4662 | { |
| 4663 | if (!HASHITEM_EMPTY(hi)) |
| 4664 | { |
| 4665 | --todo; |
| 4666 | for (bw = HI2BW(hi); bw != NULL; bw = nbw) |
| 4667 | { |
| 4668 | nbw = bw->bw_next; |
| 4669 | free_basicword(bw); |
| 4670 | } |
| 4671 | } |
| 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | /* |
| 4676 | * Free a basicword_T and what it contains. |
| 4677 | */ |
| 4678 | static void |
| 4679 | free_basicword(bw) |
| 4680 | basicword_T *bw; |
| 4681 | { |
| 4682 | ga_clear(&bw->bw_prefix); |
| 4683 | ga_clear(&bw->bw_suffix); |
| 4684 | vim_free(bw->bw_caseword); |
| 4685 | vim_free(bw->bw_leadstring); |
| 4686 | vim_free(bw->bw_addstring); |
| 4687 | vim_free(bw); |
| 4688 | } |
| 4689 | |
| 4690 | /* |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4691 | * Free a list of affentry_T and what they contain. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4692 | */ |
| 4693 | static void |
| 4694 | free_affixentries(first) |
| 4695 | affentry_T *first; |
| 4696 | { |
| 4697 | affentry_T *ap, *an; |
| 4698 | |
| 4699 | for (ap = first; ap != NULL; ap = an) |
| 4700 | { |
| 4701 | an = ap->ae_next; |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4702 | free_affix_entry(ap); |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4703 | } |
| 4704 | } |
| 4705 | |
Bram Moolenaar | 5482f33 | 2005-04-17 20:18:43 +0000 | [diff] [blame] | 4706 | /* |
| 4707 | * Free one affentry_T and what it contains. |
| 4708 | */ |
| 4709 | static void |
| 4710 | free_affix_entry(ap) |
| 4711 | affentry_T *ap; |
| 4712 | { |
| 4713 | vim_free(ap->ae_chop); |
| 4714 | vim_free(ap->ae_add); |
| 4715 | vim_free(ap->ae_cond); |
| 4716 | vim_free(ap->ae_prog); |
| 4717 | vim_free(ap); |
| 4718 | } |
| 4719 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 4720 | #endif /* FEAT_MBYTE */ |
| 4721 | |
| 4722 | #endif /* FEAT_SYN_HL */ |