# HG changeset patch # User Bram Moolenaar # Date 1576017904 -3600 # Node ID 9449ed2ee8d484a001c006bee9819c823bd175a4 # Parent 964f1ada2f220b531a4668e04003916aaf5b77a3 patch 8.1.2418: bufnr('$') is wrong after recycling popup buffer Commit: https://github.com/vim/vim/commit/99ebf22c523e3fdb491b2c92b6f3a7d42721361d Author: Bram Moolenaar Date: Tue Dec 10 23:44:48 2019 +0100 patch 8.1.2418: bufnr('$') is wrong after recycling popup buffer Problem: bufnr('$') is wrong after recycling popup buffer. Solution: Sort the buffer list by buffer number. (closes https://github.com/vim/vim/issues/5335) diff --git a/src/buffer.c b/src/buffer.c --- a/src/buffer.c +++ b/src/buffer.c @@ -2093,6 +2093,25 @@ buflist_new( // buffer number grows rapidly. --buf_reuse.ga_len; buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len]; + + // Move buffer to the right place in the buffer list. + while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum) + { + buf_T *prev = buf->b_prev; + + prev->b_next = buf->b_next; + if (prev->b_next != NULL) + prev->b_next->b_prev = prev; + buf->b_next = prev; + buf->b_prev = prev->b_prev; + if (buf->b_prev != NULL) + buf->b_prev->b_next = buf; + prev->b_prev = buf; + if (lastbuf == buf) + lastbuf = prev; + if (firstbuf == prev) + firstbuf = buf; + } } else buf->b_fnum = top_file_num++; diff --git a/src/testdir/test_popupwin.vim b/src/testdir/test_popupwin.vim --- a/src/testdir/test_popupwin.vim +++ b/src/testdir/test_popupwin.vim @@ -3168,4 +3168,21 @@ func Test_popupwin_sign() call delete('XtestPopupSign') endfunc +func Test_popupwin_bufnr() + let popwin = popup_create(['blah'], #{}) + let popbuf = winbufnr(popwin) + split asdfasdf + let newbuf = bufnr() + call assert_true(newbuf > popbuf, 'New buffer number is higher') + call assert_equal(newbuf, bufnr('$')) + call popup_clear() + let popwin = popup_create(['blah'], #{}) + " reuses previous buffer number + call assert_equal(popbuf, winbufnr(popwin)) + call assert_equal(newbuf, bufnr('$')) + + call popup_clear() + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -743,6 +743,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2418, +/**/ 2417, /**/ 2416,