patch 9.0.0900: cursor moves too far with 'smoothscroll'

Problem:    Cursor moves too far with 'smoothscroll'.
Solution:   Only move as far as really needed. (Yee Cheng Chin, closes #11504)
diff --git a/src/move.c b/src/move.c
index 3c35946..aac2c24 100644
--- a/src/move.c
+++ b/src/move.c
@@ -202,6 +202,26 @@
 #endif
 
 /*
+ * Calculates how much overlap the smoothscroll marker "<<<" overlaps with
+ * buffer text for curwin.
+ * Parameter "extra2" should be the padding on the 2nd line, not the first
+ * line.
+ * Returns the number of columns of overlap with buffer text, excluding the
+ * extra padding on the ledge.
+ */
+    static int
+smoothscroll_marker_overlap(int extra2)
+{
+#if defined(FEAT_LINEBREAK)
+    // We don't draw the <<< marker when in showbreak mode, thus no need to
+    // account for it.  See wlv_screen_line().
+    if (*get_showbreak_value(curwin) != NUL)
+	return 0;
+#endif
+    return extra2 > 3 ? 0 : 3 - extra2;
+}
+
+/*
  * Set curwin->s_skipcol to zero and redraw later if needed.
  */
     static void
@@ -311,10 +331,13 @@
 	    {
 		colnr_T vcol;
 
-		// check the cursor position is visible.  Add 3 for the ">>>"
-		// displayed in the top-left.
+		// Check that the cursor position is visible.  Add columns for
+		// the smoothscroll marker "<<<" displayed in the top-left if
+		// needed.
 		getvvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
-		if (curwin->w_skipcol + 3 >= vcol)
+		int smoothscroll_overlap = smoothscroll_marker_overlap(
+					 curwin_col_off() - curwin_col_off2());
+		if (curwin->w_skipcol + smoothscroll_overlap > vcol)
 		    check_topline = TRUE;
 	    }
 	}
