1 /* vi:set ts=8 sts=4 sw=4 noet: 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 12 * 13 * See spellfile.c for the Vim spell file format. 14 * 15 * The spell checking mechanism uses a tree (aka trie). Each node in the tree 16 * has a list of bytes that can appear (siblings). For each byte there is a 17 * pointer to the node with the byte that follows in the word (child). 18 * 19 * A NUL byte is used where the word may end. The bytes are sorted, so that 20 * binary searching can be used and the NUL bytes are at the start. The 21 * number of possible bytes is stored before the list of bytes. 22 * 23 * The tree uses two arrays: "byts" stores the characters, "idxs" stores 24 * either the next index or flags. The tree starts at index 0. For example, 25 * to lookup "vi" this sequence is followed: 26 * i = 0 27 * len = byts[i] 28 * n = where "v" appears in byts[i + 1] to byts[i + len] 29 * i = idxs[n] 30 * len = byts[i] 31 * n = where "i" appears in byts[i + 1] to byts[i + len] 32 * i = idxs[n] 33 * len = byts[i] 34 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi". 35 * 36 * There are two word trees: one with case-folded words and one with words in 37 * original case. The second one is only used for keep-case words and is 38 * usually small. 39 * 40 * There is one additional tree for when not all prefixes are applied when 41 * generating the .spl file. This tree stores all the possible prefixes, as 42 * if they were words. At each word (prefix) end the prefix nr is stored, the 43 * following word must support this prefix nr. And the condition nr is 44 * stored, used to lookup the condition that the word must match with. 45 * 46 * Thanks to Olaf Seibert for providing an example implementation of this tree 47 * and the compression mechanism. 48 * LZ trie ideas: 49 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf 50 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html 51 * 52 * Matching involves checking the caps type: Onecap ALLCAP KeepCap. 53 * 54 * Why doesn't Vim use aspell/ispell/myspell/etc.? 55 * See ":help develop-spell". 56 */ 57 58 #define IN_SPELL_C 59 #include "vim.h" 60 61 #if defined(FEAT_SPELL) || defined(PROTO) 62 63 #ifndef UNIX // it's in os_unix.h for Unix 64 # include <time.h> // for time_t 65 #endif 66 67 #define REGION_ALL 0xff // word valid in all regions 68 69 // Result values. Lower number is accepted over higher one. 70 #define SP_BANNED -1 71 #define SP_OK 0 72 #define SP_RARE 1 73 #define SP_LOCAL 2 74 #define SP_BAD 3 75 76 /* 77 * Structure to store info for word matching. 78 */ 79 typedef struct matchinf_S 80 { 81 langp_T *mi_lp; // info for language and region 82 83 // pointers to original text to be checked 84 char_u *mi_word; // start of word being checked 85 char_u *mi_end; // end of matching word so far 86 char_u *mi_fend; // next char to be added to mi_fword 87 char_u *mi_cend; // char after what was used for 88 // mi_capflags 89 90 // case-folded text 91 char_u mi_fword[MAXWLEN + 1]; // mi_word case-folded 92 int mi_fwordlen; // nr of valid bytes in mi_fword 93 94 // for when checking word after a prefix 95 int mi_prefarridx; // index in sl_pidxs with list of 96 // affixID/condition 97 int mi_prefcnt; // number of entries at mi_prefarridx 98 int mi_prefixlen; // byte length of prefix 99 int mi_cprefixlen; // byte length of prefix in original 100 // case 101 102 // for when checking a compound word 103 int mi_compoff; // start of following word offset 104 char_u mi_compflags[MAXWLEN]; // flags for compound words used 105 int mi_complen; // nr of compound words used 106 int mi_compextra; // nr of COMPOUNDROOT words 107 108 // others 109 int mi_result; // result so far: SP_BAD, SP_OK, etc. 110 int mi_capflags; // WF_ONECAP WF_ALLCAP WF_KEEPCAP 111 win_T *mi_win; // buffer being checked 112 113 // for NOBREAK 114 int mi_result2; // "mi_resul" without following word 115 char_u *mi_end2; // "mi_end" without following word 116 } matchinf_T; 117 118 119 static int spell_mb_isword_class(int cl, win_T *wp); 120 121 // mode values for find_word 122 #define FIND_FOLDWORD 0 // find word case-folded 123 #define FIND_KEEPWORD 1 // find keep-case word 124 #define FIND_PREFIX 2 // find word after prefix 125 #define FIND_COMPOUND 3 // find case-folded compound word 126 #define FIND_KEEPCOMPOUND 4 // find keep-case compound word 127 128 static void find_word(matchinf_T *mip, int mode); 129 static void find_prefix(matchinf_T *mip, int mode); 130 static int fold_more(matchinf_T *mip); 131 static void spell_load_cb(char_u *fname, void *cookie); 132 static int count_syllables(slang_T *slang, char_u *word); 133 static void clear_midword(win_T *buf); 134 static void use_midword(slang_T *lp, win_T *buf); 135 static int find_region(char_u *rp, char_u *region); 136 static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res); 137 static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res); 138 static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res); 139 static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum); 140 static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum); 141 142 /* 143 * Main spell-checking function. 144 * "ptr" points to a character that could be the start of a word. 145 * "*attrp" is set to the highlight index for a badly spelled word. For a 146 * non-word or when it's OK it remains unchanged. 147 * This must only be called when 'spelllang' is not empty. 148 * 149 * "capcol" is used to check for a Capitalised word after the end of a 150 * sentence. If it's zero then perform the check. Return the column where to 151 * check next, or -1 when no sentence end was found. If it's NULL then don't 152 * worry. 153 * 154 * Returns the length of the word in bytes, also when it's OK, so that the 155 * caller can skip over the word. 156 */ 157 int 158 spell_check( 159 win_T *wp, // current window 160 char_u *ptr, 161 hlf_T *attrp, 162 int *capcol, // column to check for Capital 163 int docount) // count good words 164 { 165 matchinf_T mi; // Most things are put in "mi" so that it can 166 // be passed to functions quickly. 167 int nrlen = 0; // found a number first 168 int c; 169 int wrongcaplen = 0; 170 int lpi; 171 int count_word = docount; 172 int use_camel_case = *wp->w_s->b_p_spo != NUL; 173 int camel_case = 0; 174 175 // A word never starts at a space or a control character. Return quickly 176 // then, skipping over the character. 177 if (*ptr <= ' ') 178 return 1; 179 180 // Return here when loading language files failed. 181 if (wp->w_s->b_langp.ga_len == 0) 182 return 1; 183 184 CLEAR_FIELD(mi); 185 186 // A number is always OK. Also skip hexadecimal numbers 0xFF99 and 187 // 0X99FF. But always do check spelling to find "3GPP" and "11 188 // julifeest". 189 if (*ptr >= '0' && *ptr <= '9') 190 { 191 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B')) 192 mi.mi_end = skipbin(ptr + 2); 193 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) 194 mi.mi_end = skiphex(ptr + 2); 195 else 196 mi.mi_end = skipdigits(ptr); 197 nrlen = (int)(mi.mi_end - ptr); 198 } 199 200 // Find the normal end of the word (until the next non-word character). 201 mi.mi_word = ptr; 202 mi.mi_fend = ptr; 203 if (spell_iswordp(mi.mi_fend, wp)) 204 { 205 int prev_upper; 206 int this_upper = FALSE; // init for gcc 207 208 if (use_camel_case) 209 { 210 c = PTR2CHAR(mi.mi_fend); 211 this_upper = SPELL_ISUPPER(c); 212 } 213 214 do 215 { 216 MB_PTR_ADV(mi.mi_fend); 217 if (use_camel_case) 218 { 219 prev_upper = this_upper; 220 c = PTR2CHAR(mi.mi_fend); 221 this_upper = SPELL_ISUPPER(c); 222 camel_case = !prev_upper && this_upper; 223 } 224 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp) 225 && !camel_case); 226 227 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL) 228 { 229 // Check word starting with capital letter. 230 c = PTR2CHAR(ptr); 231 if (!SPELL_ISUPPER(c)) 232 wrongcaplen = (int)(mi.mi_fend - ptr); 233 } 234 } 235 if (capcol != NULL) 236 *capcol = -1; 237 238 // We always use the characters up to the next non-word character, 239 // also for bad words. 240 mi.mi_end = mi.mi_fend; 241 242 // Check caps type later. 243 mi.mi_capflags = 0; 244 mi.mi_cend = NULL; 245 mi.mi_win = wp; 246 247 // case-fold the word with one non-word character, so that we can check 248 // for the word end. 249 if (*mi.mi_fend != NUL) 250 MB_PTR_ADV(mi.mi_fend); 251 252 (void)spell_casefold(wp, ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, 253 MAXWLEN + 1); 254 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword); 255 256 if (camel_case) 257 // Introduce a fake word end space into the folded word. 258 mi.mi_fword[mi.mi_fwordlen - 1] = ' '; 259 260 // The word is bad unless we recognize it. 261 mi.mi_result = SP_BAD; 262 mi.mi_result2 = SP_BAD; 263 264 /* 265 * Loop over the languages specified in 'spelllang'. 266 * We check them all, because a word may be matched longer in another 267 * language. 268 */ 269 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi) 270 { 271 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi); 272 273 // If reloading fails the language is still in the list but everything 274 // has been cleared. 275 if (mi.mi_lp->lp_slang->sl_fidxs == NULL) 276 continue; 277 278 // Check for a matching word in case-folded words. 279 find_word(&mi, FIND_FOLDWORD); 280 281 // Check for a matching word in keep-case words. 282 find_word(&mi, FIND_KEEPWORD); 283 284 // Check for matching prefixes. 285 find_prefix(&mi, FIND_FOLDWORD); 286 287 // For a NOBREAK language, may want to use a word without a following 288 // word as a backup. 289 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD 290 && mi.mi_result2 != SP_BAD) 291 { 292 mi.mi_result = mi.mi_result2; 293 mi.mi_end = mi.mi_end2; 294 } 295 296 // Count the word in the first language where it's found to be OK. 297 if (count_word && mi.mi_result == SP_OK) 298 { 299 count_common_word(mi.mi_lp->lp_slang, ptr, 300 (int)(mi.mi_end - ptr), 1); 301 count_word = FALSE; 302 } 303 } 304 305 if (mi.mi_result != SP_OK) 306 { 307 // If we found a number skip over it. Allows for "42nd". Do flag 308 // rare and local words, e.g., "3GPP". 309 if (nrlen > 0) 310 { 311 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) 312 return nrlen; 313 } 314 315 // When we are at a non-word character there is no error, just 316 // skip over the character (try looking for a word after it). 317 else if (!spell_iswordp_nmw(ptr, wp)) 318 { 319 if (capcol != NULL && wp->w_s->b_cap_prog != NULL) 320 { 321 regmatch_T regmatch; 322 int r; 323 324 // Check for end of sentence. 325 regmatch.regprog = wp->w_s->b_cap_prog; 326 regmatch.rm_ic = FALSE; 327 r = vim_regexec(®match, ptr, 0); 328 wp->w_s->b_cap_prog = regmatch.regprog; 329 if (r) 330 *capcol = (int)(regmatch.endp[0] - ptr); 331 } 332 333 if (has_mbyte) 334 return (*mb_ptr2len)(ptr); 335 return 1; 336 } 337 else if (mi.mi_end == ptr) 338 // Always include at least one character. Required for when there 339 // is a mixup in "midword". 340 MB_PTR_ADV(mi.mi_end); 341 else if (mi.mi_result == SP_BAD 342 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak) 343 { 344 char_u *p, *fp; 345 int save_result = mi.mi_result; 346 347 // First language in 'spelllang' is NOBREAK. Find first position 348 // at which any word would be valid. 349 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0); 350 if (mi.mi_lp->lp_slang->sl_fidxs != NULL) 351 { 352 p = mi.mi_word; 353 fp = mi.mi_fword; 354 for (;;) 355 { 356 MB_PTR_ADV(p); 357 MB_PTR_ADV(fp); 358 if (p >= mi.mi_end) 359 break; 360 mi.mi_compoff = (int)(fp - mi.mi_fword); 361 find_word(&mi, FIND_COMPOUND); 362 if (mi.mi_result != SP_BAD) 363 { 364 mi.mi_end = p; 365 break; 366 } 367 } 368 mi.mi_result = save_result; 369 } 370 } 371 372 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) 373 *attrp = HLF_SPB; 374 else if (mi.mi_result == SP_RARE) 375 *attrp = HLF_SPR; 376 else 377 *attrp = HLF_SPL; 378 } 379 380 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE)) 381 { 382 // Report SpellCap only when the word isn't badly spelled. 383 *attrp = HLF_SPC; 384 return wrongcaplen; 385 } 386 387 return (int)(mi.mi_end - ptr); 388 } 389 390 /* 391 * Check if the word at "mip->mi_word" is in the tree. 392 * When "mode" is FIND_FOLDWORD check in fold-case word tree. 393 * When "mode" is FIND_KEEPWORD check in keep-case word tree. 394 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word 395 * tree. 396 * 397 * For a match mip->mi_result is updated. 398 */ 399 static void 400 find_word(matchinf_T *mip, int mode) 401 { 402 idx_T arridx = 0; 403 int endlen[MAXWLEN]; // length at possible word endings 404 idx_T endidx[MAXWLEN]; // possible word endings 405 int endidxcnt = 0; 406 int len; 407 int wlen = 0; 408 int flen; 409 int c; 410 char_u *ptr; 411 idx_T lo, hi, m; 412 char_u *s; 413 char_u *p; 414 int res = SP_BAD; 415 slang_T *slang = mip->mi_lp->lp_slang; 416 unsigned flags; 417 char_u *byts; 418 idx_T *idxs; 419 int word_ends; 420 int prefix_found; 421 int nobreak_result; 422 423 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND) 424 { 425 // Check for word with matching case in keep-case tree. 426 ptr = mip->mi_word; 427 flen = 9999; // no case folding, always enough bytes 428 byts = slang->sl_kbyts; 429 idxs = slang->sl_kidxs; 430 431 if (mode == FIND_KEEPCOMPOUND) 432 // Skip over the previously found word(s). 433 wlen += mip->mi_compoff; 434 } 435 else 436 { 437 // Check for case-folded in case-folded tree. 438 ptr = mip->mi_fword; 439 flen = mip->mi_fwordlen; // available case-folded bytes 440 byts = slang->sl_fbyts; 441 idxs = slang->sl_fidxs; 442 443 if (mode == FIND_PREFIX) 444 { 445 // Skip over the prefix. 446 wlen = mip->mi_prefixlen; 447 flen -= mip->mi_prefixlen; 448 } 449 else if (mode == FIND_COMPOUND) 450 { 451 // Skip over the previously found word(s). 452 wlen = mip->mi_compoff; 453 flen -= mip->mi_compoff; 454 } 455 456 } 457 458 if (byts == NULL) 459 return; // array is empty 460 461 /* 462 * Repeat advancing in the tree until: 463 * - there is a byte that doesn't match, 464 * - we reach the end of the tree, 465 * - or we reach the end of the line. 466 */ 467 for (;;) 468 { 469 if (flen <= 0 && *mip->mi_fend != NUL) 470 flen = fold_more(mip); 471 472 len = byts[arridx++]; 473 474 // If the first possible byte is a zero the word could end here. 475 // Remember this index, we first check for the longest word. 476 if (byts[arridx] == 0) 477 { 478 if (endidxcnt == MAXWLEN) 479 { 480 // Must be a corrupted spell file. 481 emsg(_(e_format)); 482 return; 483 } 484 endlen[endidxcnt] = wlen; 485 endidx[endidxcnt++] = arridx++; 486 --len; 487 488 // Skip over the zeros, there can be several flag/region 489 // combinations. 490 while (len > 0 && byts[arridx] == 0) 491 { 492 ++arridx; 493 --len; 494 } 495 if (len == 0) 496 break; // no children, word must end here 497 } 498 499 // Stop looking at end of the line. 500 if (ptr[wlen] == NUL) 501 break; 502 503 // Perform a binary search in the list of accepted bytes. 504 c = ptr[wlen]; 505 if (c == TAB) // <Tab> is handled like <Space> 506 c = ' '; 507 lo = arridx; 508 hi = arridx + len - 1; 509 while (lo < hi) 510 { 511 m = (lo + hi) / 2; 512 if (byts[m] > c) 513 hi = m - 1; 514 else if (byts[m] < c) 515 lo = m + 1; 516 else 517 { 518 lo = hi = m; 519 break; 520 } 521 } 522 523 // Stop if there is no matching byte. 524 if (hi < lo || byts[lo] != c) 525 break; 526 527 // Continue at the child (if there is one). 528 arridx = idxs[lo]; 529 ++wlen; 530 --flen; 531 532 // One space in the good word may stand for several spaces in the 533 // checked word. 534 if (c == ' ') 535 { 536 for (;;) 537 { 538 if (flen <= 0 && *mip->mi_fend != NUL) 539 flen = fold_more(mip); 540 if (ptr[wlen] != ' ' && ptr[wlen] != TAB) 541 break; 542 ++wlen; 543 --flen; 544 } 545 } 546 } 547 548 /* 549 * Verify that one of the possible endings is valid. Try the longest 550 * first. 551 */ 552 while (endidxcnt > 0) 553 { 554 --endidxcnt; 555 arridx = endidx[endidxcnt]; 556 wlen = endlen[endidxcnt]; 557 558 if ((*mb_head_off)(ptr, ptr + wlen) > 0) 559 continue; // not at first byte of character 560 if (spell_iswordp(ptr + wlen, mip->mi_win)) 561 { 562 if (slang->sl_compprog == NULL && !slang->sl_nobreak) 563 continue; // next char is a word character 564 word_ends = FALSE; 565 } 566 else 567 word_ends = TRUE; 568 // The prefix flag is before compound flags. Once a valid prefix flag 569 // has been found we try compound flags. 570 prefix_found = FALSE; 571 572 if (mode != FIND_KEEPWORD && has_mbyte) 573 { 574 // Compute byte length in original word, length may change 575 // when folding case. This can be slow, take a shortcut when the 576 // case-folded word is equal to the keep-case word. 577 p = mip->mi_word; 578 if (STRNCMP(ptr, p, wlen) != 0) 579 { 580 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s)) 581 MB_PTR_ADV(p); 582 wlen = (int)(p - mip->mi_word); 583 } 584 } 585 586 // Check flags and region. For FIND_PREFIX check the condition and 587 // prefix ID. 588 // Repeat this if there are more flags/region alternatives until there 589 // is a match. 590 res = SP_BAD; 591 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; 592 --len, ++arridx) 593 { 594 flags = idxs[arridx]; 595 596 // For the fold-case tree check that the case of the checked word 597 // matches with what the word in the tree requires. 598 // For keep-case tree the case is always right. For prefixes we 599 // don't bother to check. 600 if (mode == FIND_FOLDWORD) 601 { 602 if (mip->mi_cend != mip->mi_word + wlen) 603 { 604 // mi_capflags was set for a different word length, need 605 // to do it again. 606 mip->mi_cend = mip->mi_word + wlen; 607 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); 608 } 609 610 if (mip->mi_capflags == WF_KEEPCAP 611 || !spell_valid_case(mip->mi_capflags, flags)) 612 continue; 613 } 614 615 // When mode is FIND_PREFIX the word must support the prefix: 616 // check the prefix ID and the condition. Do that for the list at 617 // mip->mi_prefarridx that find_prefix() filled. 618 else if (mode == FIND_PREFIX && !prefix_found) 619 { 620 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, 621 flags, 622 mip->mi_word + mip->mi_cprefixlen, slang, 623 FALSE); 624 if (c == 0) 625 continue; 626 627 // Use the WF_RARE flag for a rare prefix. 628 if (c & WF_RAREPFX) 629 flags |= WF_RARE; 630 prefix_found = TRUE; 631 } 632 633 if (slang->sl_nobreak) 634 { 635 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND) 636 && (flags & WF_BANNED) == 0) 637 { 638 // NOBREAK: found a valid following word. That's all we 639 // need to know, so return. 640 mip->mi_result = SP_OK; 641 break; 642 } 643 } 644 645 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND 646 || !word_ends)) 647 { 648 // If there is no compound flag or the word is shorter than 649 // COMPOUNDMIN reject it quickly. 650 // Makes you wonder why someone puts a compound flag on a word 651 // that's too short... Myspell compatibility requires this 652 // anyway. 653 if (((unsigned)flags >> 24) == 0 654 || wlen - mip->mi_compoff < slang->sl_compminlen) 655 continue; 656 // For multi-byte chars check character length against 657 // COMPOUNDMIN. 658 if (has_mbyte 659 && slang->sl_compminlen > 0 660 && mb_charlen_len(mip->mi_word + mip->mi_compoff, 661 wlen - mip->mi_compoff) < slang->sl_compminlen) 662 continue; 663 664 // Limit the number of compound words to COMPOUNDWORDMAX if no 665 // maximum for syllables is specified. 666 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2 667 > slang->sl_compmax 668 && slang->sl_compsylmax == MAXWLEN) 669 continue; 670 671 // Don't allow compounding on a side where an affix was added, 672 // unless COMPOUNDPERMITFLAG was used. 673 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF)) 674 continue; 675 if (!word_ends && (flags & WF_NOCOMPAFT)) 676 continue; 677 678 // Quickly check if compounding is possible with this flag. 679 if (!byte_in_str(mip->mi_complen == 0 680 ? slang->sl_compstartflags 681 : slang->sl_compallflags, 682 ((unsigned)flags >> 24))) 683 continue; 684 685 // If there is a match with a CHECKCOMPOUNDPATTERN rule 686 // discard the compound word. 687 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat)) 688 continue; 689 690 if (mode == FIND_COMPOUND) 691 { 692 int capflags; 693 694 // Need to check the caps type of the appended compound 695 // word. 696 if (has_mbyte && STRNCMP(ptr, mip->mi_word, 697 mip->mi_compoff) != 0) 698 { 699 // case folding may have changed the length 700 p = mip->mi_word; 701 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s)) 702 MB_PTR_ADV(p); 703 } 704 else 705 p = mip->mi_word + mip->mi_compoff; 706 capflags = captype(p, mip->mi_word + wlen); 707 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP 708 && (flags & WF_FIXCAP) != 0)) 709 continue; 710 711 if (capflags != WF_ALLCAP) 712 { 713 // When the character before the word is a word 714 // character we do not accept a Onecap word. We do 715 // accept a no-caps word, even when the dictionary 716 // word specifies ONECAP. 717 MB_PTR_BACK(mip->mi_word, p); 718 if (spell_iswordp_nmw(p, mip->mi_win) 719 ? capflags == WF_ONECAP 720 : (flags & WF_ONECAP) != 0 721 && capflags != WF_ONECAP) 722 continue; 723 } 724 } 725 726 // If the word ends the sequence of compound flags of the 727 // words must match with one of the COMPOUNDRULE items and 728 // the number of syllables must not be too large. 729 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24); 730 mip->mi_compflags[mip->mi_complen + 1] = NUL; 731 if (word_ends) 732 { 733 char_u fword[MAXWLEN]; 734 735 if (slang->sl_compsylmax < MAXWLEN) 736 { 737 // "fword" is only needed for checking syllables. 738 if (ptr == mip->mi_word) 739 (void)spell_casefold(mip->mi_win, 740 ptr, wlen, fword, MAXWLEN); 741 else 742 vim_strncpy(fword, ptr, endlen[endidxcnt]); 743 } 744 if (!can_compound(slang, fword, mip->mi_compflags)) 745 continue; 746 } 747 else if (slang->sl_comprules != NULL 748 && !match_compoundrule(slang, mip->mi_compflags)) 749 // The compound flags collected so far do not match any 750 // COMPOUNDRULE, discard the compounded word. 751 continue; 752 } 753 754 // Check NEEDCOMPOUND: can't use word without compounding. 755 else if (flags & WF_NEEDCOMP) 756 continue; 757 758 nobreak_result = SP_OK; 759 760 if (!word_ends) 761 { 762 int save_result = mip->mi_result; 763 char_u *save_end = mip->mi_end; 764 langp_T *save_lp = mip->mi_lp; 765 int lpi; 766 767 // Check that a valid word follows. If there is one and we 768 // are compounding, it will set "mi_result", thus we are 769 // always finished here. For NOBREAK we only check that a 770 // valid word follows. 771 // Recursive! 772 if (slang->sl_nobreak) 773 mip->mi_result = SP_BAD; 774 775 // Find following word in case-folded tree. 776 mip->mi_compoff = endlen[endidxcnt]; 777 if (has_mbyte && mode == FIND_KEEPWORD) 778 { 779 // Compute byte length in case-folded word from "wlen": 780 // byte length in keep-case word. Length may change when 781 // folding case. This can be slow, take a shortcut when 782 // the case-folded word is equal to the keep-case word. 783 p = mip->mi_fword; 784 if (STRNCMP(ptr, p, wlen) != 0) 785 { 786 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s)) 787 MB_PTR_ADV(p); 788 mip->mi_compoff = (int)(p - mip->mi_fword); 789 } 790 } 791 #if 0 // Disabled, see below 792 c = mip->mi_compoff; 793 #endif 794 ++mip->mi_complen; 795 if (flags & WF_COMPROOT) 796 ++mip->mi_compextra; 797 798 // For NOBREAK we need to try all NOBREAK languages, at least 799 // to find the ".add" file(s). 800 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi) 801 { 802 if (slang->sl_nobreak) 803 { 804 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi); 805 if (mip->mi_lp->lp_slang->sl_fidxs == NULL 806 || !mip->mi_lp->lp_slang->sl_nobreak) 807 continue; 808 } 809 810 find_word(mip, FIND_COMPOUND); 811 812 // When NOBREAK any word that matches is OK. Otherwise we 813 // need to find the longest match, thus try with keep-case 814 // and prefix too. 815 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) 816 { 817 // Find following word in keep-case tree. 818 mip->mi_compoff = wlen; 819 find_word(mip, FIND_KEEPCOMPOUND); 820 821 #if 0 // Disabled, a prefix must not appear halfway a compound word, 822 // unless the COMPOUNDPERMITFLAG is used and then it can't be a 823 // postponed prefix. 824 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) 825 { 826 // Check for following word with prefix. 827 mip->mi_compoff = c; 828 find_prefix(mip, FIND_COMPOUND); 829 } 830 #endif 831 } 832 833 if (!slang->sl_nobreak) 834 break; 835 } 836 --mip->mi_complen; 837 if (flags & WF_COMPROOT) 838 --mip->mi_compextra; 839 mip->mi_lp = save_lp; 840 841 if (slang->sl_nobreak) 842 { 843 nobreak_result = mip->mi_result; 844 mip->mi_result = save_result; 845 mip->mi_end = save_end; 846 } 847 else 848 { 849 if (mip->mi_result == SP_OK) 850 break; 851 continue; 852 } 853 } 854 855 if (flags & WF_BANNED) 856 res = SP_BANNED; 857 else if (flags & WF_REGION) 858 { 859 // Check region. 860 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) 861 res = SP_OK; 862 else 863 res = SP_LOCAL; 864 } 865 else if (flags & WF_RARE) 866 res = SP_RARE; 867 else 868 res = SP_OK; 869 870 // Always use the longest match and the best result. For NOBREAK 871 // we separately keep the longest match without a following good 872 // word as a fall-back. 873 if (nobreak_result == SP_BAD) 874 { 875 if (mip->mi_result2 > res) 876 { 877 mip->mi_result2 = res; 878 mip->mi_end2 = mip->mi_word + wlen; 879 } 880 else if (mip->mi_result2 == res 881 && mip->mi_end2 < mip->mi_word + wlen) 882 mip->mi_end2 = mip->mi_word + wlen; 883 } 884 else if (mip->mi_result > res) 885 { 886 mip->mi_result = res; 887 mip->mi_end = mip->mi_word + wlen; 888 } 889 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) 890 mip->mi_end = mip->mi_word + wlen; 891 892 if (mip->mi_result == SP_OK) 893 break; 894 } 895 896 if (mip->mi_result == SP_OK) 897 break; 898 } 899 } 900 901 /* 902 * Return TRUE if there is a match between the word ptr[wlen] and 903 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another 904 * word. 905 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the 906 * end of ptr[wlen] and the second part matches after it. 907 */ 908 int 909 match_checkcompoundpattern( 910 char_u *ptr, 911 int wlen, 912 garray_T *gap) // &sl_comppat 913 { 914 int i; 915 char_u *p; 916 int len; 917 918 for (i = 0; i + 1 < gap->ga_len; i += 2) 919 { 920 p = ((char_u **)gap->ga_data)[i + 1]; 921 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0) 922 { 923 // Second part matches at start of following compound word, now 924 // check if first part matches at end of previous word. 925 p = ((char_u **)gap->ga_data)[i]; 926 len = (int)STRLEN(p); 927 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) 928 return TRUE; 929 } 930 } 931 return FALSE; 932 } 933 934 /* 935 * Return TRUE if "flags" is a valid sequence of compound flags and "word" 936 * does not have too many syllables. 937 */ 938 int 939 can_compound(slang_T *slang, char_u *word, char_u *flags) 940 { 941 char_u uflags[MAXWLEN * 2]; 942 int i; 943 char_u *p; 944 945 if (slang->sl_compprog == NULL) 946 return FALSE; 947 if (enc_utf8) 948 { 949 // Need to convert the single byte flags to utf8 characters. 950 p = uflags; 951 for (i = 0; flags[i] != NUL; ++i) 952 p += utf_char2bytes(flags[i], p); 953 *p = NUL; 954 p = uflags; 955 } 956 else 957 p = flags; 958 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0)) 959 return FALSE; 960 961 // Count the number of syllables. This may be slow, do it last. If there 962 // are too many syllables AND the number of compound words is above 963 // COMPOUNDWORDMAX then compounding is not allowed. 964 if (slang->sl_compsylmax < MAXWLEN 965 && count_syllables(slang, word) > slang->sl_compsylmax) 966 return (int)STRLEN(flags) < slang->sl_compmax; 967 return TRUE; 968 } 969 970 /* 971 * Return TRUE if the compound flags in compflags[] match the start of any 972 * compound rule. This is used to stop trying a compound if the flags 973 * collected so far can't possibly match any compound rule. 974 * Caller must check that slang->sl_comprules is not NULL. 975 */ 976 int 977 match_compoundrule(slang_T *slang, char_u *compflags) 978 { 979 char_u *p; 980 int i; 981 int c; 982 983 // loop over all the COMPOUNDRULE entries 984 for (p = slang->sl_comprules; *p != NUL; ++p) 985 { 986 // loop over the flags in the compound word we have made, match 987 // them against the current rule entry 988 for (i = 0; ; ++i) 989 { 990 c = compflags[i]; 991 if (c == NUL) 992 // found a rule that matches for the flags we have so far 993 return TRUE; 994 if (*p == '/' || *p == NUL) 995 break; // end of rule, it's too short 996 if (*p == '[') 997 { 998 int match = FALSE; 999 1000 // compare against all the flags in [] 1001 ++p; 1002 while (*p != ']' && *p != NUL) 1003 if (*p++ == c) 1004 match = TRUE; 1005 if (!match) 1006 break; // none matches 1007 } 1008 else if (*p != c) 1009 break; // flag of word doesn't match flag in pattern 1010 ++p; 1011 } 1012 1013 // Skip to the next "/", where the next pattern starts. 1014 p = vim_strchr(p, '/'); 1015 if (p == NULL) 1016 break; 1017 } 1018 1019 // Checked all the rules and none of them match the flags, so there 1020 // can't possibly be a compound starting with these flags. 1021 return FALSE; 1022 } 1023 1024 /* 1025 * Return non-zero if the prefix indicated by "arridx" matches with the prefix 1026 * ID in "flags" for the word "word". 1027 * The WF_RAREPFX flag is included in the return value for a rare prefix. 1028 */ 1029 int 1030 valid_word_prefix( 1031 int totprefcnt, // nr of prefix IDs 1032 int arridx, // idx in sl_pidxs[] 1033 int flags, 1034 char_u *word, 1035 slang_T *slang, 1036 int cond_req) // only use prefixes with a condition 1037 { 1038 int prefcnt; 1039 int pidx; 1040 regprog_T **rp; 1041 int prefid; 1042 1043 prefid = (unsigned)flags >> 24; 1044 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) 1045 { 1046 pidx = slang->sl_pidxs[arridx + prefcnt]; 1047 1048 // Check the prefix ID. 1049 if (prefid != (pidx & 0xff)) 1050 continue; 1051 1052 // Check if the prefix doesn't combine and the word already has a 1053 // suffix. 1054 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) 1055 continue; 1056 1057 // Check the condition, if there is one. The condition index is 1058 // stored in the two bytes above the prefix ID byte. 1059 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; 1060 if (*rp != NULL) 1061 { 1062 if (!vim_regexec_prog(rp, FALSE, word, 0)) 1063 continue; 1064 } 1065 else if (cond_req) 1066 continue; 1067 1068 // It's a match! Return the WF_ flags. 1069 return pidx; 1070 } 1071 return 0; 1072 } 1073 1074 /* 1075 * Check if the word at "mip->mi_word" has a matching prefix. 1076 * If it does, then check the following word. 1077 * 1078 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a 1079 * prefix in a compound word. 1080 * 1081 * For a match mip->mi_result is updated. 1082 */ 1083 static void 1084 find_prefix(matchinf_T *mip, int mode) 1085 { 1086 idx_T arridx = 0; 1087 int len; 1088 int wlen = 0; 1089 int flen; 1090 int c; 1091 char_u *ptr; 1092 idx_T lo, hi, m; 1093 slang_T *slang = mip->mi_lp->lp_slang; 1094 char_u *byts; 1095 idx_T *idxs; 1096 1097 byts = slang->sl_pbyts; 1098 if (byts == NULL) 1099 return; // array is empty 1100 1101 // We use the case-folded word here, since prefixes are always 1102 // case-folded. 1103 ptr = mip->mi_fword; 1104 flen = mip->mi_fwordlen; // available case-folded bytes 1105 if (mode == FIND_COMPOUND) 1106 { 1107 // Skip over the previously found word(s). 1108 ptr += mip->mi_compoff; 1109 flen -= mip->mi_compoff; 1110 } 1111 idxs = slang->sl_pidxs; 1112 1113 /* 1114 * Repeat advancing in the tree until: 1115 * - there is a byte that doesn't match, 1116 * - we reach the end of the tree, 1117 * - or we reach the end of the line. 1118 */ 1119 for (;;) 1120 { 1121 if (flen == 0 && *mip->mi_fend != NUL) 1122 flen = fold_more(mip); 1123 1124 len = byts[arridx++]; 1125 1126 // If the first possible byte is a zero the prefix could end here. 1127 // Check if the following word matches and supports the prefix. 1128 if (byts[arridx] == 0) 1129 { 1130 // There can be several prefixes with different conditions. We 1131 // try them all, since we don't know which one will give the 1132 // longest match. The word is the same each time, pass the list 1133 // of possible prefixes to find_word(). 1134 mip->mi_prefarridx = arridx; 1135 mip->mi_prefcnt = len; 1136 while (len > 0 && byts[arridx] == 0) 1137 { 1138 ++arridx; 1139 --len; 1140 } 1141 mip->mi_prefcnt -= len; 1142 1143 // Find the word that comes after the prefix. 1144 mip->mi_prefixlen = wlen; 1145 if (mode == FIND_COMPOUND) 1146 // Skip over the previously found word(s). 1147 mip->mi_prefixlen += mip->mi_compoff; 1148 1149 if (has_mbyte) 1150 { 1151 // Case-folded length may differ from original length. 1152 mip->mi_cprefixlen = nofold_len(mip->mi_fword, 1153 mip->mi_prefixlen, mip->mi_word); 1154 } 1155 else 1156 mip->mi_cprefixlen = mip->mi_prefixlen; 1157 find_word(mip, FIND_PREFIX); 1158 1159 1160 if (len == 0) 1161 break; // no children, word must end here 1162 } 1163 1164 // Stop looking at end of the line. 1165 if (ptr[wlen] == NUL) 1166 break; 1167 1168 // Perform a binary search in the list of accepted bytes. 1169 c = ptr[wlen]; 1170 lo = arridx; 1171 hi = arridx + len - 1; 1172 while (lo < hi) 1173 { 1174 m = (lo + hi) / 2; 1175 if (byts[m] > c) 1176 hi = m - 1; 1177 else if (byts[m] < c) 1178 lo = m + 1; 1179 else 1180 { 1181 lo = hi = m; 1182 break; 1183 } 1184 } 1185 1186 // Stop if there is no matching byte. 1187 if (hi < lo || byts[lo] != c) 1188 break; 1189 1190 // Continue at the child (if there is one). 1191 arridx = idxs[lo]; 1192 ++wlen; 1193 --flen; 1194 } 1195 } 1196 1197 /* 1198 * Need to fold at least one more character. Do until next non-word character 1199 * for efficiency. Include the non-word character too. 1200 * Return the length of the folded chars in bytes. 1201 */ 1202 static int 1203 fold_more(matchinf_T *mip) 1204 { 1205 int flen; 1206 char_u *p; 1207 1208 p = mip->mi_fend; 1209 do 1210 MB_PTR_ADV(mip->mi_fend); 1211 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win)); 1212 1213 // Include the non-word character so that we can check for the word end. 1214 if (*mip->mi_fend != NUL) 1215 MB_PTR_ADV(mip->mi_fend); 1216 1217 (void)spell_casefold(mip->mi_win, p, (int)(mip->mi_fend - p), 1218 mip->mi_fword + mip->mi_fwordlen, 1219 MAXWLEN - mip->mi_fwordlen); 1220 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen); 1221 mip->mi_fwordlen += flen; 1222 return flen; 1223 } 1224 1225 /* 1226 * Check case flags for a word. Return TRUE if the word has the requested 1227 * case. 1228 */ 1229 int 1230 spell_valid_case( 1231 int wordflags, // flags for the checked word. 1232 int treeflags) // flags for the word in the spell tree 1233 { 1234 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) 1235 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 1236 && ((treeflags & WF_ONECAP) == 0 1237 || (wordflags & WF_ONECAP) != 0))); 1238 } 1239 1240 /* 1241 * Return TRUE if spell checking is not enabled. 1242 */ 1243 int 1244 no_spell_checking(win_T *wp) 1245 { 1246 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL 1247 || wp->w_s->b_langp.ga_len == 0) 1248 { 1249 emsg(_(e_no_spell)); 1250 return TRUE; 1251 } 1252 return FALSE; 1253 } 1254 1255 /* 1256 * Move to next spell error. 1257 * "curline" is FALSE for "[s", "]s", "[S" and "]S". 1258 * "curline" is TRUE to find word under/after cursor in the same line. 1259 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move 1260 * to after badly spelled word before the cursor. 1261 * Return 0 if not found, length of the badly spelled word otherwise. 1262 */ 1263 int 1264 spell_move_to( 1265 win_T *wp, 1266 int dir, // FORWARD or BACKWARD 1267 int allwords, // TRUE for "[s"/"]s", FALSE for "[S"/"]S" 1268 int curline, 1269 hlf_T *attrp) // return: attributes of bad word or NULL 1270 // (only when "dir" is FORWARD) 1271 { 1272 linenr_T lnum; 1273 pos_T found_pos; 1274 int found_len = 0; 1275 char_u *line; 1276 char_u *p; 1277 char_u *endp; 1278 hlf_T attr; 1279 int len; 1280 #ifdef FEAT_SYN_HL 1281 int has_syntax = syntax_present(wp); 1282 #endif 1283 int col; 1284 int can_spell; 1285 char_u *buf = NULL; 1286 int buflen = 0; 1287 int skip = 0; 1288 int capcol = -1; 1289 int found_one = FALSE; 1290 int wrapped = FALSE; 1291 1292 if (no_spell_checking(wp)) 1293 return 0; 1294 1295 /* 1296 * Start looking for bad word at the start of the line, because we can't 1297 * start halfway a word, we don't know where it starts or ends. 1298 * 1299 * When searching backwards, we continue in the line to find the last 1300 * bad word (in the cursor line: before the cursor). 1301 * 1302 * We concatenate the start of the next line, so that wrapped words work 1303 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards 1304 * though... 1305 */ 1306 lnum = wp->w_cursor.lnum; 1307 CLEAR_POS(&found_pos); 1308 1309 while (!got_int) 1310 { 1311 line = ml_get_buf(wp->w_buffer, lnum, FALSE); 1312 1313 len = (int)STRLEN(line); 1314 if (buflen < len + MAXWLEN + 2) 1315 { 1316 vim_free(buf); 1317 buflen = len + MAXWLEN + 2; 1318 buf = alloc(buflen); 1319 if (buf == NULL) 1320 break; 1321 } 1322 1323 // In first line check first word for Capital. 1324 if (lnum == 1) 1325 capcol = 0; 1326 1327 // For checking first word with a capital skip white space. 1328 if (capcol == 0) 1329 capcol = getwhitecols(line); 1330 else if (curline && wp == curwin) 1331 { 1332 // For spellbadword(): check if first word needs a capital. 1333 col = getwhitecols(line); 1334 if (check_need_cap(lnum, col)) 1335 capcol = col; 1336 1337 // Need to get the line again, may have looked at the previous 1338 // one. 1339 line = ml_get_buf(wp->w_buffer, lnum, FALSE); 1340 } 1341 1342 // Copy the line into "buf" and append the start of the next line if 1343 // possible. 1344 STRCPY(buf, line); 1345 if (lnum < wp->w_buffer->b_ml.ml_line_count) 1346 spell_cat_line(buf + STRLEN(buf), 1347 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN); 1348 1349 p = buf + skip; 1350 endp = buf + len; 1351 while (p < endp) 1352 { 1353 // When searching backward don't search after the cursor. Unless 1354 // we wrapped around the end of the buffer. 1355 if (dir == BACKWARD 1356 && lnum == wp->w_cursor.lnum 1357 && !wrapped 1358 && (colnr_T)(p - buf) >= wp->w_cursor.col) 1359 break; 1360 1361 // start of word 1362 attr = HLF_COUNT; 1363 len = spell_check(wp, p, &attr, &capcol, FALSE); 1364 1365 if (attr != HLF_COUNT) 1366 { 1367 // We found a bad word. Check the attribute. 1368 if (allwords || attr == HLF_SPB) 1369 { 1370 // When searching forward only accept a bad word after 1371 // the cursor. 1372 if (dir == BACKWARD 1373 || lnum != wp->w_cursor.lnum 1374 || (lnum == wp->w_cursor.lnum 1375 && (wrapped 1376 || (colnr_T)(curline ? p - buf + len 1377 : p - buf) 1378 > wp->w_cursor.col))) 1379 { 1380 #ifdef FEAT_SYN_HL 1381 if (has_syntax) 1382 { 1383 col = (int)(p - buf); 1384 (void)syn_get_id(wp, lnum, (colnr_T)col, 1385 FALSE, &can_spell, FALSE); 1386 if (!can_spell) 1387 attr = HLF_COUNT; 1388 } 1389 else 1390 #endif 1391 can_spell = TRUE; 1392 1393 if (can_spell) 1394 { 1395 found_one = TRUE; 1396 found_pos.lnum = lnum; 1397 found_pos.col = (int)(p - buf); 1398 found_pos.coladd = 0; 1399 if (dir == FORWARD) 1400 { 1401 // No need to search further. 1402 wp->w_cursor = found_pos; 1403 vim_free(buf); 1404 if (attrp != NULL) 1405 *attrp = attr; 1406 return len; 1407 } 1408 else if (curline) 1409 // Insert mode completion: put cursor after 1410 // the bad word. 1411 found_pos.col += len; 1412 found_len = len; 1413 } 1414 } 1415 else 1416 found_one = TRUE; 1417 } 1418 } 1419 1420 // advance to character after the word 1421 p += len; 1422 capcol -= len; 1423 } 1424 1425 if (dir == BACKWARD && found_pos.lnum != 0) 1426 { 1427 // Use the last match in the line (before the cursor). 1428 wp->w_cursor = found_pos; 1429 vim_free(buf); 1430 return found_len; 1431 } 1432 1433 if (curline) 1434 break; // only check cursor line 1435 1436 // If we are back at the starting line and searched it again there 1437 // is no match, give up. 1438 if (lnum == wp->w_cursor.lnum && wrapped) 1439 break; 1440 1441 // Advance to next line. 1442 if (dir == BACKWARD) 1443 { 1444 if (lnum > 1) 1445 --lnum; 1446 else if (!p_ws) 1447 break; // at first line and 'nowrapscan' 1448 else 1449 { 1450 // Wrap around to the end of the buffer. May search the 1451 // starting line again and accept the last match. 1452 lnum = wp->w_buffer->b_ml.ml_line_count; 1453 wrapped = TRUE; 1454 if (!shortmess(SHM_SEARCH)) 1455 give_warning((char_u *)_(top_bot_msg), TRUE); 1456 } 1457 capcol = -1; 1458 } 1459 else 1460 { 1461 if (lnum < wp->w_buffer->b_ml.ml_line_count) 1462 ++lnum; 1463 else if (!p_ws) 1464 break; // at first line and 'nowrapscan' 1465 else 1466 { 1467 // Wrap around to the start of the buffer. May search the 1468 // starting line again and accept the first match. 1469 lnum = 1; 1470 wrapped = TRUE; 1471 if (!shortmess(SHM_SEARCH)) 1472 give_warning((char_u *)_(bot_top_msg), TRUE); 1473 } 1474 1475 // If we are back at the starting line and there is no match then 1476 // give up. 1477 if (lnum == wp->w_cursor.lnum && !found_one) 1478 break; 1479 1480 // Skip the characters at the start of the next line that were 1481 // included in a match crossing line boundaries. 1482 if (attr == HLF_COUNT) 1483 skip = (int)(p - endp); 1484 else 1485 skip = 0; 1486 1487 // Capcol skips over the inserted space. 1488 --capcol; 1489 1490 // But after empty line check first word in next line 1491 if (*skipwhite(line) == NUL) 1492 capcol = 0; 1493 } 1494 1495 line_breakcheck(); 1496 } 1497 1498 vim_free(buf); 1499 return 0; 1500 } 1501 1502 /* 1503 * For spell checking: concatenate the start of the following line "line" into 1504 * "buf", blanking-out special characters. Copy less then "maxlen" bytes. 1505 * Keep the blanks at the start of the next line, this is used in win_line() 1506 * to skip those bytes if the word was OK. 1507 */ 1508 void 1509 spell_cat_line(char_u *buf, char_u *line, int maxlen) 1510 { 1511 char_u *p; 1512 int n; 1513 1514 p = skipwhite(line); 1515 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) 1516 p = skipwhite(p + 1); 1517 1518 if (*p != NUL) 1519 { 1520 // Only worth concatenating if there is something else than spaces to 1521 // concatenate. 1522 n = (int)(p - line) + 1; 1523 if (n < maxlen - 1) 1524 { 1525 vim_memset(buf, ' ', n); 1526 vim_strncpy(buf + n, p, maxlen - 1 - n); 1527 } 1528 } 1529 } 1530 1531 /* 1532 * Structure used for the cookie argument of do_in_runtimepath(). 1533 */ 1534 typedef struct spelload_S 1535 { 1536 char_u sl_lang[MAXWLEN + 1]; // language name 1537 slang_T *sl_slang; // resulting slang_T struct 1538 int sl_nobreak; // NOBREAK language found 1539 } spelload_T; 1540 1541 /* 1542 * Load word list(s) for "lang" from Vim spell file(s). 1543 * "lang" must be the language without the region: e.g., "en". 1544 */ 1545 static void 1546 spell_load_lang(char_u *lang) 1547 { 1548 char_u fname_enc[85]; 1549 int r; 1550 spelload_T sl; 1551 int round; 1552 1553 // Copy the language name to pass it to spell_load_cb() as a cookie. 1554 // It's truncated when an error is detected. 1555 STRCPY(sl.sl_lang, lang); 1556 sl.sl_slang = NULL; 1557 sl.sl_nobreak = FALSE; 1558 1559 // We may retry when no spell file is found for the language, an 1560 // autocommand may load it then. 1561 for (round = 1; round <= 2; ++round) 1562 { 1563 /* 1564 * Find the first spell file for "lang" in 'runtimepath' and load it. 1565 */ 1566 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, 1567 #ifdef VMS 1568 "spell/%s_%s.spl", 1569 #else 1570 "spell/%s.%s.spl", 1571 #endif 1572 lang, spell_enc()); 1573 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); 1574 1575 if (r == FAIL && *sl.sl_lang != NUL) 1576 { 1577 // Try loading the ASCII version. 1578 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, 1579 #ifdef VMS 1580 "spell/%s_ascii.spl", 1581 #else 1582 "spell/%s.ascii.spl", 1583 #endif 1584 lang); 1585 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); 1586 1587 if (r == FAIL && *sl.sl_lang != NUL && round == 1 1588 && apply_autocmds(EVENT_SPELLFILEMISSING, lang, 1589 curbuf->b_fname, FALSE, curbuf)) 1590 continue; 1591 break; 1592 } 1593 break; 1594 } 1595 1596 if (r == FAIL) 1597 { 1598 smsg( 1599 #ifdef VMS 1600 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""), 1601 #else 1602 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""), 1603 #endif 1604 lang, spell_enc(), lang); 1605 } 1606 else if (sl.sl_slang != NULL) 1607 { 1608 // At least one file was loaded, now load ALL the additions. 1609 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); 1610 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl); 1611 } 1612 } 1613 1614 /* 1615 * Return the encoding used for spell checking: Use 'encoding', except that we 1616 * use "latin1" for "latin9". And limit to 60 characters (just in case). 1617 */ 1618 char_u * 1619 spell_enc(void) 1620 { 1621 1622 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) 1623 return p_enc; 1624 return (char_u *)"latin1"; 1625 } 1626 1627 /* 1628 * Get the name of the .spl file for the internal wordlist into 1629 * "fname[MAXPATHL]". 1630 */ 1631 static void 1632 int_wordlist_spl(char_u *fname) 1633 { 1634 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL, 1635 int_wordlist, spell_enc()); 1636 } 1637 1638 /* 1639 * Allocate a new slang_T for language "lang". "lang" can be NULL. 1640 * Caller must fill "sl_next". 1641 */ 1642 slang_T * 1643 slang_alloc(char_u *lang) 1644 { 1645 slang_T *lp; 1646 1647 lp = ALLOC_CLEAR_ONE(slang_T); 1648 if (lp != NULL) 1649 { 1650 if (lang != NULL) 1651 lp->sl_name = vim_strsave(lang); 1652 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); 1653 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10); 1654 lp->sl_compmax = MAXWLEN; 1655 lp->sl_compsylmax = MAXWLEN; 1656 hash_init(&lp->sl_wordcount); 1657 } 1658 1659 return lp; 1660 } 1661 1662 /* 1663 * Free the contents of an slang_T and the structure itself. 1664 */ 1665 void 1666 slang_free(slang_T *lp) 1667 { 1668 vim_free(lp->sl_name); 1669 vim_free(lp->sl_fname); 1670 slang_clear(lp); 1671 vim_free(lp); 1672 } 1673 1674 /* 1675 * Clear an slang_T so that the file can be reloaded. 1676 */ 1677 void 1678 slang_clear(slang_T *lp) 1679 { 1680 garray_T *gap; 1681 fromto_T *ftp; 1682 salitem_T *smp; 1683 int i; 1684 int round; 1685 1686 VIM_CLEAR(lp->sl_fbyts); 1687 VIM_CLEAR(lp->sl_kbyts); 1688 VIM_CLEAR(lp->sl_pbyts); 1689 1690 VIM_CLEAR(lp->sl_fidxs); 1691 VIM_CLEAR(lp->sl_kidxs); 1692 VIM_CLEAR(lp->sl_pidxs); 1693 1694 for (round = 1; round <= 2; ++round) 1695 { 1696 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal; 1697 while (gap->ga_len > 0) 1698 { 1699 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; 1700 vim_free(ftp->ft_from); 1701 vim_free(ftp->ft_to); 1702 } 1703 ga_clear(gap); 1704 } 1705 1706 gap = &lp->sl_sal; 1707 if (lp->sl_sofo) 1708 { 1709 // "ga_len" is set to 1 without adding an item for latin1 1710 if (gap->ga_data != NULL) 1711 // SOFOFROM and SOFOTO items: free lists of wide characters. 1712 for (i = 0; i < gap->ga_len; ++i) 1713 vim_free(((int **)gap->ga_data)[i]); 1714 } 1715 else 1716 // SAL items: free salitem_T items 1717 while (gap->ga_len > 0) 1718 { 1719 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; 1720 vim_free(smp->sm_lead); 1721 // Don't free sm_oneof and sm_rules, they point into sm_lead. 1722 vim_free(smp->sm_to); 1723 vim_free(smp->sm_lead_w); 1724 vim_free(smp->sm_oneof_w); 1725 vim_free(smp->sm_to_w); 1726 } 1727 ga_clear(gap); 1728 1729 for (i = 0; i < lp->sl_prefixcnt; ++i) 1730 vim_regfree(lp->sl_prefprog[i]); 1731 lp->sl_prefixcnt = 0; 1732 VIM_CLEAR(lp->sl_prefprog); 1733 1734 VIM_CLEAR(lp->sl_info); 1735 1736 VIM_CLEAR(lp->sl_midword); 1737 1738 vim_regfree(lp->sl_compprog); 1739 lp->sl_compprog = NULL; 1740 VIM_CLEAR(lp->sl_comprules); 1741 VIM_CLEAR(lp->sl_compstartflags); 1742 VIM_CLEAR(lp->sl_compallflags); 1743 1744 VIM_CLEAR(lp->sl_syllable); 1745 ga_clear(&lp->sl_syl_items); 1746 1747 ga_clear_strings(&lp->sl_comppat); 1748 1749 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF); 1750 hash_init(&lp->sl_wordcount); 1751 1752 hash_clear_all(&lp->sl_map_hash, 0); 1753 1754 // Clear info from .sug file. 1755 slang_clear_sug(lp); 1756 1757 lp->sl_compmax = MAXWLEN; 1758 lp->sl_compminlen = 0; 1759 lp->sl_compsylmax = MAXWLEN; 1760 lp->sl_regions[0] = NUL; 1761 } 1762 1763 /* 1764 * Clear the info from the .sug file in "lp". 1765 */ 1766 void 1767 slang_clear_sug(slang_T *lp) 1768 { 1769 VIM_CLEAR(lp->sl_sbyts); 1770 VIM_CLEAR(lp->sl_sidxs); 1771 close_spellbuf(lp->sl_sugbuf); 1772 lp->sl_sugbuf = NULL; 1773 lp->sl_sugloaded = FALSE; 1774 lp->sl_sugtime = 0; 1775 } 1776 1777 /* 1778 * Load one spell file and store the info into a slang_T. 1779 * Invoked through do_in_runtimepath(). 1780 */ 1781 static void 1782 spell_load_cb(char_u *fname, void *cookie) 1783 { 1784 spelload_T *slp = (spelload_T *)cookie; 1785 slang_T *slang; 1786 1787 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE); 1788 if (slang != NULL) 1789 { 1790 // When a previously loaded file has NOBREAK also use it for the 1791 // ".add" files. 1792 if (slp->sl_nobreak && slang->sl_add) 1793 slang->sl_nobreak = TRUE; 1794 else if (slang->sl_nobreak) 1795 slp->sl_nobreak = TRUE; 1796 1797 slp->sl_slang = slang; 1798 } 1799 } 1800 1801 1802 /* 1803 * Add a word to the hashtable of common words. 1804 * If it's already there then the counter is increased. 1805 */ 1806 void 1807 count_common_word( 1808 slang_T *lp, 1809 char_u *word, 1810 int len, // word length, -1 for up to NUL 1811 int count) // 1 to count once, 10 to init 1812 { 1813 hash_T hash; 1814 hashitem_T *hi; 1815 wordcount_T *wc; 1816 char_u buf[MAXWLEN]; 1817 char_u *p; 1818 1819 if (len == -1) 1820 p = word; 1821 else if (len >= MAXWLEN) 1822 return; 1823 else 1824 { 1825 vim_strncpy(buf, word, len); 1826 p = buf; 1827 } 1828 1829 hash = hash_hash(p); 1830 hi = hash_lookup(&lp->sl_wordcount, p, hash); 1831 if (HASHITEM_EMPTY(hi)) 1832 { 1833 wc = alloc(sizeof(wordcount_T) + STRLEN(p)); 1834 if (wc == NULL) 1835 return; 1836 STRCPY(wc->wc_word, p); 1837 wc->wc_count = count; 1838 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash); 1839 } 1840 else 1841 { 1842 wc = HI2WC(hi); 1843 if ((wc->wc_count += count) < (unsigned)count) // check for overflow 1844 wc->wc_count = MAXWORDCOUNT; 1845 } 1846 } 1847 1848 /* 1849 * Return TRUE if byte "n" appears in "str". 1850 * Like strchr() but independent of locale. 1851 */ 1852 int 1853 byte_in_str(char_u *str, int n) 1854 { 1855 char_u *p; 1856 1857 for (p = str; *p != NUL; ++p) 1858 if (*p == n) 1859 return TRUE; 1860 return FALSE; 1861 } 1862 1863 #define SY_MAXLEN 30 1864 typedef struct syl_item_S 1865 { 1866 char_u sy_chars[SY_MAXLEN]; // the sequence of chars 1867 int sy_len; 1868 } syl_item_T; 1869 1870 /* 1871 * Truncate "slang->sl_syllable" at the first slash and put the following items 1872 * in "slang->sl_syl_items". 1873 */ 1874 int 1875 init_syl_tab(slang_T *slang) 1876 { 1877 char_u *p; 1878 char_u *s; 1879 int l; 1880 syl_item_T *syl; 1881 1882 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4); 1883 p = vim_strchr(slang->sl_syllable, '/'); 1884 while (p != NULL) 1885 { 1886 *p++ = NUL; 1887 if (*p == NUL) // trailing slash 1888 break; 1889 s = p; 1890 p = vim_strchr(p, '/'); 1891 if (p == NULL) 1892 l = (int)STRLEN(s); 1893 else 1894 l = (int)(p - s); 1895 if (l >= SY_MAXLEN) 1896 return SP_FORMERROR; 1897 if (ga_grow(&slang->sl_syl_items, 1) == FAIL) 1898 return SP_OTHERERROR; 1899 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) 1900 + slang->sl_syl_items.ga_len++; 1901 vim_strncpy(syl->sy_chars, s, l); 1902 syl->sy_len = l; 1903 } 1904 return OK; 1905 } 1906 1907 /* 1908 * Count the number of syllables in "word". 1909 * When "word" contains spaces the syllables after the last space are counted. 1910 * Returns zero if syllables are not defines. 1911 */ 1912 static int 1913 count_syllables(slang_T *slang, char_u *word) 1914 { 1915 int cnt = 0; 1916 int skip = FALSE; 1917 char_u *p; 1918 int len; 1919 int i; 1920 syl_item_T *syl; 1921 int c; 1922 1923 if (slang->sl_syllable == NULL) 1924 return 0; 1925 1926 for (p = word; *p != NUL; p += len) 1927 { 1928 // When running into a space reset counter. 1929 if (*p == ' ') 1930 { 1931 len = 1; 1932 cnt = 0; 1933 continue; 1934 } 1935 1936 // Find longest match of syllable items. 1937 len = 0; 1938 for (i = 0; i < slang->sl_syl_items.ga_len; ++i) 1939 { 1940 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i; 1941 if (syl->sy_len > len 1942 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0) 1943 len = syl->sy_len; 1944 } 1945 if (len != 0) // found a match, count syllable 1946 { 1947 ++cnt; 1948 skip = FALSE; 1949 } 1950 else 1951 { 1952 // No recognized syllable item, at least a syllable char then? 1953 c = mb_ptr2char(p); 1954 len = (*mb_ptr2len)(p); 1955 if (vim_strchr(slang->sl_syllable, c) == NULL) 1956 skip = FALSE; // No, search for next syllable 1957 else if (!skip) 1958 { 1959 ++cnt; // Yes, count it 1960 skip = TRUE; // don't count following syllable chars 1961 } 1962 } 1963 } 1964 return cnt; 1965 } 1966 1967 /* 1968 * Parse 'spelllang' and set w_s->b_langp accordingly. 1969 * Returns NULL if it's OK, an error message otherwise. 1970 */ 1971 char * 1972 did_set_spelllang(win_T *wp) 1973 { 1974 garray_T ga; 1975 char_u *splp; 1976 char_u *region; 1977 char_u region_cp[3]; 1978 int filename; 1979 int region_mask; 1980 slang_T *slang; 1981 int c; 1982 char_u lang[MAXWLEN + 1]; 1983 char_u spf_name[MAXPATHL]; 1984 int len; 1985 char_u *p; 1986 int round; 1987 char_u *spf; 1988 char_u *use_region = NULL; 1989 int dont_use_region = FALSE; 1990 int nobreak = FALSE; 1991 int i, j; 1992 langp_T *lp, *lp2; 1993 static int recursive = FALSE; 1994 char *ret_msg = NULL; 1995 char_u *spl_copy; 1996 bufref_T bufref; 1997 1998 set_bufref(&bufref, wp->w_buffer); 1999 2000 // We don't want to do this recursively. May happen when a language is 2001 // not available and the SpellFileMissing autocommand opens a new buffer 2002 // in which 'spell' is set. 2003 if (recursive) 2004 return NULL; 2005 recursive = TRUE; 2006 2007 ga_init2(&ga, sizeof(langp_T), 2); 2008 clear_midword(wp); 2009 2010 // Make a copy of 'spelllang', the SpellFileMissing autocommands may change 2011 // it under our fingers. 2012 spl_copy = vim_strsave(wp->w_s->b_p_spl); 2013 if (spl_copy == NULL) 2014 goto theend; 2015 2016 wp->w_s->b_cjk = 0; 2017 2018 // Loop over comma separated language names. 2019 for (splp = spl_copy; *splp != NUL; ) 2020 { 2021 // Get one language name. 2022 copy_option_part(&splp, lang, MAXWLEN, ","); 2023 region = NULL; 2024 len = (int)STRLEN(lang); 2025 2026 if (!valid_spelllang(lang)) 2027 continue; 2028 2029 if (STRCMP(lang, "cjk") == 0) 2030 { 2031 wp->w_s->b_cjk = 1; 2032 continue; 2033 } 2034 2035 // If the name ends in ".spl" use it as the name of the spell file. 2036 // If there is a region name let "region" point to it and remove it 2037 // from the name. 2038 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) 2039 { 2040 filename = TRUE; 2041 2042 // Locate a region and remove it from the file name. 2043 p = vim_strchr(gettail(lang), '_'); 2044 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2]) 2045 && !ASCII_ISALPHA(p[3])) 2046 { 2047 vim_strncpy(region_cp, p + 1, 2); 2048 mch_memmove(p, p + 3, len - (p - lang) - 2); 2049 region = region_cp; 2050 } 2051 else 2052 dont_use_region = TRUE; 2053 2054 // Check if we loaded this language before. 2055 FOR_ALL_SPELL_LANGS(slang) 2056 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME) 2057 break; 2058 } 2059 else 2060 { 2061 filename = FALSE; 2062 if (len > 3 && lang[len - 3] == '_') 2063 { 2064 region = lang + len - 2; 2065 len -= 3; 2066 lang[len] = NUL; 2067 } 2068 else 2069 dont_use_region = TRUE; 2070 2071 // Check if we loaded this language before. 2072 FOR_ALL_SPELL_LANGS(slang) 2073 if (STRICMP(lang, slang->sl_name) == 0) 2074 break; 2075 } 2076 2077 if (region != NULL) 2078 { 2079 // If the region differs from what was used before then don't 2080 // use it for 'spellfile'. 2081 if (use_region != NULL && STRCMP(region, use_region) != 0) 2082 dont_use_region = TRUE; 2083 use_region = region; 2084 } 2085 2086 // If not found try loading the language now. 2087 if (slang == NULL) 2088 { 2089 if (filename) 2090 (void)spell_load_file(lang, lang, NULL, FALSE); 2091 else 2092 { 2093 spell_load_lang(lang); 2094 // SpellFileMissing autocommands may do anything, including 2095 // destroying the buffer we are using... 2096 if (!bufref_valid(&bufref)) 2097 { 2098 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer"); 2099 goto theend; 2100 } 2101 } 2102 } 2103 2104 /* 2105 * Loop over the languages, there can be several files for "lang". 2106 */ 2107 FOR_ALL_SPELL_LANGS(slang) 2108 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) 2109 == FPC_SAME 2110 : STRICMP(lang, slang->sl_name) == 0) 2111 { 2112 region_mask = REGION_ALL; 2113 if (!filename && region != NULL) 2114 { 2115 // find region in sl_regions 2116 c = find_region(slang->sl_regions, region); 2117 if (c == REGION_ALL) 2118 { 2119 if (slang->sl_add) 2120 { 2121 if (*slang->sl_regions != NUL) 2122 // This addition file is for other regions. 2123 region_mask = 0; 2124 } 2125 else 2126 // This is probably an error. Give a warning and 2127 // accept the words anyway. 2128 smsg(_("Warning: region %s not supported"), 2129 region); 2130 } 2131 else 2132 region_mask = 1 << c; 2133 } 2134 2135 if (region_mask != 0) 2136 { 2137 if (ga_grow(&ga, 1) == FAIL) 2138 { 2139 ga_clear(&ga); 2140 ret_msg = e_outofmem; 2141 goto theend; 2142 } 2143 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; 2144 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; 2145 ++ga.ga_len; 2146 use_midword(slang, wp); 2147 if (slang->sl_nobreak) 2148 nobreak = TRUE; 2149 } 2150 } 2151 } 2152 2153 // round 0: load int_wordlist, if possible. 2154 // round 1: load first name in 'spellfile'. 2155 // round 2: load second name in 'spellfile. 2156 // etc. 2157 spf = curwin->w_s->b_p_spf; 2158 for (round = 0; round == 0 || *spf != NUL; ++round) 2159 { 2160 if (round == 0) 2161 { 2162 // Internal wordlist, if there is one. 2163 if (int_wordlist == NULL) 2164 continue; 2165 int_wordlist_spl(spf_name); 2166 } 2167 else 2168 { 2169 // One entry in 'spellfile'. 2170 copy_option_part(&spf, spf_name, MAXPATHL - 5, ","); 2171 STRCAT(spf_name, ".spl"); 2172 2173 // If it was already found above then skip it. 2174 for (c = 0; c < ga.ga_len; ++c) 2175 { 2176 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname; 2177 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE) 2178 == FPC_SAME) 2179 break; 2180 } 2181 if (c < ga.ga_len) 2182 continue; 2183 } 2184 2185 // Check if it was loaded already. 2186 FOR_ALL_SPELL_LANGS(slang) 2187 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE) 2188 == FPC_SAME) 2189 break; 2190 if (slang == NULL) 2191 { 2192 // Not loaded, try loading it now. The language name includes the 2193 // region name, the region is ignored otherwise. for int_wordlist 2194 // use an arbitrary name. 2195 if (round == 0) 2196 STRCPY(lang, "internal wordlist"); 2197 else 2198 { 2199 vim_strncpy(lang, gettail(spf_name), MAXWLEN); 2200 p = vim_strchr(lang, '.'); 2201 if (p != NULL) 2202 *p = NUL; // truncate at ".encoding.add" 2203 } 2204 slang = spell_load_file(spf_name, lang, NULL, TRUE); 2205 2206 // If one of the languages has NOBREAK we assume the addition 2207 // files also have this. 2208 if (slang != NULL && nobreak) 2209 slang->sl_nobreak = TRUE; 2210 } 2211 if (slang != NULL && ga_grow(&ga, 1) == OK) 2212 { 2213 region_mask = REGION_ALL; 2214 if (use_region != NULL && !dont_use_region) 2215 { 2216 // find region in sl_regions 2217 c = find_region(slang->sl_regions, use_region); 2218 if (c != REGION_ALL) 2219 region_mask = 1 << c; 2220 else if (*slang->sl_regions != NUL) 2221 // This spell file is for other regions. 2222 region_mask = 0; 2223 } 2224 2225 if (region_mask != 0) 2226 { 2227 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; 2228 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL; 2229 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL; 2230 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; 2231 ++ga.ga_len; 2232 use_midword(slang, wp); 2233 } 2234 } 2235 } 2236 2237 // Everything is fine, store the new b_langp value. 2238 ga_clear(&wp->w_s->b_langp); 2239 wp->w_s->b_langp = ga; 2240 2241 // For each language figure out what language to use for sound folding and 2242 // REP items. If the language doesn't support it itself use another one 2243 // with the same name. E.g. for "en-math" use "en". 2244 for (i = 0; i < ga.ga_len; ++i) 2245 { 2246 lp = LANGP_ENTRY(ga, i); 2247 2248 // sound folding 2249 if (lp->lp_slang->sl_sal.ga_len > 0) 2250 // language does sound folding itself 2251 lp->lp_sallang = lp->lp_slang; 2252 else 2253 // find first similar language that does sound folding 2254 for (j = 0; j < ga.ga_len; ++j) 2255 { 2256 lp2 = LANGP_ENTRY(ga, j); 2257 if (lp2->lp_slang->sl_sal.ga_len > 0 2258 && STRNCMP(lp->lp_slang->sl_name, 2259 lp2->lp_slang->sl_name, 2) == 0) 2260 { 2261 lp->lp_sallang = lp2->lp_slang; 2262 break; 2263 } 2264 } 2265 2266 // REP items 2267 if (lp->lp_slang->sl_rep.ga_len > 0) 2268 // language has REP items itself 2269 lp->lp_replang = lp->lp_slang; 2270 else 2271 // find first similar language that has REP items 2272 for (j = 0; j < ga.ga_len; ++j) 2273 { 2274 lp2 = LANGP_ENTRY(ga, j); 2275 if (lp2->lp_slang->sl_rep.ga_len > 0 2276 && STRNCMP(lp->lp_slang->sl_name, 2277 lp2->lp_slang->sl_name, 2) == 0) 2278 { 2279 lp->lp_replang = lp2->lp_slang; 2280 break; 2281 } 2282 } 2283 } 2284 redraw_win_later(wp, NOT_VALID); 2285 2286 theend: 2287 vim_free(spl_copy); 2288 recursive = FALSE; 2289 return ret_msg; 2290 } 2291 2292 /* 2293 * Clear the midword characters for buffer "buf". 2294 */ 2295 static void 2296 clear_midword(win_T *wp) 2297 { 2298 CLEAR_FIELD(wp->w_s->b_spell_ismw); 2299 VIM_CLEAR(wp->w_s->b_spell_ismw_mb); 2300 } 2301 2302 /* 2303 * Use the "sl_midword" field of language "lp" for buffer "buf". 2304 * They add up to any currently used midword characters. 2305 */ 2306 static void 2307 use_midword(slang_T *lp, win_T *wp) 2308 { 2309 char_u *p; 2310 2311 if (lp->sl_midword == NULL) // there aren't any 2312 return; 2313 2314 for (p = lp->sl_midword; *p != NUL; ) 2315 if (has_mbyte) 2316 { 2317 int c, l, n; 2318 char_u *bp; 2319 2320 c = mb_ptr2char(p); 2321 l = (*mb_ptr2len)(p); 2322 if (c < 256 && l <= 2) 2323 wp->w_s->b_spell_ismw[c] = TRUE; 2324 else if (wp->w_s->b_spell_ismw_mb == NULL) 2325 // First multi-byte char in "b_spell_ismw_mb". 2326 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l); 2327 else 2328 { 2329 // Append multi-byte chars to "b_spell_ismw_mb". 2330 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb); 2331 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l); 2332 if (bp != NULL) 2333 { 2334 vim_free(wp->w_s->b_spell_ismw_mb); 2335 wp->w_s->b_spell_ismw_mb = bp; 2336 vim_strncpy(bp + n, p, l); 2337 } 2338 } 2339 p += l; 2340 } 2341 else 2342 wp->w_s->b_spell_ismw[*p++] = TRUE; 2343 } 2344 2345 /* 2346 * Find the region "region[2]" in "rp" (points to "sl_regions"). 2347 * Each region is simply stored as the two characters of its name. 2348 * Returns the index if found (first is 0), REGION_ALL if not found. 2349 */ 2350 static int 2351 find_region(char_u *rp, char_u *region) 2352 { 2353 int i; 2354 2355 for (i = 0; ; i += 2) 2356 { 2357 if (rp[i] == NUL) 2358 return REGION_ALL; 2359 if (rp[i] == region[0] && rp[i + 1] == region[1]) 2360 break; 2361 } 2362 return i / 2; 2363 } 2364 2365 /* 2366 * Return case type of word: 2367 * w word 0 2368 * Word WF_ONECAP 2369 * W WORD WF_ALLCAP 2370 * WoRd wOrd WF_KEEPCAP 2371 */ 2372 int 2373 captype( 2374 char_u *word, 2375 char_u *end) // When NULL use up to NUL byte. 2376 { 2377 char_u *p; 2378 int c; 2379 int firstcap; 2380 int allcap; 2381 int past_second = FALSE; // past second word char 2382 2383 // find first letter 2384 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p)) 2385 if (end == NULL ? *p == NUL : p >= end) 2386 return 0; // only non-word characters, illegal word 2387 if (has_mbyte) 2388 c = mb_ptr2char_adv(&p); 2389 else 2390 c = *p++; 2391 firstcap = allcap = SPELL_ISUPPER(c); 2392 2393 /* 2394 * Need to check all letters to find a word with mixed upper/lower. 2395 * But a word with an upper char only at start is a ONECAP. 2396 */ 2397 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p)) 2398 if (spell_iswordp_nmw(p, curwin)) 2399 { 2400 c = PTR2CHAR(p); 2401 if (!SPELL_ISUPPER(c)) 2402 { 2403 // UUl -> KEEPCAP 2404 if (past_second && allcap) 2405 return WF_KEEPCAP; 2406 allcap = FALSE; 2407 } 2408 else if (!allcap) 2409 // UlU -> KEEPCAP 2410 return WF_KEEPCAP; 2411 past_second = TRUE; 2412 } 2413 2414 if (allcap) 2415 return WF_ALLCAP; 2416 if (firstcap) 2417 return WF_ONECAP; 2418 return 0; 2419 } 2420 2421 /* 2422 * Delete the internal wordlist and its .spl file. 2423 */ 2424 void 2425 spell_delete_wordlist(void) 2426 { 2427 char_u fname[MAXPATHL]; 2428 2429 if (int_wordlist != NULL) 2430 { 2431 mch_remove(int_wordlist); 2432 int_wordlist_spl(fname); 2433 mch_remove(fname); 2434 VIM_CLEAR(int_wordlist); 2435 } 2436 } 2437 2438 /* 2439 * Free all languages. 2440 */ 2441 void 2442 spell_free_all(void) 2443 { 2444 slang_T *slang; 2445 buf_T *buf; 2446 2447 // Go through all buffers and handle 'spelllang'. <VN> 2448 FOR_ALL_BUFFERS(buf) 2449 ga_clear(&buf->b_s.b_langp); 2450 2451 while (first_lang != NULL) 2452 { 2453 slang = first_lang; 2454 first_lang = slang->sl_next; 2455 slang_free(slang); 2456 } 2457 2458 spell_delete_wordlist(); 2459 2460 VIM_CLEAR(repl_to); 2461 VIM_CLEAR(repl_from); 2462 } 2463 2464 /* 2465 * Clear all spelling tables and reload them. 2466 * Used after 'encoding' is set and when ":mkspell" was used. 2467 */ 2468 void 2469 spell_reload(void) 2470 { 2471 win_T *wp; 2472 2473 // Initialize the table for spell_iswordp(). 2474 init_spell_chartab(); 2475 2476 // Unload all allocated memory. 2477 spell_free_all(); 2478 2479 // Go through all buffers and handle 'spelllang'. 2480 FOR_ALL_WINDOWS(wp) 2481 { 2482 // Only load the wordlists when 'spelllang' is set and there is a 2483 // window for this buffer in which 'spell' is set. 2484 if (*wp->w_s->b_p_spl != NUL) 2485 { 2486 if (wp->w_p_spell) 2487 { 2488 (void)did_set_spelllang(wp); 2489 break; 2490 } 2491 } 2492 } 2493 } 2494 2495 /* 2496 * Open a spell buffer. This is a nameless buffer that is not in the buffer 2497 * list and only contains text lines. Can use a swapfile to reduce memory 2498 * use. 2499 * Most other fields are invalid! Esp. watch out for string options being 2500 * NULL and there is no undo info. 2501 * Returns NULL when out of memory. 2502 */ 2503 buf_T * 2504 open_spellbuf(void) 2505 { 2506 buf_T *buf; 2507 2508 buf = ALLOC_CLEAR_ONE(buf_T); 2509 if (buf != NULL) 2510 { 2511 buf->b_spell = TRUE; 2512 buf->b_p_swf = TRUE; // may create a swap file 2513 #ifdef FEAT_CRYPT 2514 buf->b_p_key = empty_option; 2515 #endif 2516 ml_open(buf); 2517 ml_open_file(buf); // create swap file now 2518 } 2519 return buf; 2520 } 2521 2522 /* 2523 * Close the buffer used for spell info. 2524 */ 2525 void 2526 close_spellbuf(buf_T *buf) 2527 { 2528 if (buf != NULL) 2529 { 2530 ml_close(buf, TRUE); 2531 vim_free(buf); 2532 } 2533 } 2534 2535 /* 2536 * Init the chartab used for spelling for ASCII. 2537 * EBCDIC is not supported! 2538 */ 2539 void 2540 clear_spell_chartab(spelltab_T *sp) 2541 { 2542 int i; 2543 2544 // Init everything to FALSE (zero). 2545 CLEAR_FIELD(sp->st_isw); 2546 CLEAR_FIELD(sp->st_isu); 2547 for (i = 0; i < 256; ++i) 2548 { 2549 sp->st_fold[i] = i; 2550 sp->st_upper[i] = i; 2551 } 2552 2553 // We include digits. A word shouldn't start with a digit, but handling 2554 // that is done separately. 2555 for (i = '0'; i <= '9'; ++i) 2556 sp->st_isw[i] = TRUE; 2557 for (i = 'A'; i <= 'Z'; ++i) 2558 { 2559 sp->st_isw[i] = TRUE; 2560 sp->st_isu[i] = TRUE; 2561 sp->st_fold[i] = i + 0x20; 2562 } 2563 for (i = 'a'; i <= 'z'; ++i) 2564 { 2565 sp->st_isw[i] = TRUE; 2566 sp->st_upper[i] = i - 0x20; 2567 } 2568 } 2569 2570 /* 2571 * Init the chartab used for spelling. Only depends on 'encoding'. 2572 * Called once while starting up and when 'encoding' changes. 2573 * The default is to use isalpha(), but the spell file should define the word 2574 * characters to make it possible that 'encoding' differs from the current 2575 * locale. For utf-8 we don't use isalpha() but our own functions. 2576 */ 2577 void 2578 init_spell_chartab(void) 2579 { 2580 int i; 2581 2582 did_set_spelltab = FALSE; 2583 clear_spell_chartab(&spelltab); 2584 if (enc_dbcs) 2585 { 2586 // DBCS: assume double-wide characters are word characters. 2587 for (i = 128; i <= 255; ++i) 2588 if (MB_BYTE2LEN(i) == 2) 2589 spelltab.st_isw[i] = TRUE; 2590 } 2591 else if (enc_utf8) 2592 { 2593 for (i = 128; i < 256; ++i) 2594 { 2595 int f = utf_fold(i); 2596 int u = utf_toupper(i); 2597 2598 spelltab.st_isu[i] = utf_isupper(i); 2599 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); 2600 // The folded/upper-cased value is different between latin1 and 2601 // utf8 for 0xb5, causing E763 for no good reason. Use the latin1 2602 // value for utf-8 to avoid this. 2603 spelltab.st_fold[i] = (f < 256) ? f : i; 2604 spelltab.st_upper[i] = (u < 256) ? u : i; 2605 } 2606 } 2607 else 2608 { 2609 // Rough guess: use locale-dependent library functions. 2610 for (i = 128; i < 256; ++i) 2611 { 2612 if (MB_ISUPPER(i)) 2613 { 2614 spelltab.st_isw[i] = TRUE; 2615 spelltab.st_isu[i] = TRUE; 2616 spelltab.st_fold[i] = MB_TOLOWER(i); 2617 } 2618 else if (MB_ISLOWER(i)) 2619 { 2620 spelltab.st_isw[i] = TRUE; 2621 spelltab.st_upper[i] = MB_TOUPPER(i); 2622 } 2623 } 2624 } 2625 } 2626 2627 2628 /* 2629 * Return TRUE if "p" points to a word character. 2630 * As a special case we see "midword" characters as word character when it is 2631 * followed by a word character. This finds they'there but not 'they there'. 2632 * Thus this only works properly when past the first character of the word. 2633 */ 2634 int 2635 spell_iswordp( 2636 char_u *p, 2637 win_T *wp) // buffer used 2638 { 2639 char_u *s; 2640 int l; 2641 int c; 2642 2643 if (has_mbyte) 2644 { 2645 l = mb_ptr2len(p); 2646 s = p; 2647 if (l == 1) 2648 { 2649 // be quick for ASCII 2650 if (wp->w_s->b_spell_ismw[*p]) 2651 s = p + 1; // skip a mid-word character 2652 } 2653 else 2654 { 2655 c = mb_ptr2char(p); 2656 if (c < 256 ? wp->w_s->b_spell_ismw[c] 2657 : (wp->w_s->b_spell_ismw_mb != NULL 2658 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL)) 2659 s = p + l; 2660 } 2661 2662 c = mb_ptr2char(s); 2663 if (c > 255) 2664 return spell_mb_isword_class(mb_get_class(s), wp); 2665 return spelltab.st_isw[c]; 2666 } 2667 2668 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]]; 2669 } 2670 2671 /* 2672 * Return TRUE if "p" points to a word character. 2673 * Unlike spell_iswordp() this doesn't check for "midword" characters. 2674 */ 2675 int 2676 spell_iswordp_nmw(char_u *p, win_T *wp) 2677 { 2678 int c; 2679 2680 if (has_mbyte) 2681 { 2682 c = mb_ptr2char(p); 2683 if (c > 255) 2684 return spell_mb_isword_class(mb_get_class(p), wp); 2685 return spelltab.st_isw[c]; 2686 } 2687 return spelltab.st_isw[*p]; 2688 } 2689 2690 /* 2691 * Return TRUE if word class indicates a word character. 2692 * Only for characters above 255. 2693 * Unicode subscript and superscript are not considered word characters. 2694 * See also dbcs_class() and utf_class() in mbyte.c. 2695 */ 2696 static int 2697 spell_mb_isword_class(int cl, win_T *wp) 2698 { 2699 if (wp->w_s->b_cjk) 2700 // East Asian characters are not considered word characters. 2701 return cl == 2 || cl == 0x2800; 2702 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3; 2703 } 2704 2705 /* 2706 * Return TRUE if "p" points to a word character. 2707 * Wide version of spell_iswordp(). 2708 */ 2709 static int 2710 spell_iswordp_w(int *p, win_T *wp) 2711 { 2712 int *s; 2713 2714 if (*p < 256 ? wp->w_s->b_spell_ismw[*p] 2715 : (wp->w_s->b_spell_ismw_mb != NULL 2716 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL)) 2717 s = p + 1; 2718 else 2719 s = p; 2720 2721 if (*s > 255) 2722 { 2723 if (enc_utf8) 2724 return spell_mb_isword_class(utf_class(*s), wp); 2725 if (enc_dbcs) 2726 return spell_mb_isword_class( 2727 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp); 2728 return 0; 2729 } 2730 return spelltab.st_isw[*s]; 2731 } 2732 2733 /* 2734 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. 2735 * Uses the character definitions from the .spl file. 2736 * When using a multi-byte 'encoding' the length may change! 2737 * Returns FAIL when something wrong. 2738 */ 2739 int 2740 spell_casefold( 2741 win_T *wp, 2742 char_u *str, 2743 int len, 2744 char_u *buf, 2745 int buflen) 2746 { 2747 int i; 2748 2749 if (len >= buflen) 2750 { 2751 buf[0] = NUL; 2752 return FAIL; // result will not fit 2753 } 2754 2755 if (has_mbyte) 2756 { 2757 int outi = 0; 2758 char_u *p; 2759 int c; 2760 2761 // Fold one character at a time. 2762 for (p = str; p < str + len; ) 2763 { 2764 if (outi + MB_MAXBYTES > buflen) 2765 { 2766 buf[outi] = NUL; 2767 return FAIL; 2768 } 2769 c = mb_cptr2char_adv(&p); 2770 2771 // Exception: greek capital sigma 0x03A3 folds to 0x03C3, except 2772 // when it is the last character in a word, then it folds to 2773 // 0x03C2. 2774 if (c == 0x03a3 || c == 0x03c2) 2775 { 2776 if (p == str + len || !spell_iswordp(p, wp)) 2777 c = 0x03c2; 2778 else 2779 c = 0x03c3; 2780 } 2781 else 2782 c = SPELL_TOFOLD(c); 2783 2784 outi += mb_char2bytes(c, buf + outi); 2785 } 2786 buf[outi] = NUL; 2787 } 2788 else 2789 { 2790 // Be quick for non-multibyte encodings. 2791 for (i = 0; i < len; ++i) 2792 buf[i] = spelltab.st_fold[str[i]]; 2793 buf[i] = NUL; 2794 } 2795 2796 return OK; 2797 } 2798 2799 /* 2800 * Check if the word at line "lnum" column "col" is required to start with a 2801 * capital. This uses 'spellcapcheck' of the current buffer. 2802 */ 2803 int 2804 check_need_cap(linenr_T lnum, colnr_T col) 2805 { 2806 int need_cap = FALSE; 2807 char_u *line; 2808 char_u *line_copy = NULL; 2809 char_u *p; 2810 colnr_T endcol; 2811 regmatch_T regmatch; 2812 2813 if (curwin->w_s->b_cap_prog == NULL) 2814 return FALSE; 2815 2816 line = ml_get_curline(); 2817 endcol = 0; 2818 if (getwhitecols(line) >= (int)col) 2819 { 2820 // At start of line, check if previous line is empty or sentence 2821 // ends there. 2822 if (lnum == 1) 2823 need_cap = TRUE; 2824 else 2825 { 2826 line = ml_get(lnum - 1); 2827 if (*skipwhite(line) == NUL) 2828 need_cap = TRUE; 2829 else 2830 { 2831 // Append a space in place of the line break. 2832 line_copy = concat_str(line, (char_u *)" "); 2833 line = line_copy; 2834 endcol = (colnr_T)STRLEN(line); 2835 } 2836 } 2837 } 2838 else 2839 endcol = col; 2840 2841 if (endcol > 0) 2842 { 2843 // Check if sentence ends before the bad word. 2844 regmatch.regprog = curwin->w_s->b_cap_prog; 2845 regmatch.rm_ic = FALSE; 2846 p = line + endcol; 2847 for (;;) 2848 { 2849 MB_PTR_BACK(line, p); 2850 if (p == line || spell_iswordp_nmw(p, curwin)) 2851 break; 2852 if (vim_regexec(®match, p, 0) 2853 && regmatch.endp[0] == line + endcol) 2854 { 2855 need_cap = TRUE; 2856 break; 2857 } 2858 } 2859 curwin->w_s->b_cap_prog = regmatch.regprog; 2860 } 2861 2862 vim_free(line_copy); 2863 2864 return need_cap; 2865 } 2866 2867 2868 /* 2869 * ":spellrepall" 2870 */ 2871 void 2872 ex_spellrepall(exarg_T *eap UNUSED) 2873 { 2874 pos_T pos = curwin->w_cursor; 2875 char_u *frompat; 2876 int addlen; 2877 char_u *line; 2878 char_u *p; 2879 int save_ws = p_ws; 2880 linenr_T prev_lnum = 0; 2881 2882 if (repl_from == NULL || repl_to == NULL) 2883 { 2884 emsg(_("E752: No previous spell replacement")); 2885 return; 2886 } 2887 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from)); 2888 2889 frompat = alloc(STRLEN(repl_from) + 7); 2890 if (frompat == NULL) 2891 return; 2892 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); 2893 p_ws = FALSE; 2894 2895 sub_nsubs = 0; 2896 sub_nlines = 0; 2897 curwin->w_cursor.lnum = 0; 2898 while (!got_int) 2899 { 2900 if (do_search(NULL, '/', '/', frompat, 1L, SEARCH_KEEP, NULL) == 0 2901 || u_save_cursor() == FAIL) 2902 break; 2903 2904 // Only replace when the right word isn't there yet. This happens 2905 // when changing "etc" to "etc.". 2906 line = ml_get_curline(); 2907 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, 2908 repl_to, STRLEN(repl_to)) != 0) 2909 { 2910 p = alloc(STRLEN(line) + addlen + 1); 2911 if (p == NULL) 2912 break; 2913 mch_memmove(p, line, curwin->w_cursor.col); 2914 STRCPY(p + curwin->w_cursor.col, repl_to); 2915 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); 2916 ml_replace(curwin->w_cursor.lnum, p, FALSE); 2917 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); 2918 2919 if (curwin->w_cursor.lnum != prev_lnum) 2920 { 2921 ++sub_nlines; 2922 prev_lnum = curwin->w_cursor.lnum; 2923 } 2924 ++sub_nsubs; 2925 } 2926 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to); 2927 } 2928 2929 p_ws = save_ws; 2930 curwin->w_cursor = pos; 2931 vim_free(frompat); 2932 2933 if (sub_nsubs == 0) 2934 semsg(_("E753: Not found: %s"), repl_from); 2935 else 2936 do_sub_msg(FALSE); 2937 } 2938 2939 /* 2940 * Make a copy of "word", with the first letter upper or lower cased, to 2941 * "wcopy[MAXWLEN]". "word" must not be empty. 2942 * The result is NUL terminated. 2943 */ 2944 void 2945 onecap_copy( 2946 char_u *word, 2947 char_u *wcopy, 2948 int upper) // TRUE: first letter made upper case 2949 { 2950 char_u *p; 2951 int c; 2952 int l; 2953 2954 p = word; 2955 if (has_mbyte) 2956 c = mb_cptr2char_adv(&p); 2957 else 2958 c = *p++; 2959 if (upper) 2960 c = SPELL_TOUPPER(c); 2961 else 2962 c = SPELL_TOFOLD(c); 2963 if (has_mbyte) 2964 l = mb_char2bytes(c, wcopy); 2965 else 2966 { 2967 l = 1; 2968 wcopy[0] = c; 2969 } 2970 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); 2971 } 2972 2973 /* 2974 * Make a copy of "word" with all the letters upper cased into 2975 * "wcopy[MAXWLEN]". The result is NUL terminated. 2976 */ 2977 void 2978 allcap_copy(char_u *word, char_u *wcopy) 2979 { 2980 char_u *s; 2981 char_u *d; 2982 int c; 2983 2984 d = wcopy; 2985 for (s = word; *s != NUL; ) 2986 { 2987 if (has_mbyte) 2988 c = mb_cptr2char_adv(&s); 2989 else 2990 c = *s++; 2991 2992 // We only change 0xdf to SS when we are certain latin1 is used. It 2993 // would cause weird errors in other 8-bit encodings. 2994 if (enc_latin1like && c == 0xdf) 2995 { 2996 c = 'S'; 2997 if (d - wcopy >= MAXWLEN - 1) 2998 break; 2999 *d++ = c; 3000 } 3001 else 3002 c = SPELL_TOUPPER(c); 3003 3004 if (has_mbyte) 3005 { 3006 if (d - wcopy >= MAXWLEN - MB_MAXBYTES) 3007 break; 3008 d += mb_char2bytes(c, d); 3009 } 3010 else 3011 { 3012 if (d - wcopy >= MAXWLEN - 1) 3013 break; 3014 *d++ = c; 3015 } 3016 } 3017 *d = NUL; 3018 } 3019 3020 /* 3021 * Case-folding may change the number of bytes: Count nr of chars in 3022 * fword[flen] and return the byte length of that many chars in "word". 3023 */ 3024 int 3025 nofold_len(char_u *fword, int flen, char_u *word) 3026 { 3027 char_u *p; 3028 int i = 0; 3029 3030 for (p = fword; p < fword + flen; MB_PTR_ADV(p)) 3031 ++i; 3032 for (p = word; i > 0; MB_PTR_ADV(p)) 3033 --i; 3034 return (int)(p - word); 3035 } 3036 3037 /* 3038 * Copy "fword" to "cword", fixing case according to "flags". 3039 */ 3040 void 3041 make_case_word(char_u *fword, char_u *cword, int flags) 3042 { 3043 if (flags & WF_ALLCAP) 3044 // Make it all upper-case 3045 allcap_copy(fword, cword); 3046 else if (flags & WF_ONECAP) 3047 // Make the first letter upper-case 3048 onecap_copy(fword, cword, TRUE); 3049 else 3050 // Use goodword as-is. 3051 STRCPY(cword, fword); 3052 } 3053 3054 #if defined(FEAT_EVAL) || defined(PROTO) 3055 /* 3056 * Soundfold a string, for soundfold(). 3057 * Result is in allocated memory, NULL for an error. 3058 */ 3059 char_u * 3060 eval_soundfold(char_u *word) 3061 { 3062 langp_T *lp; 3063 char_u sound[MAXWLEN]; 3064 int lpi; 3065 3066 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) 3067 // Use the sound-folding of the first language that supports it. 3068 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) 3069 { 3070 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); 3071 if (lp->lp_slang->sl_sal.ga_len > 0) 3072 { 3073 // soundfold the word 3074 spell_soundfold(lp->lp_slang, word, FALSE, sound); 3075 return vim_strsave(sound); 3076 } 3077 } 3078 3079 // No language with sound folding, return word as-is. 3080 return vim_strsave(word); 3081 } 3082 #endif 3083 3084 /* 3085 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". 3086 * 3087 * There are many ways to turn a word into a sound-a-like representation. The 3088 * oldest is Soundex (1918!). A nice overview can be found in "Approximate 3089 * swedish name matching - survey and test of different algorithms" by Klas 3090 * Erikson. 3091 * 3092 * We support two methods: 3093 * 1. SOFOFROM/SOFOTO do a simple character mapping. 3094 * 2. SAL items define a more advanced sound-folding (and much slower). 3095 */ 3096 void 3097 spell_soundfold( 3098 slang_T *slang, 3099 char_u *inword, 3100 int folded, // "inword" is already case-folded 3101 char_u *res) 3102 { 3103 char_u fword[MAXWLEN]; 3104 char_u *word; 3105 3106 if (slang->sl_sofo) 3107 // SOFOFROM and SOFOTO used 3108 spell_soundfold_sofo(slang, inword, res); 3109 else 3110 { 3111 // SAL items used. Requires the word to be case-folded. 3112 if (folded) 3113 word = inword; 3114 else 3115 { 3116 (void)spell_casefold(curwin, 3117 inword, (int)STRLEN(inword), fword, MAXWLEN); 3118 word = fword; 3119 } 3120 3121 if (has_mbyte) 3122 spell_soundfold_wsal(slang, word, res); 3123 else 3124 spell_soundfold_sal(slang, word, res); 3125 } 3126 } 3127 3128 /* 3129 * Perform sound folding of "inword" into "res" according to SOFOFROM and 3130 * SOFOTO lines. 3131 */ 3132 static void 3133 spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res) 3134 { 3135 char_u *s; 3136 int ri = 0; 3137 int c; 3138 3139 if (has_mbyte) 3140 { 3141 int prevc = 0; 3142 int *ip; 3143 3144 // The sl_sal_first[] table contains the translation for chars up to 3145 // 255, sl_sal the rest. 3146 for (s = inword; *s != NUL; ) 3147 { 3148 c = mb_cptr2char_adv(&s); 3149 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) 3150 c = ' '; 3151 else if (c < 256) 3152 c = slang->sl_sal_first[c]; 3153 else 3154 { 3155 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; 3156 if (ip == NULL) // empty list, can't match 3157 c = NUL; 3158 else 3159 for (;;) // find "c" in the list 3160 { 3161 if (*ip == 0) // not found 3162 { 3163 c = NUL; 3164 break; 3165 } 3166 if (*ip == c) // match! 3167 { 3168 c = ip[1]; 3169 break; 3170 } 3171 ip += 2; 3172 } 3173 } 3174 3175 if (c != NUL && c != prevc) 3176 { 3177 ri += mb_char2bytes(c, res + ri); 3178 if (ri + MB_MAXBYTES > MAXWLEN) 3179 break; 3180 prevc = c; 3181 } 3182 } 3183 } 3184 else 3185 { 3186 // The sl_sal_first[] table contains the translation. 3187 for (s = inword; (c = *s) != NUL; ++s) 3188 { 3189 if (VIM_ISWHITE(c)) 3190 c = ' '; 3191 else 3192 c = slang->sl_sal_first[c]; 3193 if (c != NUL && (ri == 0 || res[ri - 1] != c)) 3194 res[ri++] = c; 3195 } 3196 } 3197 3198 res[ri] = NUL; 3199 } 3200 3201 static void 3202 spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res) 3203 { 3204 salitem_T *smp; 3205 char_u word[MAXWLEN]; 3206 char_u *s = inword; 3207 char_u *t; 3208 char_u *pf; 3209 int i, j, z; 3210 int reslen; 3211 int n, k = 0; 3212 int z0; 3213 int k0; 3214 int n0; 3215 int c; 3216 int pri; 3217 int p0 = -333; 3218 int c0; 3219 3220 // Remove accents, if wanted. We actually remove all non-word characters. 3221 // But keep white space. We need a copy, the word may be changed here. 3222 if (slang->sl_rem_accents) 3223 { 3224 t = word; 3225 while (*s != NUL) 3226 { 3227 if (VIM_ISWHITE(*s)) 3228 { 3229 *t++ = ' '; 3230 s = skipwhite(s); 3231 } 3232 else 3233 { 3234 if (spell_iswordp_nmw(s, curwin)) 3235 *t++ = *s; 3236 ++s; 3237 } 3238 } 3239 *t = NUL; 3240 } 3241 else 3242 vim_strncpy(word, s, MAXWLEN - 1); 3243 3244 smp = (salitem_T *)slang->sl_sal.ga_data; 3245 3246 /* 3247 * This comes from Aspell phonet.cpp. Converted from C++ to C. 3248 * Changed to keep spaces. 3249 */ 3250 i = reslen = z = 0; 3251 while ((c = word[i]) != NUL) 3252 { 3253 // Start with the first rule that has the character in the word. 3254 n = slang->sl_sal_first[c]; 3255 z0 = 0; 3256 3257 if (n >= 0) 3258 { 3259 // check all rules for the same letter 3260 for (; (s = smp[n].sm_lead)[0] == c; ++n) 3261 { 3262 // Quickly skip entries that don't match the word. Most 3263 // entries are less then three chars, optimize for that. 3264 k = smp[n].sm_leadlen; 3265 if (k > 1) 3266 { 3267 if (word[i + 1] != s[1]) 3268 continue; 3269 if (k > 2) 3270 { 3271 for (j = 2; j < k; ++j) 3272 if (word[i + j] != s[j]) 3273 break; 3274 if (j < k) 3275 continue; 3276 } 3277 } 3278 3279 if ((pf = smp[n].sm_oneof) != NULL) 3280 { 3281 // Check for match with one of the chars in "sm_oneof". 3282 while (*pf != NUL && *pf != word[i + k]) 3283 ++pf; 3284 if (*pf == NUL) 3285 continue; 3286 ++k; 3287 } 3288 s = smp[n].sm_rules; 3289 pri = 5; // default priority 3290 3291 p0 = *s; 3292 k0 = k; 3293 while (*s == '-' && k > 1) 3294 { 3295 k--; 3296 s++; 3297 } 3298 if (*s == '<') 3299 s++; 3300 if (VIM_ISDIGIT(*s)) 3301 { 3302 // determine priority 3303 pri = *s - '0'; 3304 s++; 3305 } 3306 if (*s == '^' && *(s + 1) == '^') 3307 s++; 3308 3309 if (*s == NUL 3310 || (*s == '^' 3311 && (i == 0 || !(word[i - 1] == ' ' 3312 || spell_iswordp(word + i - 1, curwin))) 3313 && (*(s + 1) != '$' 3314 || (!spell_iswordp(word + i + k0, curwin)))) 3315 || (*s == '$' && i > 0 3316 && spell_iswordp(word + i - 1, curwin) 3317 && (!spell_iswordp(word + i + k0, curwin)))) 3318 { 3319 // search for followup rules, if: 3320 // followup and k > 1 and NO '-' in searchstring 3321 c0 = word[i + k - 1]; 3322 n0 = slang->sl_sal_first[c0]; 3323 3324 if (slang->sl_followup && k > 1 && n0 >= 0 3325 && p0 != '-' && word[i + k] != NUL) 3326 { 3327 // test follow-up rule for "word[i + k]" 3328 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) 3329 { 3330 // Quickly skip entries that don't match the word. 3331 // 3332 k0 = smp[n0].sm_leadlen; 3333 if (k0 > 1) 3334 { 3335 if (word[i + k] != s[1]) 3336 continue; 3337 if (k0 > 2) 3338 { 3339 pf = word + i + k + 1; 3340 for (j = 2; j < k0; ++j) 3341 if (*pf++ != s[j]) 3342 break; 3343 if (j < k0) 3344 continue; 3345 } 3346 } 3347 k0 += k - 1; 3348 3349 if ((pf = smp[n0].sm_oneof) != NULL) 3350 { 3351 // Check for match with one of the chars in 3352 // "sm_oneof". 3353 while (*pf != NUL && *pf != word[i + k0]) 3354 ++pf; 3355 if (*pf == NUL) 3356 continue; 3357 ++k0; 3358 } 3359 3360 p0 = 5; 3361 s = smp[n0].sm_rules; 3362 while (*s == '-') 3363 { 3364 // "k0" gets NOT reduced because 3365 // "if (k0 == k)" 3366 s++; 3367 } 3368 if (*s == '<') 3369 s++; 3370 if (VIM_ISDIGIT(*s)) 3371 { 3372 p0 = *s - '0'; 3373 s++; 3374 } 3375 3376 if (*s == NUL 3377 // *s == '^' cuts 3378 || (*s == '$' 3379 && !spell_iswordp(word + i + k0, 3380 curwin))) 3381 { 3382 if (k0 == k) 3383 // this is just a piece of the string 3384 continue; 3385 3386 if (p0 < pri) 3387 // priority too low 3388 continue; 3389 // rule fits; stop search 3390 break; 3391 } 3392 } 3393 3394 if (p0 >= pri && smp[n0].sm_lead[0] == c0) 3395 continue; 3396 } 3397 3398 // replace string 3399 s = smp[n].sm_to; 3400 if (s == NULL) 3401 s = (char_u *)""; 3402 pf = smp[n].sm_rules; 3403 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; 3404 if (p0 == 1 && z == 0) 3405 { 3406 // rule with '<' is used 3407 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c 3408 || res[reslen - 1] == *s)) 3409 reslen--; 3410 z0 = 1; 3411 z = 1; 3412 k0 = 0; 3413 while (*s != NUL && word[i + k0] != NUL) 3414 { 3415 word[i + k0] = *s; 3416 k0++; 3417 s++; 3418 } 3419 if (k > k0) 3420 STRMOVE(word + i + k0, word + i + k); 3421 3422 // new "actual letter" 3423 c = word[i]; 3424 } 3425 else 3426 { 3427 // no '<' rule used 3428 i += k - 1; 3429 z = 0; 3430 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) 3431 { 3432 if (reslen == 0 || res[reslen - 1] != *s) 3433 res[reslen++] = *s; 3434 s++; 3435 } 3436 // new "actual letter" 3437 c = *s; 3438 if (strstr((char *)pf, "^^") != NULL) 3439 { 3440 if (c != NUL) 3441 res[reslen++] = c; 3442 STRMOVE(word, word + i + 1); 3443 i = 0; 3444 z0 = 1; 3445 } 3446 } 3447 break; 3448 } 3449 } 3450 } 3451 else if (VIM_ISWHITE(c)) 3452 { 3453 c = ' '; 3454 k = 1; 3455 } 3456 3457 if (z0 == 0) 3458 { 3459 if (k && !p0 && reslen < MAXWLEN && c != NUL 3460 && (!slang->sl_collapse || reslen == 0 3461 || res[reslen - 1] != c)) 3462 // condense only double letters 3463 res[reslen++] = c; 3464 3465 i++; 3466 z = 0; 3467 k = 0; 3468 } 3469 } 3470 3471 res[reslen] = NUL; 3472 } 3473 3474 /* 3475 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". 3476 * Multi-byte version of spell_soundfold(). 3477 */ 3478 static void 3479 spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) 3480 { 3481 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; 3482 int word[MAXWLEN]; 3483 int wres[MAXWLEN]; 3484 int l; 3485 char_u *s; 3486 int *ws; 3487 char_u *t; 3488 int *pf; 3489 int i, j, z; 3490 int reslen; 3491 int n, k = 0; 3492 int z0; 3493 int k0; 3494 int n0; 3495 int c; 3496 int pri; 3497 int p0 = -333; 3498 int c0; 3499 int did_white = FALSE; 3500 int wordlen; 3501 3502 3503 /* 3504 * Convert the multi-byte string to a wide-character string. 3505 * Remove accents, if wanted. We actually remove all non-word characters. 3506 * But keep white space. 3507 */ 3508 wordlen = 0; 3509 for (s = inword; *s != NUL; ) 3510 { 3511 t = s; 3512 c = mb_cptr2char_adv(&s); 3513 if (slang->sl_rem_accents) 3514 { 3515 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) 3516 { 3517 if (did_white) 3518 continue; 3519 c = ' '; 3520 did_white = TRUE; 3521 } 3522 else 3523 { 3524 did_white = FALSE; 3525 if (!spell_iswordp_nmw(t, curwin)) 3526 continue; 3527 } 3528 } 3529 word[wordlen++] = c; 3530 } 3531 word[wordlen] = NUL; 3532 3533 /* 3534 * This algorithm comes from Aspell phonet.cpp. 3535 * Converted from C++ to C. Added support for multi-byte chars. 3536 * Changed to keep spaces. 3537 */ 3538 i = reslen = z = 0; 3539 while ((c = word[i]) != NUL) 3540 { 3541 // Start with the first rule that has the character in the word. 3542 n = slang->sl_sal_first[c & 0xff]; 3543 z0 = 0; 3544 3545 if (n >= 0) 3546 { 3547 // Check all rules for the same index byte. 3548 // If c is 0x300 need extra check for the end of the array, as 3549 // (c & 0xff) is NUL. 3550 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff) 3551 && ws[0] != NUL; ++n) 3552 { 3553 // Quickly skip entries that don't match the word. Most 3554 // entries are less then three chars, optimize for that. 3555 if (c != ws[0]) 3556 continue; 3557 k = smp[n].sm_leadlen; 3558 if (k > 1) 3559 { 3560 if (word[i + 1] != ws[1]) 3561 continue; 3562 if (k > 2) 3563 { 3564 for (j = 2; j < k; ++j) 3565 if (word[i + j] != ws[j]) 3566 break; 3567 if (j < k) 3568 continue; 3569 } 3570 } 3571 3572 if ((pf = smp[n].sm_oneof_w) != NULL) 3573 { 3574 // Check for match with one of the chars in "sm_oneof". 3575 while (*pf != NUL && *pf != word[i + k]) 3576 ++pf; 3577 if (*pf == NUL) 3578 continue; 3579 ++k; 3580 } 3581 s = smp[n].sm_rules; 3582 pri = 5; // default priority 3583 3584 p0 = *s; 3585 k0 = k; 3586 while (*s == '-' && k > 1) 3587 { 3588 k--; 3589 s++; 3590 } 3591 if (*s == '<') 3592 s++; 3593 if (VIM_ISDIGIT(*s)) 3594 { 3595 // determine priority 3596 pri = *s - '0'; 3597 s++; 3598 } 3599 if (*s == '^' && *(s + 1) == '^') 3600 s++; 3601 3602 if (*s == NUL 3603 || (*s == '^' 3604 && (i == 0 || !(word[i - 1] == ' ' 3605 || spell_iswordp_w(word + i - 1, curwin))) 3606 && (*(s + 1) != '$' 3607 || (!spell_iswordp_w(word + i + k0, curwin)))) 3608 || (*s == '$' && i > 0 3609 && spell_iswordp_w(word + i - 1, curwin) 3610 && (!spell_iswordp_w(word + i + k0, curwin)))) 3611 { 3612 // search for followup rules, if: 3613 // followup and k > 1 and NO '-' in searchstring 3614 c0 = word[i + k - 1]; 3615 n0 = slang->sl_sal_first[c0 & 0xff]; 3616 3617 if (slang->sl_followup && k > 1 && n0 >= 0 3618 && p0 != '-' && word[i + k] != NUL) 3619 { 3620 // Test follow-up rule for "word[i + k]"; loop over 3621 // all entries with the same index byte. 3622 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) 3623 == (c0 & 0xff); ++n0) 3624 { 3625 // Quickly skip entries that don't match the word. 3626 if (c0 != ws[0]) 3627 continue; 3628 k0 = smp[n0].sm_leadlen; 3629 if (k0 > 1) 3630 { 3631 if (word[i + k] != ws[1]) 3632 continue; 3633 if (k0 > 2) 3634 { 3635 pf = word + i + k + 1; 3636 for (j = 2; j < k0; ++j) 3637 if (*pf++ != ws[j]) 3638 break; 3639 if (j < k0) 3640 continue; 3641 } 3642 } 3643 k0 += k - 1; 3644 3645 if ((pf = smp[n0].sm_oneof_w) != NULL) 3646 { 3647 // Check for match with one of the chars in 3648 // "sm_oneof". 3649 while (*pf != NUL && *pf != word[i + k0]) 3650 ++pf; 3651 if (*pf == NUL) 3652 continue; 3653 ++k0; 3654 } 3655 3656 p0 = 5; 3657 s = smp[n0].sm_rules; 3658 while (*s == '-') 3659 { 3660 // "k0" gets NOT reduced because 3661 // "if (k0 == k)" 3662 s++; 3663 } 3664 if (*s == '<') 3665 s++; 3666 if (VIM_ISDIGIT(*s)) 3667 { 3668 p0 = *s - '0'; 3669 s++; 3670 } 3671 3672 if (*s == NUL 3673 // *s == '^' cuts 3674 || (*s == '$' 3675 && !spell_iswordp_w(word + i + k0, 3676 curwin))) 3677 { 3678 if (k0 == k) 3679 // this is just a piece of the string 3680 continue; 3681 3682 if (p0 < pri) 3683 // priority too low 3684 continue; 3685 // rule fits; stop search 3686 break; 3687 } 3688 } 3689 3690 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) 3691 == (c0 & 0xff)) 3692 continue; 3693 } 3694 3695 // replace string 3696 ws = smp[n].sm_to_w; 3697 s = smp[n].sm_rules; 3698 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; 3699 if (p0 == 1 && z == 0) 3700 { 3701 // rule with '<' is used 3702 if (reslen > 0 && ws != NULL && *ws != NUL 3703 && (wres[reslen - 1] == c 3704 || wres[reslen - 1] == *ws)) 3705 reslen--; 3706 z0 = 1; 3707 z = 1; 3708 k0 = 0; 3709 if (ws != NULL) 3710 while (*ws != NUL && word[i + k0] != NUL) 3711 { 3712 word[i + k0] = *ws; 3713 k0++; 3714 ws++; 3715 } 3716 if (k > k0) 3717 mch_memmove(word + i + k0, word + i + k, 3718 sizeof(int) * (wordlen - (i + k) + 1)); 3719 3720 // new "actual letter" 3721 c = word[i]; 3722 } 3723 else 3724 { 3725 // no '<' rule used 3726 i += k - 1; 3727 z = 0; 3728 if (ws != NULL) 3729 while (*ws != NUL && ws[1] != NUL 3730 && reslen < MAXWLEN) 3731 { 3732 if (reslen == 0 || wres[reslen - 1] != *ws) 3733 wres[reslen++] = *ws; 3734 ws++; 3735 } 3736 // new "actual letter" 3737 if (ws == NULL) 3738 c = NUL; 3739 else 3740 c = *ws; 3741 if (strstr((char *)s, "^^") != NULL) 3742 { 3743 if (c != NUL) 3744 wres[reslen++] = c; 3745 mch_memmove(word, word + i + 1, 3746 sizeof(int) * (wordlen - (i + 1) + 1)); 3747 i = 0; 3748 z0 = 1; 3749 } 3750 } 3751 break; 3752 } 3753 } 3754 } 3755 else if (VIM_ISWHITE(c)) 3756 { 3757 c = ' '; 3758 k = 1; 3759 } 3760 3761 if (z0 == 0) 3762 { 3763 if (k && !p0 && reslen < MAXWLEN && c != NUL 3764 && (!slang->sl_collapse || reslen == 0 3765 || wres[reslen - 1] != c)) 3766 // condense only double letters 3767 wres[reslen++] = c; 3768 3769 i++; 3770 z = 0; 3771 k = 0; 3772 } 3773 } 3774 3775 // Convert wide characters in "wres" to a multi-byte string in "res". 3776 l = 0; 3777 for (n = 0; n < reslen; ++n) 3778 { 3779 l += mb_char2bytes(wres[n], res + l); 3780 if (l + MB_MAXBYTES > MAXWLEN) 3781 break; 3782 } 3783 res[l] = NUL; 3784 } 3785 3786 /* 3787 * ":spellinfo" 3788 */ 3789 void 3790 ex_spellinfo(exarg_T *eap UNUSED) 3791 { 3792 int lpi; 3793 langp_T *lp; 3794 char_u *p; 3795 3796 if (no_spell_checking(curwin)) 3797 return; 3798 3799 msg_start(); 3800 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi) 3801 { 3802 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); 3803 msg_puts("file: "); 3804 msg_puts((char *)lp->lp_slang->sl_fname); 3805 msg_putchar('\n'); 3806 p = lp->lp_slang->sl_info; 3807 if (p != NULL) 3808 { 3809 msg_puts((char *)p); 3810 msg_putchar('\n'); 3811 } 3812 } 3813 msg_end(); 3814 } 3815 3816 #define DUMPFLAG_KEEPCASE 1 // round 2: keep-case tree 3817 #define DUMPFLAG_COUNT 2 // include word count 3818 #define DUMPFLAG_ICASE 4 // ignore case when finding matches 3819 #define DUMPFLAG_ONECAP 8 // pattern starts with capital 3820 #define DUMPFLAG_ALLCAP 16 // pattern is all capitals 3821 3822 /* 3823 * ":spelldump" 3824 */ 3825 void 3826 ex_spelldump(exarg_T *eap) 3827 { 3828 char_u *spl; 3829 long dummy; 3830 3831 if (no_spell_checking(curwin)) 3832 return; 3833 (void)get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL); 3834 3835 // Create a new empty buffer in a new window. 3836 do_cmdline_cmd((char_u *)"new"); 3837 3838 // enable spelling locally in the new window 3839 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL); 3840 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); 3841 vim_free(spl); 3842 3843 if (!BUFEMPTY()) 3844 return; 3845 3846 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); 3847 3848 // Delete the empty line that we started with. 3849 if (curbuf->b_ml.ml_line_count > 1) 3850 ml_delete(curbuf->b_ml.ml_line_count); 3851 3852 redraw_later(NOT_VALID); 3853 } 3854 3855 /* 3856 * Go through all possible words and: 3857 * 1. When "pat" is NULL: dump a list of all words in the current buffer. 3858 * "ic" and "dir" are not used. 3859 * 2. When "pat" is not NULL: add matching words to insert mode completion. 3860 */ 3861 void 3862 spell_dump_compl( 3863 char_u *pat, // leading part of the word 3864 int ic, // ignore case 3865 int *dir, // direction for adding matches 3866 int dumpflags_arg) // DUMPFLAG_* 3867 { 3868 langp_T *lp; 3869 slang_T *slang; 3870 idx_T arridx[MAXWLEN]; 3871 int curi[MAXWLEN]; 3872 char_u word[MAXWLEN]; 3873 int c; 3874 char_u *byts; 3875 idx_T *idxs; 3876 linenr_T lnum = 0; 3877 int round; 3878 int depth; 3879 int n; 3880 int flags; 3881 char_u *region_names = NULL; // region names being used 3882 int do_region = TRUE; // dump region names and numbers 3883 char_u *p; 3884 int lpi; 3885 int dumpflags = dumpflags_arg; 3886 int patlen; 3887 3888 // When ignoring case or when the pattern starts with capital pass this on 3889 // to dump_word(). 3890 if (pat != NULL) 3891 { 3892 if (ic) 3893 dumpflags |= DUMPFLAG_ICASE; 3894 else 3895 { 3896 n = captype(pat, NULL); 3897 if (n == WF_ONECAP) 3898 dumpflags |= DUMPFLAG_ONECAP; 3899 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat)) 3900 dumpflags |= DUMPFLAG_ALLCAP; 3901 } 3902 } 3903 3904 // Find out if we can support regions: All languages must support the same 3905 // regions or none at all. 3906 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) 3907 { 3908 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); 3909 p = lp->lp_slang->sl_regions; 3910 if (p[0] != 0) 3911 { 3912 if (region_names == NULL) // first language with regions 3913 region_names = p; 3914 else if (STRCMP(region_names, p) != 0) 3915 { 3916 do_region = FALSE; // region names are different 3917 break; 3918 } 3919 } 3920 } 3921 3922 if (do_region && region_names != NULL) 3923 { 3924 if (pat == NULL) 3925 { 3926 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); 3927 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); 3928 } 3929 } 3930 else 3931 do_region = FALSE; 3932 3933 /* 3934 * Loop over all files loaded for the entries in 'spelllang'. 3935 */ 3936 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) 3937 { 3938 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); 3939 slang = lp->lp_slang; 3940 if (slang->sl_fbyts == NULL) // reloading failed 3941 continue; 3942 3943 if (pat == NULL) 3944 { 3945 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); 3946 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); 3947 } 3948 3949 // When matching with a pattern and there are no prefixes only use 3950 // parts of the tree that match "pat". 3951 if (pat != NULL && slang->sl_pbyts == NULL) 3952 patlen = (int)STRLEN(pat); 3953 else 3954 patlen = -1; 3955 3956 // round 1: case-folded tree 3957 // round 2: keep-case tree 3958 for (round = 1; round <= 2; ++round) 3959 { 3960 if (round == 1) 3961 { 3962 dumpflags &= ~DUMPFLAG_KEEPCASE; 3963 byts = slang->sl_fbyts; 3964 idxs = slang->sl_fidxs; 3965 } 3966 else 3967 { 3968 dumpflags |= DUMPFLAG_KEEPCASE; 3969 byts = slang->sl_kbyts; 3970 idxs = slang->sl_kidxs; 3971 } 3972 if (byts == NULL) 3973 continue; // array is empty 3974 3975 depth = 0; 3976 arridx[0] = 0; 3977 curi[0] = 1; 3978 while (depth >= 0 && !got_int 3979 && (pat == NULL || !ins_compl_interrupted())) 3980 { 3981 if (curi[depth] > byts[arridx[depth]]) 3982 { 3983 // Done all bytes at this node, go up one level. 3984 --depth; 3985 line_breakcheck(); 3986 ins_compl_check_keys(50, FALSE); 3987 } 3988 else 3989 { 3990 // Do one more byte at this node. 3991 n = arridx[depth] + curi[depth]; 3992 ++curi[depth]; 3993 c = byts[n]; 3994 if (c == 0) 3995 { 3996 // End of word, deal with the word. 3997 // Don't use keep-case words in the fold-case tree, 3998 // they will appear in the keep-case tree. 3999 // Only use the word when the region matches. 4000 flags = (int)idxs[n]; 4001 if ((round == 2 || (flags & WF_KEEPCAP) == 0) 4002 && (flags & WF_NEEDCOMP) == 0 4003 && (do_region 4004 || (flags & WF_REGION) == 0 4005 || (((unsigned)flags >> 16) 4006 & lp->lp_region) != 0)) 4007 { 4008 word[depth] = NUL; 4009 if (!do_region) 4010 flags &= ~WF_REGION; 4011 4012 // Dump the basic word if there is no prefix or 4013 // when it's the first one. 4014 c = (unsigned)flags >> 24; 4015 if (c == 0 || curi[depth] == 2) 4016 { 4017 dump_word(slang, word, pat, dir, 4018 dumpflags, flags, lnum); 4019 if (pat == NULL) 4020 ++lnum; 4021 } 4022 4023 // Apply the prefix, if there is one. 4024 if (c != 0) 4025 lnum = dump_prefixes(slang, word, pat, dir, 4026 dumpflags, flags, lnum); 4027 } 4028 } 4029 else 4030 { 4031 // Normal char, go one level deeper. 4032 word[depth++] = c; 4033 arridx[depth] = idxs[n]; 4034 curi[depth] = 1; 4035 4036 // Check if this characters matches with the pattern. 4037 // If not skip the whole tree below it. 4038 // Always ignore case here, dump_word() will check 4039 // proper case later. This isn't exactly right when 4040 // length changes for multi-byte characters with 4041 // ignore case... 4042 if (depth <= patlen 4043 && MB_STRNICMP(word, pat, depth) != 0) 4044 --depth; 4045 } 4046 } 4047 } 4048 } 4049 } 4050 } 4051 4052 /* 4053 * Dump one word: apply case modifications and append a line to the buffer. 4054 * When "lnum" is zero add insert mode completion. 4055 */ 4056 static void 4057 dump_word( 4058 slang_T *slang, 4059 char_u *word, 4060 char_u *pat, 4061 int *dir, 4062 int dumpflags, 4063 int wordflags, 4064 linenr_T lnum) 4065 { 4066 int keepcap = FALSE; 4067 char_u *p; 4068 char_u *tw; 4069 char_u cword[MAXWLEN]; 4070 char_u badword[MAXWLEN + 10]; 4071 int i; 4072 int flags = wordflags; 4073 4074 if (dumpflags & DUMPFLAG_ONECAP) 4075 flags |= WF_ONECAP; 4076 if (dumpflags & DUMPFLAG_ALLCAP) 4077 flags |= WF_ALLCAP; 4078 4079 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0) 4080 { 4081 // Need to fix case according to "flags". 4082 make_case_word(word, cword, flags); 4083 p = cword; 4084 } 4085 else 4086 { 4087 p = word; 4088 if ((dumpflags & DUMPFLAG_KEEPCASE) 4089 && ((captype(word, NULL) & WF_KEEPCAP) == 0 4090 || (flags & WF_FIXCAP) != 0)) 4091 keepcap = TRUE; 4092 } 4093 tw = p; 4094 4095 if (pat == NULL) 4096 { 4097 // Add flags and regions after a slash. 4098 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) 4099 { 4100 STRCPY(badword, p); 4101 STRCAT(badword, "/"); 4102 if (keepcap) 4103 STRCAT(badword, "="); 4104 if (flags & WF_BANNED) 4105 STRCAT(badword, "!"); 4106 else if (flags & WF_RARE) 4107 STRCAT(badword, "?"); 4108 if (flags & WF_REGION) 4109 for (i = 0; i < 7; ++i) 4110 if (flags & (0x10000 << i)) 4111 sprintf((char *)badword + STRLEN(badword), "%d", i + 1); 4112 p = badword; 4113 } 4114 4115 if (dumpflags & DUMPFLAG_COUNT) 4116 { 4117 hashitem_T *hi; 4118 4119 // Include the word count for ":spelldump!". 4120 hi = hash_find(&slang->sl_wordcount, tw); 4121 if (!HASHITEM_EMPTY(hi)) 4122 { 4123 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d", 4124 tw, HI2WC(hi)->wc_count); 4125 p = IObuff; 4126 } 4127 } 4128 4129 ml_append(lnum, p, (colnr_T)0, FALSE); 4130 } 4131 else if (((dumpflags & DUMPFLAG_ICASE) 4132 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0 4133 : STRNCMP(p, pat, STRLEN(pat)) == 0) 4134 && ins_compl_add_infercase(p, (int)STRLEN(p), 4135 p_ic, NULL, *dir, FALSE) == OK) 4136 // if dir was BACKWARD then honor it just once 4137 *dir = FORWARD; 4138 } 4139 4140 /* 4141 * For ":spelldump": Find matching prefixes for "word". Prepend each to 4142 * "word" and append a line to the buffer. 4143 * When "lnum" is zero add insert mode completion. 4144 * Return the updated line number. 4145 */ 4146 static linenr_T 4147 dump_prefixes( 4148 slang_T *slang, 4149 char_u *word, // case-folded word 4150 char_u *pat, 4151 int *dir, 4152 int dumpflags, 4153 int flags, // flags with prefix ID 4154 linenr_T startlnum) 4155 { 4156 idx_T arridx[MAXWLEN]; 4157 int curi[MAXWLEN]; 4158 char_u prefix[MAXWLEN]; 4159 char_u word_up[MAXWLEN]; 4160 int has_word_up = FALSE; 4161 int c; 4162 char_u *byts; 4163 idx_T *idxs; 4164 linenr_T lnum = startlnum; 4165 int depth; 4166 int n; 4167 int len; 4168 int i; 4169 4170 // If the word starts with a lower-case letter make the word with an 4171 // upper-case letter in word_up[]. 4172 c = PTR2CHAR(word); 4173 if (SPELL_TOUPPER(c) != c) 4174 { 4175 onecap_copy(word, word_up, TRUE); 4176 has_word_up = TRUE; 4177 } 4178 4179 byts = slang->sl_pbyts; 4180 idxs = slang->sl_pidxs; 4181 if (byts != NULL) // array not is empty 4182 { 4183 /* 4184 * Loop over all prefixes, building them byte-by-byte in prefix[]. 4185 * When at the end of a prefix check that it supports "flags". 4186 */ 4187 depth = 0; 4188 arridx[0] = 0; 4189 curi[0] = 1; 4190 while (depth >= 0 && !got_int) 4191 { 4192 n = arridx[depth]; 4193 len = byts[n]; 4194 if (curi[depth] > len) 4195 { 4196 // Done all bytes at this node, go up one level. 4197 --depth; 4198 line_breakcheck(); 4199 } 4200 else 4201 { 4202 // Do one more byte at this node. 4203 n += curi[depth]; 4204 ++curi[depth]; 4205 c = byts[n]; 4206 if (c == 0) 4207 { 4208 // End of prefix, find out how many IDs there are. 4209 for (i = 1; i < len; ++i) 4210 if (byts[n + i] != 0) 4211 break; 4212 curi[depth] += i - 1; 4213 4214 c = valid_word_prefix(i, n, flags, word, slang, FALSE); 4215 if (c != 0) 4216 { 4217 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); 4218 dump_word(slang, prefix, pat, dir, dumpflags, 4219 (c & WF_RAREPFX) ? (flags | WF_RARE) 4220 : flags, lnum); 4221 if (lnum != 0) 4222 ++lnum; 4223 } 4224 4225 // Check for prefix that matches the word when the 4226 // first letter is upper-case, but only if the prefix has 4227 // a condition. 4228 if (has_word_up) 4229 { 4230 c = valid_word_prefix(i, n, flags, word_up, slang, 4231 TRUE); 4232 if (c != 0) 4233 { 4234 vim_strncpy(prefix + depth, word_up, 4235 MAXWLEN - depth - 1); 4236 dump_word(slang, prefix, pat, dir, dumpflags, 4237 (c & WF_RAREPFX) ? (flags | WF_RARE) 4238 : flags, lnum); 4239 if (lnum != 0) 4240 ++lnum; 4241 } 4242 } 4243 } 4244 else 4245 { 4246 // Normal char, go one level deeper. 4247 prefix[depth++] = c; 4248 arridx[depth] = idxs[n]; 4249 curi[depth] = 1; 4250 } 4251 } 4252 } 4253 } 4254 4255 return lnum; 4256 } 4257 4258 /* 4259 * Move "p" to the end of word "start". 4260 * Uses the spell-checking word characters. 4261 */ 4262 char_u * 4263 spell_to_word_end(char_u *start, win_T *win) 4264 { 4265 char_u *p = start; 4266 4267 while (*p != NUL && spell_iswordp(p, win)) 4268 MB_PTR_ADV(p); 4269 return p; 4270 } 4271 4272 /* 4273 * For Insert mode completion CTRL-X s: 4274 * Find start of the word in front of column "startcol". 4275 * We don't check if it is badly spelled, with completion we can only change 4276 * the word in front of the cursor. 4277 * Returns the column number of the word. 4278 */ 4279 int 4280 spell_word_start(int startcol) 4281 { 4282 char_u *line; 4283 char_u *p; 4284 int col = 0; 4285 4286 if (no_spell_checking(curwin)) 4287 return startcol; 4288 4289 // Find a word character before "startcol". 4290 line = ml_get_curline(); 4291 for (p = line + startcol; p > line; ) 4292 { 4293 MB_PTR_BACK(line, p); 4294 if (spell_iswordp_nmw(p, curwin)) 4295 break; 4296 } 4297 4298 // Go back to start of the word. 4299 while (p > line) 4300 { 4301 col = (int)(p - line); 4302 MB_PTR_BACK(line, p); 4303 if (!spell_iswordp(p, curwin)) 4304 break; 4305 col = 0; 4306 } 4307 4308 return col; 4309 } 4310 4311 /* 4312 * Need to check for 'spellcapcheck' now, the word is removed before 4313 * expand_spelling() is called. Therefore the ugly global variable. 4314 */ 4315 static int spell_expand_need_cap; 4316 4317 void 4318 spell_expand_check_cap(colnr_T col) 4319 { 4320 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col); 4321 } 4322 4323 /* 4324 * Get list of spelling suggestions. 4325 * Used for Insert mode completion CTRL-X ?. 4326 * Returns the number of matches. The matches are in "matchp[]", array of 4327 * allocated strings. 4328 */ 4329 int 4330 expand_spelling( 4331 linenr_T lnum UNUSED, 4332 char_u *pat, 4333 char_u ***matchp) 4334 { 4335 garray_T ga; 4336 4337 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE); 4338 *matchp = ga.ga_data; 4339 return ga.ga_len; 4340 } 4341 4342 /* 4343 * Return TRUE if "val" is a valid 'spelllang' value. 4344 */ 4345 int 4346 valid_spelllang(char_u *val) 4347 { 4348 return valid_name(val, ".-_,@"); 4349 } 4350 4351 /* 4352 * Return TRUE if "val" is a valid 'spellfile' value. 4353 */ 4354 int 4355 valid_spellfile(char_u *val) 4356 { 4357 char_u *s; 4358 4359 for (s = val; *s != NUL; ++s) 4360 if (!vim_isfilec(*s) && *s != ',' && *s != ' ') 4361 return FALSE; 4362 return TRUE; 4363 } 4364 4365 /* 4366 * Handle side effects of setting 'spell'. 4367 * Return an error message or NULL for success. 4368 */ 4369 char * 4370 did_set_spell_option(int is_spellfile) 4371 { 4372 char *errmsg = NULL; 4373 win_T *wp; 4374 int l; 4375 4376 if (is_spellfile) 4377 { 4378 l = (int)STRLEN(curwin->w_s->b_p_spf); 4379 if (l > 0 && (l < 4 4380 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0)) 4381 errmsg = e_invarg; 4382 } 4383 4384 if (errmsg == NULL) 4385 { 4386 FOR_ALL_WINDOWS(wp) 4387 if (wp->w_buffer == curbuf && wp->w_p_spell) 4388 { 4389 errmsg = did_set_spelllang(wp); 4390 break; 4391 } 4392 } 4393 return errmsg; 4394 } 4395 4396 /* 4397 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'. 4398 * Return error message when failed, NULL when OK. 4399 */ 4400 char * 4401 compile_cap_prog(synblock_T *synblock) 4402 { 4403 regprog_T *rp = synblock->b_cap_prog; 4404 char_u *re; 4405 4406 if (synblock->b_p_spc == NULL || *synblock->b_p_spc == NUL) 4407 synblock->b_cap_prog = NULL; 4408 else 4409 { 4410 // Prepend a ^ so that we only match at one column 4411 re = concat_str((char_u *)"^", synblock->b_p_spc); 4412 if (re != NULL) 4413 { 4414 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC); 4415 vim_free(re); 4416 if (synblock->b_cap_prog == NULL) 4417 { 4418 synblock->b_cap_prog = rp; // restore the previous program 4419 return e_invarg; 4420 } 4421 } 4422 } 4423 4424 vim_regfree(rp); 4425 return NULL; 4426 } 4427 4428 #endif // FEAT_SPELL 4429