diff options
Diffstat (limited to 'src/nvim/spell.c')
-rw-r--r-- | src/nvim/spell.c | 1913 |
1 files changed, 1067 insertions, 846 deletions
diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 610a359141..3e56ad561b 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -72,15 +72,14 @@ #include <inttypes.h> #include <limits.h> #include <stdbool.h> -#include <string.h> #include <stdlib.h> +#include <string.h> #include <wctype.h> -/* for offsetof() */ +// for offsetof() #include <stddef.h> #include "nvim/ascii.h" -#include "nvim/spell.h" #include "nvim/buffer.h" #include "nvim/change.h" #include "nvim/charset.h" @@ -92,6 +91,7 @@ #include "nvim/ex_docmd.h" #include "nvim/fileio.h" #include "nvim/func_attr.h" +#include "nvim/garray.h" #include "nvim/getchar.h" #include "nvim/hashtab.h" #include "nvim/mark.h" @@ -100,21 +100,21 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/garray.h" #include "nvim/normal.h" #include "nvim/option.h" +#include "nvim/os/input.h" +#include "nvim/os/os.h" #include "nvim/os_unix.h" #include "nvim/path.h" #include "nvim/regexp.h" #include "nvim/screen.h" #include "nvim/search.h" +#include "nvim/spell.h" #include "nvim/spellfile.h" #include "nvim/strings.h" #include "nvim/syntax.h" -#include "nvim/undo.h" #include "nvim/ui.h" -#include "nvim/os/os.h" -#include "nvim/os/input.h" +#include "nvim/undo.h" // only used for su_badflags #define WF_MIXCAP 0x20 // mix of upper and lower case: macaRONI @@ -151,26 +151,26 @@ typedef struct suginfo_S { int su_maxscore; // maximum score for adding to su_ga int su_sfmaxscore; // idem, for when doing soundfold words garray_T su_sga; // like su_ga, sound-folded scoring - char_u *su_badptr; // start of bad word in line + char_u *su_badptr; // start of bad word in line int su_badlen; // length of detected bad word in line int su_badflags; // caps flags for bad word char_u su_badword[MAXWLEN]; // bad word truncated at su_badlen char_u su_fbadword[MAXWLEN]; // su_badword case-folded char_u su_sal_badword[MAXWLEN]; // su_badword soundfolded hashtab_T su_banned; // table with banned words - slang_T *su_sallang; // default language for sound folding + slang_T *su_sallang; // default language for sound folding } suginfo_T; // One word suggestion. Used in "si_ga". typedef struct { - char_u *st_word; // suggested word, allocated string + char_u *st_word; // suggested word, allocated string int st_wordlen; // STRLEN(st_word) int st_orglen; // length of replaced text int st_score; // lower is better int st_altscore; // used when st_score compares equal bool st_salscore; // st_score is for soundalike bool st_had_bonus; // bonus already included in score - slang_T *st_slang; // language used for sound folding + slang_T *st_slang; // language used for sound folding } suggest_T; #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) @@ -235,14 +235,14 @@ typedef struct { // Structure to store info for word matching. typedef struct matchinf_S { - langp_T *mi_lp; // info for language and region + langp_T *mi_lp; // info for language and region // pointers to original text to be checked - char_u *mi_word; // start of word being checked - char_u *mi_end; // end of matching word so far - char_u *mi_fend; // next char to be added to mi_fword - char_u *mi_cend; // char after what was used for - // mi_capflags + char_u *mi_word; // start of word being checked + char_u *mi_end; // end of matching word so far + char_u *mi_fend; // next char to be added to mi_fword + char_u *mi_cend; // char after what was used for + // mi_capflags // case-folded text char_u mi_fword[MAXWLEN + 1]; // mi_word case-folded @@ -265,11 +265,11 @@ typedef struct matchinf_S { // others int mi_result; // result so far: SP_BAD, SP_OK, etc. int mi_capflags; // WF_ONECAP WF_ALLCAP WF_KEEPCAP - win_T *mi_win; // buffer being checked + win_T *mi_win; // buffer being checked // for NOBREAK int mi_result2; // "mi_resul" without following word - char_u *mi_end2; // "mi_end" without following word + char_u *mi_end2; // "mi_end" without following word } matchinf_T; // Structure used for the cookie argument of do_in_runtimepath(). @@ -334,26 +334,24 @@ char *e_format = N_("E759: Format error in spell file"); static char_u *repl_from = NULL; static char_u *repl_to = NULL; -// Main spell-checking function. -// "ptr" points to a character that could be the start of a word. -// "*attrp" is set to the highlight index for a badly spelled word. For a -// non-word or when it's OK it remains unchanged. -// This must only be called when 'spelllang' is not empty. -// -// "capcol" is used to check for a Capitalised word after the end of a -// sentence. If it's zero then perform the check. Return the column where to -// check next, or -1 when no sentence end was found. If it's NULL then don't -// worry. -// -// Returns the length of the word in bytes, also when it's OK, so that the -// caller can skip over the word. -size_t spell_check( - win_T *wp, // current window - char_u *ptr, - hlf_T *attrp, - int *capcol, // column to check for Capital - bool docount // count good words -) +/// Main spell-checking function. +/// "ptr" points to a character that could be the start of a word. +/// "*attrp" is set to the highlight index for a badly spelled word. For a +/// non-word or when it's OK it remains unchanged. +/// This must only be called when 'spelllang' is not empty. +/// +/// "capcol" is used to check for a Capitalised word after the end of a +/// sentence. If it's zero then perform the check. Return the column where to +/// check next, or -1 when no sentence end was found. If it's NULL then don't +/// worry. +/// +/// @param wp current window +/// @param capcol column to check for Capital +/// @param docount count good words +/// +/// @return the length of the word in bytes, also when it's OK, so that the +/// caller can skip over the word. +size_t spell_check(win_T *wp, char_u *ptr, hlf_T *attrp, int *capcol, bool docount) { matchinf_T mi; // Most things are put in "mi" so that it can // be passed to functions quickly. @@ -383,7 +381,7 @@ size_t spell_check( // julifeest". if (*ptr >= '0' && *ptr <= '9') { if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B')) { - mi.mi_end = (char_u*) skipbin((char*) ptr + 2); + mi.mi_end = (char_u *)skipbin((char *)ptr + 2); } else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) { mi.mi_end = skiphex(ptr + 2); } else { @@ -486,7 +484,7 @@ size_t spell_check( // Count the word in the first language where it's found to be OK. if (count_word && mi.mi_result == SP_OK) { count_common_word(mi.mi_lp->lp_slang, ptr, - (int)(mi.mi_end - ptr), 1); + (int)(mi.mi_end - ptr), 1); count_word = false; } } @@ -499,8 +497,8 @@ size_t spell_check( return nrlen; } } else if (!spell_iswordp_nmw(ptr, wp)) { - // When we are at a non-word character there is no error, just - // skip over the character (try looking for a word after it). + // When we are at a non-word character there is no error, just + // skip over the character (try looking for a word after it). if (capcol != NULL && wp->w_s->b_cap_prog != NULL) { regmatch_T regmatch; @@ -521,7 +519,7 @@ size_t spell_check( MB_PTR_ADV(mi.mi_end); } else if (mi.mi_result == SP_BAD && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak) { - char_u *p, *fp; + char_u *p, *fp; int save_result = mi.mi_result; // First language in 'spelllang' is NOBREAK. Find first position @@ -576,10 +574,10 @@ static void find_word(matchinf_T *mip, int mode) { int wlen = 0; int flen; - char_u *ptr; - slang_T *slang = mip->mi_lp->lp_slang; - char_u *byts; - idx_T *idxs; + char_u *ptr; + slang_T *slang = mip->mi_lp->lp_slang; + char_u *byts; + idx_T *idxs; if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND) { // Check for word with matching case in keep-case tree. @@ -588,9 +586,10 @@ static void find_word(matchinf_T *mip, int mode) byts = slang->sl_kbyts; idxs = slang->sl_kidxs; - if (mode == FIND_KEEPCOMPOUND) + if (mode == FIND_KEEPCOMPOUND) { // Skip over the previously found word(s). wlen += mip->mi_compoff; + } } else { // Check for case-folded in case-folded tree. ptr = mip->mi_fword; @@ -607,12 +606,11 @@ static void find_word(matchinf_T *mip, int mode) wlen = mip->mi_compoff; flen -= mip->mi_compoff; } - } - if (byts == NULL) + if (byts == NULL) { return; // array is empty - + } idx_T arridx = 0; int endlen[MAXWLEN]; // length at possible word endings idx_T endidx[MAXWLEN]; // possible word endings @@ -625,8 +623,9 @@ static void find_word(matchinf_T *mip, int mode) // - we reach the end of the tree, // - or we reach the end of the line. for (;; ) { - if (flen <= 0 && *mip->mi_fend != NUL) + if (flen <= 0 && *mip->mi_fend != NUL) { flen = fold_more(mip); + } len = byts[arridx++]; @@ -648,35 +647,39 @@ static void find_word(matchinf_T *mip, int mode) ++arridx; --len; } - if (len == 0) + if (len == 0) { break; // no children, word must end here + } } // Stop looking at end of the line. - if (ptr[wlen] == NUL) + if (ptr[wlen] == NUL) { break; + } // Perform a binary search in the list of accepted bytes. c = ptr[wlen]; - if (c == TAB) // <Tab> is handled like <Space> + if (c == TAB) { // <Tab> is handled like <Space> c = ' '; + } idx_T lo = arridx; idx_T hi = arridx + len - 1; while (lo < hi) { idx_T m = (lo + hi) / 2; - if (byts[m] > c) + if (byts[m] > c) { hi = m - 1; - else if (byts[m] < c) + } else if (byts[m] < c) { lo = m + 1; - else { + } else { lo = hi = m; break; } } // Stop if there is no matching byte. - if (hi < lo || byts[lo] != c) + if (hi < lo || byts[lo] != c) { break; + } // Continue at the child (if there is one). arridx = idxs[lo]; @@ -687,10 +690,12 @@ static void find_word(matchinf_T *mip, int mode) // checked word. if (c == ' ') { for (;; ) { - if (flen <= 0 && *mip->mi_fend != NUL) + if (flen <= 0 && *mip->mi_fend != NUL) { flen = fold_more(mip); - if (ptr[wlen] != ' ' && ptr[wlen] != TAB) + } + if (ptr[wlen] != ' ' && ptr[wlen] != TAB) { break; + } ++wlen; --flen; } @@ -711,11 +716,13 @@ static void find_word(matchinf_T *mip, int mode) continue; // not at first byte of character } if (spell_iswordp(ptr + wlen, mip->mi_win)) { - if (slang->sl_compprog == NULL && !slang->sl_nobreak) + if (slang->sl_compprog == NULL && !slang->sl_nobreak) { continue; // next char is a word character + } word_ends = false; - } else + } else { word_ends = true; + } // The prefix flag is before compound flags. Once a valid prefix flag // has been found we try compound flags. bool prefix_found = false; @@ -754,23 +761,26 @@ static void find_word(matchinf_T *mip, int mode) } if (mip->mi_capflags == WF_KEEPCAP - || !spell_valid_case(mip->mi_capflags, flags)) + || !spell_valid_case(mip->mi_capflags, flags)) { continue; + } } // When mode is FIND_PREFIX the word must support the prefix: // check the prefix ID and the condition. Do that for the list at // mip->mi_prefarridx that find_prefix() filled. else if (mode == FIND_PREFIX && !prefix_found) { c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, - flags, - mip->mi_word + mip->mi_cprefixlen, slang, - false); - if (c == 0) + flags, + mip->mi_word + mip->mi_cprefixlen, slang, + false); + if (c == 0) { continue; + } // Use the WF_RARE flag for a rare prefix. - if (c & WF_RAREPFX) + if (c & WF_RAREPFX) { flags |= WF_RARE; + } prefix_found = true; } @@ -790,8 +800,9 @@ static void find_word(matchinf_T *mip, int mode) // that's too short... Myspell compatibility requires this // anyway. if (((unsigned)flags >> 24) == 0 - || wlen - mip->mi_compoff < slang->sl_compminlen) + || wlen - mip->mi_compoff < slang->sl_compminlen) { continue; + } // For multi-byte chars check character length against // COMPOUNDMIN. if (slang->sl_compminlen > 0 @@ -804,27 +815,32 @@ static void find_word(matchinf_T *mip, int mode) // maximum for syllables is specified. if (!word_ends && mip->mi_complen + mip->mi_compextra + 2 > slang->sl_compmax - && slang->sl_compsylmax == MAXWLEN) + && slang->sl_compsylmax == MAXWLEN) { continue; + } // Don't allow compounding on a side where an affix was added, // unless COMPOUNDPERMITFLAG was used. - if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF)) + if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF)) { continue; - if (!word_ends && (flags & WF_NOCOMPAFT)) + } + if (!word_ends && (flags & WF_NOCOMPAFT)) { continue; + } // Quickly check if compounding is possible with this flag. if (!byte_in_str(mip->mi_complen == 0 ? slang->sl_compstartflags : slang->sl_compallflags, - ((unsigned)flags >> 24))) + ((unsigned)flags >> 24))) { continue; + } // If there is a match with a CHECKCOMPOUNDPATTERN rule // discard the compound word. - if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat)) + if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat)) { continue; + } if (mode == FIND_COMPOUND) { int capflags; @@ -842,8 +858,9 @@ static void find_word(matchinf_T *mip, int mode) } capflags = captype(p, mip->mi_word + wlen); if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP - && (flags & WF_FIXCAP) != 0)) + && (flags & WF_FIXCAP) != 0)) { continue; + } if (capflags != WF_ALLCAP) { // When the character before the word is a word @@ -876,23 +893,26 @@ static void find_word(matchinf_T *mip, int mode) STRLCPY(fword, ptr, endlen[endidxcnt] + 1); } } - if (!can_compound(slang, fword, mip->mi_compflags)) + if (!can_compound(slang, fword, mip->mi_compflags)) { continue; + } } else if (slang->sl_comprules != NULL - && !match_compoundrule(slang, mip->mi_compflags)) + && !match_compoundrule(slang, mip->mi_compflags)) { // The compound flags collected so far do not match any // COMPOUNDRULE, discard the compounded word. continue; + } } // Check NEEDCOMPOUND: can't use word without compounding. - else if (flags & WF_NEEDCOMP) + else if (flags & WF_NEEDCOMP) { continue; + } int nobreak_result = SP_OK; if (!word_ends) { int save_result = mip->mi_result; - char_u *save_end = mip->mi_end; + char_u *save_end = mip->mi_end; langp_T *save_lp = mip->mi_lp; // Check that a valid word follows. If there is one and we @@ -900,8 +920,9 @@ static void find_word(matchinf_T *mip, int mode) // always finished here. For NOBREAK we only check that a // valid word follows. // Recursive! - if (slang->sl_nobreak) + if (slang->sl_nobreak) { mip->mi_result = SP_BAD; + } // Find following word in case-folded tree. mip->mi_compoff = endlen[endidxcnt]; @@ -922,8 +943,9 @@ static void find_word(matchinf_T *mip, int mode) c = mip->mi_compoff; #endif ++mip->mi_complen; - if (flags & WF_COMPROOT) + if (flags & WF_COMPROOT) { ++mip->mi_compextra; + } // For NOBREAK we need to try all NOBREAK languages, at least // to find the ".add" file(s). @@ -931,8 +953,9 @@ static void find_word(matchinf_T *mip, int mode) if (slang->sl_nobreak) { mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi); if (mip->mi_lp->lp_slang->sl_fidxs == NULL - || !mip->mi_lp->lp_slang->sl_nobreak) + || !mip->mi_lp->lp_slang->sl_nobreak) { continue; + } } find_word(mip, FIND_COMPOUND); @@ -956,12 +979,14 @@ static void find_word(matchinf_T *mip, int mode) #endif } - if (!slang->sl_nobreak) + if (!slang->sl_nobreak) { break; + } } --mip->mi_complen; - if (flags & WF_COMPROOT) + if (flags & WF_COMPROOT) { --mip->mi_compextra; + } mip->mi_lp = save_lp; if (slang->sl_nobreak) { @@ -969,25 +994,28 @@ static void find_word(matchinf_T *mip, int mode) mip->mi_result = save_result; mip->mi_end = save_end; } else { - if (mip->mi_result == SP_OK) + if (mip->mi_result == SP_OK) { break; + } continue; } } int res = SP_BAD; - if (flags & WF_BANNED) + if (flags & WF_BANNED) { res = SP_BANNED; - else if (flags & WF_REGION) { + } else if (flags & WF_REGION) { // Check region. - if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) + if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) { res = SP_OK; - else + } else { res = SP_LOCAL; - } else if (flags & WF_RARE) + } + } else if (flags & WF_RARE) { res = SP_RARE; - else + } else { res = SP_OK; + } // Always use the longest match and the best result. For NOBREAK // we separately keep the longest match without a following good @@ -997,36 +1025,37 @@ static void find_word(matchinf_T *mip, int mode) mip->mi_result2 = res; mip->mi_end2 = mip->mi_word + wlen; } else if (mip->mi_result2 == res - && mip->mi_end2 < mip->mi_word + wlen) + && mip->mi_end2 < mip->mi_word + wlen) { mip->mi_end2 = mip->mi_word + wlen; + } } else if (mip->mi_result > res) { mip->mi_result = res; mip->mi_end = mip->mi_word + wlen; - } else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) + } else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) { mip->mi_end = mip->mi_word + wlen; + } - if (mip->mi_result == SP_OK) + if (mip->mi_result == SP_OK) { break; + } } - if (mip->mi_result == SP_OK) + if (mip->mi_result == SP_OK) { break; + } } } -// Returns true if there is a match between the word ptr[wlen] and -// CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another -// word. -// A match means that the first part of CHECKCOMPOUNDPATTERN matches at the -// end of ptr[wlen] and the second part matches after it. -static bool -match_checkcompoundpattern ( - char_u *ptr, - int wlen, - garray_T *gap // &sl_comppat -) +/// Returns true if there is a match between the word ptr[wlen] and +/// CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another +/// word. +/// A match means that the first part of CHECKCOMPOUNDPATTERN matches at the +/// end of ptr[wlen] and the second part matches after it. +/// +/// @param gap &sl_comppat +static bool match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap) { - char_u *p; + char_u *p; int len; for (int i = 0; i + 1 < gap->ga_len; i += 2) { @@ -1036,8 +1065,9 @@ match_checkcompoundpattern ( // check if first part matches at end of previous word. p = ((char_u **)gap->ga_data)[i]; len = (int)STRLEN(p); - if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) + if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) { return true; + } } } return false; @@ -1045,8 +1075,7 @@ match_checkcompoundpattern ( // Returns true if "flags" is a valid sequence of compound flags and "word" // does not have too many syllables. -static bool can_compound(slang_T *slang, const char_u *word, - const char_u *flags) +static bool can_compound(slang_T *slang, const char_u *word, const char_u *flags) FUNC_ATTR_NONNULL_ALL { char_u uflags[MAXWLEN * 2] = { 0 }; @@ -1069,8 +1098,9 @@ static bool can_compound(slang_T *slang, const char_u *word, // are too many syllables AND the number of compound words is above // COMPOUNDWORDMAX then compounding is not allowed. if (slang->sl_compsylmax < MAXWLEN - && count_syllables(slang, word) > slang->sl_compsylmax) + && count_syllables(slang, word) > slang->sl_compsylmax) { return (int)STRLEN(flags) < slang->sl_compmax; + } return true; } @@ -1082,8 +1112,9 @@ static bool can_be_compound(trystate_T *sp, slang_T *slang, char_u *compflags, i // If the flag doesn't appear in sl_compstartflags or sl_compallflags // then it can't possibly compound. if (!byte_in_str(sp->ts_complen == sp->ts_compsplit - ? slang->sl_compstartflags : slang->sl_compallflags, flag)) + ? slang->sl_compstartflags : slang->sl_compallflags, flag)) { return false; + } // If there are no wildcards, we can check if the flags collected so far // possibly can form a match with COMPOUNDRULE patterns. This only @@ -1105,7 +1136,7 @@ static bool can_be_compound(trystate_T *sp, slang_T *slang, char_u *compflags, i // Caller must check that slang->sl_comprules is not NULL. static bool match_compoundrule(slang_T *slang, char_u *compflags) { - char_u *p; + char_u *p; int i; int c; @@ -1115,30 +1146,37 @@ static bool match_compoundrule(slang_T *slang, char_u *compflags) // them against the current rule entry for (i = 0;; ++i) { c = compflags[i]; - if (c == NUL) + if (c == NUL) { // found a rule that matches for the flags we have so far return true; - if (*p == '/' || *p == NUL) + } + if (*p == '/' || *p == NUL) { break; // end of rule, it's too short + } if (*p == '[') { bool match = false; // compare against all the flags in [] ++p; - while (*p != ']' && *p != NUL) - if (*p++ == c) + while (*p != ']' && *p != NUL) { + if (*p++ == c) { match = true; - if (!match) + } + } + if (!match) { break; // none matches - } else if (*p != c) + } + } else if (*p != c) { break; // flag of word doesn't match flag in pattern + } ++p; } // Skip to the next "/", where the next pattern starts. p = vim_strchr(p, '/'); - if (p == NULL) + if (p == NULL) { break; + } } // Checked all the rules and none of them match the flags, so there @@ -1146,18 +1184,15 @@ static bool match_compoundrule(slang_T *slang, char_u *compflags) return false; } -// Return non-zero if the prefix indicated by "arridx" matches with the prefix -// ID in "flags" for the word "word". -// The WF_RAREPFX flag is included in the return value for a rare prefix. -static int -valid_word_prefix ( - int totprefcnt, // nr of prefix IDs - int arridx, // idx in sl_pidxs[] - int flags, - char_u *word, - slang_T *slang, - bool cond_req // only use prefixes with a condition -) +/// Return non-zero if the prefix indicated by "arridx" matches with the prefix +/// ID in "flags" for the word "word". +/// The WF_RAREPFX flag is included in the return value for a rare prefix. +/// +/// @param totprefcnt nr of prefix IDs +/// @param arridx idx in sl_pidxs[] +/// @param cond_req only use prefixes with a condition +static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, + bool cond_req) { int prefcnt; int pidx; @@ -1168,13 +1203,15 @@ valid_word_prefix ( pidx = slang->sl_pidxs[arridx + prefcnt]; // Check the prefix ID. - if (prefid != (pidx & 0xff)) + if (prefid != (pidx & 0xff)) { continue; + } // Check if the prefix doesn't combine and the word already has a // suffix. - if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) + if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) { continue; + } // Check the condition, if there is one. The condition index is // stored in the two bytes above the prefix ID byte. @@ -1183,8 +1220,9 @@ valid_word_prefix ( if (!vim_regexec_prog(rp, false, word, 0)) { continue; } - } else if (cond_req) + } else if (cond_req) { continue; + } // It's a match! Return the WF_ flags. return pidx; @@ -1206,16 +1244,16 @@ static void find_prefix(matchinf_T *mip, int mode) int wlen = 0; int flen; int c; - char_u *ptr; + char_u *ptr; idx_T lo, hi, m; - slang_T *slang = mip->mi_lp->lp_slang; - char_u *byts; - idx_T *idxs; + slang_T *slang = mip->mi_lp->lp_slang; + char_u *byts; + idx_T *idxs; byts = slang->sl_pbyts; - if (byts == NULL) + if (byts == NULL) { return; // array is empty - + } // We use the case-folded word here, since prefixes are always // case-folded. ptr = mip->mi_fword; @@ -1232,8 +1270,9 @@ static void find_prefix(matchinf_T *mip, int mode) // - we reach the end of the tree, // - or we reach the end of the line. for (;; ) { - if (flen == 0 && *mip->mi_fend != NUL) + if (flen == 0 && *mip->mi_fend != NUL) { flen = fold_more(mip); + } len = byts[arridx++]; @@ -1254,9 +1293,10 @@ static void find_prefix(matchinf_T *mip, int mode) // Find the word that comes after the prefix. mip->mi_prefixlen = wlen; - if (mode == FIND_COMPOUND) + if (mode == FIND_COMPOUND) { // Skip over the previously found word(s). mip->mi_prefixlen += mip->mi_compoff; + } // Case-folded length may differ from original length. mip->mi_cprefixlen = nofold_len(mip->mi_fword, mip->mi_prefixlen, @@ -1264,13 +1304,15 @@ static void find_prefix(matchinf_T *mip, int mode) find_word(mip, FIND_PREFIX); - if (len == 0) + if (len == 0) { break; // no children, word must end here + } } // Stop looking at end of the line. - if (ptr[wlen] == NUL) + if (ptr[wlen] == NUL) { break; + } // Perform a binary search in the list of accepted bytes. c = ptr[wlen]; @@ -1278,19 +1320,20 @@ static void find_prefix(matchinf_T *mip, int mode) hi = arridx + len - 1; while (lo < hi) { m = (lo + hi) / 2; - if (byts[m] > c) + if (byts[m] > c) { hi = m - 1; - else if (byts[m] < c) + } else if (byts[m] < c) { lo = m + 1; - else { + } else { lo = hi = m; break; } } // Stop if there is no matching byte. - if (hi < lo || byts[lo] != c) + if (hi < lo || byts[lo] != c) { break; + } // Continue at the child (if there is one). arridx = idxs[lo]; @@ -1305,7 +1348,7 @@ static void find_prefix(matchinf_T *mip, int mode) static int fold_more(matchinf_T *mip) { int flen; - char_u *p; + char_u *p; p = mip->mi_fend; do { @@ -1349,42 +1392,40 @@ static bool no_spell_checking(win_T *wp) return false; } -// Moves to the next spell error. -// "curline" is false for "[s", "]s", "[S" and "]S". -// "curline" is true to find word under/after cursor in the same line. -// For Insert mode completion "dir" is BACKWARD and "curline" is true: move -// to after badly spelled word before the cursor. -// Return 0 if not found, length of the badly spelled word otherwise. -size_t -spell_move_to ( - win_T *wp, - int dir, // FORWARD or BACKWARD - bool allwords, // true for "[s"/"]s", false for "[S"/"]S" - bool curline, - hlf_T *attrp // return: attributes of bad word or NULL - // (only when "dir" is FORWARD) -) +/// Moves to the next spell error. +/// "curline" is false for "[s", "]s", "[S" and "]S". +/// "curline" is true to find word under/after cursor in the same line. +/// For Insert mode completion "dir" is BACKWARD and "curline" is true: move +/// to after badly spelled word before the cursor. +/// +/// @param dir FORWARD or BACKWARD +/// @param allwords true for "[s"/"]s", false for "[S"/"]S" +/// @param attrp return: attributes of bad word or NULL (only when "dir" is FORWARD) +/// +/// @return 0 if not found, length of the badly spelled word otherwise. +size_t spell_move_to(win_T *wp, int dir, bool allwords, bool curline, hlf_T *attrp) { linenr_T lnum; pos_T found_pos; size_t found_len = 0; - char_u *line; - char_u *p; - char_u *endp; + char_u *line; + char_u *p; + char_u *endp; hlf_T attr = HLF_COUNT; size_t len; int has_syntax = syntax_present(wp); int col; bool can_spell; - char_u *buf = NULL; + char_u *buf = NULL; size_t buflen = 0; int skip = 0; int capcol = -1; bool found_one = false; bool wrapped = false; - if (no_spell_checking(wp)) + if (no_spell_checking(wp)) { return 0; + } // Start looking for bad word at the start of the line, because we can't // start halfway through a word, we don't know where it starts or ends. @@ -1410,8 +1451,9 @@ spell_move_to ( assert(buf && buflen >= len + MAXWLEN + 2); // In first line check first word for Capital. - if (lnum == 1) + if (lnum == 1) { capcol = 0; + } // For checking first word with a capital skip white space. if (capcol == 0) { @@ -1431,10 +1473,11 @@ spell_move_to ( // Copy the line into "buf" and append the start of the next line if // possible. STRCPY(buf, line); - if (lnum < wp->w_buffer->b_ml.ml_line_count) + if (lnum < wp->w_buffer->b_ml.ml_line_count) { spell_cat_line(buf + STRLEN(buf), ml_get_buf(wp->w_buffer, lnum + 1, false), MAXWLEN); + } p = buf + skip; endp = buf + len; while (p < endp) { @@ -1443,8 +1486,9 @@ spell_move_to ( if (dir == BACKWARD && lnum == wp->w_cursor.lnum && !wrapped - && (colnr_T)(p - buf) >= wp->w_cursor.col) + && (colnr_T)(p - buf) >= wp->w_cursor.col) { break; + } // start of word attr = HLF_COUNT; @@ -1464,11 +1508,13 @@ spell_move_to ( if (has_syntax) { col = (int)(p - buf); (void)syn_get_id(wp, lnum, (colnr_T)col, - FALSE, &can_spell, FALSE); - if (!can_spell) + FALSE, &can_spell, FALSE); + if (!can_spell) { attr = HLF_COUNT; - } else + } + } else { can_spell = true; + } if (can_spell) { found_one = true; @@ -1479,8 +1525,9 @@ spell_move_to ( // No need to search further. wp->w_cursor = found_pos; xfree(buf); - if (attrp != NULL) + if (attrp != NULL) { *attrp = attr; + } return len; } else if (curline) { // Insert mode completion: put cursor after @@ -1490,8 +1537,9 @@ spell_move_to ( } found_len = len; } - } else + } else { found_one = true; + } } } @@ -1529,22 +1577,24 @@ spell_move_to ( // starting line again and accept the last match. lnum = wp->w_buffer->b_ml.ml_line_count; wrapped = true; - if (!shortmess(SHM_SEARCH)) + if (!shortmess(SHM_SEARCH)) { give_warning((char_u *)_(top_bot_msg), true); + } } capcol = -1; } else { - if (lnum < wp->w_buffer->b_ml.ml_line_count) + if (lnum < wp->w_buffer->b_ml.ml_line_count) { ++lnum; - else if (!p_ws) + } else if (!p_ws) { break; // at first line and 'nowrapscan' - else { + } else { // Wrap around to the start of the buffer. May search the // starting line again and accept the first match. lnum = 1; wrapped = true; - if (!shortmess(SHM_SEARCH)) + if (!shortmess(SHM_SEARCH)) { give_warning((char_u *)_(bot_top_msg), true); + } } // If we are back at the starting line and there is no match then @@ -1555,17 +1605,19 @@ spell_move_to ( // Skip the characters at the start of the next line that were // included in a match crossing line boundaries. - if (attr == HLF_COUNT) + if (attr == HLF_COUNT) { skip = (int)(p - endp); - else + } else { skip = 0; + } // Capcol skips over the inserted space. --capcol; // But after empty line check first word in next line - if (*skipwhite(line) == NUL) + if (*skipwhite(line) == NUL) { capcol = 0; + } } line_breakcheck(); @@ -1581,12 +1633,13 @@ spell_move_to ( // to skip those bytes if the word was OK. void spell_cat_line(char_u *buf, char_u *line, int maxlen) { - char_u *p; + char_u *p; int n; p = skipwhite(line); - while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) + while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) { p = skipwhite(p + 1); + } if (*p != NUL) { // Only worth concatenating if there is something else than spaces to @@ -1630,8 +1683,9 @@ static void spell_load_lang(char_u *lang) if (r == FAIL && *sl.sl_lang != NUL && round == 1 && apply_autocmds(EVENT_SPELLFILEMISSING, lang, - curbuf->b_fname, FALSE, curbuf)) + curbuf->b_fname, FALSE, curbuf)) { continue; + } break; } break; @@ -1647,9 +1701,8 @@ static void spell_load_lang(char_u *lang) lang); do_cmdline_cmd(autocmd_buf); } else { - smsg( - _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""), - lang, spell_enc(), lang); + smsg(_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""), + lang, spell_enc(), lang); } } else if (sl.sl_slang != NULL) { // At least one file was loaded, now load ALL the additions. @@ -1662,9 +1715,9 @@ static void spell_load_lang(char_u *lang) // use "latin1" for "latin9". And limit to 60 characters (just in case). char_u *spell_enc(void) { - - if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) + if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) { return p_enc; + } return (char_u *)"latin1"; } @@ -1673,7 +1726,7 @@ char_u *spell_enc(void) static void int_wordlist_spl(char_u *fname) { vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL, - int_wordlist, spell_enc()); + int_wordlist, spell_enc()); } // Allocate a new slang_T for language "lang". "lang" can be NULL. @@ -1683,8 +1736,9 @@ slang_T *slang_alloc(char_u *lang) { slang_T *lp = xcalloc(1, sizeof(slang_T)); - if (lang != NULL) + if (lang != NULL) { lp->sl_name = vim_strsave(lang); + } ga_init(&lp->sl_rep, sizeof(fromto_T), 10); ga_init(&lp->sl_repsal, sizeof(fromto_T), 10); lp->sl_compmax = MAXWLEN; @@ -1722,7 +1776,7 @@ static void free_fromto(fromto_T *ftp) { // Clear an slang_T so that the file can be reloaded. void slang_clear(slang_T *lp) { - garray_T *gap; + garray_T *gap; XFREE_CLEAR(lp->sl_fbyts); XFREE_CLEAR(lp->sl_kbyts); @@ -1792,17 +1846,18 @@ void slang_clear_sug(slang_T *lp) // Invoked through do_in_runtimepath(). static void spell_load_cb(char_u *fname, void *cookie) { - spelload_T *slp = (spelload_T *)cookie; - slang_T *slang; + spelload_T *slp = (spelload_T *)cookie; + slang_T *slang; slang = spell_load_file(fname, slp->sl_lang, NULL, false); if (slang != NULL) { // When a previously loaded file has NOBREAK also use it for the // ".add" files. - if (slp->sl_nobreak && slang->sl_add) + if (slp->sl_nobreak && slang->sl_add) { slang->sl_nobreak = true; - else if (slang->sl_nobreak) + } else if (slang->sl_nobreak) { slp->sl_nobreak = true; + } slp->sl_slang = slang; } @@ -1818,10 +1873,10 @@ static void spell_load_cb(char_u *fname, void *cookie) void count_common_word(slang_T *lp, char_u *word, int len, int count) { hash_T hash; - hashitem_T *hi; + hashitem_T *hi; wordcount_T *wc; char_u buf[MAXWLEN]; - char_u *p; + char_u *p; if (len == -1) { p = word; @@ -1842,21 +1897,18 @@ void count_common_word(slang_T *lp, char_u *word, int len, int count) hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash); } else { wc = HI2WC(hi); - if ((wc->wc_count += count) < (unsigned)count) // check for overflow + if ((wc->wc_count += count) < (unsigned)count) { // check for overflow wc->wc_count = MAXWORDCOUNT; + } } } -// Adjust the score of common words. -static int -score_wordcount_adj ( - slang_T *slang, - int score, - char_u *word, - bool split // word was split, less bonus -) +/// Adjust the score of common words. +/// +/// @param split word was split, less bonus +static int score_wordcount_adj(slang_T *slang, int score, char_u *word, bool split) { - hashitem_T *hi; + hashitem_T *hi; wordcount_T *wc; int bonus; int newscore; @@ -1864,18 +1916,21 @@ score_wordcount_adj ( hi = hash_find(&slang->sl_wordcount, word); if (!HASHITEM_EMPTY(hi)) { wc = HI2WC(hi); - if (wc->wc_count < SCORE_THRES2) + if (wc->wc_count < SCORE_THRES2) { bonus = SCORE_COMMON1; - else if (wc->wc_count < SCORE_THRES3) + } else if (wc->wc_count < SCORE_THRES3) { bonus = SCORE_COMMON2; - else + } else { bonus = SCORE_COMMON3; - if (split) + } + if (split) { newscore = score - bonus / 2; - else + } else { newscore = score - bonus; - if (newscore < 0) + } + if (newscore < 0) { return 0; + } return newscore; } return score; @@ -1885,11 +1940,13 @@ score_wordcount_adj ( // Like strchr() but independent of locale. bool byte_in_str(char_u *str, int n) { - char_u *p; + char_u *p; - for (p = str; *p != NUL; ++p) - if (*p == n) + for (p = str; *p != NUL; ++p) { + if (*p == n) { return true; + } + } return false; } @@ -1897,24 +1954,27 @@ bool byte_in_str(char_u *str, int n) // in "slang->sl_syl_items". int init_syl_tab(slang_T *slang) { - char_u *p; - char_u *s; + char_u *p; + char_u *s; int l; ga_init(&slang->sl_syl_items, sizeof(syl_item_T), 4); p = vim_strchr(slang->sl_syllable, '/'); while (p != NULL) { *p++ = NUL; - if (*p == NUL) // trailing slash + if (*p == NUL) { // trailing slash break; + } s = p; p = vim_strchr(p, '/'); - if (p == NULL) + if (p == NULL) { l = (int)STRLEN(s); - else + } else { l = (int)(p - s); - if (l >= SY_MAXLEN) + } + if (l >= SY_MAXLEN) { return SP_FORMERROR; + } syl_item_T *syl = GA_APPEND_VIA_PTR(syl_item_T, &slang->sl_syl_items); STRLCPY(syl->sy_chars, s, l + 1); @@ -1932,11 +1992,12 @@ static int count_syllables(slang_T *slang, const char_u *word) int cnt = 0; bool skip = false; int len; - syl_item_T *syl; + syl_item_T *syl; int c; - if (slang->sl_syllable == NULL) + if (slang->sl_syllable == NULL) { return 0; + } for (const char_u *p = word; *p != NUL; p += len) { // When running into a space reset counter. @@ -1951,8 +2012,9 @@ static int count_syllables(slang_T *slang, const char_u *word) for (int i = 0; i < slang->sl_syl_items.ga_len; ++i) { syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i; if (syl->sy_len > len - && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0) + && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0) { len = syl->sy_len; + } } if (len != 0) { // found a match, count syllable ++cnt; @@ -1961,9 +2023,9 @@ static int count_syllables(slang_T *slang, const char_u *word) // No recognized syllable item, at least a syllable char then? c = utf_ptr2char(p); len = (*mb_ptr2len)(p); - if (vim_strchr(slang->sl_syllable, c) == NULL) + if (vim_strchr(slang->sl_syllable, c) == NULL) { skip = false; // No, search for next syllable - else if (!skip) { + } else if (!skip) { ++cnt; // Yes, count it skip = true; // don't count following syllable chars } @@ -1977,26 +2039,26 @@ static int count_syllables(slang_T *slang, const char_u *word) char_u *did_set_spelllang(win_T *wp) { garray_T ga; - char_u *splp; - char_u *region; + char_u *splp; + char_u *region; char_u region_cp[3]; bool filename; int region_mask; - slang_T *slang; + slang_T *slang; int c; char_u lang[MAXWLEN + 1]; char_u spf_name[MAXPATHL]; int len; - char_u *p; + char_u *p; int round; - char_u *spf; - char_u *use_region = NULL; + char_u *spf; + char_u *use_region = NULL; bool dont_use_region = false; bool nobreak = false; - langp_T *lp, *lp2; + langp_T *lp, *lp2; static bool recursive = false; - char_u *ret_msg = NULL; - char_u *spl_copy; + char_u *ret_msg = NULL; + char_u *spl_copy; bufref_T bufref; set_bufref(&bufref, wp->w_buffer); @@ -2004,8 +2066,9 @@ char_u *did_set_spelllang(win_T *wp) // We don't want to do this recursively. May happen when a language is // not available and the SpellFileMissing autocommand opens a new buffer // in which 'spell' is set. - if (recursive) + if (recursive) { return NULL; + } recursive = true; ga_init(&ga, sizeof(langp_T), 2); @@ -2046,8 +2109,9 @@ char_u *did_set_spelllang(win_T *wp) STRLCPY(region_cp, p + 1, 3); memmove(p, p + 3, len - (p - lang) - 2); region = region_cp; - } else + } else { dont_use_region = true; + } // Check if we loaded this language before. for (slang = first_lang; slang != NULL; slang = slang->sl_next) { @@ -2061,28 +2125,32 @@ char_u *did_set_spelllang(win_T *wp) if (len > 3 && lang[len - 3] == '_') { region = lang + len - 2; lang[len - 3] = NUL; - } else + } else { dont_use_region = true; + } // Check if we loaded this language before. - for (slang = first_lang; slang != NULL; slang = slang->sl_next) - if (STRICMP(lang, slang->sl_name) == 0) + for (slang = first_lang; slang != NULL; slang = slang->sl_next) { + if (STRICMP(lang, slang->sl_name) == 0) { break; + } + } } if (region != NULL) { // If the region differs from what was used before then don't // use it for 'spellfile'. - if (use_region != NULL && STRCMP(region, use_region) != 0) + if (use_region != NULL && STRCMP(region, use_region) != 0) { dont_use_region = true; + } use_region = region; } // If not found try loading the language now. if (slang == NULL) { - if (filename) + if (filename) { (void)spell_load_file(lang, lang, NULL, false); - else { + } else { spell_load_lang(lang); // SpellFileMissing autocommands may do anything, including // destroying the buffer we are using... @@ -2105,16 +2173,19 @@ char_u *did_set_spelllang(win_T *wp) c = find_region(slang->sl_regions, region); if (c == REGION_ALL) { if (slang->sl_add) { - if (*slang->sl_regions != NUL) + if (*slang->sl_regions != NUL) { // This addition file is for other regions. region_mask = 0; - } else + } + } else { // This is probably an error. Give a warning and // accept the words anyway. smsg(_("Warning: region %s not supported"), region); - } else + } + } else { region_mask = 1 << c; + } } if (region_mask != 0) { @@ -2123,8 +2194,9 @@ char_u *did_set_spelllang(win_T *wp) p_->lp_region = region_mask; use_midword(slang, wp); - if (slang->sl_nobreak) + if (slang->sl_nobreak) { nobreak = true; + } } } } @@ -2138,8 +2210,9 @@ char_u *did_set_spelllang(win_T *wp) for (round = 0; round == 0 || *spf != NUL; ++round) { if (round == 0) { // Internal wordlist, if there is one. - if (int_wordlist == NULL) + if (int_wordlist == NULL) { continue; + } int_wordlist_spl(spf_name); } else { // One entry in 'spellfile'. @@ -2154,8 +2227,9 @@ char_u *did_set_spelllang(win_T *wp) break; } } - if (c < ga.ga_len) + if (c < ga.ga_len) { continue; + } } // Check if it was loaded already. @@ -2169,31 +2243,34 @@ char_u *did_set_spelllang(win_T *wp) // Not loaded, try loading it now. The language name includes the // region name, the region is ignored otherwise. for int_wordlist // use an arbitrary name. - if (round == 0) + if (round == 0) { STRCPY(lang, "internal wordlist"); - else { + } else { STRLCPY(lang, path_tail(spf_name), MAXWLEN + 1); p = vim_strchr(lang, '.'); - if (p != NULL) + if (p != NULL) { *p = NUL; // truncate at ".encoding.add" + } } slang = spell_load_file(spf_name, lang, NULL, true); // If one of the languages has NOBREAK we assume the addition // files also have this. - if (slang != NULL && nobreak) + if (slang != NULL && nobreak) { slang->sl_nobreak = true; + } } if (slang != NULL) { region_mask = REGION_ALL; if (use_region != NULL && !dont_use_region) { // find region in sl_regions c = find_region(slang->sl_regions, use_region); - if (c != REGION_ALL) + if (c != REGION_ALL) { region_mask = 1 << c; - else if (*slang->sl_regions != NUL) + } else if (*slang->sl_regions != NUL) { // This spell file is for other regions. region_mask = 0; + } } if (region_mask != 0) { @@ -2219,36 +2296,38 @@ char_u *did_set_spelllang(win_T *wp) lp = LANGP_ENTRY(ga, i); // sound folding - if (!GA_EMPTY(&lp->lp_slang->sl_sal)) + if (!GA_EMPTY(&lp->lp_slang->sl_sal)) { // language does sound folding itself lp->lp_sallang = lp->lp_slang; - else + } else { // find first similar language that does sound folding for (int j = 0; j < ga.ga_len; ++j) { lp2 = LANGP_ENTRY(ga, j); if (!GA_EMPTY(&lp2->lp_slang->sl_sal) && STRNCMP(lp->lp_slang->sl_name, - lp2->lp_slang->sl_name, 2) == 0) { + lp2->lp_slang->sl_name, 2) == 0) { lp->lp_sallang = lp2->lp_slang; break; } } + } // REP items - if (!GA_EMPTY(&lp->lp_slang->sl_rep)) + if (!GA_EMPTY(&lp->lp_slang->sl_rep)) { // language has REP items itself lp->lp_replang = lp->lp_slang; - else + } else { // find first similar language that has REP items for (int j = 0; j < ga.ga_len; ++j) { lp2 = LANGP_ENTRY(ga, j); if (!GA_EMPTY(&lp2->lp_slang->sl_rep) && STRNCMP(lp->lp_slang->sl_name, - lp2->lp_slang->sl_name, 2) == 0) { + lp2->lp_slang->sl_name, 2) == 0) { lp->lp_replang = lp2->lp_slang; break; } } + } } theend: @@ -2302,10 +2381,12 @@ static int find_region(char_u *rp, char_u *region) int i; for (i = 0;; i += 2) { - if (rp[i] == NUL) + if (rp[i] == NUL) { return REGION_ALL; - if (rp[i] == region[0] && rp[i + 1] == region[1]) + } + if (rp[i] == region[0] && rp[i + 1] == region[1]) { break; + } } return i / 2; } @@ -2323,7 +2404,7 @@ static int find_region(char_u *rp, char_u *region) int captype(char_u *word, char_u *end) FUNC_ATTR_NONNULL_ARG(1) { - char_u *p; + char_u *p; int firstcap; bool allcap; bool past_second = false; // past second word char @@ -2356,10 +2437,12 @@ int captype(char_u *word, char_u *end) } } - if (allcap) + if (allcap) { return WF_ALLCAP; - if (firstcap) + } + if (firstcap) { return WF_ONECAP; + } return 0; } @@ -2373,7 +2456,7 @@ static int badword_captype(char_u *word, char_u *end) int c; int l, u; bool first; - char_u *p; + char_u *p; if (flags & WF_KEEPCAP) { // Count the number of UPPER and lower case letters. @@ -2383,23 +2466,27 @@ static int badword_captype(char_u *word, char_u *end) c = PTR2CHAR(p); if (SPELL_ISUPPER(c)) { ++u; - if (p == word) + if (p == word) { first = true; - } else + } + } else { ++l; + } } // If there are more UPPER than lower case letters suggest an // ALLCAP word. Otherwise, if the first letter is UPPER then // suggest ONECAP. Exception: "ALl" most likely should be "All", // require three upper case letters. - if (u > l && u > 2) + if (u > l && u > 2) { flags |= WF_ALLCAP; - else if (first) + } else if (first) { flags |= WF_ONECAP; + } - if (u >= 2 && l >= 2) // maCARONI maCAroni + if (u >= 2 && l >= 2) { // maCARONI maCAroni flags |= WF_MIXCAP; + } } return flags; } @@ -2407,7 +2494,7 @@ static int badword_captype(char_u *word, char_u *end) // Delete the internal wordlist and its .spl file. void spell_delete_wordlist(void) { - char_u fname[MAXPATHL] = {0}; + char_u fname[MAXPATHL] = { 0 }; if (int_wordlist != NULL) { os_remove((char *)int_wordlist); @@ -2420,7 +2507,7 @@ void spell_delete_wordlist(void) // Free all languages. void spell_free_all(void) { - slang_T *slang; + slang_T *slang; // Go through all buffers and handle 'spelllang'. <VN> FOR_ALL_BUFFERS(buf) { @@ -2475,10 +2562,10 @@ static int bytes2offset(char_u **pp) c = *p++; if ((c & 0x80) == 0x00) { // 1 byte nr = c - 1; - } else if ((c & 0xc0) == 0x80) { // 2 bytes + } else if ((c & 0xc0) == 0x80) { // 2 bytes nr = (c & 0x3f) - 1; nr = nr * 255 + (*p++ - 1); - } else if ((c & 0xe0) == 0xc0) { // 3 bytes + } else if ((c & 0xe0) == 0xc0) { // 3 bytes nr = (c & 0x1f) - 1; nr = nr * 255 + (*p++ - 1); nr = nr * 255 + (*p++ - 1); @@ -2537,8 +2624,9 @@ void clear_spell_chartab(spelltab_T *sp) // We include digits. A word shouldn't start with a digit, but handling // that is done separately. - for (i = '0'; i <= '9'; ++i) + for (i = '0'; i <= '9'; ++i) { sp->st_isw[i] = true; + } for (i = 'A'; i <= 'Z'; ++i) { sp->st_isw[i] = true; sp->st_isu[i] = true; @@ -2627,9 +2715,10 @@ bool spell_iswordp_nmw(const char_u *p, win_T *wp) static bool spell_mb_isword_class(int cl, const win_T *wp) FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { - if (wp->w_s->b_cjk) + if (wp->w_s->b_cjk) { // East Asian characters are not considered word characters. return cl == 2 || cl == 0x2800; + } return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3; } @@ -2641,11 +2730,12 @@ static bool spell_iswordp_w(const int *p, const win_T *wp) const int *s; if (*p < 256 ? wp->w_s->b_spell_ismw[*p] - : (wp->w_s->b_spell_ismw_mb != NULL - && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL)) + : (wp->w_s->b_spell_ismw_mb != NULL + && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL)) { s = p + 1; - else + } else { s = p; + } if (*s > 255) { return spell_mb_isword_class(utf_class(*s), wp); @@ -2657,8 +2747,7 @@ static bool spell_iswordp_w(const int *p, const win_T *wp) // Uses the character definitions from the .spl file. // When using a multi-byte 'encoding' the length may change! // Returns FAIL when something wrong. -int spell_casefold(const win_T *wp, char_u *str, int len, char_u *buf, - int buflen) +int spell_casefold(const win_T *wp, char_u *str, int len, char_u *buf, int buflen) FUNC_ATTR_NONNULL_ALL { if (len >= buflen) { @@ -2708,8 +2797,8 @@ static int sps_limit = 9999; // max nr of suggestions given // Sets "sps_flags" and "sps_limit". int spell_check_sps(void) { - char_u *p; - char_u *s; + char_u *p; + char_u *s; char_u buf[MAXPATHL]; int f; @@ -2742,12 +2831,14 @@ int spell_check_sps(void) sps_limit = 9999; return FAIL; } - if (f != 0) + if (f != 0) { sps_flags = f; + } } - if (sps_flags == 0) + if (sps_flags == 0) { sps_flags = SPS_BEST; + } return OK; } @@ -2758,13 +2849,13 @@ int spell_check_sps(void) // When "count" is non-zero use that suggestion. void spell_suggest(int count) { - char_u *line; + char_u *line; pos_T prev_cursor = curwin->w_cursor; char_u wcopy[MAXWLEN + 2]; - char_u *p; + char_u *p; int c; suginfo_T sug; - suggest_T *stp; + suggest_T *stp; int mouse_used; int need_cap; int limit; @@ -2833,36 +2924,39 @@ void spell_suggest(int count) // Get the list of suggestions. Limit to 'lines' - 2 or the number in // 'spellsuggest', whatever is smaller. - if (sps_limit > (int)Rows - 2) + if (sps_limit > (int)Rows - 2) { limit = (int)Rows - 2; - else + } else { limit = sps_limit; + } spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit, - true, need_cap, true); + true, need_cap, true); - if (GA_EMPTY(&sug.su_ga)) + if (GA_EMPTY(&sug.su_ga)) { MSG(_("Sorry, no suggestions")); - else if (count > 0) { - if (count > sug.su_ga.ga_len) + } else if (count > 0) { + if (count > sug.su_ga.ga_len) { smsg(_("Sorry, only %" PRId64 " suggestions"), (int64_t)sug.su_ga.ga_len); + } } else { // When 'rightleft' is set the list is drawn right-left. cmdmsg_rl = curwin->w_p_rl; - if (cmdmsg_rl) + if (cmdmsg_rl) { msg_col = Columns - 1; + } // List the suggestions. msg_start(); msg_row = Rows - 1; // for when 'cmdheight' > 1 lines_left = Rows; // avoid more prompt vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), - sug.su_badlen, sug.su_badptr); + sug.su_badlen, sug.su_badptr); if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0) { // And now the rabbit from the high hat: Avoid showing the // untranslated message rightleft. vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC", - sug.su_badlen, sug.su_badptr); + sug.su_badlen, sug.su_badptr); } msg_puts((const char *)IObuff); msg_clr_eos(); @@ -2875,10 +2969,11 @@ void spell_suggest(int count) // The suggested word may replace only part of the bad word, add // the not replaced part. STRLCPY(wcopy, stp->st_word, MAXWLEN + 1); - if (sug.su_badlen > stp->st_orglen) + if (sug.su_badlen > stp->st_orglen) { STRLCPY(wcopy + stp->st_wordlen, - sug.su_badptr + stp->st_orglen, - sug.su_badlen - stp->st_orglen + 1); + sug.su_badptr + stp->st_orglen, + sug.su_badlen - stp->st_orglen + 1); + } vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1); if (cmdmsg_rl) { rl_mirror(IObuff); @@ -2897,16 +2992,18 @@ void spell_suggest(int count) if (p_verbose > 0) { // Add the score. - if (sps_flags & (SPS_DOUBLE | SPS_BEST)) + if (sps_flags & (SPS_DOUBLE | SPS_BEST)) { vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)", - stp->st_salscore ? "s " : "", - stp->st_score, stp->st_altscore); - else + stp->st_salscore ? "s " : "", + stp->st_score, stp->st_altscore); + } else { vim_snprintf((char *)IObuff, IOSIZE, " (%d)", - stp->st_score); - if (cmdmsg_rl) + stp->st_score); + } + if (cmdmsg_rl) { // Mirror the numbers, but keep the leading space. rl_mirror(IObuff + 1); + } msg_advance(30); msg_puts((const char *)IObuff); } @@ -2941,8 +3038,8 @@ void spell_suggest(int count) // repl_to. repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen); vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word, - sug.su_badlen - stp->st_orglen, - sug.su_badptr + stp->st_orglen); + sug.su_badlen - stp->st_orglen, + sug.su_badptr + stp->st_orglen); repl_to = vim_strsave(IObuff); } else { // Replacing su_badlen or more, use the whole word. @@ -2961,7 +3058,7 @@ void spell_suggest(int count) ResetRedobuff(); AppendToRedobuff("ciw"); AppendToRedobuffLit(p + c, - stp->st_wordlen + sug.su_badlen - stp->st_orglen); + stp->st_wordlen + sug.su_badlen - stp->st_orglen); AppendCharToRedobuff(ESC); // "p" may be freed here @@ -2969,8 +3066,9 @@ void spell_suggest(int count) curwin->w_cursor.col = c; changed_bytes(curwin->w_cursor.lnum, c); - } else + } else { curwin->w_cursor = prev_cursor; + } spell_find_cleanup(&sug); xfree(line); @@ -2982,27 +3080,28 @@ void spell_suggest(int count) static bool check_need_cap(linenr_T lnum, colnr_T col) { bool need_cap = false; - char_u *line; - char_u *line_copy = NULL; - char_u *p; + char_u *line; + char_u *line_copy = NULL; + char_u *p; colnr_T endcol; regmatch_T regmatch; - if (curwin->w_s->b_cap_prog == NULL) + if (curwin->w_s->b_cap_prog == NULL) { return false; + } line = get_cursor_line_ptr(); endcol = 0; if (getwhitecols(line) >= (int)col) { // At start of line, check if previous line is empty or sentence // ends there. - if (lnum == 1) + if (lnum == 1) { need_cap = true; - else { + } else { line = ml_get(lnum - 1); - if (*skipwhite(line) == NUL) + if (*skipwhite(line) == NUL) { need_cap = true; - else { + } else { // Append a space in place of the line break. line_copy = concat_str(line, (char_u *)" "); line = line_copy; @@ -3042,10 +3141,10 @@ static bool check_need_cap(linenr_T lnum, colnr_T col) void ex_spellrepall(exarg_T *eap) { pos_T pos = curwin->w_cursor; - char_u *frompat; + char_u *frompat; int addlen; - char_u *line; - char_u *p; + char_u *line; + char_u *p; bool save_ws = p_ws; linenr_T prev_lnum = 0; @@ -3072,7 +3171,7 @@ void ex_spellrepall(exarg_T *eap) // when changing "etc" to "etc.". line = get_cursor_line_ptr(); if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, - repl_to, STRLEN(repl_to)) != 0) { + repl_to, STRLEN(repl_to)) != 0) { p = xmalloc(STRLEN(line) + addlen + 1); memmove(p, line, curwin->w_cursor.col); STRCPY(p + curwin->w_cursor.col, repl_to); @@ -3093,26 +3192,23 @@ void ex_spellrepall(exarg_T *eap) curwin->w_cursor = pos; xfree(frompat); - if (sub_nsubs == 0) + if (sub_nsubs == 0) { EMSG2(_("E753: Not found: %s"), repl_from); - else + } else { do_sub_msg(false); + } } -// Find spell suggestions for "word". Return them in the growarray "*gap" as -// a list of allocated strings. -void -spell_suggest_list ( - garray_T *gap, - char_u *word, - int maxcount, // maximum nr of suggestions - bool need_cap, // 'spellcapcheck' matched - bool interactive -) +/// Find spell suggestions for "word". Return them in the growarray "*gap" as +/// a list of allocated strings. +/// +/// @param maxcount maximum nr of suggestions +/// @param need_cap 'spellcapcheck' matched +void spell_suggest_list(garray_T *gap, char_u *word, int maxcount, bool need_cap, bool interactive) { suginfo_T sug; - suggest_T *stp; - char_u *wcopy; + suggest_T *stp; + char_u *wcopy; spell_find_suggest(word, 0, &sug, maxcount, false, need_cap, interactive); @@ -3134,44 +3230,41 @@ spell_suggest_list ( spell_find_cleanup(&sug); } -// Find spell suggestions for the word at the start of "badptr". -// Return the suggestions in "su->su_ga". -// The maximum number of suggestions is "maxcount". -// Note: does use info for the current window. -// This is based on the mechanisms of Aspell, but completely reimplemented. -static void -spell_find_suggest ( - char_u *badptr, - int badlen, // length of bad word or 0 if unknown - suginfo_T *su, - int maxcount, - bool banbadword, // don't include badword in suggestions - bool need_cap, // word should start with capital - bool interactive -) +/// Find spell suggestions for the word at the start of "badptr". +/// Return the suggestions in "su->su_ga". +/// The maximum number of suggestions is "maxcount". +/// Note: does use info for the current window. +/// This is based on the mechanisms of Aspell, but completely reimplemented. +/// +/// @param badlen length of bad word or 0 if unknown +/// @param banbadword don't include badword in suggestions +/// @param need_cap word should start with capital +static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, + bool banbadword, bool need_cap, bool interactive) { hlf_T attr = HLF_COUNT; char_u buf[MAXPATHL]; - char_u *p; + char_u *p; bool do_combine = false; - char_u *sps_copy; + char_u *sps_copy; static bool expr_busy = false; int c; - langp_T *lp; + langp_T *lp; bool did_intern = false; // Set the info in "*su". memset(su, 0, sizeof(suginfo_T)); ga_init(&su->su_ga, (int)sizeof(suggest_T), 10); ga_init(&su->su_sga, (int)sizeof(suggest_T), 10); - if (*badptr == NUL) + if (*badptr == NUL) { return; + } hash_init(&su->su_banned); su->su_badptr = badptr; - if (badlen != 0) + if (badlen != 0) { su->su_badlen = badlen; - else { + } else { size_t tmplen = spell_check(curwin, su->su_badptr, &attr, NULL, false); assert(tmplen <= INT_MAX); su->su_badlen = (int)tmplen; @@ -3179,8 +3272,9 @@ spell_find_suggest ( su->su_maxcount = maxcount; su->su_maxscore = SCORE_MAXINIT; - if (su->su_badlen >= MAXWLEN) + if (su->su_badlen >= MAXWLEN) { su->su_badlen = MAXWLEN - 1; // just in case + } STRLCPY(su->su_badword, su->su_badptr, su->su_badlen + 1); (void)spell_casefold(curwin, su->su_badptr, su->su_badlen, su->su_fbadword, MAXWLEN); @@ -3192,9 +3286,10 @@ spell_find_suggest ( // get caps flags for bad word su->su_badflags = badword_captype(su->su_badptr, - su->su_badptr + su->su_badlen); - if (need_cap) + su->su_badptr + su->su_badlen); + if (need_cap) { su->su_badflags |= WF_ONECAP; + } // Find the default language for sound folding. We simply use the first // one in 'spelllang' that supports sound folding. That's good for when @@ -3210,9 +3305,10 @@ spell_find_suggest ( // Soundfold the bad word with the default sound folding, so that we don't // have to do this many times. - if (su->su_sallang != NULL) + if (su->su_sallang != NULL) { spell_soundfold(su->su_sallang, su->su_fbadword, true, - su->su_sal_badword); + su->su_sal_badword); + } // If the word is not capitalised and spell_check() doesn't consider the // word to be bad then it might need to be capitalised. Add a suggestion @@ -3221,12 +3317,13 @@ spell_find_suggest ( if (!SPELL_ISUPPER(c) && attr == HLF_COUNT) { make_case_word(su->su_badword, buf, WF_ONECAP); add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, - 0, true, su->su_sallang, false); + 0, true, su->su_sallang, false); } // Ban the bad word itself. It may appear in another region. - if (banbadword) + if (banbadword) { add_banned(su, su->su_badword); + } // Make a copy of 'spellsuggest', because the expression may change it. sps_copy = vim_strsave(p_sps); @@ -3258,10 +3355,11 @@ spell_find_suggest ( xfree(sps_copy); - if (do_combine) + if (do_combine) { // Combine the two list of suggestions. This must be done last, // because sorting changes the order again. score_combine(su); + } } // Find suggestions by evaluating expression "expr". @@ -3297,9 +3395,9 @@ static void spell_suggest_expr(suginfo_T *su, char_u *expr) // Find suggestions in file "fname". Used for "file:" in 'spellsuggest'. static void spell_suggest_file(suginfo_T *su, char_u *fname) { - FILE *fd; + FILE *fd; char_u line[MAXWLEN * 2]; - char_u *p; + char_u *p; int len; char_u cword[MAXWLEN]; @@ -3315,13 +3413,15 @@ static void spell_suggest_file(suginfo_T *su, char_u *fname) line_breakcheck(); p = vim_strchr(line, '/'); - if (p == NULL) + if (p == NULL) { continue; // No Tab found, just skip the line. + } *p++ = NUL; if (STRICMP(su->su_badword, line) == 0) { // Match! Isolate the good word, until CR or NL. - for (len = 0; p[len] >= ' '; ++len) + for (len = 0; p[len] >= ' '; ++len) { ; + } p[len] = NUL; // If the suggestion doesn't have specific case duplicate the case @@ -3332,7 +3432,7 @@ static void spell_suggest_file(suginfo_T *su, char_u *fname) } add_suggestion(su, &su->su_ga, p, su->su_badlen, - SCORE_FILE, 0, true, su->su_sallang, false); + SCORE_FILE, 0, true, su->su_sallang, false); } } @@ -3360,15 +3460,17 @@ static void spell_suggest_intern(suginfo_T *su, bool interactive) suggest_try_change(su); // For the resulting top-scorers compute the sound-a-like score. - if (sps_flags & SPS_DOUBLE) + if (sps_flags & SPS_DOUBLE) { score_comp_sal(su); + } // 3. Try finding sound-a-like words. if ((sps_flags & SPS_FAST) == 0) { - if (sps_flags & SPS_BEST) + if (sps_flags & SPS_BEST) { // Adjust the word score for the suggestions found so far for how // they sounds like. rescore_suggestions(su); + } // While going through the soundfold tree "su_maxscore" is the score // for the soundfold word, limits the changes that are being tried, @@ -3407,9 +3509,10 @@ static void spell_suggest_intern(suginfo_T *su, bool interactive) } if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) { - if (sps_flags & SPS_BEST) + if (sps_flags & SPS_BEST) { // Adjust the word score for how it sounds like. rescore_suggestions(su); + } // Remove bogus suggestions, sort and truncate at "maxcount". check_suggestions(su, &su->su_ga); @@ -3420,7 +3523,7 @@ static void spell_suggest_intern(suginfo_T *su, bool interactive) // Free the info put in "*su" by spell_find_suggest(). static void spell_find_cleanup(suginfo_T *su) { -# define FREE_SUG_WORD(sug) xfree(sug->st_word) +#define FREE_SUG_WORD(sug) xfree(sug->st_word) // Free the suggestions. GA_DEEP_CLEAR(&su->su_ga, suggest_T, FREE_SUG_WORD); GA_DEEP_CLEAR(&su->su_sga, suggest_T, FREE_SUG_WORD); @@ -3459,11 +3562,13 @@ static void allcap_copy(char_u *word, char_u *wcopy) if (c == 0xdf) { c = 'S'; - if (d - wcopy >= MAXWLEN - 1) + if (d - wcopy >= MAXWLEN - 1) { break; + } *d++ = c; - } else + } else { c = SPELL_TOUPPER(c); + } if (d - wcopy >= MAXWLEN - MB_MAXBYTES) { break; @@ -3476,7 +3581,7 @@ static void allcap_copy(char_u *word, char_u *wcopy) // Try finding suggestions by recognizing specific situations. static void suggest_try_special(suginfo_T *su) { - char_u *p; + char_u *p; size_t len; int c; char_u word[MAXWLEN]; @@ -3496,7 +3601,7 @@ static void suggest_try_special(suginfo_T *su) // Give a soundalike score of 0, compute the score as if deleting one // character. add_suggestion(su, &su->su_ga, word, su->su_badlen, - RESCORE(SCORE_REP, 0), 0, true, su->su_sallang, false); + RESCORE(SCORE_REP, 0), 0, true, su->su_sallang, false); } } @@ -3509,8 +3614,7 @@ proftime_T total; proftime_T times[STATE_FINAL + 1]; long counts[STATE_FINAL + 1]; - static void -prof_init(void) +static void prof_init(void) { for (int i = 0; i <= STATE_FINAL; i++) { profile_zero(×[i]); @@ -3521,8 +3625,7 @@ prof_init(void) } // call before changing state - static void -prof_store(state_T state) +static void prof_store(state_T state) { profile_end(¤t); profile_add(×[state], ¤t); @@ -3531,8 +3634,7 @@ prof_store(state_T state) } # define PROF_STORE(state) prof_store(state); - static void -prof_report(char *name) +static void prof_report(char *name) { FILE *fd = fopen("suggestprof", "a"); @@ -3554,8 +3656,8 @@ static void suggest_try_change(suginfo_T *su) { char_u fword[MAXWLEN]; // copy of the bad word, case-folded int n; - char_u *p; - langp_T *lp; + char_u *p; + langp_T *lp; // We make a copy of the case-folded bad word, so that we can modify it // to find matches (esp. REP items). Append some more text, changing @@ -3570,8 +3672,9 @@ static void suggest_try_change(suginfo_T *su) // If reloading a spell file fails it's still in the list but // everything has been cleared. - if (lp->lp_slang->sl_fbyts == NULL) + if (lp->lp_slang->sl_fbyts == NULL) { continue; + } // Try it for this language. Will add possible suggestions. // @@ -3623,28 +3726,28 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so char_u tword[MAXWLEN]; // good word collected so far trystate_T stack[MAXWLEN]; char_u preword[MAXWLEN * 3] = { 0 }; // word found with proper case; - // concatenation of prefix compound - // words and split word. NUL terminated - // when going deeper but not when coming - // back. + // concatenation of prefix compound + // words and split word. NUL terminated + // when going deeper but not when coming + // back. char_u compflags[MAXWLEN]; // compound flags, one for each word - trystate_T *sp; + trystate_T *sp; int newscore; int score; - char_u *byts, *fbyts, *pbyts; - idx_T *idxs, *fidxs, *pidxs; + char_u *byts, *fbyts, *pbyts; + idx_T *idxs, *fidxs, *pidxs; int depth; int c, c2, c3; int n = 0; int flags; - garray_T *gap; + garray_T *gap; idx_T arridx; int len; - char_u *p; - fromto_T *ftp; + char_u *p; + fromto_T *ftp; int fl = 0, tl; int repextra = 0; // extra bytes in fword[] from REP item - slang_T *slang = lp->lp_slang; + slang_T *slang = lp->lp_slang; int fword_ends; bool goodword_ends; #ifdef DEBUG_TRIEWALK @@ -3709,8 +3812,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so if (sp->ts_prefixdepth == PFD_PREFIXTREE) { // Skip over the NUL bytes, we use them later. - for (n = 0; n < len && byts[arridx + n] == 0; ++n) + for (n = 0; n < len && byts[arridx + n] == 0; ++n) { ; + } sp->ts_curi += n; // Always past NUL bytes now. @@ -3727,7 +3831,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so n = nofold_len(fword, sp->ts_fidx, su->su_badptr); flags = badword_captype(su->su_badptr, su->su_badptr + n); su->su_badflags = badword_captype(su->su_badptr + n, - su->su_badptr + su->su_badlen); + su->su_badptr + su->su_badlen); #ifdef DEBUG_TRIEWALK sprintf(changename[depth], "prefix"); #endif @@ -3743,7 +3847,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // and make find_keepcap_word() works. tword[sp->ts_twordlen] = NUL; make_case_word(tword + sp->ts_splitoff, - preword + sp->ts_prewordlen, flags); + preword + sp->ts_prewordlen, flags); sp->ts_prewordlen = (char_u)STRLEN(preword); sp->ts_splitoff = sp->ts_twordlen; } @@ -3764,8 +3868,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so flags = (int)idxs[arridx]; // Skip words with the NOSUGGEST flag. - if (flags & WF_NOSUGGEST) + if (flags & WF_NOSUGGEST) { break; + } fword_ends = (fword[sp->ts_fidx] == NUL || (soundfold @@ -3782,17 +3887,20 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // none this must be the first try without a prefix. n = stack[sp->ts_prefixdepth].ts_arridx; len = pbyts[n++]; - for (c = 0; c < len && pbyts[n + c] == 0; ++c) + for (c = 0; c < len && pbyts[n + c] == 0; ++c) { ; + } if (c > 0) { c = valid_word_prefix(c, n, flags, - tword + sp->ts_splitoff, slang, false); - if (c == 0) + tword + sp->ts_splitoff, slang, false); + if (c == 0) { break; + } // Use the WF_RARE flag for a rare prefix. - if (c & WF_RAREPFX) + if (c & WF_RAREPFX) { flags |= WF_RARE; + } // Tricky: when checking for both prefix and compounding // we run into the prefix flag first. @@ -3805,10 +3913,11 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Check NEEDCOMPOUND: can't use word without compounding. Do try // appending another compound word below. if (sp->ts_complen == sp->ts_compsplit && fword_ends - && (flags & WF_NEEDCOMP)) + && (flags & WF_NEEDCOMP)) { goodword_ends = false; - else + } else { goodword_ends = true; + } p = NULL; compound_ok = true; @@ -3821,18 +3930,19 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so if (sp->ts_fidx - sp->ts_splitfidx == sp->ts_twordlen - sp->ts_splitoff && STRNCMP(fword + sp->ts_splitfidx, - tword + sp->ts_splitoff, - sp->ts_fidx - sp->ts_splitfidx) == 0) { + tword + sp->ts_splitoff, + sp->ts_fidx - sp->ts_splitfidx) == 0) { preword[sp->ts_prewordlen] = NUL; newscore = score_wordcount_adj(slang, sp->ts_score, - preword + sp->ts_prewordlen, - sp->ts_prewordlen > 0); + preword + sp->ts_prewordlen, + sp->ts_prewordlen > 0); // Add the suggestion if the score isn't too bad. - if (newscore <= su->su_maxscore) + if (newscore <= su->su_maxscore) { add_suggestion(su, &su->su_ga, preword, - sp->ts_splitfidx - repextra, - newscore, 0, false, - lp->lp_sallang, false); + sp->ts_splitfidx - repextra, + newscore, 0, false, + lp->lp_sallang, false); + } break; } } else { @@ -3856,23 +3966,26 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so compflags[sp->ts_complen] = ((unsigned)flags >> 24); compflags[sp->ts_complen + 1] = NUL; STRLCPY(preword + sp->ts_prewordlen, - tword + sp->ts_splitoff, - sp->ts_twordlen - sp->ts_splitoff + 1); + tword + sp->ts_splitoff, + sp->ts_twordlen - sp->ts_splitoff + 1); // Verify CHECKCOMPOUNDPATTERN rules. if (match_checkcompoundpattern(preword, sp->ts_prewordlen, - &slang->sl_comppat)) + &slang->sl_comppat)) { compound_ok = false; + } if (compound_ok) { p = preword; - while (*skiptowhite(p) != NUL) + while (*skiptowhite(p) != NUL) { p = skipwhite(skiptowhite(p)); + } if (fword_ends && !can_compound(slang, p, - compflags + sp->ts_compsplit)) + compflags + sp->ts_compsplit)) { // Compound is not allowed. But it may still be // possible if we add another (short) word. compound_ok = false; + } } // Get pointer to last char of previous word. @@ -3884,29 +3997,31 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Form the word with proper case in preword. // If there is a word from a previous split, append. // For the soundfold tree don't change the case, simply append. - if (soundfold) + if (soundfold) { STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff); - else if (flags & WF_KEEPCAP) + } else if (flags & WF_KEEPCAP) { // Must find the word in the keep-case tree. find_keepcap_word(slang, tword + sp->ts_splitoff, - preword + sp->ts_prewordlen); - else { + preword + sp->ts_prewordlen); + } else { // Include badflags: If the badword is onecap or allcap // use that for the goodword too. But if the badword is // allcap and it's only one char long use onecap. c = su->su_badflags; if ((c & WF_ALLCAP) - && su->su_badlen == (*mb_ptr2len)(su->su_badptr) - ) + && su->su_badlen == + (*mb_ptr2len)(su->su_badptr)) { c = WF_ONECAP; + } c |= flags; // When appending a compound word after a word character don't // use Onecap. - if (p != NULL && spell_iswordp_nmw(p, curwin)) + if (p != NULL && spell_iswordp_nmw(p, curwin)) { c &= ~WF_ONECAP; + } make_case_word(tword + sp->ts_splitoff, - preword + sp->ts_prewordlen, c); + preword + sp->ts_prewordlen, c); } if (!soundfold) { @@ -3919,8 +4034,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so if ((sp->ts_complen == sp->ts_compsplit && WAS_BANNED(su, preword + sp->ts_prewordlen)) || WAS_BANNED(su, preword)) { - if (slang->sl_compprog == NULL) + if (slang->sl_compprog == NULL) { break; + } // the word so far was banned but we may try compounding goodword_ends = false; } @@ -3929,14 +4045,17 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so newscore = 0; if (!soundfold) { // soundfold words don't have flags if ((flags & WF_REGION) - && (((unsigned)flags >> 16) & lp->lp_region) == 0) + && (((unsigned)flags >> 16) & lp->lp_region) == 0) { newscore += SCORE_REGION; - if (flags & WF_RARE) + } + if (flags & WF_RARE) { newscore += SCORE_RARE; + } if (!spell_valid_case(su->su_badflags, - captype(preword + sp->ts_prewordlen, NULL))) + captype(preword + sp->ts_prewordlen, NULL))) { newscore += SCORE_ICASE; + } } // TODO: how about splitting in the soundfold tree? @@ -3951,15 +4070,16 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // print the stack of changes that brought us here smsg("------ %s -------", fword); - for (j = 0; j < depth; ++j) + for (j = 0; j < depth; ++j) { smsg("%s", changename[j]); + } } #endif if (soundfold) { // For soundfolded words we need to find the original // words, the edit distance and then add them. add_sound_suggest(su, preword, sp->ts_score, lp); - } else if (sp->ts_fidx > 0) { + } else if (sp->ts_fidx > 0) { // Give a penalty when changing non-word char to word // char, e.g., "thes," -> "these". p = fword + sp->ts_fidx; @@ -3974,15 +4094,15 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Give a bonus to words seen before. score = score_wordcount_adj(slang, - sp->ts_score + newscore, - preword + sp->ts_prewordlen, - sp->ts_prewordlen > 0); + sp->ts_score + newscore, + preword + sp->ts_prewordlen, + sp->ts_prewordlen > 0); // Add the suggestion if the score isn't too bad. if (score <= su->su_maxscore) { add_suggestion(su, &su->su_ga, preword, - sp->ts_fidx - repextra, - score, 0, false, lp->lp_sallang, false); + sp->ts_fidx - repextra, + score, 0, false, lp->lp_sallang, false); if (su->su_badflags & WF_MIXCAP) { // We really don't know if the word should be @@ -3990,13 +4110,13 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so c = captype(preword, NULL); if (c == 0 || c == WF_ALLCAP) { make_case_word(tword + sp->ts_splitoff, - preword + sp->ts_prewordlen, - c == 0 ? WF_ALLCAP : 0); + preword + sp->ts_prewordlen, + c == 0 ? WF_ALLCAP : 0); add_suggestion(su, &su->su_ga, preword, - sp->ts_fidx - repextra, - score + SCORE_ICASE, 0, false, - lp->lp_sallang, false); + sp->ts_fidx - repextra, + score + SCORE_ICASE, 0, false, + lp->lp_sallang, false); } } } @@ -4006,8 +4126,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Try word split and/or compounding. if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends) // Don't split in the middle of a character - && (sp->ts_tcharlen == 0) - ) { + && (sp->ts_tcharlen == 0)) { bool try_compound; int try_split; @@ -4045,7 +4164,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so || sp->ts_complen + 1 - sp->ts_compsplit < slang->sl_compmax) && (can_be_compound(sp, slang, - compflags, ((unsigned)flags >> 24)))) { + compflags, ((unsigned)flags >> 24)))) { try_compound = true; compflags[sp->ts_complen] = ((unsigned)flags >> 24); compflags[sp->ts_complen + 1] = NUL; @@ -4076,35 +4195,40 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // is only one word it must not have the NEEDCOMPOUND // flag. if (sp->ts_complen == sp->ts_compsplit - && (flags & WF_NEEDCOMP)) + && (flags & WF_NEEDCOMP)) { break; + } p = preword; - while (*skiptowhite(p) != NUL) + while (*skiptowhite(p) != NUL) { p = skipwhite(skiptowhite(p)); + } if (sp->ts_complen > sp->ts_compsplit && !can_compound(slang, p, - compflags + sp->ts_compsplit)) + compflags + sp->ts_compsplit)) { break; + } - if (slang->sl_nosplitsugs) + if (slang->sl_nosplitsugs) { newscore += SCORE_SPLIT_NO; - else + } else { newscore += SCORE_SPLIT; + } // Give a bonus to words seen before. newscore = score_wordcount_adj(slang, newscore, - preword + sp->ts_prewordlen, true); + preword + sp->ts_prewordlen, true); } if (TRY_DEEPER(su, stack, depth, newscore)) { go_deeper(stack, depth, newscore); #ifdef DEBUG_TRIEWALK - if (!try_compound && !fword_ends) + if (!try_compound && !fword_ends) { sprintf(changename[depth], "%.*s-%s: split", - sp->ts_twordlen, tword, fword + sp->ts_fidx); - else + sp->ts_twordlen, tword, fword + sp->ts_fidx); + } else { sprintf(changename[depth], "%.*s-%s: compound", - sp->ts_twordlen, tword, fword + sp->ts_fidx); + sp->ts_twordlen, tword, fword + sp->ts_fidx); + } #endif // Save things to be restored at STATE_SPLITUNDO. sp->ts_save_badflags = su->su_badflags; @@ -4115,8 +4239,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so sp = &stack[depth]; // Append a space to preword when splitting. - if (!try_compound && !fword_ends) + if (!try_compound && !fword_ends) { STRCAT(preword, " "); + } sp->ts_prewordlen = (char_u)STRLEN(preword); sp->ts_splitoff = sp->ts_twordlen; sp->ts_splitfidx = sp->ts_fidx; @@ -4127,8 +4252,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // character when the word ends. But only when the // good word can end. if (((!try_compound && !spell_iswordp_nmw(fword - + sp->ts_fidx, - curwin)) + + sp->ts_fidx, + curwin)) || fword_ends) && fword[sp->ts_fidx] != NUL && goodword_ends) { @@ -4138,28 +4263,30 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so if (fword_ends) { // Copy the skipped character to preword. memmove(preword + sp->ts_prewordlen, - fword + sp->ts_fidx, l); + fword + sp->ts_fidx, l); sp->ts_prewordlen += l; preword[sp->ts_prewordlen] = NUL; - } else + } else { sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; + } sp->ts_fidx += l; } // When compounding include compound flag in // compflags[] (already set above). When splitting we // may start compounding over again. - if (try_compound) + if (try_compound) { ++sp->ts_complen; - else + } else { sp->ts_compsplit = sp->ts_complen; + } sp->ts_prefixdepth = PFD_NOPREFIX; // set su->su_badflags to the caps type at this // position n = nofold_len(fword, sp->ts_fidx, su->su_badptr); su->su_badflags = badword_captype(su->su_badptr + n, - su->su_badptr + su->su_badlen); + su->su_badptr + su->su_badlen); // Restart at top of the tree. sp->ts_arridx = 0; @@ -4194,8 +4321,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Past the NUL bytes in the node. su->su_badflags = sp->ts_save_badflags; if (fword[sp->ts_fidx] == NUL - && sp->ts_tcharlen == 0 - ) { + && sp->ts_tcharlen == 0) { // The badword ends, can't use STATE_PLAIN. PROF_STORE(sp->ts_state) sp->ts_state = STATE_DEL; @@ -4228,11 +4354,12 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // just deleted this byte, accepting it is always cheaper than // delete + substitute. if (c == fword[sp->ts_fidx] - || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE) - ) + || (sp->ts_tcharlen > 0 && + sp->ts_isdiff != DIFF_NONE)) { newscore = 0; - else + } else { newscore = SCORE_SUBST; + } if ((newscore == 0 || (sp->ts_fidx >= sp->ts_fidxtry && ((sp->ts_flags & TSF_DIDDEL) == 0 @@ -4240,14 +4367,15 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so && TRY_DEEPER(su, stack, depth, newscore)) { go_deeper(stack, depth, newscore); #ifdef DEBUG_TRIEWALK - if (newscore > 0) + if (newscore > 0) { sprintf(changename[depth], "%.*s-%s: subst %c to %c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - fword[sp->ts_fidx], c); - else + sp->ts_twordlen, tword, fword + sp->ts_fidx, + fword[sp->ts_fidx], c); + } else { sprintf(changename[depth], "%.*s-%s: accept %c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - fword[sp->ts_fidx]); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + fword[sp->ts_fidx]); + } #endif ++depth; sp = &stack[depth]; @@ -4289,12 +4417,11 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so + sp->ts_fcharstart))) { sp->ts_score -= SCORE_SUBST - SCORE_SUBCOMP; } else if ( - !soundfold - && slang->sl_has_map - && similar_chars( - slang, - utf_ptr2char(tword + sp->ts_twordlen - sp->ts_tcharlen), - utf_ptr2char(fword + sp->ts_fcharstart))) { + !soundfold + && slang->sl_has_map + && similar_chars(slang, + utf_ptr2char(tword + sp->ts_twordlen - sp->ts_tcharlen), + utf_ptr2char(fword + sp->ts_fcharstart))) { // For a similar character adjust score from // SCORE_SUBST to SCORE_SIMILAR. sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; @@ -4339,19 +4466,20 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so PROF_STORE(sp->ts_state) sp->ts_state = STATE_INS_PREP; sp->ts_curi = 1; - if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*') + if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*') { // Deleting a vowel at the start of a word counts less, see // soundalike_score(). newscore = 2 * SCORE_DEL / 3; - else + } else { newscore = SCORE_DEL; + } if (fword[sp->ts_fidx] != NUL && TRY_DEEPER(su, stack, depth, newscore)) { go_deeper(stack, depth, newscore); #ifdef DEBUG_TRIEWALK sprintf(changename[depth], "%.*s-%s: delete %c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - fword[sp->ts_fidx]); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + fword[sp->ts_fidx]); #endif ++depth; @@ -4421,19 +4549,20 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // accepting that byte is always better. n += sp->ts_curi++; c = byts[n]; - if (soundfold && sp->ts_twordlen == 0 && c == '*') + if (soundfold && sp->ts_twordlen == 0 && c == '*') { // Inserting a vowel at the start of a word counts less, // see soundalike_score(). newscore = 2 * SCORE_INS / 3; - else + } else { newscore = SCORE_INS; + } if (c != fword[sp->ts_fidx] && TRY_DEEPER(su, stack, depth, newscore)) { go_deeper(stack, depth, newscore); #ifdef DEBUG_TRIEWALK sprintf(changename[depth], "%.*s-%s: insert %c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - c); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + c); #endif ++depth; sp = &stack[depth]; @@ -4454,8 +4583,9 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // soundfold words (illogical but does give a better // score). if (sp->ts_twordlen >= 2 - && tword[sp->ts_twordlen - 2] == c) + && tword[sp->ts_twordlen - 2] == c) { sp->ts_score -= SCORE_INS - SCORE_INSDUP; + } } } break; @@ -4566,8 +4696,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so go_deeper(stack, depth, SCORE_SWAP3); #ifdef DEBUG_TRIEWALK sprintf(changename[depth], "%.*s-%s: swap3 %c and %c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - c, c3); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + c, c3); #endif PROF_STORE(sp->ts_state) sp->ts_state = STATE_UNSWAP3; @@ -4611,8 +4741,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so #ifdef DEBUG_TRIEWALK p = fword + sp->ts_fidx; sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - p[0], p[1], p[2]); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + p[0], p[1], p[2]); #endif PROF_STORE(sp->ts_state) sp->ts_state = STATE_UNROT3L; @@ -4648,8 +4778,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so #ifdef DEBUG_TRIEWALK p = fword + sp->ts_fidx; sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - p[0], p[1], p[2]); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + p[0], p[1], p[2]); #endif PROF_STORE(sp->ts_state) sp->ts_state = STATE_UNROT3R; @@ -4696,10 +4826,11 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // Use the first byte to quickly find the first entry that may // match. If the index is -1 there is none. - if (soundfold) + if (soundfold) { sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]]; - else + } else { sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]]; + } if (sp->ts_curi < 0) { PROF_STORE(sp->ts_state) @@ -4717,10 +4848,11 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // valid. p = fword + sp->ts_fidx; - if (soundfold) + if (soundfold) { gap = &slang->sl_repsal; - else + } else { gap = &lp->lp_replang->sl_rep; + } while (sp->ts_curi < gap->ga_len) { ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; if (*ftp->ft_from != *p) { @@ -4733,8 +4865,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so go_deeper(stack, depth, SCORE_REP); #ifdef DEBUG_TRIEWALK sprintf(changename[depth], "%.*s-%s: replace %s with %s", - sp->ts_twordlen, tword, fword + sp->ts_fidx, - ftp->ft_from, ftp->ft_to); + sp->ts_twordlen, tword, fword + sp->ts_fidx, + ftp->ft_from, ftp->ft_to); #endif // Need to undo this afterwards. PROF_STORE(sp->ts_state) @@ -4755,19 +4887,21 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so } } - if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP) + if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP) { // No (more) matches. PROF_STORE(sp->ts_state) sp->ts_state = STATE_FINAL; + } break; case STATE_REP_UNDO: // Undo a REP replacement and continue with the next one. - if (soundfold) + if (soundfold) { gap = &slang->sl_repsal; - else + } else { gap = &lp->lp_replang->sl_rep; + } ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1; fl = (int)STRLEN(ftp->ft_from); tl = (int)STRLEN(ftp->ft_to); @@ -4815,7 +4949,7 @@ static void go_deeper(trystate_T *stack, int depth, int score_add) // fword[flen] and return the byte length of that many chars in "word". static int nofold_len(char_u *fword, int flen, char_u *word) { - char_u *p; + char_u *p; int i = 0; for (p = fword; p < fword + flen; MB_PTR_ADV(p)) { @@ -4849,9 +4983,9 @@ static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) int len; int c; idx_T lo, hi, m; - char_u *p; - char_u *byts = slang->sl_kbyts; // array with bytes of the words - idx_T *idxs = slang->sl_kidxs; // array with indexes + char_u *p; + char_u *byts = slang->sl_kbyts; // array with bytes of the words + idx_T *idxs = slang->sl_kidxs; // array with indexes if (byts == NULL) { // array is empty: "cannot happen" @@ -4882,7 +5016,7 @@ static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) // kword is getting too long, continue one level up --depth; - } else if (++round[depth] > 2) { + } else if (++round[depth] > 2) { // tried both fold-case and upper-case character, continue one // level up --depth; @@ -4907,19 +5041,20 @@ static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) hi = tryidx + len - 1; while (lo < hi) { m = (lo + hi) / 2; - if (byts[m] > c) + if (byts[m] > c) { hi = m - 1; - else if (byts[m] < c) + } else if (byts[m] < c) { lo = m + 1; - else { + } else { lo = hi = m; break; } } // Stop if there is no matching byte. - if (hi < lo || byts[lo] != c) + if (hi < lo || byts[lo] != c) { break; + } // Continue at the child (if there is one). tryidx = idxs[lo]; @@ -4930,11 +5065,11 @@ static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) // level deeper. if (round[depth] == 1) { STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], - flen); + flen); kwordlen[depth + 1] = kwordlen[depth] + flen; } else { STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], - ulen); + ulen); kwordlen[depth + 1] = kwordlen[depth] + ulen; } fwordidx[depth + 1] = fwordidx[depth] + flen; @@ -4955,11 +5090,11 @@ static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) // su->su_sga. static void score_comp_sal(suginfo_T *su) { - langp_T *lp; + langp_T *lp; char_u badsound[MAXWLEN]; int i; - suggest_T *stp; - suggest_T *sstp; + suggest_T *stp; + suggest_T *sstp; int score; ga_grow(&su->su_sga, su->su_ga.ga_len); @@ -4998,13 +5133,13 @@ static void score_comp_sal(suginfo_T *su) static void score_combine(suginfo_T *su) { garray_T ga; - garray_T *gap; - langp_T *lp; - suggest_T *stp; - char_u *p; + garray_T *gap; + langp_T *lp; + suggest_T *stp; + char_u *p; char_u badsound[MAXWLEN]; int round; - slang_T *slang = NULL; + slang_T *slang = NULL; // Add the alternate score to su_ga. for (int lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) { @@ -5017,11 +5152,12 @@ static void score_combine(suginfo_T *su) for (int i = 0; i < su->su_ga.ga_len; ++i) { stp = &SUG(su->su_ga, i); stp->st_altscore = stp_sal_score(stp, su, slang, badsound); - if (stp->st_altscore == SCORE_MAXMAX) + if (stp->st_altscore == SCORE_MAXMAX) { stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; - else + } else { stp->st_score = (stp->st_score * 3 + stp->st_altscore) / 4; + } stp->st_salscore = false; } break; @@ -5030,7 +5166,7 @@ static void score_combine(suginfo_T *su) if (slang == NULL) { // Using "double" without sound folding. (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, - su->su_maxcount); + su->su_maxcount); return; } @@ -5038,11 +5174,12 @@ static void score_combine(suginfo_T *su) for (int i = 0; i < su->su_sga.ga_len; ++i) { stp = &SUG(su->su_sga, i); stp->st_altscore = spell_edit_score(slang, - su->su_badword, stp->st_word); - if (stp->st_score == SCORE_MAXMAX) + su->su_badword, stp->st_word); + if (stp->st_score == SCORE_MAXMAX) { stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; - else + } else { stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; + } stp->st_salscore = true; } @@ -5066,13 +5203,16 @@ static void score_combine(suginfo_T *su) // Don't add a word if it's already there. p = SUG(*gap, i).st_word; int j; - for (j = 0; j < ga.ga_len; ++j) - if (STRCMP(stp[j].st_word, p) == 0) + for (j = 0; j < ga.ga_len; ++j) { + if (STRCMP(stp[j].st_word, p) == 0) { break; - if (j == ga.ga_len) + } + } + if (j == ga.ga_len) { stp[ga.ga_len++] = SUG(*gap, i); - else + } else { xfree(p); + } } } } @@ -5091,19 +5231,15 @@ static void score_combine(suginfo_T *su) su->su_ga = ga; } -// For the goodword in "stp" compute the soundalike score compared to the -// badword. -static int -stp_sal_score ( - suggest_T *stp, - suginfo_T *su, - slang_T *slang, - char_u *badsound // sound-folded badword -) +/// For the goodword in "stp" compute the soundalike score compared to the +/// badword. +/// +/// @param badsound sound-folded badword +static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound) { - char_u *p; - char_u *pbad; - char_u *pgood; + char_u *p; + char_u *pbad; + char_u *pgood; char_u badsound2[MAXWLEN]; char_u fword[MAXWLEN]; char_u goodsound[MAXWLEN]; @@ -5111,9 +5247,9 @@ stp_sal_score ( int lendiff; lendiff = su->su_badlen - stp->st_orglen; - if (lendiff >= 0) + if (lendiff >= 0) { pbad = badsound; - else { + } else { // soundfold the bad word with more characters following (void)spell_casefold(curwin, su->su_badptr, stp->st_orglen, fword, MAXWLEN); @@ -5122,9 +5258,11 @@ stp_sal_score ( // removing the space. Don't do it when the good word also contains a // space. if (ascii_iswhite(su->su_badptr[su->su_badlen]) - && *skiptowhite(stp->st_word) == NUL) - for (p = fword; *(p = skiptowhite(p)) != NUL; ) + && *skiptowhite(stp->st_word) == NUL) { + for (p = fword; *(p = skiptowhite(p)) != NUL; ) { STRMOVE(p, p + 1); + } + } spell_soundfold(slang, fword, true, badsound2); pbad = badsound2; @@ -5135,10 +5273,11 @@ stp_sal_score ( // what replaces the bad word. STRCPY(goodword, stp->st_word); STRLCPY(goodword + stp->st_wordlen, - su->su_badptr + su->su_badlen - lendiff, lendiff + 1); + su->su_badptr + su->su_badlen - lendiff, lendiff + 1); pgood = goodword; - } else + } else { pgood = stp->st_word; + } // Sound-fold the word and compute the score for the difference. spell_soundfold(slang, pgood, false, goodsound); @@ -5153,17 +5292,18 @@ static sftword_T dumsft; // Prepare for calling suggest_try_soundalike(). static void suggest_try_soundalike_prep(void) { - langp_T *lp; - slang_T *slang; + langp_T *lp; + slang_T *slang; // Do this for all languages that support sound folding and for which a // .sug file has been loaded. for (int lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) { lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); slang = lp->lp_slang; - if (!GA_EMPTY(&slang->sl_sal) && slang->sl_sbyts != NULL) + if (!GA_EMPTY(&slang->sl_sal) && slang->sl_sbyts != NULL) { // prepare the hashtable used by add_sound_suggest() hash_init(&slang->sl_sounddone); + } } } @@ -5172,8 +5312,8 @@ static void suggest_try_soundalike_prep(void) static void suggest_try_soundalike(suginfo_T *su) { char_u salword[MAXWLEN]; - langp_T *lp; - slang_T *slang; + langp_T *lp; + slang_T *slang; // Do this for all languages that support sound folding and for which a // .sug file has been loaded. @@ -5201,10 +5341,10 @@ static void suggest_try_soundalike(suginfo_T *su) // Finish up after calling suggest_try_soundalike(). static void suggest_try_soundalike_finish(void) { - langp_T *lp; - slang_T *slang; + langp_T *lp; + slang_T *slang; int todo; - hashitem_T *hi; + hashitem_T *hi; // Do this for all languages that support sound folding and for which a // .sug file has been loaded. @@ -5214,11 +5354,12 @@ static void suggest_try_soundalike_finish(void) if (!GA_EMPTY(&slang->sl_sal) && slang->sl_sbyts != NULL) { // Free the info about handled words. todo = (int)slang->sl_sounddone.ht_used; - for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi) + for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi) { if (!HASHITEM_EMPTY(hi)) { xfree(HI2SFT(hi)); --todo; } + } // Clear the hashtable, it may also be used by another region. hash_clear(&slang->sl_sounddone); @@ -5227,32 +5368,28 @@ static void suggest_try_soundalike_finish(void) } } -// A match with a soundfolded word is found. Add the good word(s) that -// produce this soundfolded word. -static void -add_sound_suggest ( - suginfo_T *su, - char_u *goodword, - int score, // soundfold score - langp_T *lp -) +/// A match with a soundfolded word is found. Add the good word(s) that +/// produce this soundfolded word. +/// +/// @param score soundfold score +static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp) { - slang_T *slang = lp->lp_slang; // language for sound folding + slang_T *slang = lp->lp_slang; // language for sound folding int sfwordnr; - char_u *nrline; + char_u *nrline; int orgnr; char_u theword[MAXWLEN]; int i; int wlen; - char_u *byts; - idx_T *idxs; + char_u *byts; + idx_T *idxs; int n; int wordcount; int wc; int goodscore; hash_T hash; - hashitem_T *hi; - sftword_T *sft; + hashitem_T *hi; + sftword_T *sft; int bc, gc; int limit; @@ -5271,8 +5408,9 @@ add_sound_suggest ( hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash); } else { sft = HI2SFT(hi); - if (score >= sft->sft_score) + if (score >= sft->sft_score) { return; + } sft->sft_score = score; } @@ -5299,25 +5437,28 @@ add_sound_suggest ( wordcount = 0; for (wlen = 0; wlen < MAXWLEN - 3; ++wlen) { i = 1; - if (wordcount == orgnr && byts[n + 1] == NUL) + if (wordcount == orgnr && byts[n + 1] == NUL) { break; // found end of word - - if (byts[n + 1] == NUL) + } + if (byts[n + 1] == NUL) { ++wordcount; + } // skip over the NUL bytes - for (; byts[n + i] == NUL; ++i) + for (; byts[n + i] == NUL; ++i) { if (i > byts[n]) { // safety check STRCPY(theword + wlen, "BAD"); wlen += 3; goto badword; } + } // One of the siblings must have the word. for (; i < byts[n]; ++i) { wc = idxs[idxs[n + i]]; // nr of words under this byte - if (wordcount + wc > orgnr) + if (wordcount + wc > orgnr) { break; + } wordcount += wc; } @@ -5330,12 +5471,13 @@ badword: // Go over the possible flags and regions. for (; i <= byts[n] && byts[n + i] == NUL; ++i) { char_u cword[MAXWLEN]; - char_u *p; + char_u *p; int flags = (int)idxs[n + i]; // Skip words with the NOSUGGEST flag - if (flags & WF_NOSUGGEST) + if (flags & WF_NOSUGGEST) { continue; + } if (flags & WF_KEEPCAP) { // Must find the word in the keep-case tree. @@ -5347,23 +5489,26 @@ badword: // Need to fix case according to "flags". make_case_word(theword, cword, flags); p = cword; - } else + } else { p = theword; + } } // Add the suggestion. if (sps_flags & SPS_DOUBLE) { // Add the suggestion if the score isn't too bad. - if (score <= su->su_maxscore) + if (score <= su->su_maxscore) { add_suggestion(su, &su->su_sga, p, su->su_badlen, - score, 0, false, slang, false); + score, 0, false, slang, false); + } } else { // Add a penalty for words in another region. if ((flags & WF_REGION) - && (((unsigned)flags >> 16) & lp->lp_region) == 0) + && (((unsigned)flags >> 16) & lp->lp_region) == 0) { goodscore = SCORE_REGION; - else + } else { goodscore = 0; + } // Add a small penalty for changing the first letter from // lower to upper case. Helps for "tath" -> "Kath", which is @@ -5373,8 +5518,9 @@ badword: if (SPELL_ISUPPER(gc)) { bc = PTR2CHAR(su->su_badword); if (!SPELL_ISUPPER(bc) - && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc)) + && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc)) { goodscore += SCORE_ICASE / 2; + } } // Compute the score for the good word. This only does letter @@ -5385,11 +5531,12 @@ badword: // If the limit is very high then the iterative method is // inefficient, using an array is quicker. limit = MAXSCORE(su->su_sfmaxscore - goodscore, score); - if (limit > SCORE_LIMITMAX) + if (limit > SCORE_LIMITMAX) { goodscore += spell_edit_score(slang, su->su_badword, p); - else + } else { goodscore += spell_edit_score_limit(slang, su->su_badword, - p, limit); + p, limit); + } // When going over the limit don't bother to do the rest. if (goodscore < SCORE_MAXMAX) { @@ -5398,9 +5545,10 @@ badword: // Add the suggestion if the score isn't too bad. goodscore = RESCORE(goodscore, score); - if (goodscore <= su->su_sfmaxscore) + if (goodscore <= su->su_sfmaxscore) { add_suggestion(su, &su->su_ga, p, su->su_badlen, - goodscore, score, true, slang, true); + goodscore, score, true, slang, true); + } } } } @@ -5414,9 +5562,9 @@ static int soundfold_find(slang_T *slang, char_u *word) int len; int wlen = 0; int c; - char_u *ptr = word; - char_u *byts; - idx_T *idxs; + char_u *ptr = word; + char_u *byts; + idx_T *idxs; int wordnr = 0; byts = slang->sl_sbyts; @@ -5430,35 +5578,41 @@ static int soundfold_find(slang_T *slang, char_u *word) // If the word ends we found the word. If not skip the NUL bytes. c = ptr[wlen]; if (byts[arridx] == NUL) { - if (c == NUL) + if (c == NUL) { break; + } // Skip over the zeros, there can be several. while (len > 0 && byts[arridx] == NUL) { ++arridx; --len; } - if (len == 0) + if (len == 0) { return -1; // no children, word should have ended here + } ++wordnr; } // If the word ends we didn't find it. - if (c == NUL) + if (c == NUL) { return -1; + } // Perform a binary search in the list of accepted bytes. - if (c == TAB) // <Tab> is handled like <Space> + if (c == TAB) { // <Tab> is handled like <Space> c = ' '; + } while (byts[arridx] < c) { // The word count is in the first idxs[] entry of the child. wordnr += idxs[idxs[arridx]]; ++arridx; - if (--len == 0) // end of the bytes, didn't find it + if (--len == 0) { // end of the bytes, didn't find it return -1; + } } - if (byts[arridx] != c) // didn't find the byte + if (byts[arridx] != c) { // didn't find the byte return -1; + } // Continue at the child (if there is one). arridx = idxs[arridx]; @@ -5466,9 +5620,11 @@ static int soundfold_find(slang_T *slang, char_u *word) // One space in the good word may stand for several spaces in the // checked word. - if (c == ' ') - while (ptr[wlen] == ' ' || ptr[wlen] == TAB) + if (c == ' ') { + while (ptr[wlen] == ' ' || ptr[wlen] == TAB) { ++wlen; + } + } } return wordnr; @@ -5477,15 +5633,16 @@ static int soundfold_find(slang_T *slang, char_u *word) // Copy "fword" to "cword", fixing case according to "flags". static void make_case_word(char_u *fword, char_u *cword, int flags) { - if (flags & WF_ALLCAP) + if (flags & WF_ALLCAP) { // Make it all upper-case allcap_copy(fword, cword); - else if (flags & WF_ONECAP) + } else if (flags & WF_ONECAP) { // Make the first letter upper-case onecap_copy(fword, cword, true); - else + } else { // Use goodword as-is. STRCPY(cword, fword); + } } // Returns true if "c1" and "c2" are similar characters according to the MAP @@ -5494,7 +5651,7 @@ static bool similar_chars(slang_T *slang, int c1, int c2) { int m1, m2; char_u buf[MB_MAXBYTES + 1]; - hashitem_T *hi; + hashitem_T *hi; if (c1 >= 256) { buf[utf_char2bytes(c1, buf)] = 0; @@ -5526,25 +5683,20 @@ static bool similar_chars(slang_T *slang, int c1, int c2) return m1 == m2; } -// Adds a suggestion to the list of suggestions. -// For a suggestion that is already in the list the lowest score is remembered. -static void -add_suggestion ( - suginfo_T *su, - garray_T *gap, // either su_ga or su_sga - const char_u *goodword, - int badlenarg, // len of bad word replaced with "goodword" - int score, - int altscore, - bool had_bonus, // value for st_had_bonus - slang_T *slang, // language for sound folding - bool maxsf // su_maxscore applies to soundfold score, - // su_sfmaxscore to the total score. -) +/// Adds a suggestion to the list of suggestions. +/// For a suggestion that is already in the list the lowest score is remembered. +/// +/// @param gap either su_ga or su_sga +/// @param badlenarg len of bad word replaced with "goodword" +/// @param had_bonus value for st_had_bonus +/// @param slang language for sound folding +/// @param maxsf su_maxscore applies to soundfold score, su_sfmaxscore to the total score. +static void add_suggestion(suginfo_T *su, garray_T *gap, const char_u *goodword, int badlenarg, + int score, int altscore, bool had_bonus, slang_T *slang, bool maxsf) { int goodlen; // len of goodword changed int badlen; // len of bad word changed - suggest_T *stp; + suggest_T *stp; suggest_T new_sug; // Minimize "badlen" for consistency. Avoids that changing "the the" to @@ -5554,8 +5706,9 @@ add_suggestion ( for (;; ) { goodlen = (int)(pgood - goodword); badlen = (int)(pbad - su->su_badptr); - if (goodlen <= 0 || badlen <= 0) + if (goodlen <= 0 || badlen <= 0) { break; + } MB_PTR_BACK(goodword, pgood); MB_PTR_BACK(su->su_badptr, pbad); if (utf_ptr2char(pgood) != utf_ptr2char(pbad)) { @@ -5563,10 +5716,11 @@ add_suggestion ( } } - if (badlen == 0 && goodlen == 0) + if (badlen == 0 && goodlen == 0) { // goodword doesn't change anything; may happen for "the the" changing // the first "the" to itself. return; + } int i; if (GA_EMPTY(gap)) { @@ -5581,8 +5735,9 @@ add_suggestion ( && stp->st_orglen == badlen && STRNCMP(stp->st_word, goodword, goodlen) == 0) { // Found it. Remember the word with the lowest score. - if (stp->st_slang == NULL) + if (stp->st_slang == NULL) { stp->st_slang = slang; + } new_sug.st_score = score; new_sug.st_altscore = altscore; @@ -5595,9 +5750,9 @@ add_suggestion ( // suggest_try_change() doesn't compute the soundalike // word to keep it fast, while some special methods set // the soundalike score to zero. - if (had_bonus) + if (had_bonus) { rescore_one(su, stp); - else { + } else { new_sug.st_word = stp->st_word; new_sug.st_wordlen = stp->st_wordlen; new_sug.st_slang = stp->st_slang; @@ -5630,25 +5785,24 @@ add_suggestion ( // If we have too many suggestions now, sort the list and keep // the best suggestions. if (gap->ga_len > SUG_MAX_COUNT(su)) { - if (maxsf) + if (maxsf) { su->su_sfmaxscore = cleanup_suggestions(gap, - su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); - else + su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); + } else { su->su_maxscore = cleanup_suggestions(gap, - su->su_maxscore, SUG_CLEAN_COUNT(su)); + su->su_maxscore, SUG_CLEAN_COUNT(su)); + } } } } -// Suggestions may in fact be flagged as errors. Esp. for banned words and -// for split words, such as "the the". Remove these from the list here. -static void -check_suggestions ( - suginfo_T *su, - garray_T *gap // either su_ga or su_sga -) +/// Suggestions may in fact be flagged as errors. Esp. for banned words and +/// for split words, such as "the the". Remove these from the list here. +/// +/// @param gap either su_ga or su_sga +static void check_suggestions(suginfo_T *su, garray_T *gap) { - suggest_T *stp; + suggest_T *stp; char_u longword[MAXWLEN + 1]; int len; hlf_T attr; @@ -5662,16 +5816,17 @@ check_suggestions ( STRLCPY(longword, stp[i].st_word, MAXWLEN + 1); len = stp[i].st_wordlen; STRLCPY(longword + len, su->su_badptr + stp[i].st_orglen, - MAXWLEN - len + 1); + MAXWLEN - len + 1); attr = HLF_COUNT; (void)spell_check(curwin, longword, &attr, NULL, false); if (attr != HLF_COUNT) { // Remove this entry. xfree(stp[i].st_word); --gap->ga_len; - if (i < gap->ga_len) + if (i < gap->ga_len) { memmove(stp + i, stp + i + 1, - sizeof(suggest_T) * (gap->ga_len - i)); + sizeof(suggest_T) * (gap->ga_len - i)); + } } } } @@ -5680,9 +5835,9 @@ check_suggestions ( // Add a word to be banned. static void add_banned(suginfo_T *su, char_u *word) { - char_u *s; + char_u *s; hash_T hash; - hashitem_T *hi; + hashitem_T *hi; hash = hash_hash(word); const size_t word_len = STRLEN(word); @@ -5707,23 +5862,24 @@ static void rescore_suggestions(suginfo_T *su) // Recompute the score for one suggestion if sound-folding is possible. static void rescore_one(suginfo_T *su, suggest_T *stp) { - slang_T *slang = stp->st_slang; + slang_T *slang = stp->st_slang; char_u sal_badword[MAXWLEN]; - char_u *p; + char_u *p; // Only rescore suggestions that have no sal score yet and do have a // language. if (slang != NULL && !GA_EMPTY(&slang->sl_sal) && !stp->st_had_bonus) { - if (slang == su->su_sallang) + if (slang == su->su_sallang) { p = su->su_sal_badword; - else { + } else { spell_soundfold(slang, su->su_fbadword, true, sal_badword); p = sal_badword; } stp->st_altscore = stp_sal_score(stp, su, slang, p); - if (stp->st_altscore == SCORE_MAXMAX) + if (stp->st_altscore == SCORE_MAXMAX) { stp->st_altscore = SCORE_BIG; + } stp->st_score = RESCORE(stp->st_score, stp->st_altscore); stp->st_had_bonus = true; } @@ -5734,28 +5890,27 @@ static void rescore_one(suginfo_T *su, suggest_T *stp) // First on "st_score", then "st_altscore" then alphabetically. static int sug_compare(const void *s1, const void *s2) { - suggest_T *p1 = (suggest_T *)s1; - suggest_T *p2 = (suggest_T *)s2; + suggest_T *p1 = (suggest_T *)s1; + suggest_T *p2 = (suggest_T *)s2; int n = p1->st_score - p2->st_score; if (n == 0) { n = p1->st_altscore - p2->st_altscore; - if (n == 0) + if (n == 0) { n = STRICMP(p1->st_word, p2->st_word); + } } return n; } -// Cleanup the suggestions: -// - Sort on score. -// - Remove words that won't be displayed. -// Returns the maximum score in the list or "maxscore" unmodified. -static int -cleanup_suggestions ( - garray_T *gap, - int maxscore, - int keep // nr of suggestions to keep -) +/// Cleanup the suggestions: +/// - Sort on score. +/// - Remove words that won't be displayed. +/// +/// @param keep nr of suggestions to keep +/// +/// @return the maximum score in the list or "maxscore" unmodified. +static int cleanup_suggestions(garray_T *gap, int maxscore, int keep) FUNC_ATTR_NONNULL_ALL { if (gap->ga_len > 0) { @@ -5823,12 +5978,12 @@ char *eval_soundfold(const char *const word) void spell_soundfold(slang_T *slang, char_u *inword, bool folded, char_u *res) { char_u fword[MAXWLEN]; - char_u *word; + char_u *word; - if (slang->sl_sofo) + if (slang->sl_sofo) { // SOFOFROM and SOFOTO used spell_soundfold_sofo(slang, inword, res); - else { + } else { // SAL items used. Requires the word to be case-folded. if (folded) { word = inword; @@ -5892,12 +6047,12 @@ static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res) // Multi-byte version of spell_soundfold(). static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) { - salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; + salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; int word[MAXWLEN] = { 0 }; int wres[MAXWLEN] = { 0 }; int l; - int *ws; - int *pf; + int *ws; + int *pf; int i, j, z; int reslen; int n, k = 0; @@ -5954,27 +6109,34 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) && ws[0] != NUL; ++n) { // Quickly skip entries that don't match the word. Most // entries are less then three chars, optimize for that. - if (c != ws[0]) + if (c != ws[0]) { continue; + } k = smp[n].sm_leadlen; if (k > 1) { - if (word[i + 1] != ws[1]) + if (word[i + 1] != ws[1]) { continue; + } if (k > 2) { - for (j = 2; j < k; ++j) - if (word[i + j] != ws[j]) + for (j = 2; j < k; ++j) { + if (word[i + j] != ws[j]) { break; - if (j < k) + } + } + if (j < k) { continue; + } } } if ((pf = smp[n].sm_oneof_w) != NULL) { // Check for match with one of the chars in "sm_oneof". - while (*pf != NUL && *pf != word[i + k]) + while (*pf != NUL && *pf != word[i + k]) { ++pf; - if (*pf == NUL) + } + if (*pf == NUL) { continue; + } ++k; } char_u *s = smp[n].sm_rules; @@ -5986,15 +6148,17 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) k--; s++; } - if (*s == '<') + if (*s == '<') { s++; + } if (ascii_isdigit(*s)) { // determine priority pri = *s - '0'; s++; } - if (*s == '^' && *(s + 1) == '^') + if (*s == '^' && *(s + 1) == '^') { s++; + } if (*s == NUL || (*s == '^' @@ -6017,19 +6181,24 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) for (; ((ws = smp[n0].sm_lead_w)[0] & 0xff) == (c0 & 0xff); ++n0) { // Quickly skip entries that don't match the word. - if (c0 != ws[0]) + if (c0 != ws[0]) { continue; + } k0 = smp[n0].sm_leadlen; if (k0 > 1) { - if (word[i + k] != ws[1]) + if (word[i + k] != ws[1]) { continue; + } if (k0 > 2) { pf = word + i + k + 1; - for (j = 2; j < k0; ++j) - if (*pf++ != ws[j]) + for (j = 2; j < k0; ++j) { + if (*pf++ != ws[j]) { break; - if (j < k0) + } + } + if (j < k0) { continue; + } } } k0 += k - 1; @@ -6037,10 +6206,12 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) if ((pf = smp[n0].sm_oneof_w) != NULL) { // Check for match with one of the chars in // "sm_oneof". - while (*pf != NUL && *pf != word[i + k0]) + while (*pf != NUL && *pf != word[i + k0]) { ++pf; - if (*pf == NUL) + } + if (*pf == NUL) { continue; + } ++k0; } @@ -6051,8 +6222,9 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) // "if (k0 == k)" s++; } - if (*s == '<') + if (*s == '<') { s++; + } if (ascii_isdigit(*s)) { p0 = *s - '0'; s++; @@ -6062,22 +6234,25 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) // *s == '^' cuts || (*s == '$' && !spell_iswordp_w(word + i + k0, - curwin))) { - if (k0 == k) + curwin))) { + if (k0 == k) { // this is just a piece of the string continue; + } - if (p0 < pri) + if (p0 < pri) { // priority too low continue; + } // rule fits; stop search break; } } if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) - == (c0 & 0xff)) + == (c0 & 0xff)) { continue; + } } // replace string @@ -6088,20 +6263,23 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) // rule with '<' is used if (reslen > 0 && ws != NULL && *ws != NUL && (wres[reslen - 1] == c - || wres[reslen - 1] == *ws)) + || wres[reslen - 1] == *ws)) { reslen--; + } z0 = 1; z = 1; k0 = 0; - if (ws != NULL) + if (ws != NULL) { while (*ws != NUL && word[i + k0] != NUL) { word[i + k0] = *ws; k0++; ws++; } - if (k > k0) + } + if (k > k0) { memmove(word + i + k0, word + i + k, - sizeof(int) * (wordlen - (i + k) + 1)); + sizeof(int) * (wordlen - (i + k) + 1)); + } // new "actual letter" c = word[i]; @@ -6109,23 +6287,27 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) // no '<' rule used i += k - 1; z = 0; - if (ws != NULL) + if (ws != NULL) { while (*ws != NUL && ws[1] != NUL && reslen < MAXWLEN) { - if (reslen == 0 || wres[reslen - 1] != *ws) + if (reslen == 0 || wres[reslen - 1] != *ws) { wres[reslen++] = *ws; + } ws++; } + } // new "actual letter" - if (ws == NULL) + if (ws == NULL) { c = NUL; - else + } else { c = *ws; + } if (strstr((char *)s, "^^") != NULL) { - if (c != NUL) + if (c != NUL) { wres[reslen++] = c; + } memmove(word, word + i + 1, - sizeof(int) * (wordlen - (i + 1) + 1)); + sizeof(int) * (wordlen - (i + 1) + 1)); i = 0; z0 = 1; } @@ -6133,7 +6315,7 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) break; } } - } else if (ascii_iswhite(c)) { + } else if (ascii_iswhite(c)) { c = ' '; k = 1; } @@ -6141,9 +6323,10 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) if (z0 == 0) { if (k && !p0 && reslen < MAXWLEN && c != NUL && (!slang->sl_collapse || reslen == 0 - || wres[reslen - 1] != c)) + || wres[reslen - 1] != c)) { // condense only double letters wres[reslen++] = c; + } i++; z = 0; @@ -6162,35 +6345,36 @@ static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) res[l] = NUL; } -// Compute a score for two sound-a-like words. -// This permits up to two inserts/deletes/swaps/etc. to keep things fast. -// Instead of a generic loop we write out the code. That keeps it fast by -// avoiding checks that will not be possible. -static int -soundalike_score ( - char_u *goodstart, // sound-folded good word - char_u *badstart // sound-folded bad word -) +/// Compute a score for two sound-a-like words. +/// This permits up to two inserts/deletes/swaps/etc. to keep things fast. +/// Instead of a generic loop we write out the code. That keeps it fast by +/// avoiding checks that will not be possible. +/// +/// @param goodstart sound-folded good word +/// @param badstart sound-folded bad word +static int soundalike_score(char_u *goodstart, char_u *badstart) { - char_u *goodsound = goodstart; - char_u *badsound = badstart; + char_u *goodsound = goodstart; + char_u *badsound = badstart; int goodlen; int badlen; int n; - char_u *pl, *ps; - char_u *pl2, *ps2; + char_u *pl, *ps; + char_u *pl2, *ps2; int score = 0; // Adding/inserting "*" at the start (word starts with vowel) shouldn't be // counted so much, vowels in the middle of the word aren't counted at all. if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) { if ((badsound[0] == NUL && goodsound[1] == NUL) - || (goodsound[0] == NUL && badsound[1] == NUL)) + || (goodsound[0] == NUL && badsound[1] == NUL)) { // changing word with vowel to word without a sound return SCORE_DEL; - if (badsound[0] == NUL || goodsound[0] == NUL) + } + if (badsound[0] == NUL || goodsound[0] == NUL) { // more than two changes return SCORE_MAXMAX; + } if (badsound[1] == goodsound[1] || (badsound[1] != NUL @@ -6199,10 +6383,11 @@ soundalike_score ( // handle like a substitute } else { score = 2 * SCORE_DEL / 3; - if (*badsound == '*') + if (*badsound == '*') { ++badsound; - else + } else { ++goodsound; + } } } @@ -6212,8 +6397,9 @@ soundalike_score ( // Return quickly if the lengths are too different to be fixed by two // changes. n = goodlen - badlen; - if (n < -2 || n > 2) + if (n < -2 || n > 2) { return SCORE_MAXMAX; + } if (n > 0) { pl = goodsound; // goodsound is longest @@ -6239,8 +6425,9 @@ soundalike_score ( ++ps; } // strings must be equal after second delete - if (STRCMP(pl + 1, ps) == 0) + if (STRCMP(pl + 1, ps) == 0) { return score + SCORE_DEL * 2; + } // Failed to compare. break; @@ -6253,20 +6440,23 @@ soundalike_score ( pl2 = pl + 1; ps2 = ps; while (*pl2 == *ps2) { - if (*pl2 == NUL) // reached the end + if (*pl2 == NUL) { // reached the end return score + SCORE_DEL; + } ++pl2; ++ps2; } // 2: delete then swap, then rest must be equal if (pl2[0] == ps2[1] && pl2[1] == ps2[0] - && STRCMP(pl2 + 2, ps2 + 2) == 0) + && STRCMP(pl2 + 2, ps2 + 2) == 0) { return score + SCORE_DEL + SCORE_SWAP; + } // 3: delete then substitute, then the rest must be equal - if (STRCMP(pl2 + 1, ps2 + 1) == 0) + if (STRCMP(pl2 + 1, ps2 + 1) == 0) { return score + SCORE_DEL + SCORE_SUBST; + } // 4: first swap then delete if (pl[0] == ps[1] && pl[1] == ps[0]) { @@ -6277,8 +6467,9 @@ soundalike_score ( ++ps2; } // delete a char and then strings must be equal - if (STRCMP(pl2 + 1, ps2) == 0) + if (STRCMP(pl2 + 1, ps2) == 0) { return score + SCORE_SWAP + SCORE_DEL; + } } // 5: first substitute then delete @@ -6289,8 +6480,9 @@ soundalike_score ( ++ps2; } // delete a char and then strings must be equal - if (STRCMP(pl2 + 1, ps2) == 0) + if (STRCMP(pl2 + 1, ps2) == 0) { return score + SCORE_SUBST + SCORE_DEL; + } // Failed to compare. break; @@ -6299,47 +6491,54 @@ soundalike_score ( // Lengths are equal, thus changes must result in same length: An // insert is only possible in combination with a delete. // 1: check if for identical strings - if (*pl == NUL) + if (*pl == NUL) { return score; + } // 2: swap if (pl[0] == ps[1] && pl[1] == ps[0]) { pl2 = pl + 2; // swap, skip two chars ps2 = ps + 2; while (*pl2 == *ps2) { - if (*pl2 == NUL) // reached the end + if (*pl2 == NUL) { // reached the end return score + SCORE_SWAP; + } ++pl2; ++ps2; } // 3: swap and swap again if (pl2[0] == ps2[1] && pl2[1] == ps2[0] - && STRCMP(pl2 + 2, ps2 + 2) == 0) + && STRCMP(pl2 + 2, ps2 + 2) == 0) { return score + SCORE_SWAP + SCORE_SWAP; + } // 4: swap and substitute - if (STRCMP(pl2 + 1, ps2 + 1) == 0) + if (STRCMP(pl2 + 1, ps2 + 1) == 0) { return score + SCORE_SWAP + SCORE_SUBST; + } } // 5: substitute pl2 = pl + 1; ps2 = ps + 1; while (*pl2 == *ps2) { - if (*pl2 == NUL) // reached the end + if (*pl2 == NUL) { // reached the end return score + SCORE_SUBST; + } ++pl2; ++ps2; } // 6: substitute and swap if (pl2[0] == ps2[1] && pl2[1] == ps2[0] - && STRCMP(pl2 + 2, ps2 + 2) == 0) + && STRCMP(pl2 + 2, ps2 + 2) == 0) { return score + SCORE_SUBST + SCORE_SWAP; + } // 7: substitute and substitute - if (STRCMP(pl2 + 1, ps2 + 1) == 0) + if (STRCMP(pl2 + 1, ps2 + 1) == 0) { return score + SCORE_SUBST + SCORE_SUBST; + } // 8: insert then delete pl2 = pl; @@ -6348,8 +6547,9 @@ soundalike_score ( ++pl2; ++ps2; } - if (STRCMP(pl2 + 1, ps2) == 0) + if (STRCMP(pl2 + 1, ps2) == 0) { return score + SCORE_INS + SCORE_DEL; + } // 9: delete then insert pl2 = pl + 1; @@ -6358,8 +6558,9 @@ soundalike_score ( ++pl2; ++ps2; } - if (STRCMP(pl2, ps2 + 1) == 0) + if (STRCMP(pl2, ps2 + 1) == 0) { return score + SCORE_INS + SCORE_DEL; + } // Failed to compare. break; @@ -6408,8 +6609,9 @@ static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword) cnt = xmalloc(sizeof(int) * (badlen + 1) * (goodlen + 1)); CNT(0, 0) = 0; - for (j = 1; j <= goodlen; ++j) + for (j = 1; j <= goodlen; ++j) { CNT(0, j) = CNT(0, j - 1) + SCORE_INS; + } for (i = 1; i <= badlen; ++i) { CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL; @@ -6420,16 +6622,17 @@ static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword) CNT(i, j) = CNT(i - 1, j - 1); } else { // Use a better score when there is only a case difference. - if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) + if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) { CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); - else { + } else { // For a similar character use SCORE_SIMILAR. if (slang != NULL && slang->sl_has_map - && similar_chars(slang, gc, bc)) + && similar_chars(slang, gc, bc)) { CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1); - else + } else { CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); + } } if (i > 1 && j > 1) { @@ -6437,16 +6640,19 @@ static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword) pgc = wgoodword[j - 2]; if (bc == pgc && pbc == gc) { t = SCORE_SWAP + CNT(i - 2, j - 2); - if (t < CNT(i, j)) + if (t < CNT(i, j)) { CNT(i, j) = t; + } } } t = SCORE_DEL + CNT(i - 1, j); - if (t < CNT(i, j)) + if (t < CNT(i, j)) { CNT(i, j) = t; + } t = SCORE_INS + CNT(i, j - 1); - if (t < CNT(i, j)) + if (t < CNT(i, j)) { CNT(i, j) = t; + } } } } @@ -6515,11 +6721,13 @@ static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goo bc = wbadword[bi]; gc = wgoodword[gi]; - if (bc != gc) // stop at a char that's different + if (bc != gc) { // stop at a char that's different break; + } if (bc == NUL) { // both words end - if (score < minscore) + if (score < minscore) { minscore = score; + } goto pop; // do next alternative } ++bi; @@ -6528,14 +6736,16 @@ static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goo if (gc == NUL) { // goodword ends, delete badword chars do { - if ((score += SCORE_DEL) >= minscore) + if ((score += SCORE_DEL) >= minscore) { goto pop; // do next alternative + } } while (wbadword[++bi] != NUL); minscore = score; - } else if (bc == NUL) { // badword ends, insert badword chars + } else if (bc == NUL) { // badword ends, insert badword chars do { - if ((score += SCORE_INS) >= minscore) + if ((score += SCORE_INS) >= minscore) { goto pop; // do next alternative + } } while (wgoodword[++gi] != NUL); minscore = score; } else { // both words continue @@ -6586,16 +6796,17 @@ static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goo // Substitute one character for another which is the same // thing as deleting a character from both goodword and badword. // Use a better score when there is only a case difference. - if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) + if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) { score += SCORE_ICASE; - else { + } else { // For a similar character use SCORE_SIMILAR. if (slang != NULL && slang->sl_has_map - && similar_chars(slang, gc, bc)) + && similar_chars(slang, gc, bc)) { score += SCORE_SIMILAR; - else + } else { score += SCORE_SUBST; + } } if (score < minscore) { @@ -6607,8 +6818,9 @@ static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goo } pop: // Get here to try the next alternative, pop it from the stack. - if (stackidx == 0) // stack is empty, finished + if (stackidx == 0) { // stack is empty, finished break; + } // pop an item from the stack --stackidx; @@ -6620,8 +6832,9 @@ pop: // When the score goes over "limit" it may actually be much higher. // Return a very large number to avoid going below the limit when giving a // bonus. - if (minscore > limit) + if (minscore > limit) { return SCORE_MAXMAX; + } return minscore; } @@ -6656,7 +6869,7 @@ void ex_spellinfo(exarg_T *eap) // ":spelldump" void ex_spelldump(exarg_T *eap) { - char_u *spl; + char_u *spl; long dummy; if (no_spell_checking(curwin)) { @@ -6685,50 +6898,49 @@ void ex_spelldump(exarg_T *eap) redraw_later(curwin, NOT_VALID); } -// Go through all possible words and: -// 1. When "pat" is NULL: dump a list of all words in the current buffer. -// "ic" and "dir" are not used. -// 2. When "pat" is not NULL: add matching words to insert mode completion. -void -spell_dump_compl ( - char_u *pat, // leading part of the word - int ic, // ignore case - Direction *dir, // direction for adding matches - int dumpflags_arg // DUMPFLAG_* -) +/// Go through all possible words and: +/// 1. When "pat" is NULL: dump a list of all words in the current buffer. +/// "ic" and "dir" are not used. +/// 2. When "pat" is not NULL: add matching words to insert mode completion. +/// +/// @param pat leading part of the word +/// @param ic ignore case +/// @param dir direction for adding matches +/// @param dumpflags_arg DUMPFLAG_* +void spell_dump_compl(char_u *pat, int ic, Direction *dir, int dumpflags_arg) { - langp_T *lp; - slang_T *slang; + langp_T *lp; + slang_T *slang; idx_T arridx[MAXWLEN]; int curi[MAXWLEN]; char_u word[MAXWLEN]; int c; - char_u *byts; - idx_T *idxs; + char_u *byts; + idx_T *idxs; linenr_T lnum = 0; int round; int depth; int n; int flags; - char_u *region_names = NULL; // region names being used + char_u *region_names = NULL; // region names being used bool do_region = true; // dump region names and numbers - char_u *p; + char_u *p; int dumpflags = dumpflags_arg; int patlen; // When ignoring case or when the pattern starts with capital pass this on // to dump_word(). if (pat != NULL) { - if (ic) + if (ic) { dumpflags |= DUMPFLAG_ICASE; - else { + } else { n = captype(pat, NULL); - if (n == WF_ONECAP) + if (n == WF_ONECAP) { dumpflags |= DUMPFLAG_ONECAP; - else if (n == WF_ALLCAP - && (int)STRLEN(pat) > mb_ptr2len(pat) - ) + } else if (n == WF_ALLCAP + && (int)STRLEN(pat) > mb_ptr2len(pat)) { dumpflags |= DUMPFLAG_ALLCAP; + } } } @@ -6738,9 +6950,9 @@ spell_dump_compl ( lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); p = lp->lp_slang->sl_regions; if (p[0] != 0) { - if (region_names == NULL) // first language with regions + if (region_names == NULL) { // first language with regions region_names = p; - else if (STRCMP(region_names, p) != 0) { + } else if (STRCMP(region_names, p) != 0) { do_region = false; // region names are different break; } @@ -6752,15 +6964,17 @@ spell_dump_compl ( vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); ml_append(lnum++, IObuff, (colnr_T)0, false); } - } else + } else { do_region = false; + } // Loop over all files loaded for the entries in 'spelllang'. for (int lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) { lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); slang = lp->lp_slang; - if (slang->sl_fbyts == NULL) // reloading failed + if (slang->sl_fbyts == NULL) { // reloading failed continue; + } if (pat == NULL) { vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); @@ -6769,10 +6983,11 @@ spell_dump_compl ( // When matching with a pattern and there are no prefixes only use // parts of the tree that match "pat". - if (pat != NULL && slang->sl_pbyts == NULL) + if (pat != NULL && slang->sl_pbyts == NULL) { patlen = (int)STRLEN(pat); - else + } else { patlen = -1; + } // round 1: case-folded tree // round 2: keep-case tree @@ -6786,9 +7001,9 @@ spell_dump_compl ( byts = slang->sl_kbyts; idxs = slang->sl_kidxs; } - if (byts == NULL) + if (byts == NULL) { continue; // array is empty - + } depth = 0; arridx[0] = 0; curi[0] = 1; @@ -6817,23 +7032,26 @@ spell_dump_compl ( || (((unsigned)flags >> 16) & lp->lp_region) != 0)) { word[depth] = NUL; - if (!do_region) + if (!do_region) { flags &= ~WF_REGION; + } // Dump the basic word if there is no prefix or // when it's the first one. c = (unsigned)flags >> 24; if (c == 0 || curi[depth] == 2) { dump_word(slang, word, pat, dir, - dumpflags, flags, lnum); - if (pat == NULL) + dumpflags, flags, lnum); + if (pat == NULL) { ++lnum; + } } // Apply the prefix, if there is one. - if (c != 0) + if (c != 0) { lnum = dump_prefixes(slang, word, pat, dir, - dumpflags, flags, lnum); + dumpflags, flags, lnum); + } } } else { // Normal char, go one level deeper. @@ -6849,8 +7067,9 @@ spell_dump_compl ( // ignore case... assert(depth >= 0); if (depth <= patlen - && mb_strnicmp(word, pat, (size_t)depth) != 0) + && mb_strnicmp(word, pat, (size_t)depth) != 0) { --depth; + } } } } @@ -6860,22 +7079,23 @@ spell_dump_compl ( // Dumps one word: apply case modifications and append a line to the buffer. // When "lnum" is zero add insert mode completion. -static void dump_word(slang_T *slang, char_u *word, char_u *pat, - Direction *dir, int dumpflags, int wordflags, - linenr_T lnum) +static void dump_word(slang_T *slang, char_u *word, char_u *pat, Direction *dir, int dumpflags, + int wordflags, linenr_T lnum) { bool keepcap = false; - char_u *p; - char_u *tw; + char_u *p; + char_u *tw; char_u cword[MAXWLEN]; char_u badword[MAXWLEN + 10]; int i; int flags = wordflags; - if (dumpflags & DUMPFLAG_ONECAP) + if (dumpflags & DUMPFLAG_ONECAP) { flags |= WF_ONECAP; - if (dumpflags & DUMPFLAG_ALLCAP) + } + if (dumpflags & DUMPFLAG_ALLCAP) { flags |= WF_ALLCAP; + } if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0) { // Need to fix case according to "flags". @@ -6885,8 +7105,9 @@ static void dump_word(slang_T *slang, char_u *word, char_u *pat, p = word; if ((dumpflags & DUMPFLAG_KEEPCASE) && ((captype(word, NULL) & WF_KEEPCAP) == 0 - || (flags & WF_FIXCAP) != 0)) + || (flags & WF_FIXCAP) != 0)) { keepcap = true; + } } tw = p; @@ -6917,13 +7138,13 @@ static void dump_word(slang_T *slang, char_u *word, char_u *pat, } if (dumpflags & DUMPFLAG_COUNT) { - hashitem_T *hi; + hashitem_T *hi; // Include the word count for ":spelldump!". hi = hash_find(&slang->sl_wordcount, tw); if (!HASHITEM_EMPTY(hi)) { vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d", - tw, HI2WC(hi)->wc_count); + tw, HI2WC(hi)->wc_count); p = IObuff; } } @@ -6939,20 +7160,16 @@ static void dump_word(slang_T *slang, char_u *word, char_u *pat, } } -// For ":spelldump": Find matching prefixes for "word". Prepend each to -// "word" and append a line to the buffer. -// When "lnum" is zero add insert mode completion. -// Return the updated line number. -static linenr_T -dump_prefixes ( - slang_T *slang, - char_u *word, // case-folded word - char_u *pat, - Direction *dir, - int dumpflags, - int flags, // flags with prefix ID - linenr_T startlnum -) +/// For ":spelldump": Find matching prefixes for "word". Prepend each to +/// "word" and append a line to the buffer. +/// When "lnum" is zero add insert mode completion. +/// +/// @param word case-folded word +/// @param flags flags with prefix ID +/// +/// @return the updated line number. +static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, Direction *dir, + int dumpflags, int flags, linenr_T startlnum) { idx_T arridx[MAXWLEN]; int curi[MAXWLEN]; @@ -6960,8 +7177,8 @@ dump_prefixes ( char_u word_up[MAXWLEN]; bool has_word_up = false; int c; - char_u *byts; - idx_T *idxs; + char_u *byts; + idx_T *idxs; linenr_T lnum = startlnum; int depth; int n; @@ -6998,19 +7215,22 @@ dump_prefixes ( c = byts[n]; if (c == 0) { // End of prefix, find out how many IDs there are. - for (i = 1; i < len; ++i) - if (byts[n + i] != 0) + for (i = 1; i < len; ++i) { + if (byts[n + i] != 0) { break; + } + } curi[depth] += i - 1; c = valid_word_prefix(i, n, flags, word, slang, false); if (c != 0) { STRLCPY(prefix + depth, word, MAXWLEN - depth); dump_word(slang, prefix, pat, dir, dumpflags, - (c & WF_RAREPFX) ? (flags | WF_RARE) - : flags, lnum); - if (lnum != 0) + (c & WF_RAREPFX) ? (flags | WF_RARE) + : flags, lnum); + if (lnum != 0) { ++lnum; + } } // Check for prefix that matches the word when the @@ -7018,14 +7238,15 @@ dump_prefixes ( // a condition. if (has_word_up) { c = valid_word_prefix(i, n, flags, word_up, slang, - true); + true); if (c != 0) { STRLCPY(prefix + depth, word_up, MAXWLEN - depth); dump_word(slang, prefix, pat, dir, dumpflags, - (c & WF_RAREPFX) ? (flags | WF_RARE) - : flags, lnum); - if (lnum != 0) + (c & WF_RAREPFX) ? (flags | WF_RARE) + : flags, lnum); + if (lnum != 0) { ++lnum; + } } } } else { @@ -7045,7 +7266,7 @@ dump_prefixes ( // Uses the spell-checking word characters. char_u *spell_to_word_end(char_u *start, win_T *win) { - char_u *p = start; + char_u *p = start; while (*p != NUL && spell_iswordp(p, win)) { MB_PTR_ADV(p); @@ -7060,8 +7281,8 @@ char_u *spell_to_word_end(char_u *start, win_T *win) // Returns the column number of the word. int spell_word_start(int startcol) { - char_u *line; - char_u *p; + char_u *line; + char_u *p; int col = 0; if (no_spell_checking(curwin)) { |