@@ -1811,25 +1834,36 @@
     if (curwin->w_cursor.lnum == curwin->w_topline
 					    && do_sms && curwin->w_skipcol > 0)
     {
-	int	width1 = curwin->w_width - curwin_col_off();
-	int	width2 = width1 + curwin_col_off2();
+	int	col_off = curwin_col_off();
+	int	col_off2 = curwin_col_off2();
+
+	int	width1 = curwin->w_width - col_off;
+	int	width2 = width1 + col_off2;
+	int	extra2 = col_off - col_off2;
 	long	so = get_scrolloff_value();
 	int	scrolloff_cols = so == 0 ? 0 : width1 + (so - 1) * width2;
 	int	space_cols = (curwin->w_height - 1) * width2;
 
+	// If we have non-zero scrolloff, just ignore the <<< marker as we are
+	// going past it anyway.
+	int smoothscroll_overlap = scrolloff_cols != 0 ? 0 :
+					   smoothscroll_marker_overlap(extra2);
+
 	// Make sure the cursor is in a visible part of the line, taking
 	// 'scrolloff' into account, but using screen lines.
 	// If there are not enough screen lines put the cursor in the middle.
 	if (scrolloff_cols > space_cols / 2)
 	    scrolloff_cols = space_cols / 2;
 	validate_virtcol();
-	if (curwin->w_virtcol < curwin->w_skipcol + 3 + scrolloff_cols)
+	if (curwin->w_virtcol < curwin->w_skipcol
+				       + smoothscroll_overlap + scrolloff_cols)
 	{
 	    colnr_T col = curwin->w_virtcol;
 
 	    if (col < width1)
 		col += width1;
-	    while (col < curwin->w_skipcol + 3 + scrolloff_cols)
+	    while (col < curwin->w_skipcol
+				       + smoothscroll_overlap + scrolloff_cols)
 		col += width2;
 	    curwin->w_curswant = col;
 	    coladvance(curwin->w_curswant);
@@ -2818,9 +2852,9 @@
 static void get_scroll_overlap(lineoff_T *lp, int dir);
 
 /*
- * move screen 'count' pages up or down and update screen
+ * Move screen "count" pages up or down and update screen.
  *
- * return FAIL for failure, OK otherwise
+ * Return FAIL for failure, OK otherwise.
  */
     int
 onepage(int dir, long count)
diff --git a/src/testdir/dumps/Test_smooth_long_showbreak_2.dump b/src/testdir/dumps/Test_smooth_long_showbreak_2.dump
index 9f213bb..646b8e2 100644
--- a/src/testdir/dumps/Test_smooth_long_showbreak_2.dump
+++ b/src/testdir/dumps/Test_smooth_long_showbreak_2.dump
@@ -1,6 +1,6 @@
-|++0#4040ff13#ffffff0@2| |o+0#0000000&|f| |t|e|x|t| |i|n| |o|n|e| |l|i|n|e| |w|i|t|h| |l|o|t|s| |o|f| |t|e|x
-|++0#4040ff13&@2| >t+0#0000000&| |i|n| |o|n|e| |l|i|n|e| |w|i|t|h| |l|o|t|s| |o|f| |t|e|x|t| |i|n| |o
+|++0#4040ff13#ffffff0@2| >o+0#0000000&|f| |t|e|x|t| |i|n| |o|n|e| |l|i|n|e| |w|i|t|h| |l|o|t|s| |o|f| |t|e|x
+|++0#4040ff13&@2| |t+0#0000000&| |i|n| |o|n|e| |l|i|n|e| |w|i|t|h| |l|o|t|s| |o|f| |t|e|x|t| |i|n| |o
 |++0#4040ff13&@2| |n+0#0000000&|e| |l|i|n|e| |w|i|t|h| |l|o|t|s| |o|f| |t|e|x|t| |i|n| |o|n|e| |l|i|n
 |++0#4040ff13&@2| |e+0#0000000&| |w|i|t|h| |l|o|t|s| |o|f| |t|e|x|t| |i|n| |o|n|e| |l|i|n|e| @4
 |~+0#4040ff13&| @38
-| +0#0000000&@21|1|,|7@1|-|8|5| @6|A|l@1| 
+| +0#0000000&@21|1|,|4|1|-|4|5| @6|A|l@1| 
diff --git a/src/testdir/dumps/Test_smooth_number_7.dump b/src/testdir/dumps/Test_smooth_number_7.dump
index 1199593..b617dc0 100644
--- a/src/testdir/dumps/Test_smooth_number_7.dump
+++ b/src/testdir/dumps/Test_smooth_number_7.dump
@@ -1,5 +1,5 @@
-|2+0#af5f00255#ffffff0|<+0#4040ff13&@2|o+0#0000000&|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e
-| +0#af5f00255&@3>x+0#0000000&|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r
+|2+0#af5f00255#ffffff0|<+0#4040ff13&@2>o+0#0000000&|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e
+| +0#af5f00255&@3|x+0#0000000&|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r
 | +0#af5f00255&@3|y+0#0000000&| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g
 | +0#af5f00255&@3| +0#0000000&|t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| |v|e|r|y| |l|o|n|g| |t|e|x|t| 
 | +0#af5f00255&@1|1| |t+0#0000000&|h|r|e@1| @30
@@ -9,4 +9,4 @@
 |~| @38
 |~| @38
 |~| @38
-|-+0#0000000&@1|N|o|.@2|e|r|-@1| @10|2|,|7|3| @9|B|o|t| 
+|-+0#0000000&@1|N|o|.@2|e|r|-@1| @10|2|,|3|7| @9|B|o|t| 
diff --git a/src/testdir/dumps/Test_smooth_wrap_5.dump b/src/testdir/dumps/Test_smooth_wrap_5.dump
index 04a17f9..31f1544 100644
--- a/src/testdir/dumps/Test_smooth_wrap_5.dump
+++ b/src/testdir/dumps/Test_smooth_wrap_5.dump
@@ -1,8 +1,8 @@
->L+0&#ffffff0|i|n|e| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| 
+|<+0#4040ff13#ffffff0@2>h+0#0000000&| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| @10
+|L|i|n|e| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| 
 |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| 
 |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| @10
 |L|i|n|e| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| 
 |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| 
 |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| @10
-|@+0#4040ff13&@2| @36
-| +0#0000000&@21|5|,|1| @10|8|0|%| 
+@22|5|,|8|4| @9|B|o|t| 
diff --git a/src/testdir/dumps/Test_smooth_wrap_6.dump b/src/testdir/dumps/Test_smooth_wrap_6.dump
new file mode 100644
index 0000000..04a17f9
--- /dev/null
+++ b/src/testdir/dumps/Test_smooth_wrap_6.dump
@@ -0,0 +1,8 @@
+>L+0&#ffffff0|i|n|e| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| 
+|s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| 
+|w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| @10
+|L|i|n|e| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| 
+|s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| 
+|w|i|t|h| |s|o|m|e| |t|e|x|t| |w|i|t|h| |s|o|m|e| |t|e|x|t| @10
+|@+0#4040ff13&@2| @36
+| +0#0000000&@21|5|,|1| @10|8|0|%| 
diff --git a/src/testdir/test_scroll_opt.vim b/src/testdir/test_scroll_opt.vim
index 76853af..3975a1f 100644
--- a/src/testdir/test_scroll_opt.vim
+++ b/src/testdir/test_scroll_opt.vim
@@ -230,10 +230,14 @@
   call term_sendkeys(buf, "G")
   call VerifyScreenDump(buf, 'Test_smooth_wrap_4', {})
 
-  " moving cursor up - whole top line shows
-  call term_sendkeys(buf, "2k")
+  " moving cursor up right after the >>> marker - no need to show whole line
+  call term_sendkeys(buf, "2gj3l2k")
   call VerifyScreenDump(buf, 'Test_smooth_wrap_5', {})
 
+  " moving cursor up where the >>> marker is - whole top line shows
+  call term_sendkeys(buf, "2j02k")
+  call VerifyScreenDump(buf, 'Test_smooth_wrap_6', {})
+
   call StopVimInTerminal(buf)
 endfunc
 
@@ -321,7 +325,6 @@
   let buf = RunVimInTerminal('-S XSmoothLongShowbreak', #{rows: 6, cols: 40})
   call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_1', {})
   
-  " FIXME: this currently has the cursor in screen line 2, should be one up.
   call term_sendkeys(buf, "\<C-E>")
   call VerifyScreenDump(buf, 'Test_smooth_long_showbreak_2', {})
 
@@ -353,10 +356,16 @@
   call s:check_col_calc(1, 2, 41)
   exe "normal \<C-Y>"
   call s:check_col_calc(1, 3, 41)
-  normal ggg$
+
+  normal gg3l
   exe "normal \<C-E>"
 
   " Move down only 1 line when we are out of the range of the <<< display
+  call s:check_col_calc(4, 1, 24)
+  exe "normal \<C-Y>"
+  call s:check_col_calc(4, 2, 24)
+  normal ggg$
+  exe "normal \<C-E>"
   call s:check_col_calc(20, 1, 40)
   exe "normal \<C-Y>"
   call s:check_col_calc(20, 2, 40)
@@ -366,9 +375,11 @@
   setl number
   call s:check_col_calc(5, 1, 1)
   exe "normal \<C-E>"
-  call s:check_col_calc(5, 2, 33)
+
+  " Move down only 1 line when the <<< display is on the number column
+  call s:check_col_calc(5, 1, 17)
   exe "normal \<C-Y>"
-  call s:check_col_calc(5, 3, 33)
+  call s:check_col_calc(5, 2, 17)
   normal ggg$
   exe "normal \<C-E>"
   call s:check_col_calc(20, 1, 32)
@@ -376,13 +387,33 @@
   call s:check_col_calc(20, 2, 32)
   normal gg
 
+  setl numberwidth=1
+
+  " Move down another line when numberwidth is too short to cover the whole
+  " <<< display
+  call s:check_col_calc(3, 1, 1)
+  exe "normal \<C-E>"
+  call s:check_col_calc(3, 2, 37)
+  exe "normal \<C-Y>"
+  call s:check_col_calc(3, 3, 37)
+  normal ggl
+
+  " Only move 1 line down when we are just past the <<< display
+  call s:check_col_calc(4, 1, 2)
+  exe "normal \<C-E>"
+  call s:check_col_calc(4, 1, 20)
+  exe "normal \<C-Y>"
+  call s:check_col_calc(4, 2, 20)
+  normal gg
+  setl numberwidth&
+
   " Test number + showbreak, so test that the additional indentation works
   setl number showbreak=+++
   call s:check_col_calc(5, 1, 1)
   exe "normal \<C-E>"
-  call s:check_col_calc(8, 2, 30)
+  call s:check_col_calc(8, 1, 17)
   exe "normal \<C-Y>"
-  call s:check_col_calc(8, 3, 30)
+  call s:check_col_calc(8, 2, 17)
   normal gg
 
   " Test number + cpo+=n mode, where wrapped lines aren't indented
diff --git a/src/version.c b/src/version.c
index 6eadf68..fdcd5ba 100644
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    900,
+/**/
     899,
 /**/
     898,