diff --git a/etc/lf.vim b/etc/lf.vim index 6102f1fb..f7967b79 100644 --- a/etc/lf.vim +++ b/etc/lf.vim @@ -13,22 +13,68 @@ " nnoremap l :LF " -function! LF() - let temp = tempname() - exec 'silent !lf -selection-path=' . shellescape(temp) - if !filereadable(temp) - redraw! - return +let s:temp = tempname() +if executable('lf') + command! -nargs=? -bar -complete=dir FilePicker call FilePicker('lf', '-selection-path', s:temp, ) +elseif executable('ranger') + " The option --choosefiles was added in ranger 1.5.1. + " Use --choosefile with ranger 1.4.2 through 1.5.0 instead. + command! -nargs=? -bar -complete=dir FilePicker call FilePicker('ranger', '--choosefiles='..s:temp, '--selectfile', ) +elseif executable('nnn') + command! -nargs=? -bar -complete=dir FilePicker call FilePicker('nnn', '-p', s:temp, ) +endif + +if exists(':FilePicker') == 2 + function! FilePicker(...) + let path = a:000[-1] + let cmd = a:000[:-2] + (empty(path) ? + \ [filereadable(expand('%')) ? shellescape(expand('%'),1) : '.'] : [path]) + if has('nvim') + enew + call termopen(cmd, { 'on_exit': function('s:open') }) + else + if has('gui_running') + if has('terminal') + call term_start(cmd, {'exit_cb': function('s:term_close'), 'curwin': 1}) + else + echomsg 'GUI is running but terminal is not supported.' + endif + else + exec 'silent !'..join(cmd) | call s:open() + endif + endif + endfunction + + if has('gui_running') && has('terminal') + function! s:term_close(job_id, event) + if a:event == 'exit' + bwipeout! + call s:open() + endif + endfunction + endif + + function! s:open(...) + if !filereadable(s:temp) + " if &buftype ==# 'terminal' + " bwipeout! + " endif + redraw! + " Nothing to read. + return endif - let names = readfile(temp) + let names = readfile(s:temp) if empty(names) - redraw! - return + redraw! + " Nothing to open. + return endif - exec 'edit ' . fnameescape(names[0]) + " Edit the first item. + exec 'edit' fnameescape(names[0]) + " Add any remaning items to the arg list/buffer list. for name in names[1:] - exec 'argadd ' . fnameescape(name) + exec 'argadd' fnameescape(name) endfor redraw! -endfunction -command! -bar LF call LF() + endfunction +endif