From 7cb5c2415172372ecd89662279350baf890dc8a0 Mon Sep 17 00:00:00 2001 From: Sam Fuller Date: Wed, 19 Oct 2016 17:19:43 -0600 Subject: [PATCH] prevent windows from closing on middle_click MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding an option to prevent windows from being closed when a buffer in the tabline is middle clicked and the clicked buffer is currently open in a window. When this option is enabled, instead of closing the window a new buffer will be opened in all of the windows editing the clicked buffer instead. This is my first pull request AND my first experience with vimscript, so my apologies if this is a bit sloppy 😄 --- .../airline/extensions/tabline/buffers.vim | 36 ++++++++++++++++++- doc/airline.txt | 5 ++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/autoload/airline/extensions/tabline/buffers.vim b/autoload/airline/extensions/tabline/buffers.vim index d277175..8d7ba42 100644 --- a/autoload/airline/extensions/tabline/buffers.vim +++ b/autoload/airline/extensions/tabline/buffers.vim @@ -208,7 +208,41 @@ function! airline#extensions#tabline#buffers#clickbuf(minwid, clicks, button, mo silent execute 'buffer' a:minwid elseif a:button is# 'm' " middle button - delete buffer - silent execute 'bdelete' a:minwid + + if get(g:, 'airline#extensions#tabline#middle_click_preserves_windows', 0) == 0 + " just simply delete the clicked buffer. This will cause windows + " associated with the clicked buffer to be closed. + silent execute 'bdelete' a:minwid + else + " find windows displaying the clicked buffer and open an new + " buffer in them. + let current_window = bufwinnr("%") + let window_number = bufwinnr(a:minwid) + let last_window_visited = -1 + + " Set to 1 if the clicked buffer was open in any windows. + let buffer_in_window = 0 + + " Find the next window with the clicked buffer open. If bufwinnr() + " returns the same window number, this is because we clicked a new + " buffer, and then tried editing a new buffer. Vim won't create a + " new empty buffer for the same window, so we get the same window + " number from bufwinnr(). In this case we just give up and don't + " delete the buffer. + " This could be made cleaner if we could check if the clicked buffer + " is a new buffer, but I don't know if there is a way to do that. + while window_number != -1 && window_number != last_window_visited + let buffer_in_window = 1 + silent execute window_number . 'wincmd w' + silent execute 'enew' + let last_window_visited = window_number + let window_number = bufwinnr(a:minwid) + endwhile + silent execute current_window . 'wincmd w' + if window_number != last_window_visited || buffer_in_window == 0 + silent execute 'bdelete' a:minwid + endif + endif endif endif endfunction diff --git a/doc/airline.txt b/doc/airline.txt index 0cf6aee..2f3f904 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -145,7 +145,6 @@ values): > let g:airline_skip_empty_sections = 1 < - ============================================================================== COMMANDS *airline-commands* @@ -693,6 +692,10 @@ Note: Enabling this extension will modify 'showtabline' and 'guioptions'. won't update airline on |:badd| commands) > let airline#extensions#tabline#disable_refresh = 0 +* preserve windows when closing a buffer from the bufferline (default: 0) > + + let airline#extensions#tabline#middle_click_preserves_windows = 1 +< ------------------------------------- *airline-tmuxline* tmuxline