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