Skip to content

Commit

Permalink
[fzf#vim#with_preview] Support WSL bash (#1026)
Browse files Browse the repository at this point in the history
* [fzf#vim#with_preview] support wsl bash

Close #988
Close #1009

* [fzf#vim#with_preview] use abs path for bin/

WSL has issues with relative filepaths.
  • Loading branch information
janlazo authored May 23, 2020
1 parent 996ec3b commit 7a65517
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 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,7 +41,6 @@ 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
Expand Down Expand Up @@ -76,6 +75,11 @@ endfunction

" [[options to wrap], [preview window expression], [toggle-preview keys...]]
function! fzf#vim#with_preview(...)
let bash_path = exepath('bash')
if empty(bash_path)
throw 'bash is not in PATH'
endif
let is_wsl_bash = bash_path =~? 'Windows[/\\]system32[/\\]bash.exe$'
" Default options
let options = {}
let window = ''
Expand Down Expand Up @@ -104,7 +108,14 @@ function! fzf#vim#with_preview(...)
if len(window)
let preview += ['--preview-window', window]
endif
let preview += ['--preview', (s:is_win ? s:bin.preview : fzf#shellescape(s:bin.preview)).' '.placeholder]
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 = fzf#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 Down

0 comments on commit 7a65517

Please sign in to comment.