/
func.vim
515 lines (471 loc) · 14 KB
/
func.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
if !exists('g:env')
finish
endif
" func s:toggle_option() {{{1
function! s:toggle_option(option_name)
if exists('&' . a:option_name)
execute 'setlocal' a:option_name . '!'
execute 'setlocal' a:option_name . '?'
endif
endfunction
" func s:toggle_variable() {{{1
function! s:toggle_variable(variable_name)
if eval(a:variable_name)
execute 'let' a:variable_name . ' = 0'
else
execute 'let' a:variable_name . ' = 1'
endif
echo printf('%s = %s', a:variable_name, eval(a:variable_name))
endfunction
" func s:ls() {{{1
function! s:ls(path, bang)
let path = empty(a:path) ? getcwd() : expand(a:path)
if filereadable(path)
if executable("ls")
echo system("ls -l " . path)
return v:shell_error ? g:false : g:true
else
return s:error('ls: command not found')
endif
endif
if !isdirectory(path)
return s:error(path.":No such file or directory")
endif
let save_ignore = &wildignore
set wildignore=
let filelist = glob(path . "/*")
if !empty(a:bang)
let filelist .= "\n".glob(path . "/.*[^.]")
endif
let &wildignore = save_ignore
let filelist = substitute(filelist, '', '^M', 'g')
if empty(filelist)
return s:error("no file")
endif
let lists = []
for file in split(filelist, "\n")
if isdirectory(file)
call add(lists, fnamemodify(file, ":t") . "/")
else
if executable(file)
call add(lists, fnamemodify(file, ":t") . "*")
elseif getftype(file) == 'link'
call add(lists, fnamemodify(file, ":t") . "@")
else
call add(lists, fnamemodify(file, ":t"))
endif
endif
endfor
echohl WarningMsg | echon len(lists) . ":\t" | echohl None
highlight LsDirectory cterm=bold ctermfg=NONE ctermfg=26 gui=bold guifg=#0096FF guibg=NONE
highlight LsExecutable cterm=NONE ctermfg=NONE ctermfg=Green gui=NONE guifg=Green guibg=NONE
highlight LsSymbolick cterm=NONE ctermfg=NONE ctermfg=LightBlue gui=NONE guifg=LightBlue guibg=NONE
for item in lists
if item =~ '/'
echohl LsDirectory | echon item[:-2] | echohl NONE
echon item[-1:-1] . " "
elseif item =~ '*'
echohl LsExecutable | echon item[:-2] | echohl NONE
echon item[-1:-1] . " "
elseif item =~ '@'
echohl LsSymbolick | echon item[:-2] | echohl NONE
echon item[-1:-1] . " "
else
echon item . " "
endif
endfor
return g:true
endfunction
" func s:count_buffers() {{{1
function! s:count_buffers()
let l:count = 0
for i in range(1, bufnr('$'))
if bufexists(i) && buflisted(i)
let l:count += 1
endif
endfor
return l:count
endfunction
" func s:get_buflists() {{{1
function! s:get_buflists(...)
if a:0 && a:1 ==# 'n'
silent bnext
elseif a:0 && a:1 ==# 'p'
silent bprev
endif
let list = ''
let lists = []
for buf in range(1, bufnr('$'))
if bufexists(buf) && buflisted(buf)
let list = bufnr(buf) . "#" . fnamemodify(bufname(buf), ':t')
let list .= getbufvar(buf, "&modified") ? '+' : ''
if bufnr('%') ==# buf
let list = "[" . list . "]"
else
let list = " " . list . " "
endif
call add(lists, list)
endif
endfor
redraw | echo join(lists, "")
endfunction
" func s:smart_bwipeout() {{{1
function! s:smart_bwipeout(mode)
" Bwipeout! all buffers except current buffer.
if a:mode == 1
for i in range(1, bufnr('$'))
if bufexists(i)
if bufnr('%') ==# i | continue | endif
execute 'silent bwipeout! ' . i
endif
endfor
return
endif
if a:mode == 0
if winnr('$') != 1
quit
return
elseif tabpagenr('$') != 1
tabclose
return
endif
endif
let bufname = empty(bufname(bufnr('%'))) ? bufnr('%') . "#" : bufname(bufnr('%'))
if &modified == 1
echo printf("'%s' is unsaved. Quit!? [y(f)/N/w] ", bufname)
let c = nr2char(getchar())
if c ==? 'w'
let filename = ''
if bufname(bufnr("%")) ==# filename
redraw
while empty(filename)
let filename = input('Tell me filename: ')
endwhile
endif
execute "write " . filename
silent bwipeout!
elseif c ==? 'y' || c ==? 'f'
silent bwipeout!
else
redraw
echo "Do nothing"
return
endif
else
silent bwipeout
endif
if s:has_plugin("vim-buftabs")
echo "Bwipeout " . bufname
else
redraw
call <SID>get_buflists()
endif
endfunction
" func s:smart_bchange() {{{1
function! s:smart_bchange(mode)
let mode = a:mode
" If window splitted, no working
if winnr('$') != 1
" Normal bnext/bprev
execute 'silent' mode ==? 'n' ? 'bnext' : 'bprevious'
if exists("*s:get_buflists") && exists("*s:count_buffers")
if s:count_buffers() > 1
call s:get_buflists()
endif
endif
return
endif
" Get all buffer numbers in tabpages
let tablist = []
for i in range(tabpagenr('$'))
call add(tablist, tabpagebuflist(i + 1))
endfor
" Get buffer number
execute 'silent' mode ==? 'n' ? 'bnext' : 'bprevious'
let bufnr = bufnr('%')
execute 'silent' mode ==? 'n' ? 'bprevious' : 'bnext'
" Check next/prev buffer number if exists in l:tablist
let nextbuf = []
call add(nextbuf, bufnr)
if index(tablist, nextbuf) >= 0
execute 'silent tabnext' index(tablist, nextbuf) + 1
else
" Normal bnext/bprev
execute 'silent' mode ==? 'n' ? 'bnext' : 'bprevious'
endif
endfunction
function! s:bufnew(buf, bang)
let buf = empty(a:buf) ? '' : a:buf
execute "new" buf | only
if !empty(a:bang)
let bufname = empty(buf) ? '[Scratch]' : buf
setlocal bufhidden=unload
setlocal nobuflisted
setlocal buftype=nofile
setlocal noswapfile
silent file `=bufname`
endif
endfunction
" func s:buf_enqueue() {{{1
function! s:buf_enqueue(buf)
let buf = fnamemodify(a:buf, ':p')
if bufexists(buf) && buflisted(buf) && filereadable(buf)
let idx = match(s:bufqueue ,buf)
if idx != -1
call remove(s:bufqueue, idx)
endif
call add(s:bufqueue, buf)
endif
endfunction
" func s:buf_dequeue() {{{1
function! s:buf_dequeue(buf)
if empty(s:bufqueue)
throw 'bufqueue: Empty queue.'
endif
if a:buf =~# '\d\+'
return remove(s:bufqueue, a:buf)
else
return remove(s:bufqueue, index(s:bufqueue, a:buf))
endif
endfunction
" func s:buf_restore() {{{1
function! s:buf_restore()
try
execute 'edit' s:buf_dequeue(-1)
catch /^bufqueue:/
"echohl ErrorMsg
"echomsg v:exception
"echohl None
call s:error(v:exception)
endtry
endfunction
" func s:all_buffers_bwipeout() {{{1
function! s:all_buffers_bwipeout()
for i in range(1, bufnr('$'))
if bufexists(i) && buflisted(i)
execute 'bwipeout' i
endif
endfor
endfunction
" func s:win_tab_switcher() {{{1
function! s:win_tab_switcher(...)
let minus = 0
if &laststatus == 1 && winnr('$') != 1
let minus += 1
elseif &laststatus == 2
let minus += 1
endif
let minus += &cmdheight
if &showtabline == 1 && tabpagenr('$') != 1
let minus += 1
elseif &showtabline == 2
let minus += 1
endif
let is_split = winheight(0) != &lines - minus
let is_vsplit = winwidth(0) != &columns
let is_tabpage = tabpagenr('$') >= 2
let buffer_switcher = get(g:, 'buffer_switcher', 0)
if a:0 && a:1 ==# 'l'
if is_tabpage
if tabpagenr() == tabpagenr('$')
if !is_split && !is_vsplit
if buffer_switcher
silent bnext
else
echohl WarningMsg
echo 'Last tabpages'
echohl None
endif
endif
if (is_split || is_vsplit) && winnr() == winnr('$')
if buffer_switcher
silent bnext
else
echohl WarningMsg
echo 'Last tabpages'
echohl None
endif
elseif (is_split || is_vsplit) && winnr() != winnr('$')
silent wincmd w
endif
else
if !is_split && !is_vsplit
silent tabnext
endif
if (is_split || is_vsplit) && winnr() == winnr('$')
silent tabnext
elseif (is_split || is_vsplit) && winnr() != winnr('$')
silent wincmd w
endif
endif
else
if !is_split && !is_vsplit
if buffer_switcher
silent bnext
else
echohl WarningMsg
echo 'Last tabpages'
echohl None
endif
endif
if (is_split || is_vsplit) && winnr() == winnr('$')
if buffer_switcher
silent bnext
else
echohl WarningMsg
echo 'Last tabpages'
echohl None
endif
else
silent wincmd w
endif
endif
endif
if a:0 && a:1 ==# 'h'
if is_tabpage
if tabpagenr() == 1
if !is_split && !is_vsplit
if buffer_switcher
silent bprevious
else
echohl WarningMsg
echo 'First tabpages'
echohl None
endif
endif
if (is_split || is_vsplit) && winnr() == 1
if buffer_switcher
silent bprevious
else
echohl WarningMsg
echo 'First tabpages'
echohl None
endif
elseif (is_split || is_vsplit) && winnr() != 1
silent wincmd W
endif
else
if !is_split && !is_vsplit
silent tabprevious
endif
if (is_split || is_vsplit) && winnr() == 1
silent tabprevious
elseif (is_split || is_vsplit) && winnr() != 1
silent wincmd W
endif
endif
else
if !is_split && !is_vsplit
if buffer_switcher
silent bprevious
else
echohl WarningMsg
echo 'First tabpages'
echohl None
endif
endif
if (is_split || is_vsplit) && winnr() == 1
if buffer_switcher
silent bprevious
else
echohl WarningMsg
echo 'First tabpages'
echohl None
endif
else
silent wincmd W
endif
endif
endif
if s:has_plugin("vim-buftabs")
else
redraw
call <SID>get_buflists()
endif
endfunction
" func s:tabdrop() {{{1
function! s:tabdrop(target)
let target = empty(a:target) ? expand('%:p') : bufname(a:target + 0)
if !empty(target) && bufexists(target) && buflisted(target)
execute 'tabedit' target
else
call s:warning("Could not tabedit")
endif
endfunction
" func s:tabnew() {{{1
function! s:tabnew(num)
let num = empty(a:num) ? 1 : a:num
for i in range(1, num)
tabnew
endfor
endfunction
" func s:move_tabpage() {{{1
function! s:move_tabpage(dir)
if a:dir == "right"
let num = tabpagenr()
elseif a:dir == "left"
let num = tabpagenr() - 2
endif
if num >= 0
execute "tabmove" num
endif
endfunction
" func s:close_all_right_tabpages() {{{1
function! s:close_all_right_tabpages()
let current_tabnr = tabpagenr()
let last_tabnr = tabpagenr("$")
let num_close = last_tabnr - current_tabnr
let i = 0
while i < num_close
execute "tabclose " . (current_tabnr + 1)
let i = i + 1
endwhile
endfunction
" func s:close_all_left_tabpages() {{{1
function! s:close_all_left_tabpages()
let current_tabnr = tabpagenr()
let num_close = current_tabnr - 1
let i = 0
while i < num_close
execute "tabclose 1"
let i = i + 1
endwhile
endfunction
" func s:find_tabnr() {{{1
function! s:find_tabnr(bufnr)
for tabnr in range(1, tabpagenr("$"))
if index(tabpagebuflist(tabnr), a:bufnr) !=# -1
return tabnr
endif
endfor
return -1
endfunction
" func s:find_winnr() {{{1
function! s:find_winnr(bufnr)
for winnr in range(1, winnr("$"))
if a:bufnr ==# winbufnr(winnr)
return winnr
endif
endfor
return 1
endfunction
" func s:find_winnr() {{{1
function! s:recycle_open(default_open, path)
let default_action = a:default_open . ' ' . a:path
if bufexists(a:path)
let bufnr = bufnr(a:path)
let tabnr = s:find_tabnr(bufnr)
if tabnr ==# -1
execute default_action
return
endif
execute 'tabnext ' . tabnr
let winnr = s:find_winnr(bufnr)
execute winnr . 'wincmd w'
else
execute default_action
endif
endfunction
" __END__ {{{1
" vim:fdm=marker expandtab fdc=3: