Skip to content

Commit 5a6ec10

Browse files
committed
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)
1 parent 968443e commit 5a6ec10

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

runtime/doc/builtin.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ uniq({list} [, {func} [, {dict}]])
691691
values({dict}) List values in {dict}
692692
virtcol({expr} [, {list}]) Number or List
693693
screen column of cursor or mark
694+
virtcol2col({winid}, {lnum}, {col})
695+
Number byte index of a character on screen
694696
visualmode([expr]) String last visual mode used
695697
wildmenumode() Number whether 'wildmenu' mode is active
696698
win_execute({id}, {command} [, {silent}])
@@ -6211,11 +6213,17 @@ nr2char({expr} [, {utf8}]) *nr2char()*
62116213
or({expr}, {expr}) *or()*
62126214
Bitwise OR on the two arguments. The arguments are converted
62136215
to a number. A List, Dict or Float argument causes an error.
6216+
Also see `and()` and `xor()`.
62146217
Example: >
62156218
:let bits = or(bits, 0x80)
62166219
< Can also be used as a |method|: >
62176220
:let bits = bits->or(0x80)
62186221
6222+
< Rationale: The reason this is a function and not using the "|"
6223+
character like many languages, is that Vi has always used "|"
6224+
to separate commands. In many places it would not be clear if
6225+
"|" is an operator or a command separator.
6226+
62196227

62206228
pathshorten({path} [, {len}]) *pathshorten()*
62216229
Shorten directory names in the path {path} and return the
@@ -9788,6 +9796,25 @@ virtcol({expr} [, {list}]) *virtcol()*
97889796
< Can also be used as a |method|: >
97899797
GetPos()->virtcol()
97909798
9799+
virtcol2col({winid}, {lnum}, {col}) *virtcol2col()*
9800+
The result is a Number, which is the byte index of the
9801+
character in window {winid} at buffer line {lnum} and virtual
9802+
column {col}.
9803+
9804+
If {col} is greater than the last virtual column in line
9805+
{lnum}, then the byte index of the character at the last
9806+
virtual column is returned.
9807+
9808+
The {winid} argument can be the window number or the
9809+
|window-ID|. If this is zero, then the current window is used.
9810+
9811+
Returns -1 if the window {winid} doesn't exist or the buffer
9812+
line {lnum} or virtual column {col} is invalid.
9813+
9814+
See also |screenpos()|, |virtcol()| and |col()|.
9815+
9816+
Can also be used as a |method|: >
9817+
GetWinid()->virtcol2col(lnum, col)
97919818
97929819
visualmode([{expr}]) *visualmode()*
97939820
The result is a String, which describes the last Visual mode
@@ -10220,6 +10247,7 @@ writefile({object}, {fname} [, {flags}])
1022010247
xor({expr}, {expr}) *xor()*
1022110248
Bitwise XOR on the two arguments. The arguments are converted
1022210249
to a number. A List, Dict or Float argument causes an error.
10250+
Also see `and()` and `or()`.
1022310251
Example: >
1022410252
:let bits = xor(bits, 0x80)
1022510253
<

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ Cursor and mark position: *cursor-functions* *mark-functions*
835835
screencol() get screen column of the cursor
836836
screenrow() get screen row of the cursor
837837
screenpos() screen row and col of a text character
838+
virtcol2col() byte index of a text character on screen
838839
getcurpos() get position of the cursor
839840
getpos() get position of cursor, mark, etc.
840841
setpos() set position of cursor, mark, etc.

src/evalfunc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,8 @@ static funcentry_T global_functions[] =
26822682
ret_list_any, f_values},
26832683
{"virtcol", 1, 2, FEARG_1, arg2_string_or_list_bool,
26842684
ret_virtcol, f_virtcol},
2685+
{"virtcol2col", 3, 3, FEARG_1, arg3_number,
2686+
ret_number, f_virtcol2col},
26852687
{"visualmode", 0, 1, 0, arg1_bool,
26862688
ret_string, f_visualmode},
26872689
{"wildmenumode", 0, 0, 0, NULL,

src/move.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,39 @@ f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
13221322
dict_add_number(dict, "curscol", ccol);
13231323
dict_add_number(dict, "endcol", ecol);
13241324
}
1325+
1326+
/*
1327+
* "virtcol2col({winid}, {lnum}, {col})" function
1328+
*/
1329+
void
1330+
f_virtcol2col(typval_T *argvars UNUSED, typval_T *rettv)
1331+
{
1332+
win_T *wp;
1333+
linenr_T lnum;
1334+
int screencol;
1335+
int error = FALSE;
1336+
1337+
rettv->vval.v_number = -1;
1338+
1339+
if (check_for_number_arg(argvars, 0) == FAIL
1340+
|| check_for_number_arg(argvars, 1) == FAIL
1341+
|| check_for_number_arg(argvars, 2) == FAIL)
1342+
return;
1343+
1344+
wp = find_win_by_nr_or_id(&argvars[0]);
1345+
if (wp == NULL)
1346+
return;
1347+
1348+
lnum = tv_get_number_chk(&argvars[1], &error);
1349+
if (error || lnum < 0 || lnum > wp->w_buffer->b_ml.ml_line_count)
1350+
return;
1351+
1352+
screencol = tv_get_number_chk(&argvars[2], &error);
1353+
if (error || screencol < 0)
1354+
return;
1355+
1356+
rettv->vval.v_number = vcol2col(wp, lnum, screencol);
1357+
}
13251358
#endif
13261359

13271360
/*

src/proto/move.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int curwin_col_off2(void);
3030
void curs_columns(int may_scroll);
3131
void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, int *ecolp);
3232
void f_screenpos(typval_T *argvars, typval_T *rettv);
33+
void f_virtcol2col(typval_T *argvars, typval_T *rettv);
3334
void scrolldown(long line_count, int byfold);
3435
void scrollup(long line_count, int byfold);
3536
void check_topfill(win_T *wp, int down);

src/testdir/test_cursor_func.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,26 @@ func Test_setcursorcharpos()
419419
%bw!
420420
endfunc
421421

422+
" Test for virtcol2col()
423+
func Test_virtcol2col()
424+
new
425+
call setline(1, ["a\tb\tc"])
426+
call assert_equal(1, virtcol2col(0, 1, 1))
427+
call assert_equal(2, virtcol2col(0, 1, 2))
428+
call assert_equal(2, virtcol2col(0, 1, 8))
429+
call assert_equal(3, virtcol2col(0, 1, 9))
430+
call assert_equal(4, virtcol2col(0, 1, 10))
431+
call assert_equal(4, virtcol2col(0, 1, 16))
432+
call assert_equal(5, virtcol2col(0, 1, 17))
433+
call assert_equal(-1, virtcol2col(10, 1, 1))
434+
call assert_equal(-1, virtcol2col(0, 10, 1))
435+
call assert_equal(-1, virtcol2col(0, -1, 1))
436+
call assert_equal(-1, virtcol2col(0, 1, -1))
437+
call assert_equal(5, virtcol2col(0, 1, 20))
438+
call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
439+
call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
440+
call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
441+
bw!
442+
endfunc
443+
422444
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
5034,
737739
/**/
738740
5033,
739741
/**/

0 commit comments

Comments
 (0)