patch 9.1.0808: Terminal scrollback doesn't shrink when decreasing 'termwinscroll'

Problem:  Terminal scrollback doesn't shrink when reducing
          'termwinscroll'
Solution: Check if option value was decreased (Milly).

closes: #15904

Signed-off-by: Milly <milly.ca@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/option.c b/src/option.c
index 179c61e..84c469e 100644
--- a/src/option.c
+++ b/src/option.c
@@ -4259,6 +4259,26 @@
 }
 #endif
 
+#if defined(FEAT_TERMINAL) || defined(PROTO)
+/*
+ * Process the updated 'termwinscroll' option value.
+ */
+    char *
+did_set_termwinscroll(optset_T *args)
+{
+    long	*pp = (long *)args->os_varp;
+    char	*errmsg = NULL;
+
+    if (*pp < 1)
+    {
+	errmsg = e_argument_must_be_positive;
+	*pp = 1;
+    }
+
+    return errmsg;
+}
+#endif
+
 /*
  * Process the updated 'terse' option value.
  */
diff --git a/src/optiondefs.h b/src/optiondefs.h
index 42e62e7..ccc527e 100644
--- a/src/optiondefs.h
+++ b/src/optiondefs.h
@@ -2563,7 +2563,7 @@
 			    SCTX_INIT},
     {"termwinscroll", "twsl", P_NUM|P_VI_DEF|P_VIM|P_RBUF,
 #ifdef FEAT_TERMINAL
-			    (char_u *)&p_twsl, PV_TWSL, NULL, NULL,
+			    (char_u *)&p_twsl, PV_TWSL, did_set_termwinscroll, NULL,
 			    {(char_u *)10000L, (char_u *)10000L}
 #else
 			    (char_u *)NULL, PV_NONE, NULL, NULL,
diff --git a/src/proto/option.pro b/src/proto/option.pro
index 1659131..db92bf2 100644
--- a/src/proto/option.pro
+++ b/src/proto/option.pro
@@ -71,6 +71,7 @@
 char *did_set_swapfile(optset_T *args);
 char *did_set_tabclose(optset_T *args);
 char *did_set_termguicolors(optset_T *args);
+char *did_set_termwinscroll(optset_T *args);
 char *did_set_terse(optset_T *args);
 char *did_set_textauto(optset_T *args);
 char *did_set_textmode(optset_T *args);
diff --git a/src/terminal.c b/src/terminal.c
index f61a54f..6edc21a 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -3436,7 +3436,8 @@
     if (gap->ga_len < term->tl_buffer->b_p_twsl)
 	return;
 
-    int	todo = term->tl_buffer->b_p_twsl / 10;
+    int	todo = MAX(term->tl_buffer->b_p_twsl / 10,
+				     gap->ga_len - term->tl_buffer->b_p_twsl);
     int	i;
 
     curbuf = term->tl_buffer;
diff --git a/src/testdir/gen_opt_test.vim b/src/testdir/gen_opt_test.vim
index 1c231f9..34fffef 100644
--- a/src/testdir/gen_opt_test.vim
+++ b/src/testdir/gen_opt_test.vim
@@ -103,6 +103,7 @@
       \ 'sidescroll': [[0, 1, 8, 999], [-1]],
       \ 'sidescrolloff': [[0, 1, 8, 999], [-1]],
       \ 'tabstop': [[1, 4, 8, 12, 9999], [-1, 0, 10000]],
+      \ 'termwinscroll': [[1, 100, 99999], [-1, 0]],
       \ 'textwidth': [[0, 1, 8, 99], [-1]],
       \ 'timeoutlen': [[0, 8, 99999], [-1]],
       \ 'titlelen': [[0, 1, 8, 9999], [-1]],
diff --git a/src/testdir/test_options.vim b/src/testdir/test_options.vim
index cbc84a4..73e6d85 100644
--- a/src/testdir/test_options.vim
+++ b/src/testdir/test_options.vim
@@ -752,6 +752,9 @@
   call assert_fails('set tabstop=10000', 'E474:')
   call assert_fails('let &tabstop = 10000', 'E474:')
   call assert_fails('set tabstop=5500000000', 'E474:')
+  if has('terminal')
+    call assert_fails('set termwinscroll=-1', 'E487:')
+  endif
   call assert_fails('set textwidth=-1', 'E487:')
   call assert_fails('set timeoutlen=-1', 'E487:')
   call assert_fails('set updatecount=-1', 'E487:')
diff --git a/src/testdir/test_terminal.vim b/src/testdir/test_terminal.vim
index fa644d3..5343efb 100644
--- a/src/testdir/test_terminal.vim
+++ b/src/testdir/test_terminal.vim
@@ -501,6 +501,21 @@
   let lines = line('$')
   call assert_inrange(91, 100, lines)
 
+  " When 'termwinscroll' becomes small, the scrollback should become small.
+  set termwinscroll=20
+  call term_sendkeys(buf, "echo set20\<CR>")
+  call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set20') >= 0)})
+  let lines = line('$')
+  call assert_inrange(19, 20, lines)
+
+  " When 'termwinscroll' under 10 which means 10% of it will be 0,
+  " the scrollback should become small.
+  set termwinscroll=1
+  call term_sendkeys(buf, "echo set1\<CR>")
+  call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set1') >= 0)})
+  let lines = line('$')
+  call assert_inrange(1, 2, lines)
+
   call StopShellInTerminal(buf)
   exe buf . 'bwipe'
   set termwinscroll&
diff --git a/src/version.c b/src/version.c
index 2a85be9..5b4a853 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    808,
+/**/
     807,
 /**/
     806,