patch 8.1.1015: quickfix buffer shows up in list, can't get buffer number

Problem:    Quickfix buffer shows up in list, can't get buffer number.
Solution:   Make the quickfix buffer unlisted when the quickfix window is
            closed.  get the quickfix buffer number with getqflist().
            (Yegappan Lakshmanan, closes #4113)
diff --git a/src/quickfix.c b/src/quickfix.c
index 6ecb75e..d332e23 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -5907,7 +5907,8 @@
     QF_GETLIST_SIZE	= 0x80,
     QF_GETLIST_TICK	= 0x100,
     QF_GETLIST_FILEWINID	= 0x200,
-    QF_GETLIST_ALL	= 0x3FF,
+    QF_GETLIST_QFBUFNR	= 0x400,
+    QF_GETLIST_ALL	= 0x7FF,
 };
 
 /*
@@ -5977,6 +5978,17 @@
 }
 
 /*
+ * Returns the number of the buffer displayed in the quickfix/location list
+ * window. If there is no buffer associated with the list, then returns 0.
+ */
+    static int
+qf_getprop_qfbufnr(qf_info_T *qi, dict_T *retdict)
+{
+    return dict_add_number(retdict, "qfbufnr",
+					(qi == NULL) ? 0 : qi->qf_bufnr);
+}
+
+/*
  * Convert the keys in 'what' to quickfix list property flags.
  */
     static int
@@ -6022,6 +6034,9 @@
     if (loclist && dict_find(what, (char_u *)"filewinid", -1) != NULL)
 	flags |= QF_GETLIST_FILEWINID;
 
+    if (dict_find(what, (char_u *)"qfbufnr", -1) != NULL)
+	flags |= QF_GETLIST_QFBUFNR;
+
     return flags;
 }
 
@@ -6114,6 +6129,8 @@
 	status = dict_add_number(retdict, "changedtick", 0);
     if ((status == OK) && locstack && (flags & QF_GETLIST_FILEWINID))
 	status = dict_add_number(retdict, "filewinid", 0);
+    if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
+	status = qf_getprop_qfbufnr(qi, retdict);
 
     return status;
 }
@@ -6259,6 +6276,8 @@
 	status = dict_add_number(retdict, "changedtick", qfl->qf_changedtick);
     if ((status == OK) && (wp != NULL) && (flags & QF_GETLIST_FILEWINID))
 	status = qf_getprop_filewinid(wp, qi, retdict);
+    if ((status == OK) && (flags & QF_GETLIST_QFBUFNR))
+	status = qf_getprop_qfbufnr(qi, retdict);
 
     return status;
 }
diff --git a/src/testdir/test_quickfix.vim b/src/testdir/test_quickfix.vim
index 4213975..da8b4f0 100644
--- a/src/testdir/test_quickfix.vim
+++ b/src/testdir/test_quickfix.vim
@@ -3110,19 +3110,21 @@
   call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
   if a:cchar == 'c'
     call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
-		  \ 'items' : [], 'nr' : 0, 'size' : 0,
+		  \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
 		  \ 'title' : '', 'winid' : 0, 'changedtick': 0},
 		  \ g:Xgetlist({'all' : 0}))
   else
     call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
 		\ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
-		\ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0},
+		\ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
+		\ 'qfbufnr' : 0},
 		\ g:Xgetlist({'all' : 0}))
   endif
 
   " Quickfix window with empty stack
   silent! Xopen
   let qfwinid = (a:cchar == 'c') ? win_getid() : 0
+  let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
   call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
   Xclose
 
@@ -3154,11 +3156,12 @@
   if a:cchar == 'c'
     call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
 		\ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
+		\ 'qfbufnr' : qfbufnr,
 		\ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
   else
     call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
 		\ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
-		\ 'changedtick' : 0, 'filewinid' : 0},
+		\ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
 		\ g:Xgetlist({'id' : qfid, 'all' : 0}))
   endif
 
@@ -3175,11 +3178,12 @@
   if a:cchar == 'c'
     call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
 		\ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
-		\ 'changedtick' : 0}, g:Xgetlist({'nr' : 5, 'all' : 0}))
+		\ 'changedtick' : 0, 'qfbufnr' : qfbufnr},
+		\ g:Xgetlist({'nr' : 5, 'all' : 0}))
   else
     call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
 		\ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
-		\ 'changedtick' : 0, 'filewinid' : 0},
+		\ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
 		\ g:Xgetlist({'nr' : 5, 'all' : 0}))
   endif
 endfunc
@@ -3912,6 +3916,7 @@
   Xclose
   " Even after the quickfix window is closed, the buffer should be loaded
   call assert_true(bufloaded(qfbnum))
+  call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
   Xopen
   " Buffer should be reused when opening the window again
   call assert_equal(qfbnum, bufnr(''))
@@ -3930,7 +3935,7 @@
     close
     " When the location list window is closed, the buffer name should not
     " change to 'Quickfix List'
-    call assert_match(qfbnum . '  h-  "\[Location List]"', execute('ls'))
+    call assert_match(qfbnum . 'u h-  "\[Location List]"', execute('ls!'))
     call assert_true(bufloaded(qfbnum))
 
     " After deleting a location list buffer using ":bdelete", opening the
@@ -3947,6 +3952,7 @@
     " removed
     call setloclist(0, [], 'f')
     call assert_false(bufexists(qfbnum))
+    call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
 
     " When the location list is freed with the location list window open, the
     " location list buffer should not be lost. It should be reused when the
diff --git a/src/version.c b/src/version.c
index 6302294..265ca22 100644
--- a/src/version.c
+++ b/src/version.c
@@ -780,6 +780,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1015,
+/**/
     1014,
 /**/
     1013,
diff --git a/src/window.c b/src/window.c
index 98d6085..6c9c705 100644
--- a/src/window.c
+++ b/src/window.c
@@ -2382,18 +2382,24 @@
     }
 
 #ifdef FEAT_GUI
-    /* Avoid trouble with scrollbars that are going to be deleted in
-     * win_free(). */
+    // Avoid trouble with scrollbars that are going to be deleted in
+    // win_free().
     if (gui.in_use)
 	out_flush();
 #endif
 
 #ifdef FEAT_SYN_HL
-    /* Free independent synblock before the buffer is freed. */
+    // Free independent synblock before the buffer is freed.
     if (win->w_buffer != NULL)
 	reset_synblock(win);
 #endif
 
+#ifdef FEAT_QUICKFIX
+    // When the quickfix/location list window is closed, unlist the buffer.
+    if (win->w_buffer != NULL && bt_quickfix(win->w_buffer))
+	win->w_buffer->b_p_bl = FALSE;
+#endif
+
     /*
      * Close the link to the buffer.
      */