patch 8.2.0984: not using previous window when closing a shell popup window

Problem:    Not using previous window when closing a shell popup window.
Solution:   Use "prevwin" if it was set. (closes #6267)
diff --git a/src/popupwin.c b/src/popupwin.c
index fbda48b..d144be7 100644
--- a/src/popupwin.c
+++ b/src/popupwin.c
@@ -2148,6 +2148,19 @@
 }
 
 /*
+ * Make "prevwin" the current window, unless it's equal to "wp".
+ * Otherwise make "firstwin" the current window.
+ */
+    static void
+back_to_prevwin(win_T *wp)
+{
+    if (win_valid(prevwin) && wp != prevwin)
+	win_enter(prevwin, FALSE);
+    else
+	win_enter(firstwin, FALSE);
+}
+
+/*
  * Close popup "wp" and invoke any close callback for it.
  */
     static void
@@ -2178,10 +2191,8 @@
 		    break;
 	    if (owp != NULL)
 		win_enter(owp, FALSE);
-	    else if (win_valid(prevwin) && wp != prevwin)
-		win_enter(prevwin, FALSE);
 	    else
-		win_enter(firstwin, FALSE);
+		back_to_prevwin(wp);
 	}
     }
 #endif
@@ -2583,7 +2594,7 @@
 		    error_for_popup_window();
 		    return FAIL;
 		}
-		win_enter(firstwin, FALSE);
+		back_to_prevwin(wp);
 	    }
 	    if (prev == NULL)
 		first_popupwin = wp->w_next;
@@ -2620,7 +2631,7 @@
 		    error_for_popup_window();
 		    return FAIL;
 		}
-		win_enter(firstwin, FALSE);
+		back_to_prevwin(wp);
 	    }
 	    if (prev == NULL)
 		*root = wp->w_next;
diff --git a/src/testdir/test_popupwin.vim b/src/testdir/test_popupwin.vim
index 1e7d5ee..2188c2c 100644
--- a/src/testdir/test_popupwin.vim
+++ b/src/testdir/test_popupwin.vim
@@ -2461,6 +2461,23 @@
   call assert_equal(origwin, win_getid())
 endfunc
 
+func Test_popupwin_close_prevwin()
+  CheckFeature terminal
+
+  call assert_equal(1, winnr('$'))
+  split
+  wincmd b
+  call assert_equal(2, winnr())
+  let buf = term_start(&shell, #{hidden: 1})
+  call popup_create(buf, {})
+  call term_wait(buf, 100)
+  call popup_clear(1)
+  call assert_equal(2, winnr())
+
+  quit
+  exe 'bwipe! ' .. buf
+endfunc
+
 func Test_popupwin_with_buffer_and_filter()
   new Xwithfilter
   call setline(1, range(100))
diff --git a/src/version.c b/src/version.c
index 7d7b965..91ae5ea 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    984,
+/**/
     983,
 /**/
     982,