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

[fzf#vim#grep / Ag / Rg / RG] Multi-line display for narrow screens #1549

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,21 @@ let g:fzf_vim.preview_window = []
#### Command-level options

```vim
" [Buffers] Jump to the existing window if possible
" [Buffers] Jump to the existing window if possible (default: 0)
let g:fzf_vim.buffers_jump = 1

" [Ag|Rg|RG] Display path on a separate line for narrow screens (default: 0)
" * Requires Perl and fzf 0.53.0 or later
let g:fzf_vim.grep_multi_line = 0
" PATH:LINE:COL:LINE
let g:fzf_vim.grep_multi_line = 1
" PATH:LINE:COL:
" LINE
let g:fzf_vim.grep_multi_line = 2
" PATH:LINE:COL:
" LINE
" (empty line)

" [[B]Commits] Customize the options used by 'git log':
let g:fzf_vim.commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'

Expand Down
37 changes: 33 additions & 4 deletions autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,17 @@ function! s:ag_handler(name, lines)
return
endif

let list = map(filter(a:lines[1:], 'len(v:val)'), 's:ag_to_qf(v:val)')
let multi_line = min([s:conf('grep_multi_line', 0), 2])
let lines = []
if multi_line && executable('perl')
for idx in range(1, len(a:lines), multi_line + 1)
call add(lines, join(a:lines[idx:idx + multi_line + 1], ''))
endfor
else
let lines = a:lines[1:]
endif

let list = map(filter(lines, 'len(v:val)'), 's:ag_to_qf(v:val)')
if empty(list)
return
endif
Expand Down Expand Up @@ -893,6 +903,19 @@ function! fzf#vim#ag_raw(command_suffix, ...)
return call('fzf#vim#grep', extend(['ag --nogroup --column --color '.a:command_suffix, 1], a:000))
endfunction

function! s:grep_multi_line(opts)
" TODO: Non-global option
let multi_line = s:conf('grep_multi_line', 0)
if multi_line && executable('perl')
let opts = copy(a:opts)
let opts.options = extend(copy(opts.options), ['--read0', '--highlight-line'])
let delim = multi_line > 1 ? '\n\0' : '\0'
return [opts, printf(" | perl -pe 's/\\n/%s/; s/^([^:]+:){2,3}/$&\\n /'", delim)]
endif

return [a:opts, '']
endfunction

" command (string), [spec (dict)], [fullscreen (bool)]
function! fzf#vim#grep(grep_command, ...)
let args = copy(a:000)
Expand All @@ -919,9 +942,11 @@ function! fzf#vim#grep(grep_command, ...)
return s:ag_handler(get(opts, 'name', name), a:lines)
endfunction
let opts['sink*'] = remove(opts, 'sink')
let [opts, suffix] = s:grep_multi_line(opts)
let command = a:grep_command . suffix
try
let prev_default_command = $FZF_DEFAULT_COMMAND
let $FZF_DEFAULT_COMMAND = a:grep_command
let $FZF_DEFAULT_COMMAND = command
return s:fzf(name, opts, args)
finally
let $FZF_DEFAULT_COMMAND = prev_default_command
Expand All @@ -946,11 +971,15 @@ function! fzf#vim#grep2(command_prefix, query, ...)
\ 'source': s:is_win ? 'cd .' : ':',
\ 'options': ['--ansi', '--prompt', toupper(name).'> ', '--query', a:query,
\ '--disabled',
\ '--bind', 'start:reload:'.a:command_prefix.' '.fzf#shellescape(a:query),
\ '--bind', 'change:reload:'.a:command_prefix.' {q}'.fallback,
\ '--multi', '--bind', 'alt-a:select-all,alt-d:deselect-all',
\ '--delimiter', ':', '--preview-window', '+{2}-/2']
\}

let [opts, suffix] = s:grep_multi_line(opts)
let suffix = escape(suffix, '{')
call extend(opts.options, ['--bind', 'start:reload:'.a:command_prefix.' '.fzf#shellescape(a:query).suffix])
call extend(opts.options, ['--bind', 'change:reload:'.a:command_prefix.' {q}'.suffix.fallback])

if len(args) && type(args[0]) == s:TYPE.bool
call remove(args, 0)
endif
Expand Down