From f1e57311ca619f8b402dcd1db58d041738412af6 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Wed, 13 Apr 2022 10:08:57 +0800 Subject: [PATCH] feat(defx): use `p` to toggle preview windows close https://github.com/SpaceVim/SpaceVim/issues/4205 --- config/plugins/defx.vim | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/config/plugins/defx.vim b/config/plugins/defx.vim index 6fc779817..41a2f09c8 100644 --- a/config/plugins/defx.vim +++ b/config/plugins/defx.vim @@ -146,8 +146,7 @@ function! s:defx_init() \ defx#do_action('drop', 'split') nnoremap st \ defx#do_action('drop', 'tabedit') - nnoremap p - \ defx#do_action('open', 'pedit') + nnoremap p defx#do_action('call', g:defx_config_sid . 'DefxPreview') nnoremap K \ defx#do_action('new_directory') nnoremap N @@ -207,6 +206,27 @@ function! s:DefxSmartL(_) endif endfunction +function! s:DefxPreview(_) abort + if s:preview_windows_opened() + pclose + else + if !defx#is_directory() + let filepath = defx#get_candidate()['action__path'] + exe 'topleft pedit ' . filepath + endif + endif +endfunction + +fun! s:preview_windows_opened() + for nr in range(1, winnr('$')) + if getwinvar(nr, "&pvw") == 1 + " found a preview + return 1 + endif + endfor + return 0 +endfun + function! s:DefxSmartH(_) " if cursor line is first line, or in empty dir if line('.') ==# 1 || line('$') ==# 1