lf.vim (2628B)
1 if exists('g:loaded_lf_autoload') 2 finish 3 endif 4 let g:loaded_lf_autoload = 1 5 6 function! lf#LF(path, edit_cmd, user_args) 7 " Save the edit cmd in a script variable to make it accessible in the callback 8 let s:edit_cmd = a:edit_cmd 9 " To revert back guioptions after changing them for Vim 10 let oldguioptions = &guioptions 11 " To temporarily save what files were chosen by lf 12 let s:choice_file_path = tempname() 13 14 let s:lf_cmd = ['lf', '-selection-path=' . s:choice_file_path,] 15 \ + a:user_args 16 \ + [fnameescape(expand(a:path))] 17 18 if has('nvim') 19 " To be passed down to termopen 20 let termopen_opts = { } 21 function! termopen_opts.on_exit(job_id, code, event) 22 if filereadable(s:choice_file_path) 23 for f in readfile(s:choice_file_path) 24 exec s:edit_cmd . f 25 endfor 26 call delete(s:choice_file_path) 27 endif 28 if a:code == 0 29 " Iterate open buffers and search for our terminal buffer - to delete 30 " it afterwards 31 for buf in getbufinfo() 32 if has_key(buf.variables, 'terminal_job_id') && buf.variables.terminal_job_id == a:job_id 33 let bufnr = buf.bufnr 34 break 35 endif 36 endfor 37 if exists(":Bclose") 38 call execute('Bclose! ' . bufnr) 39 elseif exists(":Bdelete") 40 call execute('Bdelete! ' . bufnr) 41 else 42 " See https://vi.stackexchange.com/a/25798/6411 43 let v:errmsg = "lf.vim: Failed to close buffer lf buffer -" . 44 \ "No `:Bdelete` or `:Bclose` commands were found. You can implement either of them yourself, or install either of:\n" . 45 \ " - https://github.com/rbgrouleff/bclose.vim\n" . 46 \ " - https://github.com/moll/vim-bbye" 47 echo v:errmsg 48 endif 49 endif 50 endfunction 51 enew 52 " We want Lf to quit after it saves the selections 53 call termopen(s:lf_cmd, termopen_opts) 54 startinsert 55 else 56 function! s:EditCallback() 57 if filereadable(s:choice_file_path) 58 for f in readfile(s:choice_file_path) 59 exec s:edit_cmd . f 60 filetype detect 61 endfor 62 call delete(s:choice_file_path) 63 endif 64 redraw! 65 endfunction 66 set guioptions+=! " Make it work with MacVim 67 let buf = term_start(join(s:lf_cmd, ' '), #{hidden: 1, term_finish: 'close'}) 68 let winid = popup_dialog(buf, #{minwidth: 150, minheight: 20, highlight: 'Normal'}) 69 let bufn = winbufnr(winid) 70 exe 'autocmd! BufWinLeave <buffer='.bufn.'> call s:EditCallback()' 71 endif 72 let &guioptions=oldguioptions 73 endfunction