From e05b373ff3d1a2f74f5b678ced260997c3aa703e Mon Sep 17 00:00:00 2001 From: Chris Toomey Date: Fri, 14 Nov 2014 16:41:59 -0500 Subject: [PATCH] Prevent killing the Vim pane If the user has swapped the pane layout, it is possible that VTR could attempt to kill the Vim pane. This introduces a sanity check that prevents killing the active / Vim pane. Closes #7 - Protect against killing Vim pane --- plugin/vim-tmux-runner.vim | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/plugin/vim-tmux-runner.vim b/plugin/vim-tmux-runner.vim index 9330323..5a801ad 100644 --- a/plugin/vim-tmux-runner.vim +++ b/plugin/vim-tmux-runner.vim @@ -68,9 +68,13 @@ function! s:RequireLocalPaneOrDetached() endfunction function! s:KillLocalRunner() - let targeted_cmd = s:TargetedTmuxCommand("kill-pane", s:runner_pane) - call s:SendTmuxCommand(targeted_cmd) - unlet s:runner_pane + if s:runner_pane == s:ActiveTmuxPaneNumber() + call s:EchoError("RunnerPane is set to current pane. Cancelling") + else + let targeted_cmd = s:TargetedTmuxCommand("kill-pane", s:runner_pane) + call s:SendTmuxCommand(targeted_cmd) + unlet s:runner_pane + endif endfunction function! s:KillDetachedWindow() @@ -91,11 +95,7 @@ function! s:KillRunnerPane() endfunction function! s:ActiveTmuxPaneNumber() - for pane_title in s:TmuxPanes() - if pane_title =~ '\(active\)' - return pane_title[0] - endif - endfor + return s:SendTmuxCommand("display-message -p \"#{pane_index}\"") endfunction function! s:TmuxPanes() @@ -133,9 +133,13 @@ function! s:ZoomRunnerPane() call s:SendTmuxCommand(resize_cmd) endfunction +function! s:Strip(string) + return substitute(a:string, '^\s*\(.\{-}\)\s*$', '\1', '') +endfunction + function! s:SendTmuxCommand(command) let prefixed_command = "tmux " . a:command - return system(prefixed_command) + return s:Strip(system(prefixed_command)) endfunction function! s:TargetedTmuxCommand(command, target_pane)