Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions runtime/doc/builtin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ uniq({list} [, {func} [, {dict}]])
values({dict}) List values in {dict}
virtcol({expr} [, {list}]) Number or List
screen column of cursor or mark
virtcol2col({winid}, {lnum}, {col})
Number byte index of a character on screen
visualmode([expr]) String last visual mode used
wildmenumode() Number whether 'wildmenu' mode is active
win_execute({id}, {command} [, {silent}])
Expand Down Expand Up @@ -9788,6 +9790,25 @@ virtcol({expr} [, {list}]) *virtcol()*
< Can also be used as a |method|: >
GetPos()->virtcol()

virtcol2col({winid}, {lnum}, {col}) *virtcol2col()*
The result is a Number, which is the byte index of the
character in window {winid} at buffer line {lnum} and virtual
column {col}.

If {col} is greater than the last virtual column in line
{lnum}, then the byte index of the last virtual column is
returned.

The {winid} argument can be the window number or the
|window-ID|. If this is zero, then the current window is used.

Returns -1 if the window {winid} doesn't exist or the buffer
line {lnum} or virtual column {col} is invalid.

See also |screenpos()|, |virtcol()| and |col()|.

Can also be used as a |method|: >
GetWinid()->virtcol2col(lnum, col)

visualmode([{expr}]) *visualmode()*
The result is a String, which describes the last Visual mode
Expand Down
1 change: 1 addition & 0 deletions runtime/doc/usr_41.txt
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ Cursor and mark position: *cursor-functions* *mark-functions*
screencol() get screen column of the cursor
screenrow() get screen row of the cursor
screenpos() screen row and col of a text character
virtcol2col() byte index of a text character on screen
getcurpos() get position of the cursor
getpos() get position of cursor, mark, etc.
setpos() set position of cursor, mark, etc.
Expand Down
2 changes: 2 additions & 0 deletions src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2682,6 +2682,8 @@ static funcentry_T global_functions[] =
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,
Expand Down
33 changes: 33 additions & 0 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,39 @@ f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
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

/*
Expand Down
1 change: 1 addition & 0 deletions src/proto/move.pro
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ int curwin_col_off2(void);
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);
Expand Down
22 changes: 22 additions & 0 deletions src/testdir/test_cursor_func.vim
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,26 @@ func Test_setcursorcharpos()
%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