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

Proposal for simple fzf browser #1267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 37 additions & 14 deletions autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ function! s:prepend_opts(dict, eopts)
return s:extend_opts(a:dict, a:eopts, 1)
endfunction

function! fzf#vim#preview_path()
if s:is_win
let is_wsl_bash = exepath('bash') =~? 'Windows[/\\]system32[/\\]bash.exe$'
if empty($MSWINHOME)
let $MSWINHOME = $HOME
endif
if is_wsl_bash && $WSLENV !~# '[:]\?MSWINHOME\(\/[^:]*\)\?\(:\|$\)'
let $WSLENV = 'MSWINHOME/u:'.$WSLENV
endif
return 'bash '.(is_wsl_bash
\ ? substitute(substitute(s:bin.preview, '^\([A-Z]\):', '/mnt/\L\1', ''), '\', '/', 'g')
\ : escape(s:bin.preview, '\'))
else
return fzf#shellescape(s:bin.preview)
endif
endfunction

" [[spec to wrap], [preview window expression], [toggle-preview keys...]]
function! fzf#vim#with_preview(...)
" Default spec
Expand Down Expand Up @@ -161,20 +178,7 @@ function! fzf#vim#with_preview(...)
if len(window)
let preview += ['--preview-window', window]
endif
if s:is_win
let is_wsl_bash = exepath('bash') =~? 'Windows[/\\]system32[/\\]bash.exe$'
if empty($MSWINHOME)
let $MSWINHOME = $HOME
endif
if is_wsl_bash && $WSLENV !~# '[:]\?MSWINHOME\(\/[^:]*\)\?\(:\|$\)'
let $WSLENV = 'MSWINHOME/u:'.$WSLENV
endif
let preview_cmd = 'bash '.(is_wsl_bash
\ ? substitute(substitute(s:bin.preview, '^\([A-Z]\):', '/mnt/\L\1', ''), '\', '/', 'g')
\ : escape(s:bin.preview, '\'))
else
let preview_cmd = fzf#shellescape(s:bin.preview)
endif
let preview_cmd = fzf#vim#preview_path()
if len(placeholder)
let preview += ['--preview', preview_cmd.' '.placeholder]
end
Expand Down Expand Up @@ -1433,6 +1437,25 @@ function! fzf#vim#complete(...)
return ''
endfunction

" ------------------------------------------------------------------
" fzf#vim#browse - browser
" ------------------------------------------------------------------

function! fzf#vim#browse(dir)
let bindings = 'ctrl-l:accept,ctrl-e:preview-down,ctrl-y:preview-up,ctrl-d:preview-page-down,ctrl-u:preview-page-up,ctrl-p:up,ctrl-n:down'
let preview = 'if [ -f {} ]; then ' . fzf#vim#preview_path() . ' {}; else ls -Ap {}; fi'
let options = '--reverse --header=$PWD --border=rounded --preview-window=right:70% --bind="' . bindings . '" --preview="' . preview . '"'
let paths = fzf#run({'source': 'echo "..\n$(ls -Ap)"', 'options': options, 'dir': a:dir, 'sink': 'silent!'})

if !len(paths) | return | endif

let path = a:dir . '/' . paths[0]

if isdirectory(path) | return fzf#vim#browse(path) | endif

exe "e " path
endfunction

" ------------------------------------------------------------------
let &cpo = s:cpo_save
unlet s:cpo_save
3 changes: 2 additions & 1 deletion plugin/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ call s:defs([
\'command! -bar -bang BCommits call fzf#vim#buffer_commits(fzf#vim#with_preview({ "placeholder": "" }), <bang>0)',
\'command! -bar -bang Maps call fzf#vim#maps("n", <bang>0)',
\'command! -bar -bang Filetypes call fzf#vim#filetypes(<bang>0)',
\'command! -bang -nargs=* History call s:history(<q-args>, fzf#vim#with_preview(), <bang>0)'])
\'command! -bang -nargs=* History call s:history(<q-args>, fzf#vim#with_preview(), <bang>0)',
\'command! Browse call fzf#vim#browse(expand("%:p:h"))'])

function! s:history(arg, extra, bang)
let bang = a:bang || a:arg[len(a:arg)-1] == '!'
Expand Down