Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Junegunn Choi
db71151970
Add g:fzf_grep_preview_window for enabling Ag/grep preview
Close #225
2016-11-14 02:44:14 +09:00
3 changed files with 39 additions and 1 deletions

View file

@ -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

View file

@ -29,6 +29,7 @@ set cpo&vim
" ------------------------------------------------------------------
let s:layout_keys = ['window', 'up', 'down', 'left', 'right']
let s:bin = { 'preview': expand('<sfile>: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)

26
bin/preview.rb Executable file
View file

@ -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