diff options
-rw-r--r-- | src/nvim/buffer.c | 73 | ||||
-rw-r--r-- | src/nvim/diff.c | 9 | ||||
-rw-r--r-- | src/nvim/edit.c | 156 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 113 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 32 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 26 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 78 | ||||
-rw-r--r-- | src/nvim/fileio.c | 63 | ||||
-rw-r--r-- | src/nvim/fold.c | 10 |
9 files changed, 219 insertions, 341 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 0ebe33f2f8..99cdde300d 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -3334,9 +3334,7 @@ void maketitle(void) len = (int)STRLEN(buf_p); if (len > 100) { len -= 100; - if (has_mbyte) { - len += (*mb_tail_off)(buf_p, buf_p + len) + 1; - } + len += (*mb_tail_off)(buf_p, buf_p + len) + 1; buf_p += len; } STRCPY(icon_str, buf_p); @@ -3661,17 +3659,12 @@ int build_stl_str_hl( // truncate by removing bytes from the start of the group text. if (group_len > stl_items[stl_groupitems[groupdepth]].maxwid) { // { Determine the number of bytes to remove - long n; - if (has_mbyte) { - // Find the first character that should be included. - n = 0; - while (group_len >= stl_items[stl_groupitems[groupdepth]].maxwid) { - group_len -= ptr2cells(t + n); - n += (*mb_ptr2len)(t + n); - } - } else { - n = (long)(out_p - t) - - stl_items[stl_groupitems[groupdepth]].maxwid + 1; + + // Find the first character that should be included. + long n = 0; + while (group_len >= stl_items[stl_groupitems[groupdepth]].maxwid) { + group_len -= ptr2cells(t + n); + n += (*mb_ptr2len)(t + n); } // } @@ -4183,13 +4176,10 @@ int build_stl_str_hl( // If the item is too wide, truncate it from the beginning if (l > maxwid) { - while (l >= maxwid) - if (has_mbyte) { - l -= ptr2cells(t); - t += (*mb_ptr2len)(t); - } else { - l -= byte2cells(*t++); - } + while (l >= maxwid) { + l -= ptr2cells(t); + t += utfc_ptr2len(t); + } // Early out if there isn't enough room for the truncation marker if (out_p >= out_end_p) { @@ -4372,26 +4362,19 @@ int build_stl_str_hl( // If the truncation point we found is beyond the maximum // length of the string, truncate the end of the string. if (width - vim_strsize(trunc_p) >= maxwidth) { - // If we are using a multi-byte encoding, walk from the beginning of the + // Walk from the beginning of the // string to find the last character that will fit. - if (has_mbyte) { - trunc_p = out; - width = 0; - for (;; ) { - width += ptr2cells(trunc_p); - if (width >= maxwidth) { - break; - } - - // Note: Only advance the pointer if the next - // character will fit in the available output space - trunc_p += (*mb_ptr2len)(trunc_p); + trunc_p = out; + width = 0; + for (;; ) { + width += ptr2cells(trunc_p); + if (width >= maxwidth) { + break; } - // Otherwise put the truncation point at the end, leaving enough room - // for a single-character truncation marker - } else { - trunc_p = out + maxwidth - 1; + // Note: Only advance the pointer if the next + // character will fit in the available output space + trunc_p += utfc_ptr2len(trunc_p); } // Ignore any items in the statusline that occur after @@ -4410,16 +4393,10 @@ int build_stl_str_hl( // Truncate at the truncation point we found } else { // { Determine how many bytes to remove - long trunc_len; - if (has_mbyte) { - trunc_len = 0; - while (width >= maxwidth) { - width -= ptr2cells(trunc_p + trunc_len); - trunc_len += (*mb_ptr2len)(trunc_p + trunc_len); - } - } else { - // Truncate an extra character so we can insert our `<`. - trunc_len = (width - maxwidth) + 1; + long trunc_len = 0; + while (width >= maxwidth) { + width -= ptr2cells(trunc_p + trunc_len); + trunc_len += utfc_ptr2len(trunc_p + trunc_len); } // } diff --git a/src/nvim/diff.c b/src/nvim/diff.c index b9c293f6c8..1cdf84f9d0 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -719,15 +719,12 @@ static int diff_write_buffer(buf_T *buf, diffin_T *din) for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++) { for (s = ml_get_buf(buf, lnum, false); *s != NUL; ) { if (diff_flags & DIFF_ICASE) { - int c; - - // xdiff doesn't support ignoring case, fold-case the text. - int orig_len; char_u cbuf[MB_MAXBYTES + 1]; - c = PTR2CHAR(s); + // xdiff doesn't support ignoring case, fold-case the text. + int c = PTR2CHAR(s); c = utf_fold(c); - orig_len = utfc_ptr2len(s); + const int orig_len = utfc_ptr2len(s); if (utf_char2bytes(c, cbuf) != orig_len) { // TODO(Bram): handle byte length difference memmove(ptr + len, s, orig_len); diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 9c8d64a6b2..5d44c3274e 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -426,9 +426,9 @@ static void insert_enter(InsertState *s) || curwin->w_curswant > curwin->w_virtcol) && *(s->ptr = get_cursor_line_ptr() + curwin->w_cursor.col) != NUL) { if (s->ptr[1] == NUL) { - ++curwin->w_cursor.col; - } else if (has_mbyte) { - s->i = (*mb_ptr2len)(s->ptr); + curwin->w_cursor.col++; + } else { + s->i = utfc_ptr2len(s->ptr); if (s->ptr[s->i] == NUL) { curwin->w_cursor.col += s->i; } @@ -1299,11 +1299,10 @@ normalchar: // special character. Let CTRL-] expand abbreviations without // inserting it. if (vim_iswordc(s->c) - || (!echeck_abbr( // Add ABBR_OFF for characters above 0x100, this is // what check_abbr() expects. - (has_mbyte && s->c >= 0x100) ? (s->c + ABBR_OFF) : s->c) - && s->c != Ctrl_RSB)) { + || (!echeck_abbr((s->c >= 0x100) ? (s->c + ABBR_OFF) : s->c) + && s->c != Ctrl_RSB)) { insert_special(s->c, false, false); revins_legal++; revins_chars++; @@ -1574,14 +1573,12 @@ void edit_putchar(int c, bool highlight) pc_status = PC_STATUS_UNSET; if (curwin->w_p_rl) { pc_col += curwin->w_grid.Columns - 1 - curwin->w_wcol; - if (has_mbyte) { - int fix_col = grid_fix_col(&curwin->w_grid, pc_col, pc_row); + const int fix_col = grid_fix_col(&curwin->w_grid, pc_col, pc_row); - if (fix_col != pc_col) { - grid_putchar(&curwin->w_grid, ' ', pc_row, fix_col, attr); - curwin->w_wcol--; - pc_status = PC_STATUS_RIGHT; - } + if (fix_col != pc_col) { + grid_putchar(&curwin->w_grid, ' ', pc_row, fix_col, attr); + curwin->w_wcol--; + pc_status = PC_STATUS_RIGHT; } } else { pc_col += curwin->w_wcol; @@ -1817,10 +1814,11 @@ change_indent ( ptr = get_cursor_line_ptr(); while (vcol <= (int)curwin->w_virtcol) { last_vcol = vcol; - if (has_mbyte && new_cursor_col >= 0) - new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); - else - ++new_cursor_col; + if (new_cursor_col >= 0) { + new_cursor_col += utfc_ptr2len(ptr + new_cursor_col); + } else { + new_cursor_col++; + } vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol); } vcol = last_vcol; @@ -1975,7 +1973,7 @@ void backspace_until_column(int col) /// @return true when something was deleted. static bool del_char_after_col(int limit_col) { - if (enc_utf8 && limit_col >= 0) { + if (limit_col >= 0) { colnr_T ecol = curwin->w_cursor.col + 1; // Make sure the cursor is at the start of a character, but @@ -2174,15 +2172,14 @@ int ins_compl_add_infercase(char_u *str_arg, int len, bool icase, char_u *fname, // Infer case of completed part. // Find actual length of completion. - if (has_mbyte) { + { const char_u *p = str; actual_len = 0; while (*p != NUL) { MB_PTR_ADV(p); actual_len++; } - } else - actual_len = len; + } // Find actual length of original text. { @@ -2204,11 +2201,7 @@ int ins_compl_add_infercase(char_u *str_arg, int len, bool icase, char_u *fname, { const char_u *p = str; for (i = 0; i < actual_len; i++) { - if (has_mbyte) { - wca[i] = mb_ptr2char_adv(&p); - } else { - wca[i] = *(p++); - } + wca[i] = mb_ptr2char_adv(&p); } } @@ -2216,11 +2209,7 @@ int ins_compl_add_infercase(char_u *str_arg, int len, bool icase, char_u *fname, { const char_u *p = compl_orig_text; for (i = 0; i < min_len; i++) { - if (has_mbyte) { - c = mb_ptr2char_adv(&p); - } else { - c = *(p++); - } + c = mb_ptr2char_adv(&p); if (mb_islower(c)) { has_lower = true; if (mb_isupper(wca[i])) { @@ -2241,11 +2230,7 @@ int ins_compl_add_infercase(char_u *str_arg, int len, bool icase, char_u *fname, if (!has_lower) { const char_u *p = compl_orig_text; for (i = 0; i < min_len; i++) { - if (has_mbyte) { - c = mb_ptr2char_adv(&p); - } else { - c = *(p++); - } + c = mb_ptr2char_adv(&p); if (was_letter && mb_isupper(c) && mb_islower(wca[i])) { // Rule 2 is satisfied. for (i = actual_compl_length; i < actual_len; i++) { @@ -2261,11 +2246,7 @@ int ins_compl_add_infercase(char_u *str_arg, int len, bool icase, char_u *fname, { const char_u *p = compl_orig_text; for (i = 0; i < min_len; i++) { - if (has_mbyte) { - c = mb_ptr2char_adv(&p); - } else { - c = *(p++); - } + c = mb_ptr2char_adv(&p); if (mb_islower(c)) { wca[i] = mb_tolower(wca[i]); } else if (mb_isupper(c)) { @@ -3059,12 +3040,9 @@ static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, */ char_u *find_word_start(char_u *ptr) { - if (has_mbyte) - while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) - ptr += (*mb_ptr2len)(ptr); - else - while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) - ++ptr; + while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) { + ptr += utfc_ptr2len(ptr); + } return ptr; } @@ -3074,19 +3052,15 @@ char_u *find_word_start(char_u *ptr) */ char_u *find_word_end(char_u *ptr) { - int start_class; - - if (has_mbyte) { - start_class = mb_get_class(ptr); - if (start_class > 1) - while (*ptr != NUL) { - ptr += (*mb_ptr2len)(ptr); - if (mb_get_class(ptr) != start_class) - break; + const int start_class = mb_get_class(ptr); + if (start_class > 1) { + while (*ptr != NUL) { + ptr += utfc_ptr2len(ptr); + if (mb_get_class(ptr) != start_class) { + break; } - } else - while (vim_iswordc(*ptr)) - ++ptr; + } + } return ptr; } @@ -5557,10 +5531,9 @@ static void insert_special(int c, int allow_modmask, int ctrlv) */ # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') -# define WHITECHAR(cc) (ascii_iswhite(cc) && \ - (!enc_utf8 || \ - !utf_iscomposing( \ - utf_ptr2char(get_cursor_pos_ptr() + 1)))) +#define WHITECHAR(cc) ( \ + ascii_iswhite(cc) \ + && !utf_iscomposing(utf_ptr2char(get_cursor_pos_ptr() + 1))) /* * "flags": INSCHAR_FORMAT - force formatting @@ -5697,7 +5670,7 @@ void insertchar( // Do the check for InsertCharPre before the call to vpeekc() because the // InsertCharPre autocommand could change the input buffer. if (!ISSPECIAL(c) - && (!has_mbyte || (*mb_char2len)(c) == 1) + && (utf_char2len(c) == 1) && !has_event(EVENT_INSERTCHARPRE) && vpeekc() != NUL && !(State & REPLACE_FLAG) @@ -7175,16 +7148,11 @@ static void replace_do_bs(int limit_col) getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); orig_vcols = chartabsize(get_cursor_pos_ptr(), start_vcol); } - if (has_mbyte) { - (void)del_char_after_col(limit_col); - if (l_State & VREPLACE_FLAG) - orig_len = (int)STRLEN(get_cursor_pos_ptr()); - replace_push(cc); - } else { - pchar_cursor(cc); - if (l_State & VREPLACE_FLAG) - orig_len = (int)STRLEN(get_cursor_pos_ptr()) - 1; + (void)del_char_after_col(limit_col); + if (l_State & VREPLACE_FLAG) { + orig_len = (int)STRLEN(get_cursor_pos_ptr()); } + replace_push(cc); replace_pop_ins(); if (l_State & VREPLACE_FLAG) { @@ -7403,23 +7371,17 @@ bool in_cinkeys(int keytyped, int when, bool line_is_empty) bool match = false; if (keytyped == KEY_COMPLETE) { - char_u *s; + char_u *n, *s; /* Just completed a word, check if it starts with "look". * search back for the start of a word. */ line = get_cursor_line_ptr(); - if (has_mbyte) { - char_u *n; - - for (s = line + curwin->w_cursor.col; s > line; s = n) { - n = mb_prevptr(line, s); - if (!vim_iswordp(n)) - break; + for (s = line + curwin->w_cursor.col; s > line; s = n) { + n = mb_prevptr(line, s); + if (!vim_iswordp(n)) { + break; } - } else - for (s = line + curwin->w_cursor.col; s > line; --s) - if (!vim_iswordc(s[-1])) - break; + } assert(p >= look && (uintmax_t)(p - look) <= SIZE_MAX); if (s + (p - look) <= line + curwin->w_cursor.col && (icase @@ -8255,10 +8217,8 @@ static bool ins_bs(int c, int mode, int *inserted_space_p) } cc = gchar_cursor(); // look multi-byte character class - if (has_mbyte) { - prev_cclass = cclass; - cclass = mb_get_class(get_cursor_pos_ptr()); - } + prev_cclass = cclass; + cclass = mb_get_class(get_cursor_pos_ptr()); if (mode == BACKSPACE_WORD && !ascii_isspace(cc)) { // start of word? mode = BACKSPACE_WORD_NOT_SPACE; temp = vim_iswordc(cc); @@ -8272,19 +8232,18 @@ static bool ins_bs(int c, int mode, int *inserted_space_p) } break; } - if (State & REPLACE_FLAG) + if (State & REPLACE_FLAG) { replace_do_bs(-1); - else { - const bool l_enc_utf8 = enc_utf8; + } else { const int l_p_deco = p_deco; - if (l_enc_utf8 && l_p_deco) { + if (l_p_deco) { (void)utfc_ptr2char(get_cursor_pos_ptr(), cpc); } (void)del_char(false); // If there are combining characters and 'delcombine' is set // move the cursor back. Don't back up before the base // character. - if (l_enc_utf8 && l_p_deco && cpc[0] != NUL) { + if (l_p_deco && cpc[0] != NUL) { inc_cursor(); } if (revins_chars) { @@ -8522,13 +8481,10 @@ static void ins_right(void) AppendCharToRedobuff(K_RIGHT); } curwin->w_set_curswant = true; - if (virtual_active()) + if (virtual_active()) { oneright(); - else { - if (has_mbyte) - curwin->w_cursor.col += (*mb_ptr2len)(get_cursor_pos_ptr()); - else - ++curwin->w_cursor.col; + } else { + curwin->w_cursor.col += utfc_ptr2len(get_cursor_pos_ptr()); } revins_legal++; diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 679548ab91..eee40965e0 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -783,10 +783,10 @@ static void byteidx(typval_T *argvars, typval_T *rettv, int comp) if (*t == NUL) { // EOL reached. return; } - if (enc_utf8 && comp) { + if (comp) { t += utf_ptr2len((const char_u *)t); } else { - t += (*mb_ptr2len)((const char_u *)t); + t += utfc_ptr2len((const char_u *)t); } } rettv->vval.v_number = (varnumber_T)(t - str); @@ -1427,9 +1427,7 @@ static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr) // Make sure the cursor is in a valid position. check_cursor(); // Correct cursor for multi-byte character. - if (has_mbyte) { - mb_adjust_cursor(); - } + mb_adjust_cursor(); curwin->w_set_curswant = set_curswant; rettv->vval.v_number = 0; @@ -4198,7 +4196,7 @@ static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr) } else if (STRICMP(name, "ttyout") == 0) { n = stdout_isatty; } else if (STRICMP(name, "multi_byte_encoding") == 0) { - n = has_mbyte != 0; + n = true; } else if (STRICMP(name, "syntax_items") == 0) { n = syntax_present(curwin); #ifdef UNIX @@ -8025,14 +8023,9 @@ static void f_setcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr) if ((d = argvars[0].vval.v_dict) != NULL) { char_u *const csearch = (char_u *)tv_dict_get_string(d, "char", false); if (csearch != NULL) { - if (enc_utf8) { - int pcc[MAX_MCO]; - int c = utfc_ptr2char(csearch, pcc); - set_last_csearch(c, csearch, utfc_ptr2len(csearch)); - } - else - set_last_csearch(PTR2CHAR(csearch), - csearch, utfc_ptr2len(csearch)); + int pcc[MAX_MCO]; + const int c = utfc_ptr2char(csearch, pcc); + set_last_csearch(c, csearch, utfc_ptr2len(csearch)); } di = tv_dict_find(d, S_LEN("forward")); @@ -10711,72 +10704,54 @@ static void f_tr(typval_T *argvars, typval_T *rettv, FunPtr fptr) garray_T ga; ga_init(&ga, (int)sizeof(char), 80); - if (!has_mbyte) { - // Not multi-byte: fromstr and tostr must be the same length. - if (strlen(fromstr) != strlen(tostr)) { - goto error; - } - } - // fromstr and tostr have to contain the same number of chars. bool first = true; while (*in_str != NUL) { - if (has_mbyte) { - const char *cpstr = in_str; - const int inlen = (*mb_ptr2len)((const char_u *)in_str); - int cplen = inlen; - int idx = 0; - int fromlen; - for (const char *p = fromstr; *p != NUL; p += fromlen) { - fromlen = (*mb_ptr2len)((const char_u *)p); - if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) { - int tolen; - for (p = tostr; *p != NUL; p += tolen) { - tolen = (*mb_ptr2len)((const char_u *)p); - if (idx-- == 0) { - cplen = tolen; - cpstr = (char *)p; - break; - } - } - if (*p == NUL) { // tostr is shorter than fromstr. - goto error; - } - break; - } - idx++; - } - - if (first && cpstr == in_str) { - // Check that fromstr and tostr have the same number of - // (multi-byte) characters. Done only once when a character - // of in_str doesn't appear in fromstr. - first = false; + const char *cpstr = in_str; + const int inlen = utfc_ptr2len((const char_u *)in_str); + int cplen = inlen; + int idx = 0; + int fromlen; + for (const char *p = fromstr; *p != NUL; p += fromlen) { + fromlen = utfc_ptr2len((const char_u *)p); + if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) { int tolen; - for (const char *p = tostr; *p != NUL; p += tolen) { - tolen = (*mb_ptr2len)((const char_u *)p); - idx--; + for (p = tostr; *p != NUL; p += tolen) { + tolen = utfc_ptr2len((const char_u *)p); + if (idx-- == 0) { + cplen = tolen; + cpstr = (char *)p; + break; + } } - if (idx != 0) { + if (*p == NUL) { // tostr is shorter than fromstr. goto error; } + break; } + idx++; + } - ga_grow(&ga, cplen); - memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); - ga.ga_len += cplen; - - in_str += inlen; - } else { - // When not using multi-byte chars we can do it faster. - const char *const p = strchr(fromstr, *in_str); - if (p != NULL) { - ga_append(&ga, tostr[p - fromstr]); - } else { - ga_append(&ga, *in_str); + if (first && cpstr == in_str) { + // Check that fromstr and tostr have the same number of + // (multi-byte) characters. Done only once when a character + // of in_str doesn't appear in fromstr. + first = false; + int tolen; + for (const char *p = tostr; *p != NUL; p += tolen) { + tolen = utfc_ptr2len((const char_u *)p); + idx--; + } + if (idx != 0) { + goto error; } - in_str++; } + + ga_grow(&ga, cplen); + memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); + ga.ga_len += cplen; + + in_str += inlen; } // add a terminating NUL diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 17afb33059..b8a0c3184b 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -795,10 +795,7 @@ void ex_retab(exarg_T *eap) if (ptr[col] == NUL) break; vcol += chartabsize(ptr + col, (colnr_T)vcol); - if (has_mbyte) - col += (*mb_ptr2len)(ptr + col); - else - ++col; + col += utfc_ptr2len(ptr + col); } if (new_line == NULL) /* out of memory */ break; @@ -3465,7 +3462,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, int lastone; long nmatch_tl = 0; // nr of lines matched below lnum int do_again; // do it again after joining lines - int skip_match = false; + bool skip_match = false; linenr_T sub_firstlnum; // nr of first sub line /* @@ -3576,16 +3573,13 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, if (matchcol == prev_matchcol && regmatch.endpos[0].lnum == 0 && matchcol == regmatch.endpos[0].col) { - if (sub_firstline[matchcol] == NUL) - /* We already were at the end of the line. Don't look - * for a match in this line again. */ - skip_match = TRUE; - else { - /* search for a match at next column */ - if (has_mbyte) - matchcol += mb_ptr2len(sub_firstline + matchcol); - else - ++matchcol; + if (sub_firstline[matchcol] == NUL) { + // We already were at the end of the line. Don't look + // for a match in this line again. + skip_match = true; + } else { + // search for a match at next column + matchcol += mb_ptr2len(sub_firstline + matchcol); } // match will be pushed to preview_lines, bring it into a proper state current_match.start.col = matchcol; @@ -3609,7 +3603,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, if (nmatch > 1) { matchcol = (colnr_T)STRLEN(sub_firstline); nmatch = 1; - skip_match = TRUE; + skip_match = true; } sub_nsubs++; did_sub = TRUE; @@ -3779,7 +3773,7 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, * get stuck when pressing 'n'. */ if (nmatch > 1) { matchcol = (colnr_T)STRLEN(sub_firstline); - skip_match = TRUE; + skip_match = true; } goto skip; } @@ -3956,8 +3950,8 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, STRMOVE(new_start, p1 + 1); p1 = new_start - 1; } - } else if (has_mbyte) { - p1 += (*mb_ptr2len)(p1) - 1; + } else { + p1 += utfc_ptr2len(p1) - 1; } } size_t new_endcol = STRLEN(new_start); diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index a491a9d377..fc699e8826 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -2505,12 +2505,8 @@ static void append_command(char_u *cmd) STRCAT(IObuff, ": "); d = IObuff + STRLEN(IObuff); while (*s != NUL && d - IObuff < IOSIZE - 7) { - if ( - enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : - *s == 0xa0) { - s += - enc_utf8 ? 2 : - 1; + if (s[0] == 0xc2 && s[1] == 0xa0) { + s += 2; STRCPY(d, "<a0>"); d += 4; } else @@ -5564,7 +5560,8 @@ static char_u *uc_split_args(char_u *arg, size_t *lenp) break; len += 3; /* "," */ } else { - int charlen = (*mb_ptr2len)(p); + const int charlen = utfc_ptr2len(p); + len += charlen; p += charlen; } @@ -8266,12 +8263,10 @@ static void ex_normal(exarg_T *eap) return; } - /* - * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do - * this for the K_SPECIAL leading byte, otherwise special keys will not - * work. - */ - if (has_mbyte) { + // vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do + // this for the K_SPECIAL leading byte, otherwise special keys will not + // work. + { int len = 0; /* Count the number of characters to be escaped. */ @@ -8310,9 +8305,8 @@ static void ex_normal(exarg_T *eap) check_cursor_moved(curwin); } - exec_normal_cmd( - arg != NULL ? arg : - eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE); + exec_normal_cmd(arg != NULL ? arg : eap->arg, + eap->forceit ? REMAP_NONE : REMAP_YES, false); } while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int); } diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 53feffd2d7..9edb826ea6 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2096,7 +2096,7 @@ static int command_line_handle_key(CommandLineState *s) s->do_abbr = false; // don't do abbreviation now ccline.special_char = NUL; // may need to remove ^ when composing char was typed - if (enc_utf8 && utf_iscomposing(s->c) && !cmd_silent) { + if (utf_iscomposing(s->c) && !cmd_silent) { if (ui_has(kUICmdline)) { // TODO(bfredl): why not make unputcmdline also work with true? unputcmdline(); @@ -2143,9 +2143,8 @@ static int command_line_handle_key(CommandLineState *s) if (s->do_abbr && (IS_SPECIAL(s->c) || !vim_iswordc(s->c)) // Add ABBR_OFF for characters above 0x100, this is // what check_abbr() expects. - && (ccheck_abbr((has_mbyte && s->c >= 0x100) ? - (s->c + ABBR_OFF) : s->c) - || s->c == Ctrl_RSB)) { + && (ccheck_abbr((s->c >= 0x100) ? (s->c + ABBR_OFF) : s->c) + || s->c == Ctrl_RSB)) { return command_line_changed(s); } @@ -2254,7 +2253,7 @@ static int command_line_changed(CommandLineState *s) may_do_incsearch_highlighting(s->firstc, s->count, &s->is_state); } - if (cmdmsg_rl || (p_arshape && !p_tbidi && enc_utf8)) { + if (cmdmsg_rl || (p_arshape && !p_tbidi)) { // Always redraw the whole command line to fix shaping and // right-left typing. Not efficient, but it works. // Do it only when there are no characters left to read @@ -3139,11 +3138,9 @@ static void draw_cmdline(int start, int len) if (cmdline_star > 0) { for (int i = 0; i < len; i++) { msg_putchar('*'); - if (has_mbyte) { - i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; - } + i += utfc_ptr2len(ccline.cmdbuff + start + i) - 1; } - } else if (p_arshape && !p_tbidi && enc_utf8 && len > 0) { + } else if (p_arshape && !p_tbidi && len > 0) { bool do_arabicshape = false; int mb_l; for (int i = start; i < start + len; i += mb_l) { @@ -3439,32 +3436,31 @@ void put_on_cmdline(char_u *str, int len, int redraw) (size_t)(ccline.cmdlen - ccline.cmdpos)); ccline.cmdlen += len; } else { - if (has_mbyte) { - /* Count nr of characters in the new string. */ - m = 0; - for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) - ++m; - /* Count nr of bytes in cmdline that are overwritten by these - * characters. */ - for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; - i += (*mb_ptr2len)(ccline.cmdbuff + i)) - --m; - if (i < ccline.cmdlen) { - memmove(ccline.cmdbuff + ccline.cmdpos + len, - ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); - ccline.cmdlen += ccline.cmdpos + len - i; - } else - ccline.cmdlen = ccline.cmdpos + len; - } else if (ccline.cmdpos + len > ccline.cmdlen) + // Count nr of characters in the new string. + m = 0; + for (i = 0; i < len; i += utfc_ptr2len(str + i)) { + m++; + } + // Count nr of bytes in cmdline that are overwritten by these + // characters. + for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; + i += utfc_ptr2len(ccline.cmdbuff + i)) { + m--; + } + if (i < ccline.cmdlen) { + memmove(ccline.cmdbuff + ccline.cmdpos + len, + ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); + ccline.cmdlen += ccline.cmdpos + len - i; + } else { ccline.cmdlen = ccline.cmdpos + len; + } } memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); ccline.cmdbuff[ccline.cmdlen] = NUL; - if (enc_utf8) { - /* When the inserted text starts with a composing character, - * backup to the character before it. There could be two of them. - */ + { + // When the inserted text starts with a composing character, + // backup to the character before it. There could be two of them. i = 0; c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); while (ccline.cmdpos > 0 && utf_iscomposing(c)) { @@ -3515,23 +3511,19 @@ void put_on_cmdline(char_u *str, int len, int redraw) for (i = 0; i < len; i++) { c = cmdline_charsize(ccline.cmdpos); // count ">" for a double-wide char that doesn't fit. - if (has_mbyte) { - correct_screencol(ccline.cmdpos, c, &ccline.cmdspos); - } + correct_screencol(ccline.cmdpos, c, &ccline.cmdspos); // Stop cursor at the end of the screen, but do increment the // insert position, so that entering a very long command // works, even though you can't see it. if (ccline.cmdspos + c < m) { ccline.cmdspos += c; } - if (has_mbyte) { - c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; - if (c > len - i - 1) { - c = len - i - 1; - } - ccline.cmdpos += c; - i += c; + c = utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1; + if (c > len - i - 1) { + c = len - i - 1; } + ccline.cmdpos += c; + i += c; ccline.cmdpos++; } @@ -3676,11 +3668,7 @@ void cmdline_paste_str(char_u *s, int literally) if (cv == Ctrl_V && s[1]) { s++; } - if (has_mbyte) { - c = mb_cptr2char_adv((const char_u **)&s); - } else { - c = *s++; - } + c = mb_cptr2char_adv((const char_u **)&s); if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL || c == Ctrl_L || (c == Ctrl_BSL && *s == Ctrl_N)) { diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index e349f00fba..49de0f3bf8 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -910,20 +910,18 @@ retry: /* "ucs-bom" means we need to check the first bytes of the file * for a BOM. */ - if (STRCMP(fenc, ENC_UCSBOM) == 0) + if (STRCMP(fenc, ENC_UCSBOM) == 0) { fio_flags = FIO_UCSBOM; - - /* - * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be - * done. This is handled below after read(). Prepare the - * fio_flags to avoid having to parse the string each time. - * Also check for Unicode to Latin1 conversion, because iconv() - * appears not to handle this correctly. This works just like - * conversion to UTF-8 except how the resulting character is put in - * the buffer. - */ - else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) + } else { + // Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be + // done. This is handled below after read(). Prepare the + // fio_flags to avoid having to parse the string each time. + // Also check for Unicode to Latin1 conversion, because iconv() + // appears not to handle this correctly. This works just like + // conversion to UTF-8 except how the resulting character is put in + // the buffer. fio_flags = get_fio_flags(fenc); + } @@ -932,8 +930,7 @@ retry: if (fio_flags == 0 && !did_iconv ) { - iconv_fd = (iconv_t)my_iconv_open( - enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); + iconv_fd = (iconv_t)my_iconv_open((char_u *)"utf-8", fenc); } # endif @@ -1202,7 +1199,7 @@ retry: && (fio_flags == FIO_UCSBOM || (!curbuf->b_p_bomb && tmpname == NULL - && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) { + && (*fenc == 'u' || *fenc == NUL)))) { char_u *ccname; int blen; @@ -1468,8 +1465,8 @@ retry: memmove(line_start, buffer, (size_t)linerest); size = (long)((ptr + real_size) - dest); ptr = dest; - } else if (enc_utf8 && !curbuf->b_p_bin) { - int incomplete_tail = FALSE; + } else if (!curbuf->b_p_bin) { + bool incomplete_tail = false; // Reading UTF-8: Check if the bytes are valid UTF-8. for (p = ptr;; p++) { @@ -1486,15 +1483,16 @@ retry: // then. l = utf_ptr2len_len(p, todo); if (l > todo && !incomplete_tail) { - /* Avoid retrying with a different encoding when - * a truncated file is more likely, or attempting - * to read the rest of an incomplete sequence when - * we have already done so. */ - if (p > ptr || filesize > 0) - incomplete_tail = TRUE; - /* Incomplete byte sequence, move it to conv_rest[] - * and try to read the rest of it, unless we've - * already done so. */ + // Avoid retrying with a different encoding when + // a truncated file is more likely, or attempting + // to read the rest of an incomplete sequence when + // we have already done so. + if (p > ptr || filesize > 0) { + incomplete_tail = true; + } + // Incomplete byte sequence, move it to conv_rest[] + // and try to read the rest of it, unless we've + // already done so. if (p > ptr) { conv_restlen = todo; memmove(conv_rest, p, conv_restlen); @@ -2165,8 +2163,8 @@ readfile_charconvert ( else { close(*fdp); /* close the input file, ignore errors */ *fdp = -1; - if (eval_charconvert((char *) fenc, enc_utf8 ? "utf-8" : (char *) p_enc, - (char *) fname, (char *) tmpname) == FAIL) { + if (eval_charconvert((char *)fenc, "utf-8", + (char *)fname, (char *)tmpname) == FAIL) { errmsg = (char_u *)_("Conversion with 'charconvert' failed"); } if (errmsg == NULL && (*fdp = os_open((char *)tmpname, O_RDONLY, 0)) < 0) { @@ -3067,7 +3065,7 @@ nobackup: // Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or // Latin1 to Unicode conversion. This is handled in buf_write_bytes(). // Prepare the flags for it and allocate bw_conv_buf when needed. - if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) { + if (converted) { wb_flags = get_fio_flags(fenc); if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) { // Need to allocate a buffer to translate into. @@ -3089,8 +3087,7 @@ nobackup: # ifdef HAVE_ICONV // Use iconv() conversion when conversion is needed and it's not done // internally. - write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, - enc_utf8 ? (char_u *)"utf-8" : p_enc); + write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, (char_u *)"utf-8"); if (write_info.bw_iconv_fd != (iconv_t)-1) { /* We're going to use iconv(), allocate a buffer to convert in. */ write_info.bw_conv_buflen = bufsize * ICONV_MULT; @@ -3433,7 +3430,7 @@ restore_backup: // The file was written to a temp file, now it needs to be converted // with 'charconvert' to (overwrite) the output file. if (end != 0) { - if (eval_charconvert(enc_utf8 ? "utf-8" : (char *)p_enc, (char *)fenc, + if (eval_charconvert("utf-8", (char *)fenc, (char *)wfname, (char *)fname) == FAIL) { write_info.bw_conv_error = true; end = 0; @@ -4189,7 +4186,7 @@ static bool need_conversion(const char_u *fenc) /* Encodings differ. However, conversion is not needed when 'enc' is any * Unicode encoding and the file is UTF-8. */ - return !(enc_utf8 && fenc_flags == FIO_UTF8); + return !(fenc_flags == FIO_UTF8); } /// Return the FIO_ flags needed for the internal conversion if 'name' was diff --git a/src/nvim/fold.c b/src/nvim/fold.c index 24a73a5b9f..5e28ca6538 100644 --- a/src/nvim/fold.c +++ b/src/nvim/fold.c @@ -1037,11 +1037,11 @@ void foldAdjustVisual(void) if (hasFolding(end->lnum, NULL, &end->lnum)) { ptr = ml_get(end->lnum); end->col = (colnr_T)STRLEN(ptr); - if (end->col > 0 && *p_sel == 'o') - --end->col; - /* prevent cursor from moving on the trail byte */ - if (has_mbyte) - mb_adjust_cursor(); + if (end->col > 0 && *p_sel == 'o') { + end->col--; + } + // prevent cursor from moving on the trail byte + mb_adjust_cursor(); } } |