/
gotobuffer.vim
156 lines (132 loc) · 3.47 KB
/
gotobuffer.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
"
" GoToBuffer
"
" Author: Martin Lundberg
" Version: 0.1
"
"if exists('g:loaded_gotobuffer')
"finish
"endif
"let g:loaded_gotobuffer = 1
if !exists('g:gotobuffer_maxheight')
let g:gotobuffer_maxheight = 10
endif
function! s:GoToBufferToggle()
if bufexists(bufnr("__GOTOBUFFER__"))
execute ":" . bufnr("__GOTOBUFFER__") . 'bwipeout'
return
endif
let s:pattern = ''
let buffers = s:get_all_buffers()
let height = len(buffers) < g:gotobuffer_maxheight ? len(buffers) : g:gotobuffer_maxheight
call s:create_window(height)
call s:init_mappings()
" Remove insert abbreviations from buffer
iabc <buffer>
setlocal modifiable
for filename in buffers
put = filename
endfor
setlocal nomodifiable
endfunction
function! s:handle_key(char)
let s:pattern .= a:char
call s:filter_list()
endfunction
function! s:filter_list()
let buffers = s:get_all_buffers()
let height = len(buffers) < g:gotobuffer_maxheight ? len(buffers) : g:gotobuffer_maxheight
call s:set_window_height(height)
call s:clear_buffer()
setlocal modifiable
echo s:pattern
for filename in buffers
put = filename
endfor
setlocal nomodifiable
endfunction
function! s:init_mappings()
" Mappings for handling pattern
let numbers = [0,1,2,3,4,5,6,7,8,9]
let lowercase = split('abcdefghijklmnopqrstuvwxys', '\zs')
let uppercase = split('ABCDEFGHIJKLMNOPQRSTUVWXYS', '\zs')
"let punctuation = split('<>`@#~!"$%&/()=+*-_.,;:?\{}[] ', '\zs')
let punctuation = split('/._', '\zs')
let characters = numbers + lowercase + uppercase + punctuation
for char in characters
exec 'nnoremap <buffer> ' . char . ' :call <SID>handle_key("' . char . '")<CR>'
endfor
" Function mappings
nnoremap <buffer> <CR> :call <SID>LoadSelectedBuffer()<CR>
nnoremap <buffer> <Esc> :call <SID>close_window()<CR>
nnoremap <buffer> <BS> :call <SID>backspace()<CR>
nnoremap <buffer> <C-N> :call <SID>move_down()<CR>
nnoremap <buffer> <C-P> :call <SID>move_up()<CR>
endfunction
function! s:create_window(height)
" Create a new window and set some local options
execute "botright " . a:height . "new __GOTOBUFFER__"
setlocal noshowcmd
setlocal noswapfile
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal nowrap
setlocal nobuflisted
setlocal nospell
setlocal nomodified
setlocal nonumber
setlocal cursorline
endfunction
function! s:set_window_height(height)
setlocal modifiable
resize a:height
setlocal nomodifiable
endfunction
function! s:clear_buffer()
setlocal modifiable
%d
setlocal nomodifiable
endfunction
function! s:close_window()
bwipeout
endfunction
function! s:backspace()
let s:pattern = strpart(s:pattern, 0, (strchars(s:pattern) - 1))
echo s:pattern
call s:filter_list()
endfunction
function! s:move_up()
let line = line('.') - 1
call cursor(line, 0)
endfunction
function! s:move_down()
let line = line('.') + 1
call cursor(line, 0)
endfunction
function! s:get_all_buffers()
let buffers = []
" bufnr('$') gets the latest buffer number
let bufcount = bufnr('$')
let i = 0
while i <= bufcount
let i += 1
let bufname = bufname(l:i)
if strlen(bufname) && getbufvar(i, '&modifiable') && getbufvar(i, '&buflisted')
if bufname =~ s:pattern
call add(buffers, bufname)
endif
endif
endwhile
return buffers
endfunction
function! s:LoadSelectedBuffer()
let filename = s:get_selected_buffer()
" Remove the buffer list
bwipeout
exec ":buffer " . l:filename
endfunction
function! s:get_selected_buffer()
let line = getline('.')
return line
endfunction
nmap <leader>f :call <SID>GoToBufferToggle()<CR>