Skip to content

Commit

Permalink
Better Vim -> tmux Forwarding (#6)
Browse files Browse the repository at this point in the history
* Comment out unused function

* Specify test socket name in Makefile

* Refactor vim/tmux resize determination method

* Uncomment process list; maybe useful for debugging

* Add separate make vim for testing only vim

* Update plugin header

* Clean up unused code and add comments
  • Loading branch information
RyanMillerC authored Apr 20, 2020
1 parent 219bdec commit bc5af8e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.PHONY: test
.PHONY: test vim

test:
tmux -f test/.tmux.conf new-session "nvim -u test/.vimrc"
tmux -L tmux-testing -f test/.tmux.conf new-session "make vim"

vim:
nvim -u test/.vimrc
52 changes: 21 additions & 31 deletions plugin/better_vim_tmux_resizer.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
" Maps <M-h/j/k/l> to resize vim splits in the given direction. If there
" are no more windows in that direction, forwards the operation to tmux.
" Maps <M-h/j/k/l> to resize vim splits in the given direction.
" If the movement operation has no effect in Vim, it forwards the operation to
" Tmux.

if exists("g:loaded_tmux_resizer") || &cp || v:version < 700
finish
Expand All @@ -22,7 +23,7 @@ if !exists("g:tmux_resizer_no_mappings")
endif

function! s:VimResize(direction)
" Resize toward given direction, like tmux
" Resize Vim window toward given direction, like tmux
let l:current_window_is_last_window = (winnr() == winnr('$'))
if (a:direction == 'h' || a:direction == 'k')
let l:modifier = l:current_window_is_last_window ? '+' : '-'
Expand All @@ -37,6 +38,7 @@ function! s:VimResize(direction)
let l:command = 'resize'
let l:window_resize_count = g:tmux_resizer_resize_count
endif

execute l:command . ' ' . l:modifier . l:window_resize_count . '<CR>'
endfunction

Expand Down Expand Up @@ -67,48 +69,36 @@ function! s:TmuxCommand(args)
return system(cmd)
endfunction

function! s:TmuxResizerProcessList()
echo s:TmuxCommand("run-shell 'ps -o state= -o comm= -t ''''#{pane_tty}'''''")
endfunction
command! TmuxResizerProcessList call s:TmuxResizerProcessList()

let s:tmux_is_last_pane = 0
augroup tmux_resizer
au!
autocmd WinEnter * let s:tmux_is_last_pane = 0
augroup END

function! s:NeedsVitalityRedraw()
return exists('g:loaded_vitality') && v:version < 704 && !has("patch481")
endfunction

function! s:ShouldForwardResizeBackToTmux(tmux_last_pane, at_tab_page_edge)
return a:tmux_last_pane || a:at_tab_page_edge
endfunction

function! s:TmuxAwareResize(direction)
let nr = winnr()
let tmux_last_pane = (a:direction == 'p' && s:tmux_is_last_pane)
if !tmux_last_pane
call s:VimResize(a:direction)
endif
let at_tab_page_edge = (nr == winnr())
" Forward the resize panes command to tmux if:
" a) we're toggling between the last tmux pane;
" b) we tried resizing windows in vim but it didn't have effect.
if s:ShouldForwardResizeBackToTmux(tmux_last_pane, at_tab_page_edge)
let l:previous_window_width = winwidth(0)
let l:previous_window_height = winheight(0)

" Attempt to resize Vim window
call s:VimResize(a:direction)

" Call tmux if Vim window dimentions did not change
if (l:previous_window_height == winheight(0) && l:previous_window_width == winwidth(0))
if (a:direction == 'h' || a:direction == 'l')
let l:resize_count = g:tmux_resizer_vertical_resize_count
else
let l:resize_count = g:tmux_resizer_resize_count
endif
let args = 'resize-pane -' . tr(a:direction, 'hjkl', 'LDUR') . ' ' . l:resize_count

silent call s:TmuxCommand(args)

if s:NeedsVitalityRedraw()
redraw!
endif
let s:tmux_is_last_pane = 1
else
let s:tmux_is_last_pane = 0
endif
endfunction

" For debugging
function! s:TmuxResizerProcessList()
echo s:TmuxCommand("run-shell 'ps -o state= -o comm= -t ''''#{pane_tty}'''''")
endfunction
command! TmuxResizerProcessList call s:TmuxResizerProcessList()

0 comments on commit bc5af8e

Please sign in to comment.