0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 9.0.0712: wrong column when calling setcursorcharpos() with zero lnum

Problem:    Wrong column when calling setcursorcharpos() with zero lnum.
Solution:   Set the line number before calling buf_charidx_to_byteidx().
            (closes #11329)
This commit is contained in:
Bram Moolenaar
2022-10-10 12:42:57 +01:00
parent 084f2620ec
commit 79f234499b
4 changed files with 29 additions and 15 deletions

View File

@@ -6023,10 +6023,12 @@ var2fpos(
} }
/* /*
* Convert list in "arg" into a position and optional file number. * Convert list in "arg" into position "psop" and optional file number "fnump".
* When "fnump" is NULL there is no file number, only 3 items. * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
* Note that the column is passed on as-is, the caller may want to decrement * Note that the column is passed on as-is, the caller may want to decrement
* it to use 1 for the first column. * it to use 1 for the first column.
* If "charcol" is TRUE use the column as the character index instead of the
* byte index.
* Return FAIL when conversion is not possible, doesn't check the position for * Return FAIL when conversion is not possible, doesn't check the position for
* validity. * validity.
*/ */
@@ -6069,6 +6071,7 @@ list2fpos(
if (n < 0) if (n < 0)
return FAIL; return FAIL;
// If character position is specified, then convert to byte position // If character position is specified, then convert to byte position
// If the line number is zero use the cursor line.
if (charcol) if (charcol)
{ {
buf_T *buf; buf_T *buf;
@@ -6078,7 +6081,8 @@ list2fpos(
if (buf == NULL || buf->b_ml.ml_mfp == NULL) if (buf == NULL || buf->b_ml.ml_mfp == NULL)
return FAIL; return FAIL;
n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1; n = buf_charidx_to_byteidx(buf,
posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
} }
posp->col = n; posp->col = n;

View File

@@ -3545,7 +3545,7 @@ f_copy(typval_T *argvars, typval_T *rettv)
static void static void
set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol) set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
{ {
long line, col; long lnum, col;
long coladd = 0; long coladd = 0;
int set_curswant = TRUE; int set_curswant = TRUE;
@@ -3567,7 +3567,7 @@ set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
emsg(_(e_invalid_argument)); emsg(_(e_invalid_argument));
return; return;
} }
line = pos.lnum; lnum = pos.lnum;
col = pos.col; col = pos.col;
coladd = pos.coladd; coladd = pos.coladd;
if (curswant >= 0) if (curswant >= 0)
@@ -3576,17 +3576,19 @@ set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
set_curswant = FALSE; set_curswant = FALSE;
} }
} }
else if ((argvars[0].v_type == VAR_NUMBER || else if ((argvars[0].v_type == VAR_NUMBER
argvars[0].v_type == VAR_STRING) || argvars[0].v_type == VAR_STRING)
&& (argvars[1].v_type == VAR_NUMBER || && (argvars[1].v_type == VAR_NUMBER
argvars[1].v_type == VAR_STRING)) || argvars[1].v_type == VAR_STRING))
{ {
line = tv_get_lnum(argvars); lnum = tv_get_lnum(argvars);
if (line < 0) if (lnum < 0)
semsg(_(e_invalid_argument_str), tv_get_string(&argvars[0])); semsg(_(e_invalid_argument_str), tv_get_string(&argvars[0]));
else if (lnum == 0)
lnum = curwin->w_cursor.lnum;
col = (long)tv_get_number_chk(&argvars[1], NULL); col = (long)tv_get_number_chk(&argvars[1], NULL);
if (charcol) if (charcol)
col = buf_charidx_to_byteidx(curbuf, line, col) + 1; col = buf_charidx_to_byteidx(curbuf, lnum, col) + 1;
if (argvars[2].v_type != VAR_UNKNOWN) if (argvars[2].v_type != VAR_UNKNOWN)
coladd = (long)tv_get_number_chk(&argvars[2], NULL); coladd = (long)tv_get_number_chk(&argvars[2], NULL);
} }
@@ -3595,10 +3597,10 @@ set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
emsg(_(e_invalid_argument)); emsg(_(e_invalid_argument));
return; return;
} }
if (line < 0 || col < 0 || coladd < 0) if (lnum < 0 || col < 0 || coladd < 0)
return; // type error; errmsg already given return; // type error; errmsg already given
if (line > 0) if (lnum > 0)
curwin->w_cursor.lnum = line; curwin->w_cursor.lnum = lnum;
if (col > 0) if (col > 0)
curwin->w_cursor.col = col - 1; curwin->w_cursor.col = col - 1;
curwin->w_cursor.coladd = coladd; curwin->w_cursor.coladd = coladd;

View File

@@ -399,8 +399,14 @@ func Test_setcursorcharpos()
normal G normal G
call setcursorcharpos([1, 1]) call setcursorcharpos([1, 1])
call assert_equal([1, 1], [line('.'), col('.')]) call assert_equal([1, 1], [line('.'), col('.')])
call setcursorcharpos([2, 7, 0]) call setcursorcharpos([2, 7, 0])
call assert_equal([2, 9], [line('.'), col('.')]) call assert_equal([2, 9], [line('.'), col('.')])
call setcursorcharpos([0, 7, 0])
call assert_equal([2, 9], [line('.'), col('.')])
call setcursorcharpos(0, 7, 0)
call assert_equal([2, 9], [line('.'), col('.')])
call setcursorcharpos(3, 4) call setcursorcharpos(3, 4)
call assert_equal([3, 1], [line('.'), col('.')]) call assert_equal([3, 1], [line('.'), col('.')])
call setcursorcharpos([3, 1]) call setcursorcharpos([3, 1])

View File

@@ -699,6 +699,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
712,
/**/ /**/
711, 711,
/**/ /**/