forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefx.vim
More file actions
236 lines (201 loc) · 7.76 KB
/
defx.vim
File metadata and controls
236 lines (201 loc) · 7.76 KB
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
" Defx settings
" ---
" See https://github.com/shougo/defx.nvim
call defx#custom#option('_', {
\ 'columns': 'indent:git:icons:filename',
\ 'winwidth': 25,
\ 'split': 'vertical',
\ 'direction': 'topleft',
\ 'listed': 1,
\ 'show_ignored_files': 0,
\ 'root_marker': '≡ ',
\ 'ignored_files':
\ '.mypy_cache,.pytest_cache,.git,.hg,.svn,.stversions'
\ . ',__pycache__,.sass-cache,*.egg-info,.DS_Store,*.pyc'
\ })
call defx#custom#column('git', {
\ 'indicators': {
\ 'Modified' : '•',
\ 'Staged' : '✚',
\ 'Untracked' : 'ᵁ',
\ 'Renamed' : '≫',
\ 'Unmerged' : '≠',
\ 'Ignored' : 'ⁱ',
\ 'Deleted' : '✖',
\ 'Unknown' : '⁇'
\ }
\ })
" call defx#custom#column('filename', { 'min_width': 5, 'max_width': 55 })
" defx-icons plugin
let g:defx_icons_column_length = 2
let g:defx_icons_mark_icon = '✓'
let g:defx_icons_parent_icon = ''
" Events
" ---
augroup user_plugin_defx
autocmd!
" FIXME
" autocmd DirChanged * call s:defx_refresh_cwd(v:event)
" Delete defx if it's the only buffer left in the window
autocmd WinEnter * if &filetype == 'defx' && winnr('$') == 1 | q | endif
" Move focus to the next window if current buffer is defx
autocmd TabLeave * if &filetype == 'defx' | wincmd w | endif
autocmd TabClosed * call s:defx_close_tab(expand('<afile>'))
" Define defx window mappings
autocmd FileType defx call s:defx_mappings()
augroup END
" Internal functions
" ---
function! s:defx_close_tab(tabnr)
" When a tab is closed, find and delete any associated defx buffers
for l:nr in range(1, bufnr('$'))
let l:defx = getbufvar(l:nr, 'defx')
if empty(l:defx)
continue
endif
let l:context = get(l:defx, 'context', {})
if get(l:context, 'buffer_name', '') ==# 'tab' . a:tabnr
silent! execute 'bdelete '.l:nr
break
endif
endfor
endfunction
function! s:defx_toggle_tree() abort
" Open current file, or toggle directory expand/collapse
if defx#is_directory()
return defx#do_action('open_or_close_tree')
endif
return defx#do_action('multi', ['drop', 'quit'])
endfunction
function! s:defx_refresh_cwd(event)
" Automatically refresh opened Defx windows when changing working-directory
let l:cwd = get(a:event, 'cwd', '')
let l:scope = get(a:event, 'scope', '') " global, tab, window
let l:is_opened = bufwinnr('defx') > -1
if ! l:is_opened || empty(l:cwd) || empty(l:scope)
return
endif
" Abort if Defx is already on the cwd triggered (new files trigger this)
let l:paths = get(getbufvar('defx', 'defx', {}), 'paths', [])
if index(l:paths, l:cwd) >= 0
return
endif
let l:tab = tabpagenr()
call execute(printf('Defx -buffer-name=tab%s %s', l:tab, l:cwd))
wincmd p
endfunction
function! s:jump_dirty(dir) abort
" Jump to the next position with defx-git dirty symbols
let l:icons = get(g:, 'defx_git_indicators', {})
let l:icons_pattern = join(values(l:icons), '\|')
if ! empty(l:icons_pattern)
let l:direction = a:dir > 0 ? 'w' : 'bw'
return search(printf('\(%s\)', l:icons_pattern), l:direction)
endif
endfunction
function! s:defx_mappings() abort
" Defx window keyboard mappings
setlocal signcolumn=no
nnoremap <silent><buffer><expr> se defx#do_action('save_session')
nnoremap <silent><buffer><expr> <CR> <SID>defx_toggle_tree()
nnoremap <silent><buffer><expr> l <SID>defx_toggle_tree()
nnoremap <silent><buffer><expr> h defx#do_action('close_tree')
nnoremap <silent><buffer><expr> t defx#do_action('open_tree_recursive')
nnoremap <silent><buffer><expr> <BS> defx#async_action('cd', ['..'])
nnoremap <silent><buffer><expr> st defx#do_action('multi', [['drop', 'tabnew'], 'quit'])
nnoremap <silent><buffer><expr> sg defx#do_action('multi', [['drop', 'vsplit'], 'quit'])
nnoremap <silent><buffer><expr> sv defx#do_action('multi', [['drop', 'split'], 'quit'])
nnoremap <silent><buffer><expr> P defx#do_action('open', 'pedit')
nnoremap <silent><buffer><expr> K defx#do_action('new_directory')
nnoremap <silent><buffer><expr> N defx#do_action('new_multiple_files')
nnoremap <silent><buffer><expr> dd defx#do_action('remove_trash')
nnoremap <silent><buffer><expr> r defx#do_action('rename')
nnoremap <silent><buffer><expr> x defx#do_action('execute_system')
nnoremap <silent><buffer><expr> . defx#do_action('toggle_ignored_files')
nnoremap <silent><buffer><expr> yy defx#do_action('yank_path')
nnoremap <silent><buffer><expr> ~ defx#async_action('cd')
nnoremap <silent><buffer><expr> q defx#do_action('quit')
nnoremap <silent><buffer><expr> <Tab> winnr('$') != 1 ?
\ ':<C-u>wincmd w<CR>' :
\ ':<C-u>Defx -buffer-name=temp -split=vertical<CR>'
nnoremap <silent><buffer> [g :<C-u>call <SID>jump_dirty(-1)<CR>
nnoremap <silent><buffer> ]g :<C-u>call <SID>jump_dirty(1)<CR>
nnoremap <silent><buffer><expr><nowait> \ defx#do_action('cd', getcwd())
nnoremap <silent><buffer><expr><nowait> & defx#do_action('cd', getcwd())
nnoremap <silent><buffer><expr><nowait> c defx#do_action('copy')
nnoremap <silent><buffer><expr><nowait> m defx#do_action('move')
nnoremap <silent><buffer><expr><nowait> p defx#do_action('paste')
nnoremap <silent><buffer><expr><nowait> <Space>
\ defx#do_action('toggle_select') . 'j'
nnoremap <silent><buffer><expr> ' defx#do_action('toggle_select') . 'j'
nnoremap <silent><buffer><expr> * defx#do_action('toggle_select_all')
nnoremap <silent><buffer><expr> <C-r> defx#do_action('redraw')
nnoremap <silent><buffer><expr> <C-g> defx#do_action('print')
nnoremap <silent><buffer><expr> S defx#do_action('toggle_sort', 'Time')
nnoremap <silent><buffer><expr> C
\ defx#do_action('toggle_columns', 'indent:mark:filename:type:size:time')
" Tools
nnoremap <silent><buffer><expr> w defx#async_action('call', '<SID>toggle_width')
nnoremap <silent><buffer><expr> gx defx#async_action('execute_system')
nnoremap <silent><buffer><expr> gd defx#async_action('multi', ['drop', ['call', '<SID>git_diff']])
nnoremap <silent><buffer><expr> gr defx#do_action('call', '<SID>grep')
nnoremap <silent><buffer><expr> gf defx#do_action('call', '<SID>find_files')
if exists('$TMUX')
nnoremap <silent><buffer><expr> gl defx#async_action('call', '<SID>explorer')
endif
endfunction
" TOOLS
" ---
function! s:git_diff(context) abort
execute 'GdiffThis'
endfunction
function! s:find_files(context) abort
" Find files in parent directory with Denite
let l:target = a:context['targets'][0]
let l:parent = fnamemodify(l:target, ':h')
silent execute 'wincmd w'
silent execute 'Denite file/rec:'.l:parent
endfunction
function! s:grep(context) abort
" Grep in parent directory with Denite
let l:target = a:context['targets'][0]
let l:parent = fnamemodify(l:target, ':h')
silent execute 'wincmd w'
silent execute 'Denite grep:'.l:parent
endfunction
function! s:toggle_width(context) abort
" Toggle between defx window width and longest line
let l:max = 0
let l:original = a:context['winwidth']
for l:line in range(1, line('$'))
let l:len = len(getline(l:line))
if l:len > l:max
let l:max = l:len
endif
endfor
execute 'vertical resize ' . (l:max == winwidth('.') ? l:original : l:max)
endfunction
function! s:explorer(context) abort
" Open file-explorer split with tmux
let l:explorer = s:find_file_explorer()
if empty('$TMUX') || empty(l:explorer)
return
endif
let l:target = a:context['targets'][0]
let l:parent = fnamemodify(l:target, ':h')
let l:cmd = 'split-window -p 30 -c ' . l:parent . ' ' . l:explorer
silent execute '!tmux ' . l:cmd
endfunction
function! s:find_file_explorer() abort
" Detect terminal file-explorer
let s:file_explorer = get(g:, 'terminal_file_explorer', '')
if empty(s:file_explorer)
for l:explorer in ['lf', 'hunter', 'ranger', 'vifm']
if executable(l:explorer)
let s:file_explorer = l:explorer
break
endif
endfor
endif
return s:file_explorer
endfunction