Smarten up AttachToPane functionality

- Automatically attach if only 2 panes
- Ensure pane with index exists before attaching

The runner_pane must not be the Vim pane, so if there are only two
panes, then we can be certain that the desired pane is the other, and
automatically attach

Closes #20 - Make AttachToPane command smarter
This commit is contained in:
Chris Toomey 2014-11-14 18:40:29 -05:00
parent e05b373ff3
commit 7c4cd3d059

View File

@ -21,10 +21,10 @@ function! s:CreateRunnerPane(...)
let s:vtr_percentage = s:DictFetch(a:1, 'percentage', s:vtr_percentage) let s:vtr_percentage = s:DictFetch(a:1, 'percentage', s:vtr_percentage)
let g:VtrInitialCommand = s:DictFetch(a:1, 'cmd', g:VtrInitialCommand) let g:VtrInitialCommand = s:DictFetch(a:1, 'cmd', g:VtrInitialCommand)
endif endif
let s:vim_pane = s:ActiveTmuxPaneNumber() let s:vim_pane = s:ActivePaneIndex()
let cmd = join(["split-window -p", s:vtr_percentage, "-".s:vtr_orientation]) let cmd = join(["split-window -p", s:vtr_percentage, "-".s:vtr_orientation])
call s:SendTmuxCommand(cmd) call s:SendTmuxCommand(cmd)
let s:runner_pane = s:ActiveTmuxPaneNumber() let s:runner_pane = s:ActivePaneIndex()
call s:FocusVimPane() call s:FocusVimPane()
if g:VtrGitCdUpOnOpen if g:VtrGitCdUpOnOpen
call s:GitCdUp() call s:GitCdUp()
@ -68,7 +68,7 @@ function! s:RequireLocalPaneOrDetached()
endfunction endfunction
function! s:KillLocalRunner() function! s:KillLocalRunner()
if s:runner_pane == s:ActiveTmuxPaneNumber() if s:runner_pane == s:ActivePaneIndex()
call s:EchoError("RunnerPane is set to current pane. Cancelling") call s:EchoError("RunnerPane is set to current pane. Cancelling")
else else
let targeted_cmd = s:TargetedTmuxCommand("kill-pane", s:runner_pane) let targeted_cmd = s:TargetedTmuxCommand("kill-pane", s:runner_pane)
@ -94,7 +94,7 @@ function! s:KillRunnerPane()
endif endif
endfunction endfunction
function! s:ActiveTmuxPaneNumber() function! s:ActivePaneIndex()
return s:SendTmuxCommand("display-message -p \"#{pane_index}\"") return s:SendTmuxCommand("display-message -p \"#{pane_index}\"")
endfunction endfunction
@ -134,7 +134,7 @@ function! s:ZoomRunnerPane()
endfunction endfunction
function! s:Strip(string) function! s:Strip(string)
return substitute(a:string, '^\s*\(.\{-}\)\s*$', '\1', '') return substitute(a:string, '^\s*\(.\{-}\)\s*\n\?$', '\1', '')
endfunction endfunction
function! s:SendTmuxCommand(command) function! s:SendTmuxCommand(command)
@ -207,10 +207,40 @@ function! s:_ReattachPane()
\ s:RunnerDimensionSpec()]) \ s:RunnerDimensionSpec()])
call s:SendTmuxCommand(join_cmd) call s:SendTmuxCommand(join_cmd)
unlet s:detached_window unlet s:detached_window
let s:runner_pane = s:ActiveTmuxPaneNumber() let s:runner_pane = s:ActivePaneIndex()
endfunction
function! s:TmuxInfo(message)
" TODO: this should accept optional target pane, default to current.
" Pass that to TargetedCommand as "display-message", "-p '#{...}')
return s:SendTmuxCommand("display-message -p '#{" . a:message . "}'")
endfunction
function! s:PaneCount()
return str2nr(s:TmuxInfo('window_panes'))
endfunction
function! s:PaneIndices()
let index_slicer = 'str2nr(substitute(v:val, "\\v(\\d+):.*", "\\1", ""))'
return map(s:TmuxPanes(), index_slicer)
endfunction
function! s:AvailableRunnerPaneIndices()
return filter(s:PaneIndices(), "v:val != " . s:ActivePaneIndex())
endfunction
function! s:AltPane()
if s:PaneCount() == 2
return s:AvailableRunnerPaneIndices()[0]
else
echoerr "VTR: AltPane only valid if two panes open"
endif
endfunction endfunction
function! s:PromptForRunnerToAttach() function! s:PromptForRunnerToAttach()
if s:PaneCount() == 2
call s:AttachToPane(s:AltPane())
else
if g:VtrDisplayPaneNumbers if g:VtrDisplayPaneNumbers
call s:SendTmuxCommand('source ~/.tmux.conf && tmux display-panes') call s:SendTmuxCommand('source ~/.tmux.conf && tmux display-panes')
endif endif
@ -220,6 +250,7 @@ function! s:PromptForRunnerToAttach()
else else
call s:EchoError("No pane specified. Cancelling.") call s:EchoError("No pane specified. Cancelling.")
endif endif
endif
endfunction endfunction
function! s:AttachToPane(desired_pane) function! s:AttachToPane(desired_pane)
@ -236,8 +267,8 @@ function! s:EchoError(message)
endfunction endfunction
function! s:ValidRunnerPaneNumber(desired_pane) function! s:ValidRunnerPaneNumber(desired_pane)
if a:desired_pane == s:ActiveTmuxPaneNumber() | return 0 | endif if a:desired_pane == s:ActivePaneIndex() | return 0 | endif
if a:desired_pane > len(s:TmuxPanes()) | return 0 | endif if a:desired_pane > (s:PaneCount() - 1) | return 0 | endif
return 1 return 1
endfunction endfunction