patch 9.0.1717: virtcol2col returns last byte of a multi-byte char
Problem: virtcol2col returns last byte of a multi-byte char
Solution: Make it return the first byte for a multi-byte char
closes: #12786
closes: #12799
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
diff --git a/src/move.c b/src/move.c
index 22c37ce..94dc7dc 100644
--- a/src/move.c
+++ b/src/move.c
@@ -1557,6 +1557,25 @@
}
/*
+ * 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_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
+ char_u *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
@@ -1586,7 +1605,7 @@
if (error || screencol < 0)
return;
- rettv->vval.v_number = vcol2col(wp, lnum, screencol);
+ rettv->vval.v_number = virtcol2col(wp, lnum, screencol);
}
#endif
diff --git a/src/testdir/test_cursor_func.vim b/src/testdir/test_cursor_func.vim
index 74d7581..2ae7580 100644
--- a/src/testdir/test_cursor_func.vim
+++ b/src/testdir/test_cursor_func.vim
@@ -531,6 +531,15 @@
call assert_equal(-1, virtcol2col(0, -1, 1))
call assert_equal(-1, virtcol2col(0, 1, -1))
call assert_equal(5, virtcol2col(0, 1, 20))
+
+ " Multibyte character
+ call setline(1, ['a✅✅✅'])
+ call assert_equal(1, virtcol2col(0, 1, 1))
+ call assert_equal(2, virtcol2col(0, 1, 3))
+ call assert_equal(5, virtcol2col(0, 1, 5))
+ call assert_equal(8, virtcol2col(0, 1, 7))
+ call assert_equal(8, virtcol2col(0, 1, 8))
+
call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
diff --git a/src/version.c b/src/version.c
index 80bd77d..d5d1855 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1717,
+/**/
1716,
/**/
1715,