diff --git a/autoload/bufferline/bbye.vim b/autoload/bufferline/bbye.vim index b39a39cc..1a3c261a 100644 --- a/autoload/bufferline/bbye.vim +++ b/autoload/bufferline/bbye.vim @@ -87,18 +87,27 @@ function! s:str2bufnr(buffer) endif endfunction +let s:empty_buffer = v:null + function! s:new(bang) exe "enew" . a:bang - setl noswapfile + let s:empty_buffer = bufnr() + + let b:empty_buffer = v:true + " Regular buftype warns people if they have unsaved text there. Wouldn't " want to lose someone's data: setl buftype= + setl noswapfile " If empty and out of sight, delete it right away: setl bufhidden=wipe - let buffer_number = bufnr() - exe 'au BufWipeout call bufferline#close_direct(' buffer_number ')' + + augroup bbye_empty_buffer + au! + au BufWipeout call bufferline#close_direct(s:empty_buffer) + augroup END endfunction " Using the built-in :echoerr prints a stacktrace, which isn't that nice. diff --git a/lua/bufferline/render.lua b/lua/bufferline/render.lua index 3f871280..1703d83b 100644 --- a/lua/bufferline/render.lua +++ b/lua/bufferline/render.lua @@ -73,7 +73,12 @@ local function render() -- Store current buffer to open new ones next to this one if nvim.buf_get_option(current, 'buflisted') then - state.last_current_buffer = current + local ok, is_empty = pcall(api.nvim_buf_get_var, current, 'empty_buffer') + if ok and is_empty then + state.last_current_buffer = nil + else + state.last_current_buffer = current + end end local opts = vim.g.bufferline diff --git a/lua/bufferline/state.lua b/lua/bufferline/state.lua index dd10b9ed..fdce4731 100644 --- a/lua/bufferline/state.lua +++ b/lua/bufferline/state.lua @@ -129,11 +129,14 @@ local function open_buffers(new_buffers) -- Insert the buffers where they go for i, new_buffer in ipairs(new_buffers) do if utils.index(m.buffers, new_buffer) == nil then + local actual_index = new_index -- For special buffers, we add them at the end if vim.fn.getbufvar(new_buffer, '&buftype') ~= '' then - new_index = len(m.buffers) + 1 + actual_index = len(m.buffers) + 1 + else + new_index = new_index + 1 end - table.insert(m.buffers, new_index, new_buffer) + table.insert(m.buffers, actual_index, new_buffer) end end