-
Notifications
You must be signed in to change notification settings - Fork 1
/
buf_itv2.vim
273 lines (242 loc) · 6.6 KB
/
buf_itv2.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"Author: zzsu ([email protected])
" Buffer list in statusline
" 2011-02-14 07:07:48 v4.0
"Modified: BitRobt([email protected])
" fix some bugs under windows
" fix some conflicts with NerdTree and Taglist plugin
"License: Copyright (c) 2001-2009, zzsu
" GNU General Public License version 2 for more details.
if exists('loaded_buf_it') || &cp
finish
endif
let g:showInStatusbar = 1
if(g:iswindows==1 && has("gui_running"))
autocmd BufRead * call CloseDefaultBuf()
endif
autocmd VimEnter,BufNew,BufEnter,BufWritePost,VimResized * call UpdateStatus()
autocmd InsertLeave * call BufEcho()
nmap <s-h> :call BufPrevPart()<cr>
nmap <s-l> :call BufNextPart()<cr>
noremap <leader>be :call BufEcho()<cr>
noremap <leader>bo :call BufOnly()<cr>
let g:bufBStr = ""
let g:bufNStr= ""
let g:bufAStr= ""
let g:statusbarKeepWidth = 20
hi NowBuf term=bold ctermfg=Cyan guifg=green guibg=blue gui=bold
if exists("g:showInStatusbar")
if !exists("g:statusbarUsrDef") || g:statusbarUsrDef == 0
set statusline=%m\{%{&ff}:%{&fenc}:%Y}\ %{g:bufBStr}%#NowBuf#%{g:bufNStr}%#StatusLine#%{g:bufAStr}%<%=%c:%l/%L%<
endif
endif
"buf index now
let g:bufNowIdx = 0
"buf part now (for multi line)
let s:bufNowPartIdx = 0
"buf names
let s:bufs = {}
"buf indexes
let s:bufnrs = {}
"buf Part(for multi line)
let s:bufPartStrList = []
function! CloseDefaultBuf()
"if there is a default buf just close it
if buflisted(1) && empty(bufname(1))
exec 'bd1'
endif
call UpdateStatus()
endfun
"unmap the keys
function! BufUnMap()
for i in keys(s:bufs)
"less than 10 files
if i < 10
exec "silent! unmap <M-".i.">"
else
exec "silent! unmap <M-".i/10."><M-".i%10.">"
endif
endfor
endfun
"map keys
function! BufMap()
for i in keys(s:bufs)
if i < 10
exec "silent! noremap <M-".i."> :call BufChange(".i.")<CR>"
else
exec "silent! noremap <M-".i/10."><M-".i%10."> :call BufChange(".i.")<CR>"
endif
endfor
endfunction
"close other bufs
function! BufOnly()
let i = 1
"bufnr('$')will return the last buf,
"buflisted(i) judges if the ist buf exists,
"buwinnr return the window contains the buf,
"winnr return the default window
while(i <= bufnr('$'))
"the ist buffer exists and can be modified
if buflisted(i) && getbufvar(i, "&modifiable")
\ && (bufwinnr(i) != winnr())
exec 'bw'.i
endif
let i = i + 1
endwhile
call UpdateStatus()
endfun
"close the buf
function! BufClose(force)
if(!a:force && &modified)
echohl WarningMsg | echo "Buffer Modified!" | echohl None
return
endif
let cmd=a:force?'bd!':'bd'
let qcmd=a:force?'q!':'q'
"deal conflicts with NerdTree
if exists("t:NERDTreeBufName") && bufname("%")==t:NERDTreeBufName
return
endif
"the window is command line window, just exit
if (bufname("%")=='[Command Line]')
exec qcmd
"it's not the window which is being edited
elseif index(values(s:bufnrs),winbufnr(0))==-1
exec cmd
"the window is being edited and multi buffer opened.
elseif len(s:bufnrs)>1
"nerdtree opened, switch to the next buffer and close the buffer
"before.It's just a bug.
if exists("t:NERDTreeBufName")
let ibufNow=bufnr("%")
call BufNextPart()
exec cmd.ibufNow
else
exec cmd
endif
"only one buf.close it for three times.One is for the current buf, another
"for nerdtree, the last for taglist.
else
exec qcmd
exec qcmd
exec qcmd
endif
call UpdateStatus()
endfun
"switch to some buf
function! BufChange(idx)
exec 'b! '.s:bufnrs[a:idx]
let g:bufNowIdx=a:idx
endfunction
"split window to show some buffer
function! BufSplit(idx)
exec 'sb! '.s:bufnrs[a:idx]
endfunction
"switch to the next buf
function! BufNextPart()
call UpdateStatus()
if index(values(s:bufnrs),winbufnr(0))==-1
return
endif
let g:bufNowIdx += 1
if g:bufNowIdx >len(s:bufnrs)
let g:bufNowIdx = 1
endif
call BufChange(g:bufNowIdx)
call UpdateBufPartStr()
endfunction
"switch to the pre buf
function! BufPrevPart()
call UpdateStatus()
if index(values(s:bufnrs),winbufnr(0))==-1
return
endif
let g:bufNowIdx -= 1
if g:bufNowIdx < 1
let g:bufNowIdx = len(s:bufnrs)
endif
call BufChange(g:bufNowIdx)
call UpdateBufPartStr()
endfunction
function! UpdateBufPartStr()
let [g:bufBStr, g:bufNStr, g:bufAStr] = s:bufPartStrList[s:bufNowPartIdx]
"one screen is not enough add '<<'
if s:bufNowPartIdx > 0
let g:bufBStr = '<<'.g:bufBStr
endif
"one screen is not enough add '>>'
if s:bufNowPartIdx < len(s:bufPartStrList)-1
let g:bufAStr = g:bufAStr.'>>'
endif
endfunction
function! UpdateStatus()
"unmap all keys
call BufUnMap()
let s:bufs = {}
let s:bufNowPartIdx = 0
let s:bufnrs = {}
let s:bufPartStrList = []
let idx = 1
let i = 1
while(i <= bufnr('$'))
"exists and modifiable
if buflisted(i) && getbufvar(i, "&modifiable")
"bufName: like 1-buf_it.vim+
let bufName = idx."-"
let bufName .= fnamemodify(bufname(i), ":t")
let bufName .= getbufvar(i, "&modified")? "+":''
let bufName .= " "
let s:bufs[idx] = bufName
let s:bufnrs[idx] = i
let idx += 1
endif
let i += 1
endwhile
if empty(s:bufs)
return
endif
"the max width
let widthForBufStr = winwidth(0) - g:statusbarKeepWidth
let [POSB,POSN,POSA] = [0, 1, 2]
let [strB,strN,strA] = ["", "", ""]
let strPos = POSB
let partIdx = 0
"
for i in keys(s:bufs)
let bufName = s:bufs[i]
"the current buffer
if bufnr("%") == s:bufnrs[i]
let strPos = POSN
endif
"buffer before
if strPos == POSB
let strB .= bufName
"buffer now
elseif strPos == POSN
let strN .= bufName
let strPos = POSA
"get the buffer part which contains buffer now
let s:bufNowPartIdx = partIdx
"buffer past
elseif strPos == POSA
let strA .= bufName
endif
if len(strB.strN.strA.bufName) > widthForBufStr
call add(s:bufPartStrList, [strB, strN, strA])
let [strB,strN,strA] = ["", "", ""]
let partIdx += 1
endif
endfor
if strB != "" || strN != "" || strA != ""
call add(s:bufPartStrList, [strB, strN, strA])
endif
let g:bufNowIdx=index(values(s:bufnrs),bufnr("%"))+1
call UpdateBufPartStr()
call BufMap()
call BufEcho()
endfunction
function! BufEcho()
redraw
"let msg = g:bufBStr.'['.g:bufNStr.']'.g:bufAStr
let msg=fnamemodify(bufname("%"),":p")
echo msg
endfunction