Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 60 lines (57 sloc) 1.697 kb
2d9f25a Nate Version 1.0: Initial upload
Soares authored
1 " butane.vim: Light your buffers up.
2 "
3 " Author: Vim Tip #165 <http://vim.wikia.com/wiki/VimTip165>
4 " Maintainer: Nate Soares <http://so8r.es>
5 " Version: 1.0
6 " License: The same as vim itself. (See |license|)
7
8 if exists("g:loaded_butane") || &cp || v:version < 700
9 finish
10 endif
11 let g:loaded_butane = 1
12
13 function! s:Bclose(bang, buffer)
14 if empty(a:buffer)
15 let btarget = bufnr('%')
16 elseif a:buffer =~ '^\d\+$'
17 let btarget = bufnr(str2nr(a:buffer))
18 else
19 let btarget = bufnr(a:buffer)
20 endif
21 if btarget < 0
22 echoerr 'No matching buffer for '.a:buffer
23 return
24 endif
25 if empty(a:bang) && getbufvar(btarget, '&modified')
26 echoerr 'No write since last change. Use :Bclose!'
27 return
28 endif
29 " Numbers of windows that view target buffer which we will delete.
30 let wnums = filter(range(1, winnr('$')), 'winbufnr(v:val) == btarget')
31 let wcurrent = winnr()
32 for w in wnums
33 execute w.'wincmd w'
34 let prevbuf = bufnr('#')
35 if prevbuf > 0 && buflisted(prevbuf) && prevbuf != w
36 buffer #
37 else
38 bprevious
39 endif
40 if btarget == bufnr('%')
41 " Numbers of listed buffers which are not the target to be deleted.
42 let blisted = filter(range(1, bufnr('$')),
43 \ 'buflisted(v:val) && v:val != btarget')
44 " Listed, not target, and not displayed.
45 let bhidden = filter(copy(blisted), 'bufwinnr(v:val) < 0')
46 " Take the first buffer, if any (could be more intelligent).
47 let bjump = (bhidden + blisted + [-1])[0]
48 if bjump > 0
49 execute 'buffer '.bjump
50 else
51 execute 'enew'.a:bang
52 endif
53 endif
54 endfor
55 execute 'bdelete'.a:bang.' '.btarget
56 execute wcurrent.'wincmd w'
57 endfunction
58 command! -bang -complete=buffer -nargs=?
59 \ Bclose call s:Bclose('<bang>', '<args>')
Something went wrong with that request. Please try again.