Browse Source

Do not display BufExplorer's buffer in the buffer listing.

In most cases, BufExplorer's own buffer won't be part of the list of
buffers because the buffer list is enumerated before creating the
`[BufExplorer]` buffer.  However, there is an edge case that occurs when
the current buffer is an empty unnamed buffer at BufExplorer launch.

When Vim starts, an empty unnamed buffer is active, e.g.:

```
vim
:ls
  1 %a   "[No Name]"                    line 1
```

Normally, editing a file via `:edit somefile.txt` would allocate a new
buffer number; but if the current buffer is empty and unnamed, Vim
reuses this buffer instead of allocating a new buffer, e.g.:

```
:edit somefile.txt
:ls
  1 %a   "somefile.txt"                 line 1
```

Notice that buffer 1 has been reused as `somefile.txt`.

This same thing happens when launching BufExplorer with an empty unnamed
buffer active.  With BufExplorer 7.6.0, launch Vim and immediately
invoke BufExplorer via `\be`; this results in:

```
" Press <F1> for Help
" Sorted by mru | Locate buffer | One tab/buffer | Absolute Split path | Show terminal
"=
  1 %a    [BufExplorer] ~/testing line 1
```

At BufExplorer startup, the buffer list is scanned to acquire buffer
numbers and attributes.  For performance reasons, the other buffer
metadata is gathered at display time.  Because the `[BufExplorer]`
buffer has taken over buffer 1 by that point, it shows up in the buffer
list with that name.

In prior BufExplorer versions, the problem presented in a different
fashion.  Using BufExplorer 7.5.0 or older, launch Vim and enable
BufExplorer display of unnamed buffers via:

```
:let g:bufExplorerShowNoName = 1
```

Now launch BufExplorer via `\be`:

```
" Press <F1> for Help
" Sorted by mru | Locate buffer | One tab/buffer | Absolute Split path | Show terminal
"=
  1 %a    [No Name] ~/testing                                                       line 1
```

Buffer 1 appears to be unnamed; but in actuality, it's really the
BufExplorer buffer (which is unlisted):

```
:ls!
  1u%a-  "[BufExplorer]"                line 4
```

The fix is to record the number of BufExplorer's buffer at startup and
prevent its display.
Michael Henry 10 months ago
parent
commit
dd40e17c3e
1 changed files with 10 additions and 0 deletions
  1. 10 0
      plugin/bufexplorer.vim

+ 10 - 0
plugin/bufexplorer.vim

@@ -129,6 +129,8 @@ endfunction
 let s:MRU_Exclude_List = ["[BufExplorer]","__MRU_Files__","[Buf\ List]"]
 let s:name = '[BufExplorer]'
 let s:originBuffer = 0
+" Buffer number of the BufExplorer window.
+let s:bufExplorerBuffer = 0
 let s:running = 0
 let s:sort_by = ["number", "name", "fullpath", "mru", "extension"]
 let s:splitMode = ""
@@ -650,6 +652,9 @@ function! BufExplorer()
         execute "silent keepjumps hide edit".name
     endif
 
+    " Record BufExplorer's buffer number.
+    let s:bufExplorerBuffer = bufnr('%')
+
     call s:DisplayBufferList()
 
     " Position the cursor in the newly displayed list on the line representing
@@ -943,6 +948,11 @@ function! s:BuildBufferList()
         " calculating other buffer details (e.g., `buf.fullname`) until we know
         " the user wants to view this buffer.
 
+        " Skip BufExplorer's buffer.
+        if buf._bufnr == s:bufExplorerBuffer
+            continue
+        endif
+
         " Skip unlisted buffers if we are not to show them.
         if !g:bufExplorerShowUnlisted && buf.attributes =~ "u"
             " Skip unlisted buffers if we are not to show them.