Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defx support #2282

Merged
merged 28 commits into from
Mar 10, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix defx key binding
  • Loading branch information
wsdjeg committed Mar 10, 2019
commit 3a6f204588a9103c269d0abc17dce94a6b0d438b
40 changes: 32 additions & 8 deletions config/plugins/defx.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

" defx supported is added in https://github.com/SpaceVim/SpaceVim/pull/2282

let s:SYS = SpaceVim#api#import('system')

call defx#custom#option('_', {
\ 'winwidth': g:spacevim_sidebar_width,
\ 'split': 'vertical',
Expand Down Expand Up @@ -76,23 +78,20 @@ function! s:defx_init()
\ defx#do_action('move')
nnoremap <silent><buffer><expr> p
\ defx#do_action('paste')
nnoremap <silent><buffer><expr> h
\ defx#do_action('close_tree')
nnoremap <silent><buffer><expr> <Left>
\ defx#is_directory() ?
\ defx#do_action('close_tree') : ''
nnoremap <silent><buffer><expr> h defx#do_action('call', 'DefxSmartH')
nnoremap <silent><buffer><expr> <Left> defx#do_action('call', 'DefxSmartH')
nnoremap <silent><buffer><expr> l
\ defx#is_directory() ?
\ defx#do_action('open_tree') : defx#do_action('open')
nnoremap <silent><buffer><expr> <Right>
\ defx#is_directory() ?
\ defx#do_action('open_tree') : defx#do_action('open')
nnoremap <silent><buffer><expr> <Cr>
\ defx#is_directory() ?
\ defx#do_action('open_directory') : defx#do_action('drop')
nnoremap <silent><buffer><expr> <2-LeftMouse>
\ defx#is_directory() ?
\ defx#do_action('open_tree') : defx#do_action('drop')
nnoremap <silent><buffer><expr> <Right>
\ defx#is_directory() ?
\ defx#do_action('open_tree') : defx#do_action('open')
nnoremap <silent><buffer><expr> E
\ defx#do_action('open', 'vsplit')
nnoremap <silent><buffer><expr> P
Expand Down Expand Up @@ -128,3 +127,28 @@ function! s:defx_init()
nnoremap <silent><buffer><expr> cd
\ defx#do_action('change_vim_cwd')
endf

function! DefxSmartH(_)
" candidate is opend tree?
if defx#is_opened_tree()
return defx#call_action('close_tree')
endif

" parent is root?
let s:candidate = defx#get_candidate()
let s:parent = fnamemodify(s:candidate['action__path'], s:candidate['is_directory'] ? ':p:h:h' : ':p:h')
let sep = s:SYS.isWindows ? '\\' : '/'
if s:trim_right(s:parent, sep) == s:trim_right(b:defx.paths[0], sep)
return defx#call_action('cd', ['..'])
endif

" move to parent.
call defx#call_action('search', s:parent)

" if you want close_tree immediately, enable below line.
call defx#call_action('close_tree')
endfunction

function! s:trim_right(str, trim)
return substitute(a:str, printf('%s$', a:trim), '', 'g')
endfunction