patch 8.2.5034: there is no way to get the byte index from a virtual column
Problem: There is no way to get the byte index from a virtual column.
Solution: Add virtcol2col(). (Yegappan Lakshmanan, closes #10477,
closes #10098)
diff --git a/src/evalfunc.c b/src/evalfunc.c
index a5300a1..75c3456 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -2682,6 +2682,8 @@
ret_list_any, f_values},
{"virtcol", 1, 2, FEARG_1, arg2_string_or_list_bool,
ret_virtcol, f_virtcol},
+ {"virtcol2col", 3, 3, FEARG_1, arg3_number,
+ ret_number, f_virtcol2col},
{"visualmode", 0, 1, 0, arg1_bool,
ret_string, f_visualmode},
{"wildmenumode", 0, 0, 0, NULL,
diff --git a/src/move.c b/src/move.c
index 58bd97f..5c78d6c 100644
--- a/src/move.c
+++ b/src/move.c
@@ -1322,6 +1322,39 @@
dict_add_number(dict, "curscol", ccol);
dict_add_number(dict, "endcol", ecol);
}
+
+/*
+ * "virtcol2col({winid}, {lnum}, {col})" function
+ */
+ void
+f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
+{
+ win_T *wp;
+ linenr_T lnum;
+ int screencol;
+ int error = FALSE;
+
+ rettv->vval.v_number = -1;
+
+ if (check_for_number_arg(argvars, 0) == FAIL
+ || check_for_number_arg(argvars, 1) == FAIL
+ || check_for_number_arg(argvars, 2) == FAIL)
+ return;
+
+ wp = find_win_by_nr_or_id(&argvars[0]);
+ if (wp == NULL)
+ return;
+
+ lnum = tv_get_number_chk(&argvars[1], &error);
+ if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
+ return;
+
+ screencol = tv_get_number_chk(&argvars[2], &error);
+ if (error || screencol < 0)
+ return;
+
+ rettv->vval.v_number = vcol2col(wp, lnum, screencol);
+}
#endif
/*
diff --git a/src/proto/move.pro b/src/proto/move.pro
index e6a0601..3dd6ec1 100644
--- a/src/proto/move.pro
+++ b/src/proto/move.pro
@@ -30,6 +30,7 @@
void curs_columns(int may_scroll);
void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, int *ecolp);
void f_screenpos(typval_T *argvars, typval_T *rettv);
+void f_virtcol2col(typval_T *argvars, typval_T *rettv);
void scrolldown(long line_count, int byfold);
void scrollup(long line_count, int byfold);
void check_topfill(win_T *wp, int down);
diff --git a/src/testdir/test_cursor_func.vim b/src/testdir/test_cursor_func.vim
index 1c26f6d..d5f0ac7 100644
--- a/src/testdir/test_cursor_func.vim
+++ b/src/testdir/test_cursor_func.vim
@@ -419,4 +419,26 @@
%bw!
endfunc
+" Test for virtcol2col()
+func Test_virtcol2col()
+ new
+ call setline(1, ["a\tb\tc"])
+ call assert_equal(1, virtcol2col(0, 1, 1))
+ call assert_equal(2, virtcol2col(0, 1, 2))
+ call assert_equal(2, virtcol2col(0, 1, 8))
+ call assert_equal(3, virtcol2col(0, 1, 9))
+ call assert_equal(4, virtcol2col(0, 1, 10))
+ call assert_equal(4, virtcol2col(0, 1, 16))
+ call assert_equal(5, virtcol2col(0, 1, 17))
+ call assert_equal(-1, virtcol2col(10, 1, 1))
+ call assert_equal(-1, virtcol2col(0, 10, 1))
+ call assert_equal(-1, virtcol2col(0, -1, 1))
+ call assert_equal(-1, virtcol2col(0, 1, -1))
+ call assert_equal(5, virtcol2col(0, 1, 20))
+ 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:')
+ bw!
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index 0d666a0..adfd87b 100644
--- a/src/version.c
+++ b/src/version.c
@@ -735,6 +735,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 5034,
+/**/
5033,
/**/
5032,