vim-patch:9.1.0285: Still problems with cursor position for CTRL-D/U

Problem:  Problems with cursor position when scrolling half a page.
Solution: Rework the cursor logic. (Luuk van Baal)

78c51500f1
This commit is contained in:
Luuk van Baal
2024-04-09 21:41:54 +02:00
parent 4946489e2e
commit 832857ae09
3 changed files with 29 additions and 25 deletions

View File

@@ -2424,7 +2424,6 @@ static bool scroll_with_sms(Direction dir, int count)
if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD)) { if (labs(curwin->w_topline - prev_topline) > (dir == BACKWARD)) {
fixdir = dir * -1; fixdir = dir * -1;
} }
validate_cursor(curwin);
while (curwin->w_skipcol > 0 while (curwin->w_skipcol > 0
&& curwin->w_topline < curbuf->b_ml.ml_line_count) { && curwin->w_topline < curbuf->b_ml.ml_line_count) {
scroll_redraw(fixdir == FORWARD, 1); scroll_redraw(fixdir == FORWARD, 1);
@@ -2447,6 +2446,7 @@ int pagescroll(Direction dir, int count, bool half)
int nochange = true; int nochange = true;
int buflen = curbuf->b_ml.ml_line_count; int buflen = curbuf->b_ml.ml_line_count;
colnr_T prev_col = curwin->w_cursor.col; colnr_T prev_col = curwin->w_cursor.col;
colnr_T prev_curswant = curwin->w_curswant;
linenr_T prev_lnum = curwin->w_cursor.lnum; linenr_T prev_lnum = curwin->w_cursor.lnum;
oparg_T oa = { 0 }; oparg_T oa = { 0 };
cmdarg_T ca = { 0 }; cmdarg_T ca = { 0 };
@@ -2471,32 +2471,24 @@ int pagescroll(Direction dir, int count, bool half)
} }
} }
// Scroll the window and determine number of lines to move the cursor. // (Try to) scroll the window unless already at the end of the buffer.
if (count > 0) { if (count > 0) {
validate_cursor(curwin);
int prev_wrow = curwin->w_wrow;
nochange = scroll_with_sms(dir, count); nochange = scroll_with_sms(dir, count);
if (!nochange) { curwin->w_cursor.lnum = prev_lnum;
validate_cursor(curwin); curwin->w_cursor.col = prev_col;
curscount = abs(prev_wrow - curwin->w_wrow); curwin->w_curswant = prev_curswant;
dir = prev_wrow > curwin->w_wrow ? FORWARD : BACKWARD;
}
} }
int so = get_scrolloff_value(curwin); // Move the cursor the same amount of screen lines.
// Move the cursor the same amount of screen lines except if if (curwin->w_p_wrap) {
// 'scrolloff' is set and cursor was at start or end of buffer. nv_screengo(&oa, dir, curscount);
if (so == 0 || (prev_lnum != 1 && prev_lnum != buflen)) { } else if (dir == FORWARD) {
if (curwin->w_p_wrap) { cursor_down_inner(curwin, curscount);
nv_screengo(&oa, dir, curscount); } else {
} else if (dir == FORWARD) { cursor_up_inner(curwin, curscount);
cursor_down_inner(curwin, curscount);
} else {
cursor_up_inner(curwin, curscount);
}
} }
if (so > 0) { if (get_scrolloff_value(curwin) > 0) {
cursor_correct(curwin); cursor_correct(curwin);
} }
// Move cursor to first line of closed fold. // Move cursor to first line of closed fold.

View File

@@ -4235,4 +4235,16 @@ func Test_single_line_filler_zb()
bw! bw!
endfunc endfunc
" Test for Ctrl-U not getting stuck at end of buffer with 'scrolloff'.
func Test_halfpage_scrolloff_eob()
set scrolloff=5
call setline(1, range(1, 100))
exe "norm! Gzz\<C-U>zz"
call assert_notequal(100, line('.'))
set scrolloff&
bwipe!
endfunc
" vim: shiftwidth=2 sts=2 expandtab nofoldenable " vim: shiftwidth=2 sts=2 expandtab nofoldenable

View File

@@ -1031,10 +1031,10 @@ func Test_smoothscroll_page()
call assert_equal(415, col('.')) call assert_equal(415, col('.'))
exe "norm! \<C-D>" exe "norm! \<C-D>"
call assert_equal(520, winsaveview().skipcol) call assert_equal(520, winsaveview().skipcol)
call assert_equal(535, col('.')) call assert_equal(615, col('.'))
exe "norm! \<C-D>" exe "norm! \<C-D>"
call assert_equal(520, winsaveview().skipcol) call assert_equal(520, winsaveview().skipcol)
call assert_equal(735, col('.')) call assert_equal(815, col('.'))
exe "norm! \<C-D>" exe "norm! \<C-D>"
call assert_equal(520, winsaveview().skipcol) call assert_equal(520, winsaveview().skipcol)
call assert_equal(895, col('.')) call assert_equal(895, col('.'))
@@ -1046,10 +1046,10 @@ func Test_smoothscroll_page()
call assert_equal(495, col('.')) call assert_equal(495, col('.'))
exe "norm! \<C-U>" exe "norm! \<C-U>"
call assert_equal(0, winsaveview().skipcol) call assert_equal(0, winsaveview().skipcol)
call assert_equal(375, col('.')) call assert_equal(295, col('.'))
exe "norm! \<C-U>" exe "norm! \<C-U>"
call assert_equal(0, winsaveview().skipcol) call assert_equal(0, winsaveview().skipcol)
call assert_equal(175, col('.')) call assert_equal(95, col('.'))
exe "norm! \<C-U>" exe "norm! \<C-U>"
call assert_equal(0, winsaveview().skipcol) call assert_equal(0, winsaveview().skipcol)
call assert_equal(15, col('.')) call assert_equal(15, col('.'))