From bc5af8e053fa06af43f2e4c4919223a6fcc107b4 Mon Sep 17 00:00:00 2001 From: Ryan Miller Date: Sun, 19 Apr 2020 21:01:23 -0400 Subject: [PATCH] Better Vim -> tmux Forwarding (#6) * 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 --- Makefile | 7 ++-- plugin/better_vim_tmux_resizer.vim | 52 ++++++++++++------------------ 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 836755e..cbc702a 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/plugin/better_vim_tmux_resizer.vim b/plugin/better_vim_tmux_resizer.vim index b17bb28..790f3c3 100644 --- a/plugin/better_vim_tmux_resizer.vim +++ b/plugin/better_vim_tmux_resizer.vim @@ -1,5 +1,6 @@ -" Maps to resize vim splits in the given direction. If there -" are no more windows in that direction, forwards the operation to tmux. +" Maps 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 @@ -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 ? '+' : '-' @@ -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 . '' endfunction @@ -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()