[vim] Use fzf-tmux script for tmux integration

pull/145/head
Junegunn Choi 9 years ago
parent c22e729d9c
commit 12d81e212f

@ -285,17 +285,16 @@ of the selected items.
`fzf#run()` may take an options-dictionary: `fzf#run()` may take an options-dictionary:
| Option name | Type | Description | | Option name | Type | Description |
| --------------- | ------------- | ------------------------------------------------------------------ | | -------------------------- | ------------- | ---------------------------------------------------------------- |
| `source` | string | External command to generate input to fzf (e.g. `find .`) | | `source` | string | External command to generate input to fzf (e.g. `find .`) |
| `source` | list | Vim list as input to fzf | | `source` | list | Vim list as input to fzf |
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) | | `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
| `sink` | funcref | Reference to function to process each selected item | | `sink` | funcref | Reference to function to process each selected item |
| `options` | string | Options to fzf | | `options` | string | Options to fzf |
| `dir` | string | Working directory | | `dir` | string | Working directory |
| `tmux_width` | number/string | Use tmux vertical split with the given height (e.g. `20`, `50%`) | | `up`/`down`/`left`/`right` | number/string | Use tmux pane with the given size (e.g. `20`, `50%`) |
| `tmux_height` | number/string | Use tmux horizontal split with the given height (e.g. `20`, `50%`) | | `launcher` | string | External terminal emulator to start fzf with (Only used in GVim) |
| `launcher` | string | External terminal emulator to start fzf with (Only used in GVim) |
##### Examples ##### Examples
@ -321,10 +320,10 @@ nnoremap <silent> <Leader>C :call fzf#run({
\ 'source': \ 'source':
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"), \ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"), \ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
\ 'sink': 'colo', \ 'sink': 'colo',
\ 'options': '+m', \ 'options': '+m',
\ 'tmux_width': 20, \ 'left': 20,
\ 'launcher': 'xterm -geometry 20x30 -e bash -ic %s' \ 'launcher': 'xterm -geometry 20x30 -e bash -ic %s'
\ })<CR> \ })<CR>
``` ```
@ -345,10 +344,10 @@ function! BufOpen(e)
endfunction endfunction
nnoremap <silent> <Leader><Enter> :call fzf#run({ nnoremap <silent> <Leader><Enter> :call fzf#run({
\ 'source': reverse(BufList()), \ 'source': reverse(BufList()),
\ 'sink': function('BufOpen'), \ 'sink': function('BufOpen'),
\ 'options': '+m', \ 'options': '+m',
\ 'tmux_height': '40%' \ 'down': '40%'
\ })<CR> \ })<CR>
``` ```

@ -7,9 +7,13 @@ opt=""
skip="" skip=""
swap="" swap=""
close="" close=""
term=""
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
arg="$1" arg="$1"
case "$arg" in case "$arg" in
-)
term=1
;;
-w*|-h*|-d*|-u*|-r*|-l*) -w*|-h*|-d*|-u*|-r*|-l*)
if [ -n "$skip" ]; then if [ -n "$skip" ]; then
args+=("$1") args+=("$1")
@ -53,7 +57,11 @@ while [ $# -gt 0 ]; do
fi fi
else else
if [ -n "$swap" ]; then if [ -n "$swap" ]; then
[[ "$arg" =~ ^.l ]] && max=$(tput cols) || max=$(tput lines) if [[ "$arg" =~ ^.l ]]; then
[ -n "$COLUMNS" ] && max=$COLUMNS || max=$(tput cols)
else
[ -n "$LINES" ] && max=$LINES || max=$(tput lines)
fi
size=$(( max - size )) size=$(( max - size ))
[ $size -lt 0 ] && size=0 [ $size -lt 0 ] && size=0
opt="$opt -l $size" opt="$opt -l $size"
@ -103,7 +111,7 @@ envs="FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS") FZF_DEFAULT_COMMAND=$(pr
mkfifo $fifo2 mkfifo $fifo2
mkfifo $fifo3 mkfifo $fifo3
if [ -t 0 ]; then if [ -n "$term" -o -t 0 ]; then
tmux set-window-option -q synchronize-panes off \;\ tmux set-window-option -q synchronize-panes off \;\
split-window $opt "$envs"' sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap split-window $opt "$envs"' sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
else else

@ -21,12 +21,11 @@
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION " OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. " WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
let s:min_tmux_width = 10
let s:min_tmux_height = 3
let s:default_tmux_height = '40%' let s:default_tmux_height = '40%'
let s:launcher = 'xterm -e bash -ic %s' let s:launcher = 'xterm -e bash -ic %s'
let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf' let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'
let s:fzf_rb = expand('<sfile>:h:h').'/fzf' let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
let s:fzf_tmux = expand('<sfile>:h:h').'/bin/fzf-tmux'
let s:cpo_save = &cpo let s:cpo_save = &cpo
set cpo&vim set cpo&vim
@ -66,7 +65,7 @@ function! s:tmux_enabled()
endif endif
let s:tmux = 0 let s:tmux = 0
if exists('$TMUX') if exists('$TMUX') && executable(s:fzf_tmux)
let output = system('tmux -V') let output = system('tmux -V')
let s:tmux = !v:shell_error && output >= 'tmux 1.7' let s:tmux = !v:shell_error && output >= 'tmux 1.7'
endif endif
@ -81,8 +80,23 @@ function! s:escape(path)
return substitute(a:path, ' ', '\\ ', 'g') return substitute(a:path, ' ', '\\ ', 'g')
endfunction endfunction
" Upgrade legacy options
function! s:upgrade(dict)
let copy = copy(a:dict)
if has_key(copy, 'tmux')
let copy.down = remove(copy, 'tmux')
endif
if has_key(copy, 'tmux_height')
let copy.down = remove(copy, 'tmux_height')
endif
if has_key(copy, 'tmux_width')
let copy.right = remove(copy, 'tmux_width')
endif
return copy
endfunction
function! fzf#run(...) abort function! fzf#run(...) abort
let dict = exists('a:1') ? a:1 : {} let dict = exists('a:1') ? s:upgrade(a:1) : {}
let temps = { 'result': tempname() } let temps = { 'result': tempname() }
let optstr = get(dict, 'options', '') let optstr = get(dict, 'options', '')
try try
@ -106,26 +120,41 @@ function! fzf#run(...) abort
else else
let prefix = '' let prefix = ''
endif endif
let command = prefix.fzf_exec.' '.optstr.' > '.temps.result let split = s:tmux_enabled() && s:tmux_splittable(dict)
let command = prefix.(split ? s:fzf_tmux(dict) : fzf_exec).' '.optstr.' > '.temps.result
if s:tmux_enabled() && s:tmux_splittable(dict) if split
return s:execute_tmux(dict, command, temps) return s:execute_tmux(dict, command, temps)
else else
return s:execute(dict, command, temps) return s:execute(dict, command, temps)
endif endif
endfunction endfunction
function! s:fzf_tmux(dict)
let size = ''
for o in ['up', 'down', 'left', 'right']
if has_key(a:dict, o)
let size = '-'.o[0].a:dict[o]
endif
endfor
return printf('LINES=%d COLUMNS=%d %s %s %s --',
\ &lines, &columns, s:fzf_tmux, size, (has_key(a:dict, 'source') ? '' : '-'))
endfunction
function! s:tmux_splittable(dict) function! s:tmux_splittable(dict)
return return has_key(a:dict, 'up') ||
\ min([&columns, get(a:dict, 'tmux_width', 0)]) >= s:min_tmux_width || \ has_key(a:dict, 'down') ||
\ min([&lines, get(a:dict, 'tmux_height', get(a:dict, 'tmux', 0))]) >= s:min_tmux_height \ has_key(a:dict, 'left') ||
\ has_key(a:dict, 'right')
endfunction endfunction
function! s:pushd(dict) function! s:pushd(dict)
if !empty(get(a:dict, 'dir', '')) if !empty(get(a:dict, 'dir', ''))
let a:dict.prev_dir = getcwd() let a:dict.prev_dir = getcwd()
execute 'chdir '.s:escape(a:dict.dir) execute 'chdir '.s:escape(a:dict.dir)
return 1
endif endif
return 0
endfunction endfunction
function! s:popd(dict) function! s:popd(dict)
@ -153,7 +182,7 @@ function! s:execute(dict, command, temps)
endif endif
return [] return []
else else
return s:callback(a:dict, a:temps, 0) return s:callback(a:dict, a:temps)
endif endif
endfunction endfunction
@ -166,58 +195,20 @@ function! s:env_var(name)
endfunction endfunction
function! s:execute_tmux(dict, command, temps) function! s:execute_tmux(dict, command, temps)
let command = s:env_var('FZF_DEFAULT_OPTS').s:env_var('FZF_DEFAULT_COMMAND').a:command let command = a:command
if !empty(get(a:dict, 'dir', '')) if s:pushd(a:dict)
" -c '#{pane_current_path}' is only available on tmux 1.9 or above
let command = 'cd '.s:escape(a:dict.dir).' && '.command let command = 'cd '.s:escape(a:dict.dir).' && '.command
endif endif
let splitopt = '-v' call system(command)
if has_key(a:dict, 'tmux_width') call s:callback(a:dict, a:temps)
let splitopt = '-h'
let size = a:dict.tmux_width
else
let size = get(a:dict, 'tmux_height', get(a:dict, 'tmux'))
endif
if type(size) == 1 && size =~ '%$'
let sizeopt = '-p '.size[0:-2]
else
let sizeopt = '-l '.size
endif
let s:pane = substitute(
\ system(
\ printf(
\ 'tmux split-window %s %s -P -F "#{pane_id}" %s',
\ splitopt, sizeopt, s:shellesc(command))), '\n', '', 'g')
let s:dict = a:dict
let s:temps = a:temps
augroup fzf_tmux
autocmd!
autocmd VimResized * nested call s:tmux_check()
augroup END
endfunction endfunction
function! s:tmux_check() function! s:callback(dict, temps)
let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')
if index(panes, s:pane) < 0
augroup fzf_tmux
autocmd!
augroup END
call s:callback(s:dict, s:temps, 1)
redraw
endif
endfunction
function! s:callback(dict, temps, cd)
if !filereadable(a:temps.result) if !filereadable(a:temps.result)
let lines = [] let lines = []
else else
if a:cd | call s:pushd(a:dict) | endif
let lines = readfile(a:temps.result) let lines = readfile(a:temps.result)
if has_key(a:dict, 'sink') if has_key(a:dict, 'sink')
for line in lines for line in lines
@ -246,7 +237,7 @@ function! s:cmd(bang, ...) abort
let opts.dir = remove(args, -1) let opts.dir = remove(args, -1)
endif endif
if !a:bang if !a:bang
let opts.tmux = get(g:, 'fzf_tmux_height', s:default_tmux_height) let opts.down = get(g:, 'fzf_tmux_height', s:default_tmux_height)
endif endif
call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts)) call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
endfunction endfunction

Loading…
Cancel
Save