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

Sync with fzf.vim #14

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bce6588
Fix preview of paths starting with ~/ (#892)
ptzz Nov 1, 2019
98cf399
Change fzf#vim#grep to rely on temporary FZF_DEFAULT_COMMAND
junegunn Nov 17, 2019
fcdb7a0
Fix broken anchor in link (#908)
dvdbng Nov 24, 2019
6eda9fd
Revamp README
junegunn Nov 24, 2019
65edb6a
Update ISSUE_TEMPLATE
junegunn Nov 24, 2019
206ec5e
Use --line-range and --highlight-line options of bat
junegunn Dec 17, 2019
f71f480
Update examples
junegunn Dec 18, 2019
76669c3
Deprecate preview.rb
junegunn Dec 22, 2019
fab0597
[fzf#vim#grep] Properly handle filenames with double-colons (#928)
kaessert Jan 10, 2020
8b778dc
Remove useless line in autocmd (#924)
NilsIrl Jan 10, 2020
8a6894d
Fix typo (#914)
tockn Jan 10, 2020
360f7f3
[[B]Commits] Add support for files in submodules (#919)
matawed Jan 14, 2020
6eeeab6
Revert "[[B]Commits] Add support for files in submodules (#919)"
junegunn Jan 14, 2020
dc4c4c2
Use FZF_PREVIEW_LINES instead of LINES (#931)
Jan 14, 2020
dc7796a
Simplify the installation instruction
junegunn Feb 4, 2020
ee08c8f
Clarification on 'do' option of Plug statement
junegunn Feb 4, 2020
467c327
Recommend running `./install --bin` on update
junegunn Feb 5, 2020
48a2d80
[fzf#vim#preview] Respect $BAT_STYLE (#953)
uzxmx Feb 19, 2020
e32a9c3
Do not put --layout=reverse-list if FZF_DEFAULT_OPTS sets reverse layout
junegunn Mar 5, 2020
ed9d66c
Remove unnecessary escaping in tilde expansion (#962)
Mar 11, 2020
15ed47f
Add option to globally enable or disable preview window
junegunn Mar 15, 2020
ef24327
Enable preview window for BTags
junegunn Mar 15, 2020
d16ddcf
[History] Fix #964
junegunn Mar 15, 2020
7ccecee
Clarification: Files command runs $FZF_DEFAULT_COMMAND
junegunn Mar 17, 2020
d4d36b4
Update installation instruction
junegunn Mar 17, 2020
f90b319
[fzf#vim#with_preview] Do not override --preview-window with the defa…
junegunn Apr 5, 2020
f86ef1b
[Buffers] getbufinfo() may not be available
junegunn Apr 9, 2020
4cf475b
[Maps] Display filename and line number (#998)
ianhomer May 1, 2020
5b6f282
Update documentation: fuzzy completion functions
junegunn May 3, 2020
2c07630
Better completion example
junegunn May 3, 2020
25bed07
Use -- before the search pattern for Ag, Rg, etc. (#1012)
kaworu May 5, 2020
3925db8
Update vim help file
junegunn May 5, 2020
a74605b
Add undocumented fzf#vim#_recent_files function (#1024)
jesseleite May 12, 2020
996ec3b
[Helptags] Fix perl script for Windows (#1036)
habamax May 23, 2020
7a65517
[fzf#vim#with_preview] Support WSL bash (#1026)
janlazo May 23, 2020
5aa5977
Handle absolute windows paths in Grep/Ag/Ripgrep sink process (#1038)
habamax Jun 1, 2020
16e3efb
Disable preview window if bash is not found
junegunn Jun 7, 2020
8f1e73b
Expose some buffer related functions (#1044)
doronbehar Jun 7, 2020
2bf85d2
Fix preview.sh not running when the file path contains "binary" (#1055)
nibocn Jun 21, 2020
8173331
Merge remote-tracking branch 'upstream/master' into sync-fzf.vim
fsouza Jul 13, 2020
a9c2179
Use SKIM_DEFAULT_COMMAND in fzf#vim#grep
fsouza Jul 13, 2020
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
19 changes: 7 additions & 12 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<!-- ISSUES NOT FOLLOWING THIS TEMPLATE WILL BE CLOSED AND DELETED -->

<!-- Check all that apply [x] -->
- Category
- [ ] Question
- [ ] Bug
- [ ] Suggestion
- OS
- [ ] Linux
- [ ] macOS
- [ ] Windows
- [ ] Etc.
- Vim
- [ ] Vim
- [ ] Neovim

- [ ] I have read through https://github.com/junegunn/fzf.vim/blob/master/README.md
- [ ] I have read through https://github.com/junegunn/fzf/blob/master/README-VIM.md
- [ ] I have read through the manual page of fzf (`man fzf`)
- [ ] I have searched through the existing issues

<!--

Expand Down
330 changes: 204 additions & 126 deletions README.md

Large diffs are not rendered by default.

106 changes: 72 additions & 34 deletions autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ set cpo&vim

let s:is_win = has('win32') || has('win64')
let s:layout_keys = ['window', 'up', 'down', 'left', 'right']
let s:bin_dir = expand('<sfile>:h:h:h').'/bin/'
let s:bin_dir = expand('<sfile>:p:h:h:h').'/bin/'
let s:bin = {
\ 'preview': s:bin_dir.'preview.sh',
\ 'tags': s:bin_dir.'tags.pl' }
Expand All @@ -41,10 +41,10 @@ if s:is_win
else
let s:bin.preview = fnamemodify(s:bin.preview, ':8')
endif
let s:bin.preview = 'bash '.escape(s:bin.preview, '\')
endif

let s:wide = 120
let s:warned = 0

function! s:extend_opts(dict, eopts, prepend)
if empty(a:eopts)
Expand Down Expand Up @@ -76,9 +76,11 @@ endfunction

" [[options to wrap], [preview window expression], [toggle-preview keys...]]
function! fzf#vim#with_preview(...)
let bash_path = exepath('bash')
let is_wsl_bash = bash_path =~? 'Windows[/\\]system32[/\\]bash.exe$'
" Default options
let options = {}
let window = 'right'
let window = ''

let args = copy(a:000)

Expand All @@ -88,6 +90,17 @@ function! fzf#vim#with_preview(...)
call remove(args, 0)
endif

if empty(bash_path)
if !s:warned
call s:warn('Preview window not supported (bash not found in PATH)')
let s:warned = 1
endif
return options
endif

" Placeholder expression (TODO/TBD: undocumented)
let placeholder = get(options, 'placeholder', '{}')

" Preview window
if len(args) && type(args[0]) == s:TYPE.string
if args[0] !~# '^\(up\|down\|left\|right\)'
Expand All @@ -97,7 +110,18 @@ function! fzf#vim#with_preview(...)
call remove(args, 0)
endif

let preview = ['--preview-window', window, '--preview', (s:is_win ? s:bin.preview : skim#shellescape(s:bin.preview)).' {}']
let preview = []
if len(window)
let preview += ['--preview-window', window]
endif
if s:is_win
let preview_cmd = 'bash '.(is_wsl_bash
\ ? substitute(substitute(s:bin.preview, '^\([A-Z]\):', '/mnt/\L\1', ''), '\', '/', 'g')
\ : escape(s:bin.preview, '\'))
else
let preview_cmd = skim#shellescape(s:bin.preview)
endif
let preview += ['--preview', preview_cmd.' '.placeholder]

if len(args)
call extend(preview, ['--bind', join(map(args, 'v:val.":toggle-preview"'), ',')])
Expand All @@ -115,6 +139,14 @@ function! s:remove_layout(opts)
return a:opts
endfunction

function! s:reverse_list(opts)
let tokens = map(split($FZF_DEFAULT_OPTS, '[^a-z-]'), 'substitute(v:val, "^--", "", "")')
if index(tokens, 'reverse') < 0
return extend(['--layout=reverse-list'], a:opts)
endif
return a:opts
endfunction

function! s:wrap(name, opts, bang)
" skim#wrap does not append --expect if sink or sink* is found
let opts = copy(a:opts)
Expand Down Expand Up @@ -377,7 +409,7 @@ function! fzf#vim#lines(...)
return s:fzf('lines', {
\ 'source': lines,
\ 'sink*': s:function('s:line_handler'),
\ 'options': ['-m', '--tiebreak=index', '--prompt', 'Lines> ', '--ansi', '--extended', '--nth='.nth.'..', '--layout=reverse-list', '--tabstop=1', '--query', query]
\ 'options': s:reverse_list(['-m', '--tiebreak=index', '--prompt', 'Lines> ', '--ansi', '--extended', '--nth='.nth.'..', '--tabstop=1', '--query', query])
\}, args)
endfunction

Expand Down Expand Up @@ -422,7 +454,7 @@ function! fzf#vim#buffer_lines(...)
return s:fzf('blines', {
\ 'source': s:buffer_lines(query),
\ 'sink*': s:function('s:buffer_line_handler'),
\ 'options': ['-m', '--tiebreak=index', '--multi', '--prompt', 'BLines> ', '--ansi', '--extended', '--nth=2..', '--layout=reverse-list', '--tabstop=1']
\ 'options': s:reverse_list(['-m', '--tiebreak=index', '--multi', '--prompt', 'BLines> ', '--ansi', '--extended', '--nth=2..', '--tabstop=1'])
\}, args)
endfunction

Expand Down Expand Up @@ -454,10 +486,10 @@ endfunction
" ------------------------------------------------------------------
" History[:/]
" ------------------------------------------------------------------
function! s:all_files()
function! fzf#vim#_recent_files()
return fzf#vim#_uniq(map(
\ filter([expand('%')], 'len(v:val)')
\ + filter(map(s:buflisted_sorted(), 'bufname(v:val)'), 'len(v:val)')
\ + filter(map(fzf#vim#_buflisted_sorted(), 'bufname(v:val)'), 'len(v:val)')
\ + filter(copy(v:oldfiles), "filereadable(fnamemodify(v:val, ':p'))"),
\ 'fnamemodify(v:val, ":~:.")'))
endfunction
Expand Down Expand Up @@ -519,7 +551,7 @@ endfunction

function! fzf#vim#history(...)
return s:fzf('history-files', {
\ 'source': s:all_files(),
\ 'source': fzf#vim#_recent_files(),
\ 'options': ['-m', '--header-lines', !empty(expand('%')), '--prompt', 'Hist> ']
\}, a:000)
endfunction
Expand Down Expand Up @@ -606,15 +638,17 @@ function! s:bufopen(lines)
execute 'buffer' b
endfunction

function! s:format_buffer(b)
function! fzf#vim#_format_buffer(b)
let name = bufname(a:b)
let line = exists('*getbufinfo') ? getbufinfo(a:b)[0]['lnum'] : 0
let name = empty(name) ? '[No Name]' : fnamemodify(name, ":p:~:.")
let flag = a:b == bufnr('') ? s:blue('%', 'Conditional') :
\ (a:b == bufnr('#') ? s:magenta('#', 'Special') : ' ')
let modified = getbufvar(a:b, '&modified') ? s:red(' [+]', 'Exception') : ''
let readonly = getbufvar(a:b, '&modifiable') ? '' : s:green(' [RO]', 'Constant')
let extra = join(filter([modified, readonly], '!empty(v:val)'), '')
return s:strip(printf("[%s] %s\t%s\t%s", s:yellow(a:b, 'Number'), flag, name, extra))
let target = line == 0 ? name : name.':'.line
return s:strip(printf("%s\t[%s] %s\t%s\t%s", target, s:yellow(a:b, 'Number'), flag, name, extra))
endfunction

function! s:sort_buffers(...)
Expand All @@ -623,40 +657,39 @@ function! s:sort_buffers(...)
return b1 < b2 ? 1 : -1
endfunction

function! s:buflisted_sorted()
function! fzf#vim#_buflisted_sorted()
return sort(s:buflisted(), 's:sort_buffers')
endfunction

function! fzf#vim#buffers(...)
let [query, args] = (a:0 && type(a:1) == type('')) ?
\ [a:1, a:000[1:]] : ['', a:000]
return s:fzf('buffers', {
\ 'source': map(s:buflisted_sorted(), 's:format_buffer(v:val)'),
\ 'source': map(fzf#vim#_buflisted_sorted(), 'fzf#vim#_format_buffer(v:val)'),
\ 'sink*': s:function('s:bufopen'),
\ 'options': ['-m', '--extended', '--tiebreak=index', '--header-lines=1', '--ansi', '-d', '\t', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query]
\ 'options': ['-m', '--extended', '--tiebreak=index', '--header-lines=1', '--ansi', '-d', '\t', '--with-nth', '2..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query]
\}, args)
endfunction

" ------------------------------------------------------------------
" Ag / Rg
" ------------------------------------------------------------------
function! s:ag_to_qf(line, with_column)
let parts = split(a:line, ':')
let text = join(parts[(a:with_column ? 3 : 2):], ':')
let dict = {'filename': &acd ? fnamemodify(parts[0], ':p') : parts[0], 'lnum': parts[1], 'text': text}
if a:with_column
let dict.col = parts[2]
function! s:ag_to_qf(line, has_column)
let parts = matchlist(a:line, '\(.\{-}\):\(\d\+\)\%(:\(\d\+\)\)\?\%(:\(.*\)\)\?')
let dict = {'filename': &acd ? fnamemodify(parts[1], ':p') : parts[1], 'lnum': parts[2], 'text': parts[4]}
if a:has_column
let dict.col = parts[3]
endif
return dict
endfunction

function! s:ag_handler(lines, with_column)
function! s:ag_handler(lines, has_column)
if len(a:lines) < 2
return
endif

let cmd = s:action_for(a:lines[0], 'e')
let list = map(filter(a:lines[1:], 'len(v:val)'), 's:ag_to_qf(v:val, a:with_column)')
let list = map(filter(a:lines[1:], 'len(v:val)'), 's:ag_to_qf(v:val, a:has_column)')
if empty(list)
return
endif
Expand All @@ -665,7 +698,7 @@ function! s:ag_handler(lines, with_column)
try
call s:open(cmd, first.filename)
execute first.lnum
if a:with_column
if a:has_column
execute 'normal!' first.col.'|'
endif
normal! zz
Expand Down Expand Up @@ -721,7 +754,7 @@ function! fzf#vim#ag(query, ...)
let query = empty(a:query) ? '^(?=.)' : a:query
let args = copy(a:000)
let ag_opts = len(args) > 1 && type(args[0]) == s:TYPE.string ? remove(args, 0) : ''
let command = ag_opts . ' ' . skim#shellescape(query)
let command = ag_opts . ' -- ' . skim#shellescape(query)
return call('fzf#vim#ag_raw', insert(args, command, 0))
endfunction

Expand All @@ -733,8 +766,8 @@ function! fzf#vim#ag_raw(command_suffix, ...)
return call('fzf#vim#grep', extend(['ag --nogroup --column --color '.a:command_suffix, 1], a:000))
endfunction

" command, with_column, [options]
function! fzf#vim#grep(grep_command, with_column, ...)
" command (string), has_column (0/1), [options (dict)], [fullscreen (0/1)]
function! fzf#vim#grep(grep_command, has_column, ...)
let words = []
for word in split(a:grep_command)
if word !~# '^[a-z]'
Expand All @@ -746,8 +779,7 @@ function! fzf#vim#grep(grep_command, with_column, ...)
let name = join(words, '-')
let capname = join(map(words, 'toupper(v:val[0]).v:val[1:]'), '')
let opts = {
\ 'source': a:grep_command,
\ 'column': a:with_column,
\ 'column': a:has_column,
\ 'options': ['--ansi', '--prompt', capname.'> ',
\ '--multi', '--bind', 'alt-a:select-all,alt-d:deselect-all',
\ '--color', 'hl:4,hl+:12']
Expand All @@ -756,7 +788,13 @@ function! fzf#vim#grep(grep_command, with_column, ...)
return s:ag_handler(a:lines, self.column)
endfunction
let opts['sink*'] = remove(opts, 'sink')
return s:fzf(name, opts, a:000)
try
let prev_default_command = $SKIM_DEFAULT_COMMAND
let $SKIM_DEFAULT_COMMAND = a:grep_command
return s:fzf(name, opts, a:000)
finally
let $SKIM_DEFAULT_COMMAND = prev_default_command
endtry
endfunction

" ------------------------------------------------------------------
Expand Down Expand Up @@ -815,7 +853,7 @@ function! fzf#vim#buffer_tags(query, ...)
return s:fzf('btags', {
\ 'source': s:btags_source(tag_cmds),
\ 'sink*': s:function('s:btags_sink'),
\ 'options': ['--layout=reverse-list', '-m', '-d', '\t', '--with-nth', '1,4..', '-n', '1', '--prompt', 'BTags> ', '--query', a:query]}, args)
\ 'options': s:reverse_list(['-m', '-d', '\t', '--with-nth', '1,4..', '-n', '1', '--prompt', 'BTags> ', '--query', a:query])}, args)
catch
return s:warn(v:exception)
endtry
Expand Down Expand Up @@ -1037,7 +1075,7 @@ function! fzf#vim#helptags(...)
silent! call delete(s:helptags_script)
endif
let s:helptags_script = tempname()
call writefile(['/('.(s:is_win ? '^[A-Z]:\/.*?[^:]' : '.*?').'):(.*?)\t(.*?)\t/; printf(qq('.s:green('%-40s', 'Label').'\t%s\t%s\n), $2, $3, $1)'], s:helptags_script)
call writefile(['/('.(s:is_win ? '^[A-Z]:[\/\\].*?[^:]' : '.*?').'):(.*?)\t(.*?)\t/; printf(qq('.s:green('%-40s', 'Label').'\t%s\t%s\n), $2, $3, $1)'], s:helptags_script)
return s:fzf('helptags', {
\ 'source': 'grep -H ".*" '.join(map(tags, 'skim#shellescape(v:val)')).
\ ' | perl -n '.skim#shellescape(s:helptags_script).' | sort',
Expand Down Expand Up @@ -1160,10 +1198,10 @@ function! s:commits(buffer_local, args)
let options = {
\ 'source': source,
\ 'sink*': s:function('s:commits_sink'),
\ 'options': ['--ansi', '--multi', '--tiebreak=index', '--layout=reverse-list',
\ 'options': s:reverse_list(['--ansi', '--multi', '--tiebreak=index',
\ '--inline-info', '--prompt', command.'> ', '--bind=ctrl-s:toggle-sort',
\ '--header', ':: Press '.s:magenta('CTRL-S', 'Special').' to toggle sort, '.s:magenta('CTRL-Y', 'Special').' to yank commit hashes',
\ '--expect=ctrl-y,'.expect_keys]
\ '--expect=ctrl-y,'.expect_keys])
\ }

if a:buffer_local
Expand Down Expand Up @@ -1230,7 +1268,7 @@ function! fzf#vim#maps(mode, ...)
let curr = ''
for line in split(cout, "\n")
if line =~ "^\t"
let src = ' '.join(reverse(reverse(split(split(line)[-1], '/'))[0:2]), '/')
let src = "\t".substitute(matchstr(line, '/\zs[^/\\]*\ze$'), ' [^ ]* ', ':', '')
call add(list, printf('%s %s', curr, s:green(src, 'Comment')))
let curr = ''
else
Expand Down
58 changes: 1 addition & 57 deletions bin/preview.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,3 @@
#!/usr/bin/env ruby
#
# usage: ./preview.rb FILENAME[:LINE][:IGNORED]

require 'open3'
require 'shellwords'

COMMAND = ENV.fetch(
'FZF_PREVIEW_COMMAND',
%[bat --style=numbers --color=always {} || highlight -O ansi -l {} || coderay {} || rougify {} || cat {}]
)
ANSI = /\x1b\[[0-9;]*m/
REVERSE = "\x1b[7m"
RESET = "\x1b[m"

def usage
puts "usage: #$0 FILENAME[:LINENO][:IGNORED]"
exit 1
end

usage if ARGV.empty?

file, center, extra = ARGV.first.split(':')
if ARGV.first =~ /^[A-Z]:\\/
file << ':' + center
center = extra
end
usage unless file

path = File.expand_path(file)
unless File.readable? path
puts "File not found: #{file}"
exit 1
end

if `file --dereference --mime "#{file}"` =~ /binary/
puts "#{file} is a binary file"
exit 0
end

center = (center || 0).to_i
height =
if ENV['LINES']
ENV['LINES'].to_i
else
File.readable?('/dev/tty') ? `stty size < /dev/tty`.split.first.to_i : 40
end
offset = [1, center - height / 3].max

Open3.popen3(COMMAND.gsub('{}', Shellwords.shellescape(path))) do |_in, out, _err|
out.each_line.drop(offset - 1).take(height).each_with_index do |line, lno|
if lno + offset == center
puts REVERSE + line.chomp.gsub(ANSI) { |m| m + REVERSE } + RESET
else
puts line
end
end
end
print RESET
puts 'preview.rb is deprecated. Use preview.sh instead.'
Loading