From eb77fae3fdbfbb5c0d64b3c03138036643743a94 Mon Sep 17 00:00:00 2001 From: Sumit Date: Sun, 23 Jul 2023 22:46:02 +0200 Subject: [PATCH] qf buffers to fzf --- autoload/fzf/vim.vim | 59 ++++++++++++++++++++++++++++++++++++++++++++ plugin/fzf.vim | 1 + 2 files changed, 60 insertions(+) diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index d0248b58..a9c74aaf 100755 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -712,6 +712,65 @@ function! fzf#vim#gitfiles(args, ...) return s:fzf('gfiles-diff', wrapped, a:000) endfunction +" ------------------------------------------------------------------ +" fromqf +" ------------------------------------------------------------------ + +function! s:_qf_line_handler(lines) + if len(a:lines) < 2 + return + endif + normal! m' + let cmd = s:action_for(a:lines[0]) + if !empty(cmd) && stridx('edit', cmd) < 0 + execute 'silent' cmd + endif + + let keys = split(split(a:lines[1], '\t')[0], ':') + echom keys + execute 'buffer' keys[0] + execute keys[1] + normal! ^zvzz +endfunction + +function! fzf#vim#_qf_format_buffer(b) + let name = bufname(a:b.bufnr) + let line = a:b.lnum + let name = empty(name) ? '[No Name]' : fnamemodify(name, ":p:~:.") + let flag = a:b.bufnr == bufnr('') ? s:blue('%', 'Conditional') : + \ (a:b.bufnr == bufnr('#') ? s:magenta('#', 'Special') : ' ') + let modified = getbufvar(a:b.bufnr, '&modified') ? s:red(' [+]', 'Exception') : '' + let readonly = getbufvar(a:b.bufnr, '&modifiable') ? '' : s:green(' [RO]', 'Constant') + let extra = join(filter([modified, readonly], '!empty(v:val)'), '') + let target = line == 0 ? name : name.':'.line + let ret=s:strip(printf("%s\t%d\t[%s] %s\t%s\t%s", target, line, s:yellow(a:b.bufnr, 'Number'), flag, name, extra)) + return ret +endfunction + +function! s:_qf_sort_buffers(...) + let [b1, b2] = map(copy(a:000), 'v:val') + " Using minus between a float and a number in a sort function causes an error + return b1.bufnr < b2.bufnr ? 1 : -1 +endfunction + +function! fzf#vim#_qf_buflisted_sorted() + return sort(getqflist(), 's:_qf_sort_buffers') +endfunction + +function! fzf#vim#qf_buffers(...) + let [query, args] = (a:0 && type(a:1) == type('')) ? + \ [a:1, a:000[1:]] : ['', a:000] + let sorted = fzf#vim#_qf_buflisted_sorted() + let header_lines = '--header-lines=0' + let tabstop = 9 + return s:fzf('buffers', { + \ 'source': map(sorted, 'fzf#vim#_qf_format_buffer(v:val)'), + \ 'sink*': s:function('s:_qf_line_handler'), + \ 'options': ['+m', '-x', '--tiebreak=index', header_lines, '--ansi', '-d', '\t', '--with-nth', '3..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query, '--preview-window', '+{2}-/2', '--tabstop', tabstop] + \}, args) +endfunction + + " ------------------------------------------------------------------ " Buffers " ------------------------------------------------------------------ diff --git a/plugin/fzf.vim b/plugin/fzf.vim index c73931b9..a90dbb03 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -49,6 +49,7 @@ call s:defs([ \'command! -bang -nargs=? GitFiles call fzf#vim#gitfiles(, fzf#vim#with_preview( == "?" ? { "placeholder": "" } : {}), 0)', \'command! -bang -nargs=? GFiles call fzf#vim#gitfiles(, fzf#vim#with_preview( == "?" ? { "placeholder": "" } : {}), 0)', \'command! -bar -bang -nargs=? -complete=buffer Buffers call fzf#vim#buffers(, fzf#vim#with_preview({ "placeholder": "{1}" }), 0)', +\'command! -bar -bang -nargs=? -complete=buffer Usages call fzf#vim#qf_buffers(, fzf#vim#with_preview({ "placeholder": "{1}" }), 0)', \'command! -bang -nargs=* Lines call fzf#vim#lines(, 0)', \'command! -bang -nargs=* BLines call fzf#vim#buffer_lines(, 0)', \'command! -bar -bang Colors call fzf#vim#colors(0)',