-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebars.vim
91 lines (73 loc) · 2.14 KB
/
sidebars.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
" original source:
" https://vi.stackexchange.com/questions/40071/how-to-close-terminal-and-nerdtree-windows-if-they-are-the-last-windows
function! IsSideBar(buf_nr)
" Returns 1 if the buffer is actual a side bar
" - A terminal
" - The NERDTree side bar
" - The QuickFix window
" - The Help window
" - The UndoTree side bar
" - ...
let buf_name = bufname(a:buf_nr)
let buf_type = getbufvar(a:buf_nr, '&filetype')
let readonly = getbufvar(a:buf_nr, '&readonly')
let term_buffers = term_list()
if readonly
return 1
elseif buf_type ==# 'qf'
" QuickFix, LocationList:
" Not Read Only
return 1
elseif buf_type ==# 'help'
" Read Only
" Help Window:
return 1
elseif buf_type ==# 'undotree'
" Not Read Only
return 1
elseif buf_type ==# 'nerdtree'
" Read Only
return 1
elseif index(term_buffers, a:buf_nr) >= 0
return 1
else
return 0
endif
endfunction
function! GetNumWindows()
" Returns the number of window that are not side bars
let num_windows = 0
for win_nr in range(1, winnr('$'))
let buf_nr = winbufnr(win_nr)
if IsSideBar(buf_nr)
continue
endif
let num_windows = num_windows + 1
endfor
return num_windows
endfunction
function! KillSideBars()
let num_windows = GetNumWindows()
if num_windows > 0
" If there are some non side bar windows do nothing:
return
endif
" Delete the terminal buffers that don't correspond to a window
let term_buffers = term_list()
for buf_nr in term_buffers
if len(win_findbuf(buf_nr)) == 0
" Exit terminal not associated to a window
execute 'bd! ' . buf_nr
endif
endfor
let term_buffers = term_list()
let buf_nr = bufnr('%')
if index(term_buffers, buf_nr) >= 0
" Kill the terminal buffer and quit
call feedkeys("\<C-w>:bd!\<CR>:quit\<CR>:\<BS>")
else
" Kill the side bar window
call feedkeys(":quit\<CR>:\<BS>")
endif
endfunction
autocmd BufEnter * call KillSideBars()