forked from el-iot/buffer-tree-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplorer.vim
138 lines (111 loc) · 3.37 KB
/
explorer.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
let s:buffername = "BufferTree"
let s:previousWinId = -1
function! PressedEnter()
let current_line_contents = getline('.')
let file_regex = '\v^(:?├|─|└|│|\s)+[◎•] *(\w|\.|\/|-)+ ⇒ (\d+)$'
let current_file_regex = '\v^(:?├|─|└|│|\s)+◎ *(\w|\.|\/|-)+ ⇒ (\d+)$'
let match = matchlist(current_line_contents, file_regex)
if len(match) == 0
return
endif
setlocal modifiable
for allowed_line in b:allowed_lines
let line_contents = getline(allowed_line)
if len(matchlist(line_contents, current_file_regex)) > 0
call setline(allowed_line, substitute(line_contents, '◎', '•', ''))
break
endif
endfor
let destination_line_number = getcurpos()[1]
call setline(destination_line_number, substitute(current_line_contents, '•', '◎',''))
setlocal nomodifiable
if g:buffertree_close_on_enter == 1
execute "bd"
endif
execute "tabnew"
execute "b" . match[3]
endfunction
function! GetLineAbove()
let pos = getcurpos()[1]
let idx = 0
for allowed_line in b:allowed_lines
if allowed_line == pos
let new_pos = b:allowed_lines[(idx - 1) % len(b:allowed_lines)]
return new_pos
endif
let idx = idx + 1
endfor
endfunction
function! PressedDelete()
let current_line_contents = getline('.')
let file_regex = '\v^(:?├|─|└|│|\s)+[◎•] *(\w|\.|\/|-)+ ⇒ (\d+)$'
let match = matchlist(current_line_contents, file_regex)
let new_pos = GetLineAbove()
execute "bd" . match[3]
call RefreshBuffer()
call cursor(new_pos, 0)
endfunction
function! ScrollUp()
call ScrollHelper(-1)
endfunction
function! ScrollDown()
call ScrollHelper(1)
endfunction
function! ScrollHelper(delta)
let pos = getcurpos()[1]
let idx = 0
for allowed_line in b:allowed_lines
if allowed_line == pos
call cursor(b:allowed_lines[(idx + a:delta) % len(b:allowed_lines)], 0)
return
endif
let idx = idx + 1
endfor
call cursor(b:allowed_lines[0], 0)
endfunction
function! RefreshBuffer()
let result = buffer#RefreshBuffer()
let b:allowed_lines = result[1]
" call cursor(result[0], 0)
endfunction
function! explorer#Explore()
let previous_buffer = bufnr()
let tree = tree#BufferTree()
let result = buffer#MakeBuffer(tree, previous_buffer)
let b:allowed_lines = result[1]
" call cursor(result[0], 0)
nnoremap <buffer> <silent> <CR> :call PressedEnter()<cr>
nnoremap <buffer> <silent> k :call ScrollUp()<cr>
nnoremap <buffer> <silent> j :call ScrollDown()<cr>
nnoremap <buffer> <silent> d :call PressedDelete()<cr>
augroup CursorLine
au!
au VimEnter,WinEnter,BufWinEnter BufferTree call RefreshBuffer()
au VimEnter,WinEnter,BufWinEnter [^BufferTree] call RefreshBuffer()
augroup END
endfunction
function! s:Close()
let bufnr = bufnr(s:buffername)
if bufnr > 0 && bufexists(bufnr)
if s:previousWinId > 0
call win_gotoid(s:previousWinId)
endif
execute 'bwipeout! ' . bufnr
endif
endfunction
function! explorer#Toggle()
let bufnr = bufnr(s:buffername)
if bufnr > 0 && bufexists(bufnr)
call s:Close()
for i in range(1, bufnr('$'))
if !filereadable(fnamemodify(bufname(bufnr(i)), ":p"))
if &buftype == "terminal"
else
silent! exe 'bdelete! ' . i
endif
endif
endfor
else
call explorer#Explore()
endif
endfunction