vim-patch:9.0.1717: virtcol2col returns last byte of a multi-byte char (#24729)

Problem: virtcol2col returns last byte of a multi-byte char
Solution: Make it return the first byte for a multi-byte char

closes: vim/vim#12786
closes: vim/vim#12799

b209b86e66

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq
2023-08-16 06:28:52 +08:00
committed by GitHub
parent 10c5f35a8d
commit a245dd79a2
5 changed files with 34 additions and 1 deletions

View File

@@ -11931,6 +11931,9 @@ M.funcs = {
{lnum}, then the byte index of the character at the last
virtual column is returned.
For a multi-byte character, the column number of the first
byte in the character is returned.
The {winid} argument can be the window number or the
|window-ID|. If this is zero, then the current window is used.

View File

@@ -1163,6 +1163,21 @@ void f_screenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
tv_dict_add_nr(dict, S_LEN("endcol"), ecol);
}
/// Convert a virtual (screen) column to a character column. The first column
/// is one. For a multibyte character, the column number of the first byte is
/// returned.
static int virtcol2col(win_T *wp, linenr_T lnum, int vcol)
{
int offset = vcol2col(wp, lnum, vcol);
char *line = ml_get_buf(wp->w_buffer, lnum, false);
char *p = line + offset;
// For a multibyte character, need to return the column number of the first byte.
MB_PTR_BACK(line, p);
return (int)(p - line + 1);
}
/// "virtcol2col({winid}, {lnum}, {col})" function
void f_virtcol2col(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
@@ -1190,7 +1205,7 @@ void f_virtcol2col(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
return;
}
rettv->vval.v_number = vcol2col(wp, lnum, screencol);
rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
}
/// Scroll the current window down by "line_count" logical lines. "CTRL-Y"