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

Update lf.vim to work with Neovim and in GUI and use fallbacks #1798

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
72 changes: 59 additions & 13 deletions etc/lf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,68 @@
" nnoremap <leader>l :LF<cr>
"

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, <q-args>)
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', <q-args>)
elseif executable('nnn')
command! -nargs=? -bar -complete=dir FilePicker call FilePicker('nnn', '-p', s:temp, <q-args>)
endif

if exists(':FilePicker') == 2
function! FilePicker(...)
let path = a:000[-1]
let cmd = a:000[:-2] + (empty(path) ?
\ [filereadable(expand('%')) ? expand('%:p') : '.'] : [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