0
0
mirror of https://github.com/vim/vim.git synced 2025-09-27 04:14:06 -04:00

patch 9.0.1599: Cursor not adjusted when 'splitkeep' is not "cursor"

Problem:    Cursor not adjusted when near top or bottom of window and
            'splitkeep' is not "cursor".
Solution:   Move boundary checks to outer cursor move functions, inner
            functions should only return valid cursor positions. (Luuk van
            Baal, closes #12480)
This commit is contained in:
Luuk van Baal
2023-06-02 14:16:35 +01:00
committed by Bram Moolenaar
parent 47eec6716b
commit a109f39ef5
6 changed files with 77 additions and 59 deletions

View File

@@ -2755,17 +2755,12 @@ oneleft(void)
/*
* Move the cursor up "n" lines in window "wp".
* Takes care of closed folds.
* Returns the new cursor line or zero for failure.
*/
linenr_T
void
cursor_up_inner(win_T *wp, long n)
{
linenr_T lnum = wp->w_cursor.lnum;
// This fails if the cursor is already in the first line or the count is
// larger than the line number and '-' is in 'cpoptions'
if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
return 0;
if (n >= lnum)
lnum = 1;
else
@@ -2798,7 +2793,6 @@ cursor_up_inner(win_T *wp, long n)
lnum -= n;
wp->w_cursor.lnum = lnum;
return lnum;
}
int
@@ -2806,8 +2800,13 @@ cursor_up(
long n,
int upd_topline) // When TRUE: update topline
{
if (n > 0 && cursor_up_inner(curwin, n) == 0)
// This fails if the cursor is already in the first line or the count is
// larger than the line number and '-' is in 'cpoptions'
linenr_T lnum = curwin->w_cursor.lnum;
if (n > 0 && (lnum <= 1
|| (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)))
return FAIL;
cursor_up_inner(curwin, n);
// try to advance to the column we want to be at
coladvance(curwin->w_curswant);
@@ -2821,23 +2820,13 @@ cursor_up(
/*
* Move the cursor down "n" lines in window "wp".
* Takes care of closed folds.
* Returns the new cursor line or zero for failure.
*/
linenr_T
void
cursor_down_inner(win_T *wp, long n)
{
linenr_T lnum = wp->w_cursor.lnum;
linenr_T line_count = wp->w_buffer->b_ml.ml_line_count;
#ifdef FEAT_FOLDING
// Move to last line of fold, will fail if it's the end-of-file.
(void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
#endif
// This fails if the cursor is already in the last line or would move
// beyond the last line and '-' is in 'cpoptions'
if (lnum >= line_count
|| (lnum + n > line_count && vim_strchr(p_cpo, CPO_MINUS) != NULL))
return FAIL;
if (lnum + n >= line_count)
lnum = line_count;
else
@@ -2849,6 +2838,7 @@ cursor_down_inner(win_T *wp, long n)
// count each sequence of folded lines as one logical line
while (n--)
{
// Move to last line of fold, will fail if it's the end-of-file.
if (hasFoldingWin(wp, lnum, NULL, &last, TRUE, NULL))
lnum = last + 1;
else
@@ -2864,7 +2854,6 @@ cursor_down_inner(win_T *wp, long n)
lnum += n;
wp->w_cursor.lnum = lnum;
return lnum;
}
/*
@@ -2875,8 +2864,16 @@ cursor_down(
long n,
int upd_topline) // When TRUE: update topline
{
if (n > 0 && cursor_down_inner(curwin, n) == 0)
linenr_T lnum = curwin->w_cursor.lnum;
linenr_T line_count = curwin->w_buffer->b_ml.ml_line_count;
// This fails if the cursor is already in the last line or would move
// beyond the last line and '-' is in 'cpoptions'
if (n > 0
&& (lnum >= line_count
|| (lnum + n > line_count
&& vim_strchr(p_cpo, CPO_MINUS) != NULL)))
return FAIL;
cursor_down_inner(curwin, n);
// try to advance to the column we want to be at
coladvance(curwin->w_curswant);