diff --git a/README.md b/README.md index 3d285c1..ecd40c9 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,20 @@ Every command in fzf.vim internally calls `fzf#wrap` function of the main repository which supports a set of global option variables. So please read through [README-VIM][README-VIM] to learn more about them. +#### Preview window + +If the width of the screen is wider than 120 columns, some commands will show +the preview window on the right. You can customize the behavior with +`g:fzf_preview_window`. Here are some examples: + +```vim +" Empty value to disable preview window altogether +let g:fzf_preview_window = '' + +" Always enable preview window on the right with 60% width +let g:fzf_preview_window = 'right:60%' +``` + ### Command-local options A few commands in fzf.vim can be customized with global option variables shown diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index 6ba8e19..cf6f3db 100644 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -88,6 +88,9 @@ function! fzf#vim#with_preview(...) call remove(args, 0) 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\)' @@ -97,7 +100,7 @@ function! fzf#vim#with_preview(...) call remove(args, 0) endif - let preview = ['--preview-window', window, '--preview', (s:is_win ? s:bin.preview : fzf#shellescape(s:bin.preview)).' {}'] + let preview = ['--preview-window', window, '--preview', (s:is_win ? s:bin.preview : fzf#shellescape(s:bin.preview)).' '.placeholder] if len(args) call extend(preview, ['--bind', join(map(args, 'v:val.":toggle-preview"'), ',')]) @@ -616,13 +619,15 @@ endfunction function! s:format_buffer(b) let name = bufname(a:b) + let line = getbufinfo(a:b)[0]['lnum'] 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(...) @@ -641,7 +646,7 @@ function! fzf#vim#buffers(...) return s:fzf('buffers', { \ 'source': map(s:buflisted_sorted(), 's:format_buffer(v:val)'), \ 'sink*': s:function('s:bufopen'), - \ 'options': ['+m', '-x', '--tiebreak=index', '--header-lines=1', '--ansi', '-d', '\t', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query] + \ 'options': ['+m', '-x', '--tiebreak=index', '--header-lines=1', '--ansi', '-d', '\t', '--with-nth', '2..', '-n', '2,1..2', '--prompt', 'Buf> ', '--query', query] \}, args) endfunction diff --git a/bin/preview.sh b/bin/preview.sh index b4303b9..a0f1c07 100755 --- a/bin/preview.sh +++ b/bin/preview.sh @@ -17,14 +17,19 @@ if [[ $1 =~ ^[A-Z]:\\ ]]; then CENTER=${INPUT[2]} fi +if [[ ! "$CENTER" =~ ^[0-9]*$ ]]; then + exit 1 +fi + FILE="${FILE/#\~\//$HOME/}" if [ ! -r "$FILE" ]; then echo "File not found ${FILE}" exit 1 fi -if [[ "$(file --dereference --mime "$FILE")" =~ binary ]]; then - echo "$1 is a binary file" +MIME=$(file --dereference --mime "$FILE") +if [[ "$MIME" =~ binary ]]; then + echo "$MIME" exit 0 fi diff --git a/doc/fzf-vim.txt b/doc/fzf-vim.txt index 1b956bd..5712e05 100644 --- a/doc/fzf-vim.txt +++ b/doc/fzf-vim.txt @@ -1,4 +1,4 @@ -fzf-vim.txt fzf-vim Last change: February 5 2020 +fzf-vim.txt fzf-vim Last change: March 15 2020 FZF-VIM - TABLE OF CONTENTS *fzf-vim* *fzf-vim-toc* ============================================================================== @@ -10,6 +10,7 @@ FZF-VIM - TABLE OF CONTENTS *fzf-vim* *fzf-vim-to Commands Customization Global options + Preview window Command-local options Advanced customization Vim functions @@ -160,6 +161,22 @@ through {README-VIM}{4} to learn more about them. {4} https://github.com/junegunn/fzf/blob/master/README-VIM.md +Preview window~ + *fzf-vim-preview-window* + + *g:fzf_preview_window* + +If the width of the screen is wider than 120 columns, some commands will show +the preview window on the right. You can customize the behavior with +`g:fzf_preview_window`. Here are some examples: +> + " Empty value to disable preview window altogether + let g:fzf_preview_window = '' + + " Always enable preview window on the right with 60% width + let g:fzf_preview_window = 'right:60%' +< + < Command-local options >_____________________________________________________~ *fzf-vim-command-local-options* diff --git a/plugin/fzf.vim b/plugin/fzf.vim index a1f2c05..6cc5d1d 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -39,17 +39,25 @@ function! s:defs(commands) endfor endfunction +function! s:p(bang, ...) + let preview_window = get(g:, 'fzf_preview_window', a:bang && &columns >= 80 || &columns >= 120 ? 'right': '') + if len(preview_window) + return call('fzf#vim#with_preview', add(copy(a:000), preview_window)) + endif + return {} +endfunction + call s:defs([ -\'command! -bang -nargs=? -complete=dir Files call fzf#vim#files(, 0)', -\'command! -bang -nargs=? GitFiles call fzf#vim#gitfiles(, 0)', -\'command! -bang -nargs=? GFiles call fzf#vim#gitfiles(, 0)', -\'command! -bar -bang -nargs=? -complete=buffer Buffers call fzf#vim#buffers(, 0)', +\'command! -bang -nargs=? -complete=dir Files call fzf#vim#files(, s:p(0), 0)', +\'command! -bang -nargs=? GitFiles call fzf#vim#gitfiles(, == "?" ? {} : s:p(0), 0)', +\'command! -bang -nargs=? GFiles call fzf#vim#gitfiles(, == "?" ? {} : s:p(0), 0)', +\'command! -bar -bang -nargs=? -complete=buffer Buffers call fzf#vim#buffers(, s:p(0, { "placeholder": "{1}", "options": ["-d", "\t"] }), 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)', -\'command! -bang -nargs=+ -complete=dir Locate call fzf#vim#locate(, 0)', -\'command! -bang -nargs=* Ag call fzf#vim#ag(, 0)', -\'command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(), 1, 0)', +\'command! -bang -nargs=+ -complete=dir Locate call fzf#vim#locate(, s:p(0), 0)', +\'command! -bang -nargs=* Ag call fzf#vim#ag(, s:p(0), 0)', +\'command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(), 1, s:p(0), 0)', \'command! -bang -nargs=* Tags call fzf#vim#tags(, 0)', \'command! -bang -nargs=* BTags call fzf#vim#buffer_tags(, 0)', \'command! -bar -bang Snippets call fzf#vim#snippets(0)', @@ -61,17 +69,16 @@ call s:defs([ \'command! -bar -bang BCommits call fzf#vim#buffer_commits(0)', \'command! -bar -bang Maps call fzf#vim#maps("n", 0)', \'command! -bar -bang Filetypes call fzf#vim#filetypes(0)', -\'command! -bang -nargs=* History call s:history(, 0)']) +\'command! -bang -nargs=* History call s:history(, s:p(0), 0)']) -function! s:history(arg, bang) +function! s:history(arg, extra, bang) let bang = a:bang || a:arg[len(a:arg)-1] == '!' if a:arg[0] == ':' call fzf#vim#command_history(bang) elseif a:arg[0] == '/' call fzf#vim#search_history(bang) - else - call fzf#vim#history(bang) endif + call fzf#vim#history(a:extra, bang) endfunction function! fzf#complete(...)