From 01f7146cf6e903e31cd900944393bec86c6da4a6 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 18 Oct 2020 13:37:41 +0900 Subject: [PATCH] Put focused line in the middle of the preview window Fix #1092 Requires fzf 0.23.0 or above --- .github/ISSUE_TEMPLATE.md | 2 +- autoload/fzf/vim.vim | 43 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 552e44d..bfdf147 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -2,7 +2,7 @@ -- [ ] I have fzf 0.22.0 or above +- [ ] I have fzf 0.23.0 or above - [ ] 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`) diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index 60e6505..7974112 100644 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -28,6 +28,7 @@ set cpo&vim " Common " ------------------------------------------------------------------ +let s:min_version = '0.23.0' let s:is_win = has('win32') || has('win64') let s:layout_keys = ['window', 'up', 'down', 'left', 'right'] let s:bin_dir = expand(':p:h:h:h').'/bin/' @@ -45,6 +46,40 @@ endif let s:wide = 120 let s:warned = 0 +let s:checked = 0 + +function! s:version_requirement(val, min) + let val = split(a:val, '\.') + let min = split(a:min, '\.') + for idx in range(0, len(min) - 1) + let v = get(val, idx, 0) + if v < min[idx] | return 0 + elseif v > min[idx] | return 1 + endif + endfor + return 1 +endfunction + +function! s:check_requirements() + if s:checked + return + endif + + if !exists('*fzf#run') + throw "fzf#run function not found. You also need Vim plugin from the main fzf repository (i.e. junegunn/fzf *and* junegunn/fzf.vim)" + endif + if !exists('*fzf#exec') + throw "fzf#exec function not found. You need to upgrade Vim plugin from the main fzf repository ('junegunn/fzf')" + endif + let exec = fzf#exec() + let fzf_version = matchstr(systemlist(exec .. ' --version')[0], '[0-9.]*') + + if s:version_requirement(fzf_version, s:min_version) + let s:checked = 1 + return + end + throw printf('You need to upgrade fzf. Found: %s (%s). Required: %s or above.', fzf_version, exec, s:min_version) +endfunction function! s:extend_opts(dict, eopts, prepend) if empty(a:eopts) @@ -229,6 +264,8 @@ function! s:buflisted() endfunction function! s:fzf(name, opts, extra) + call s:check_requirements() + let [extra, bang] = [{}, 0] if len(a:extra) <= 1 let first = get(a:extra, 0, 0) @@ -666,7 +703,7 @@ function! fzf#vim#buffers(...) return s:fzf('buffers', { \ 'source': map(fzf#vim#_buflisted_sorted(), 'fzf#vim#_format_buffer(v:val)'), \ 'sink*': s:function('s:bufopen'), - \ 'options': ['+m', '-x', '--tiebreak=index', '--header-lines=1', '--ansi', '-d', '\t', '--with-nth', '3..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query, '--preview-window', '+{2}-5'] + \ 'options': ['+m', '-x', '--tiebreak=index', '--header-lines=1', '--ansi', '-d', '\t', '--with-nth', '3..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query, '--preview-window', '+{2}-/2'] \}, args) endfunction @@ -743,7 +780,7 @@ function! fzf#vim#grep(grep_command, has_column, ...) \ 'column': a:has_column, \ 'options': ['--ansi', '--prompt', capname.'> ', \ '--multi', '--bind', 'alt-a:select-all,alt-d:deselect-all', - \ '--delimiter', ':', '--preview-window', '+{2}-5', + \ '--delimiter', ':', '--preview-window', '+{2}-/2', \ '--color', 'hl:4,hl+:12'] \} function! opts.sink(lines) @@ -815,7 +852,7 @@ function! fzf#vim#buffer_tags(query, ...) return s:fzf('btags', { \ 'source': s:btags_source(tag_cmds), \ 'sink*': s:function('s:btags_sink'), - \ 'options': s:reverse_list(['-m', '-d', '\t', '--with-nth', '1,4..', '-n', '1', '--prompt', 'BTags> ', '--query', a:query, '--preview-window', '+{3}-5'])}, args) + \ 'options': s:reverse_list(['-m', '-d', '\t', '--with-nth', '1,4..', '-n', '1', '--prompt', 'BTags> ', '--query', a:query, '--preview-window', '+{3}-/2'])}, args) catch return s:warn(v:exception) endtry