• R/O
  • SSH

vim: Commit

Mirror of the Vim source from https://github.com/vim/vim


Commit MetaInfo

Revisión9f25e0ed831d4f1268c84dd8832b4138d78d7625 (tree)
Tiempo2022-05-28 06:00:03
AutorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Log Message

patch 8.2.5034: there is no way to get the byte index from a virtual column

Commit: https://github.com/vim/vim/commit/5a6ec10cc80ab02eeff644ab19b82312630ea855
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri May 27 21:58:00 2022 +0100

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 https://github.com/vim/vim/issues/10477,
closes #10098)

Cambiar Resumen

Diferencia incremental

diff -r b7960e26a397 -r 9f25e0ed831d runtime/doc/builtin.txt
--- a/runtime/doc/builtin.txt Fri May 27 22:30:03 2022 +0200
+++ b/runtime/doc/builtin.txt Fri May 27 23:00:03 2022 +0200
@@ -691,6 +691,8 @@
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 @@
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 @@
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 @@
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 <
diff -r b7960e26a397 -r 9f25e0ed831d runtime/doc/usr_41.txt
--- a/runtime/doc/usr_41.txt Fri May 27 22:30:03 2022 +0200
+++ b/runtime/doc/usr_41.txt Fri May 27 23:00:03 2022 +0200
@@ -835,6 +835,7 @@
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.
diff -r b7960e26a397 -r 9f25e0ed831d src/evalfunc.c
--- a/src/evalfunc.c Fri May 27 22:30:03 2022 +0200
+++ b/src/evalfunc.c Fri May 27 23:00:03 2022 +0200
@@ -2682,6 +2682,8 @@
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,
diff -r b7960e26a397 -r 9f25e0ed831d src/move.c
--- a/src/move.c Fri May 27 22:30:03 2022 +0200
+++ b/src/move.c Fri May 27 23:00:03 2022 +0200
@@ -1322,6 +1322,39 @@
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 /*
diff -r b7960e26a397 -r 9f25e0ed831d src/proto/move.pro
--- a/src/proto/move.pro Fri May 27 22:30:03 2022 +0200
+++ b/src/proto/move.pro Fri May 27 23:00:03 2022 +0200
@@ -30,6 +30,7 @@
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);
diff -r b7960e26a397 -r 9f25e0ed831d src/testdir/test_cursor_func.vim
--- a/src/testdir/test_cursor_func.vim Fri May 27 22:30:03 2022 +0200
+++ b/src/testdir/test_cursor_func.vim Fri May 27 23:00:03 2022 +0200
@@ -419,4 +419,26 @@
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
diff -r b7960e26a397 -r 9f25e0ed831d src/version.c
--- a/src/version.c Fri May 27 22:30:03 2022 +0200
+++ b/src/version.c Fri May 27 23:00:03 2022 +0200
@@ -735,6 +735,8 @@
735735 static int included_patches[] =
736736 { /* Add new patch number below this line */
737737 /**/
738+ 5034,
739+/**/
738740 5033,
739741 /**/
740742 5032,
Show on old repository browser