From db71151970515026b6b10de871a21bb4678e0ed8 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 14 Nov 2016 02:44:14 +0900 Subject: [PATCH] Add g:fzf_grep_preview_window for enabling Ag/grep preview Close #225 --- README.md | 5 +++++ autoload/fzf/vim.vim | 9 ++++++++- bin/preview.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100755 bin/preview.rb diff --git a/README.md b/README.md index 42b2d87..558ab98 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,11 @@ let g:fzf_tags_command = 'ctags -R' " [Commands] --expect expression for directly executing the command let g:fzf_commands_expect = 'alt-enter,ctrl-x' + +" [Ag / fzf#vim#grep] Command to preview the content +" - Requires Ruby +" - Install Highlight or CodeRay for syntax highlighting +let g:fzf_grep_preview_window = 'right:50%' ``` #### Advanced customization using autoload functions diff --git a/autoload/fzf/vim.vim b/autoload/fzf/vim.vim index 4b61387..5b91a5f 100644 --- a/autoload/fzf/vim.vim +++ b/autoload/fzf/vim.vim @@ -29,6 +29,7 @@ set cpo&vim " ------------------------------------------------------------------ let s:layout_keys = ['window', 'up', 'down', 'left', 'right'] +let s:bin = { 'preview': expand(':h:h:h').'/bin/preview.rb' } let s:TYPE = {'dict': type({}), 'funcref': type(function('call')), 'string': type('')} function s:remove_layout(opts) @@ -587,12 +588,18 @@ 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 textcol = a:with_column ? '4..' : '3..' + let preview = '' + if executable('ruby') && exists('g:fzf_grep_preview_window') + let preview = printf(' --preview-window %s --preview "%s"\ {1}\ {2}\ %d', + \ g:fzf_grep_preview_window, + \ shellescape(s:bin.preview), g:fzf_grep_preview_window =~ 'up\|down') + endif let opts = { \ 'source': a:grep_command, \ 'column': a:with_column, \ 'options': '--ansi --delimiter : --nth '.textcol.',.. --prompt "'.capname.'> " '. \ '--multi --bind alt-a:select-all,alt-d:deselect-all '. - \ '--color hl:68,hl+:110' + \ '--color hl:68,hl+:110'.preview \} function! opts.sink(lines) return s:ag_handler(a:lines, self.column) diff --git a/bin/preview.rb b/bin/preview.rb new file mode 100755 index 0000000..6a6c662 --- /dev/null +++ b/bin/preview.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby +# +# usage: ./preview.rb FILENAME LINENO VSPLIT(0|1) + +require 'shellwords' + +COMMAND = %[(highlight -O ansi -l {} || coderay {} || cat {}) 2> /dev/null] +ANSI = /\x1b\[[0-9;]*m/ +REVERSE = "\x1b[7m" +RESET = "\x1b[m" + +file, center, split = ARGV.fetch(0), ARGV.fetch(1).to_i, ARGV.fetch(2) == '1' +height = File.readable?('/dev/tty') ? `stty size < /dev/tty`.split.first.to_i : 40 +height /= 2 if split +height -= 2 # preview border +offset = [1, center - height / 3].max + +IO.popen(['sh', '-c', COMMAND.gsub('{}', Shellwords.shellescape(file))]) do |io| + io.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