smartsplit.vim (2134B)
1 " 2 " TITLE: VIM-SMARTSPLIT 3 " AUTHOR: Daniel Moch <daniel AT danielmoch DOT com> 4 " LICENSE: VIM LICENSE 5 " VERSION: 0.2.0-dev 6 " 7 if exists('g:loaded_smartsplit') || &cp 8 finish 9 endif 10 11 let g:loaded_smartsplit = 1 12 let s:save_cpo = &cpo 13 set cpo&vim 14 15 function! s:WinWidth() 16 let l:textwidth = &tw ? (&tw + 8) : 80 17 if &number 18 let l:numwidth = &numberwidth > len(line('$')) + 1 ? 19 \ &numberwidth : len(line('$')) + 1 20 else 21 let l:numwidth = 0 22 endif 23 return winwidth(0) > (2 * l:textwidth) + (2 * l:numwidth) 24 endfunction 25 26 function! s:ScratchBuffer() abort 27 let l:wincmd = s:WinWidth() ? 'vnew' : 'new' 28 exec l:wincmd 29 setlocal bufhidden=hide buftype=nofile noswapfile 30 endfunction 31 32 function! s:DiffOpt() 33 if s:WinWidth() 34 set diffopt+=vertical 35 else 36 set diffopt-=vertical 37 endif 38 endfunction 39 40 function! s:NewWindow(...) 41 if a:0 == 0 42 let l:filename = '' 43 else 44 let l:filename = a:1 45 endif 46 if s:WinWidth() 47 execute 'vnew '.l:filename 48 else 49 execute 'new '.l:filename 50 endif 51 endfunction 52 53 function! s:Help(topic) 54 if s:WinWidth() 55 execute 'vert help '.a:topic 56 else 57 execute 'help '.a:topic 58 endif 59 endfunction 60 61 function! s:Buffer(...) 62 let l:splitcmd = s:WinWidth() ? 'vsplit' : 'split' 63 execute l:splitcmd 64 if a:0 == 1 65 execute bufnr(a:1).'buffer' 66 endif 67 endfunction 68 69 function! s:CmdSplit(command) 70 if s:WinWidth() 71 execute 'vertical '.a:command 72 else 73 execute a:command 74 endif 75 endfunction 76 77 augroup smartsplit 78 autocmd! 79 autocmd VimResized * call <sid>DiffOpt() 80 autocmd VimEnter * call <sid>DiffOpt() 81 augroup END 82 83 command! -complete=file -nargs=? New call <sid>NewWindow(<f-args>) 84 command! -complete=help -nargs=1 Help call <sid>Help(<f-args>) 85 command! -complete=buffer -nargs=1 Buffer call <sid>Buffer(<f-args>) 86 command! -complete=command -nargs=1 CmdSplit call <sid>CmdSplit(<f-args>) 87 command! Split call <sid>Buffer() 88 command! ScratchBuffer call <sid>ScratchBuffer() 89 90 let &cpo = s:save_cpo 91 unlet s:save_cpo 92 " vim: et sts=4 sw=4 tw=72