diff --git a/plugin/vimroom.vim b/plugin/vimroom.vim index dbe0352..a2184af 100644 --- a/plugin/vimroom.vim +++ b/plugin/vimroom.vim @@ -124,6 +124,59 @@ function! s:sidebar_size() return ( winwidth( winnr() ) - g:vimroom_width - 2 ) / 2 endfunction +" Save current buffer name +let b:vimroom_bufname = expand("%:t") +" Close padding buffer if we quit current buffer +autocmd BufHidden call s:close_padding() + +function! s:close_padding() + " Try to close current buffer and related padding buffers + try + " Unload current buffer + bdelete + " Save buffer list + redir => buflist + silent ls + redir END + + let has_other_buf = 0 + + " Is there other buffer open? + for buf in split(buflist, "\n") + if match(buf, '_RoomPadding') == -1 + let has_other_buf = 1 + break + endif + endfor + + if has_other_buf + " If there is other buffer existing, + " we close padding buffer first. + bwipeout _RoomPadding + " Initialize environment for other buffer + let b:vimroom_bufname = expand("%:t") + let s:active = 0 + autocmd BufHidden call s:close_padding() + else + " Otherwise, we just quit vim + qa + endif + catch + " XXX It is not the best solution, but I have no other better solutions. + " If the current buffer has been changed, and we didn't save it before + " we quit, we would be in here. + + " Close padding buffers + bwipeout _RoomPadding + + " Show errors and warnings + let exc = substitute(v:exception, 'Vim([a-z]*):', '', '') + echohl ErrorMsg | echo exc | echohl None + echohl WarningMsg | echo "You are in a weird dimension." | echohl None + echohl WarningMsg | echo "The best thing you should do now is save and quit." | echohl None + endtry +endfunction + function! VimroomToggle() if s:active == 1 let s:active = 0 @@ -142,28 +195,28 @@ function! VimroomToggle() endif " Reset color scheme (or clear new colors, if no scheme is set) if s:scheme != "" - exec( "colorscheme " . s:scheme ) + exec( "colorscheme " . s:scheme ) else - hi clear + hi clear endif if s:save_t_mr != "" - exec( "set t_mr=" .s:save_t_mr ) + exec( "set t_mr=" .s:save_t_mr ) endif " Reset `scrolloff` and `laststatus` if s:save_scrolloff != "" - exec( "set scrolloff=" . s:save_scrolloff ) + exec( "set scrolloff=" . s:save_scrolloff ) endif if s:save_laststatus != "" - exec( "set laststatus=" . s:save_laststatus ) + exec( "set laststatus=" . s:save_laststatus ) endif if s:save_textwidth != "" - exec( "set textwidth=" . s:save_textwidth ) + exec( "set textwidth=" . s:save_textwidth ) endif if s:save_number != 0 - set number + set number endif if s:save_relativenumber != 0 - set relativenumber + set relativenumber endif " Remove wrapping and linebreaks set nowrap @@ -178,14 +231,14 @@ function! VimroomToggle() endif if g:vimroom_min_sidebar_width " Create the left sidebar - exec( "silent leftabove " . s:sidebar . "vsplit new" ) + exec( "silent leftabove " . s:sidebar . "vsplit _RoomPadding" ) setlocal noma setlocal nocursorline setlocal nonumber setlocal norelativenumber wincmd l " Create the right sidebar - exec( "silent rightbelow " . s:sidebar . "vsplit new" ) + exec( "silent rightbelow " . s:sidebar . "vsplit _RoomPadding" ) setlocal noma setlocal nocursorline setlocal nonumber @@ -194,14 +247,14 @@ function! VimroomToggle() endif if g:vimroom_sidebar_height " Create the top sidebar - exec( "silent leftabove " . g:vimroom_sidebar_height . "split new" ) + exec( "silent leftabove " . g:vimroom_sidebar_height . "split _RoomPadding" ) setlocal noma setlocal nocursorline setlocal nonumber setlocal norelativenumber wincmd j " Create the bottom sidebar - exec( "silent rightbelow " . g:vimroom_sidebar_height . "split new" ) + exec( "silent rightbelow " . g:vimroom_sidebar_height . "split _RoomPadding" ) setlocal noma setlocal nocursorline setlocal nonumber @@ -242,7 +295,7 @@ function! VimroomToggle() let l:highlightbgcolor = "guibg=" . g:vimroom_guibackground let l:highlightfgbgcolor = "guifg=" . g:vimroom_guibackground . " " . l:highlightbgcolor else - let l:highlightbgcolor = "ctermbg=" . g:vimroom_guibackground + let l:highlightbgcolor = "ctermbg=" . g:vimroom_ctermbackground let l:highlightfgbgcolor = "ctermfg=" . g:vimroom_ctermbackground . " " . l:highlightbgcolor endif exec( "hi Normal " . l:highlightbgcolor ) @@ -252,6 +305,8 @@ function! VimroomToggle() exec( "hi StatusLineNC " . l:highlightfgbgcolor ) set t_mr="" set fillchars+=vert:\ + " Sometimes user would change colorscheme when they are in room + autocmd ColorScheme let s:scheme = g:colors_name endif endif endfunction