]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0388: cursor() and getregion() don't handle v:maxcol well v9.1.0388
authorzeertzjq <zeertzjq@outlook.com>
Thu, 2 May 2024 11:06:24 +0000 (13:06 +0200)
committerChristian Brabandt <cb@256bit.org>
Thu, 2 May 2024 11:06:24 +0000 (13:06 +0200)
Problem:  cursor() and getregion() don't handle v:maxcol well.
Solution: Add special handling for v:maxcol like setpos() does.
          (zeertzjq)

closes: #14698

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/evalfunc.c
src/testdir/test_virtualedit.vim
src/testdir/test_visual.vim
src/version.c

index 8ee5cbddd5f63c7500813ad458091de0f9c27127..7c7a202439471a5feced97ad0b1b3d786828bd1d 100644 (file)
@@ -3840,8 +3840,9 @@ set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
        return;         // type error; errmsg already given
     if (lnum > 0)
        curwin->w_cursor.lnum = lnum;
-    if (col > 0)
-       curwin->w_cursor.col = col - 1;
+    if (col != MAXCOL && --col < 0)
+       col = 0;
+    curwin->w_cursor.col = col;
     curwin->w_cursor.coladd = coladd;
 
     // Make sure the cursor is in a valid position.
@@ -5554,17 +5555,22 @@ f_getregion(typval_T *argvars, typval_T *rettv)
        semsg(_(e_invalid_line_number_nr), p1.lnum);
        return;
     }
-    if (p1.col < 1 || p1.col > ml_get_buf_len(findbuf, p1.lnum) + 1)
+    if (p1.col == MAXCOL)
+       p1.col = ml_get_buf_len(findbuf, p1.lnum) + 1;
+    else if (p1.col < 1 || p1.col > ml_get_buf_len(findbuf, p1.lnum) + 1)
     {
        semsg(_(e_invalid_column_number_nr), p1.col);
        return;
     }
+
     if (p2.lnum < 1 || p2.lnum > findbuf->b_ml.ml_line_count)
     {
        semsg(_(e_invalid_line_number_nr), p2.lnum);
        return;
     }
-    if (p2.col < 1 || p2.col > ml_get_buf_len(findbuf, p2.lnum) + 1)
+    if (p2.col == MAXCOL)
+       p2.col = ml_get_buf_len(findbuf, p2.lnum) + 1;
+    else if (p2.col < 1 || p2.col > ml_get_buf_len(findbuf, p2.lnum) + 1)
     {
        semsg(_(e_invalid_column_number_nr), p2.col);
        return;
index 48d4aa0616432169b9e01461e5425a5dfe7705af..3d3fdd0becf6a4ec69d78ed43432c6cc6539d767 100644 (file)
@@ -709,5 +709,27 @@ func Test_virtualedit_replace_after_tab()
   bwipe!
 endfunc
 
+" Test that setpos('.') and cursor() behave the same for v:maxcol
+func Test_virtualedit_set_cursor_pos_maxcol()
+  new
+  set virtualedit=all
+
+  call setline(1, 'foobar')
+  exe "normal! V\<Esc>"
+  call assert_equal([0, 1, 1, 0], getpos("'<"))
+  call assert_equal([0, 1, v:maxcol, 0], getpos("'>"))
+  let pos = getpos("'>")
+
+  call cursor(1, 1)
+  call setpos('.', pos)
+  call assert_equal([0, 1, 7, 0], getpos('.'))
+
+  call cursor(1, 1)
+  call cursor(pos[1:])
+  call assert_equal([0, 1, 7, 0], getpos('.'))
+
+  set virtualedit&
+  bwipe!
+endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab
index adcc3e912708d12f6fa65cbe33a77e088bd60e05..dff6150135ca840e62078b76f4c279f2ac8a889e 100644 (file)
@@ -1698,6 +1698,9 @@ func Test_visual_getregion()
           \ "'a"->getpos()->getregion(getpos("'a"), {'type': 'V' }))
     call assert_equal(['one', 'two'],
           \ "."->getpos()->getregion(getpos("'a"), {'type': "\<c-v>" }))
+    call feedkeys("\<ESC>jVj\<ESC>", 'tx')
+    call assert_equal(['two', 'three'], getregion(getpos("'<"), getpos("'>")))
+    call assert_equal(['two', 'three'], getregion(getpos("'>"), getpos("'<")))
 
     #" Using List
     call cursor(1, 1)
index 6dbd93a7f6ea28c1b02df7bd15ae6f1f9d92d25b..99346430583dbc08397088c5fc759a94014e2232 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    388,
 /**/
     387,
 /**/