Skip to content

Commit bc5af8e

Browse files
author
Ryan Miller
authored
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
1 parent 219bdec commit bc5af8e

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
.PHONY: test
1+
.PHONY: test vim
22

33
test:
4-
tmux -f test/.tmux.conf new-session "nvim -u test/.vimrc"
4+
tmux -L tmux-testing -f test/.tmux.conf new-session "make vim"
5+
6+
vim:
7+
nvim -u test/.vimrc

plugin/better_vim_tmux_resizer.vim

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
" Maps <M-h/j/k/l> to resize vim splits in the given direction. If there
2-
" are no more windows in that direction, forwards the operation to tmux.
1+
" Maps <M-h/j/k/l> to resize vim splits in the given direction.
2+
" If the movement operation has no effect in Vim, it forwards the operation to
3+
" Tmux.
34

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

2425
function! s:VimResize(direction)
25-
" Resize toward given direction, like tmux
26+
" Resize Vim window toward given direction, like tmux
2627
let l:current_window_is_last_window = (winnr() == winnr('$'))
2728
if (a:direction == 'h' || a:direction == 'k')
2829
let l:modifier = l:current_window_is_last_window ? '+' : '-'
@@ -37,6 +38,7 @@ function! s:VimResize(direction)
3738
let l:command = 'resize'
3839
let l:window_resize_count = g:tmux_resizer_resize_count
3940
endif
41+
4042
execute l:command . ' ' . l:modifier . l:window_resize_count . '<CR>'
4143
endfunction
4244

@@ -67,48 +69,36 @@ function! s:TmuxCommand(args)
6769
return system(cmd)
6870
endfunction
6971

70-
function! s:TmuxResizerProcessList()
71-
echo s:TmuxCommand("run-shell 'ps -o state= -o comm= -t ''''#{pane_tty}'''''")
72-
endfunction
73-
command! TmuxResizerProcessList call s:TmuxResizerProcessList()
74-
75-
let s:tmux_is_last_pane = 0
76-
augroup tmux_resizer
77-
au!
78-
autocmd WinEnter * let s:tmux_is_last_pane = 0
79-
augroup END
80-
8172
function! s:NeedsVitalityRedraw()
8273
return exists('g:loaded_vitality') && v:version < 704 && !has("patch481")
8374
endfunction
8475

85-
function! s:ShouldForwardResizeBackToTmux(tmux_last_pane, at_tab_page_edge)
86-
return a:tmux_last_pane || a:at_tab_page_edge
87-
endfunction
88-
8976
function! s:TmuxAwareResize(direction)
90-
let nr = winnr()
91-
let tmux_last_pane = (a:direction == 'p' && s:tmux_is_last_pane)
92-
if !tmux_last_pane
93-
call s:VimResize(a:direction)
94-
endif
95-
let at_tab_page_edge = (nr == winnr())
96-
" Forward the resize panes command to tmux if:
97-
" a) we're toggling between the last tmux pane;
98-
" b) we tried resizing windows in vim but it didn't have effect.
99-
if s:ShouldForwardResizeBackToTmux(tmux_last_pane, at_tab_page_edge)
77+
let l:previous_window_width = winwidth(0)
78+
let l:previous_window_height = winheight(0)
79+
80+
" Attempt to resize Vim window
81+
call s:VimResize(a:direction)
82+
83+
" Call tmux if Vim window dimentions did not change
84+
if (l:previous_window_height == winheight(0) && l:previous_window_width == winwidth(0))
10085
if (a:direction == 'h' || a:direction == 'l')
10186
let l:resize_count = g:tmux_resizer_vertical_resize_count
10287
else
10388
let l:resize_count = g:tmux_resizer_resize_count
10489
endif
10590
let args = 'resize-pane -' . tr(a:direction, 'hjkl', 'LDUR') . ' ' . l:resize_count
91+
10692
silent call s:TmuxCommand(args)
93+
10794
if s:NeedsVitalityRedraw()
10895
redraw!
10996
endif
110-
let s:tmux_is_last_pane = 1
111-
else
112-
let s:tmux_is_last_pane = 0
11397
endif
11498
endfunction
99+
100+
" For debugging
101+
function! s:TmuxResizerProcessList()
102+
echo s:TmuxCommand("run-shell 'ps -o state= -o comm= -t ''''#{pane_tty}'''''")
103+
endfunction
104+
command! TmuxResizerProcessList call s:TmuxResizerProcessList()

0 commit comments

Comments
 (0)