2012-07-17 21:18:58 +00:00
|
|
|
if exists("g:loaded_vimux") || &cp
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_vimux = 1
|
|
|
|
|
|
|
|
command VimuxRunLastCommand :call VimuxRunLastCommand()
|
|
|
|
command VimuxCloseRunner :call VimuxCloseRunner()
|
|
|
|
command VimuxInspectRunner :call VimuxInspectRunner()
|
|
|
|
command VimuxScrollUpInspect :call VimuxScrollUpInspect()
|
|
|
|
command VimuxScrollDownInspect :call VimuxScrollDownInspect()
|
|
|
|
command VimuxInterruptRunner :call VimuxInterruptRunner()
|
|
|
|
command VimuxPromptCommand :call VimuxPromptCommand()
|
|
|
|
command VimuxClearRunnerHistory :call VimuxClearRunnerHistory()
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxRunLastCommand()
|
|
|
|
if exists("g:VimuxRunnerPaneIndex")
|
|
|
|
call VimuxRunCommand(g:VimuxLastCommand)
|
|
|
|
else
|
|
|
|
echo "No last vimux command."
|
|
|
|
endif
|
|
|
|
endfunction
|
2012-07-17 21:18:58 +00:00
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxRunCommand(command, ...)
|
|
|
|
if !exists("g:VimuxRunnerPaneIndex") || _VimuxHasPane(g:VimuxRunnerPaneIndex) == -1
|
|
|
|
call VimuxOpenPane()
|
|
|
|
endif
|
2012-07-17 21:18:58 +00:00
|
|
|
|
|
|
|
let l:autoreturn = 1
|
|
|
|
if exists("a:1")
|
|
|
|
let l:autoreturn = a:1
|
|
|
|
endif
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
let resetSequence = _VimuxOption("g:VimuxResetSequence", "q C-u")
|
|
|
|
let g:VimuxLastCommand = a:command
|
|
|
|
|
|
|
|
call VimuxSendKeys(resetSequence)
|
|
|
|
call VimuxSendText(a:command)
|
2012-07-17 21:18:58 +00:00
|
|
|
|
|
|
|
if l:autoreturn == 1
|
2013-04-03 18:03:03 +00:00
|
|
|
call VimuxSendKeys("Enter")
|
2012-07-17 21:18:58 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxSendText(text)
|
2013-05-31 19:27:06 +00:00
|
|
|
call VimuxSendKeys('"'.escape(a:text, '"').'"')
|
2013-04-03 18:03:03 +00:00
|
|
|
endfunction
|
2012-07-17 21:18:58 +00:00
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxSendKeys(keys)
|
|
|
|
if exists("g:VimuxRunnerPaneIndex")
|
|
|
|
call system("tmux send-keys -t ".g:VimuxRunnerPaneIndex." ".a:keys)
|
2012-07-17 21:18:58 +00:00
|
|
|
else
|
2013-04-03 18:03:03 +00:00
|
|
|
echo "No vimux runner pane. Create one with VimuxOpenPane"
|
2012-07-17 21:18:58 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxOpenPane()
|
|
|
|
let height = _VimuxOption("g:VimuxHeight", 20)
|
|
|
|
let orientation = _VimuxOption("g:VimuxOrientation", "v")
|
|
|
|
let nearestIndex = _VimuxNearestPaneIndex()
|
2012-07-17 21:18:58 +00:00
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
if _VimuxOption("g:VimuxUseNearestPane", 1) == 1 && nearestIndex != -1
|
|
|
|
let g:VimuxRunnerPaneIndex = nearestIndex
|
2012-07-17 21:18:58 +00:00
|
|
|
else
|
2013-04-03 18:03:03 +00:00
|
|
|
call system("tmux split-window -p ".height." -".orientation)
|
|
|
|
let g:VimuxRunnerPaneIndex = _VimuxTmuxPaneIndex()
|
|
|
|
call system("tmux last-pane")
|
2012-07-17 21:18:58 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxCloseRunner()
|
|
|
|
if exists("g:VimuxRunnerPaneIndex")
|
|
|
|
call system("tmux kill-pane -t ".g:VimuxRunnerPaneIndex)
|
|
|
|
unlet g:VimuxRunnerPaneIndex
|
|
|
|
endif
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxInspectRunner()
|
|
|
|
call system("tmux select-pane -t ".g:VimuxRunnerPaneIndex)
|
|
|
|
call system("tmux copy-mode")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxScrollUpInspect()
|
|
|
|
call VimuxInspectRunner()
|
|
|
|
call system("tmux last-pane")
|
|
|
|
call VimuxSendKeys("C-u")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxScrollDownInspect()
|
|
|
|
call VimuxInspectRunner()
|
|
|
|
call system("tmux last-pane")
|
|
|
|
call VimuxSendKeys("C-d")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxInterruptRunner()
|
|
|
|
call VimuxSendKeys("^c")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxClearRunnerHistory()
|
|
|
|
if exists("g:VimuxRunnerPaneIndex")
|
|
|
|
call system("tmux clear-history -t ".g:VimuxRunnerPaneIndex)
|
|
|
|
endif
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxPromptCommand()
|
|
|
|
let l:command = input(_VimuxOption("g:VimuxPromptString", "Command? "))
|
|
|
|
call VimuxRunCommand(l:command)
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxTmuxSession()
|
|
|
|
return _VimuxTmuxProperty("S")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxTmuxPaneIndex()
|
|
|
|
return _VimuxTmuxProperty("P")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxTmuxWindowIndex()
|
|
|
|
return _VimuxTmuxProperty("I")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxNearestPaneIndex()
|
|
|
|
let panes = split(system("tmux list-panes"), "\n")
|
2012-07-17 21:18:58 +00:00
|
|
|
|
2013-07-03 14:18:24 +00:00
|
|
|
for pane in panes
|
|
|
|
if match(pane, "(active)") == -1
|
|
|
|
return split(pane, ":")[0]
|
2013-04-03 18:03:03 +00:00
|
|
|
endif
|
|
|
|
endfor
|
2012-07-17 21:18:58 +00:00
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
return -1
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxOption(option, default)
|
|
|
|
if exists(a:option)
|
|
|
|
return eval(a:option)
|
|
|
|
else
|
|
|
|
return a:default
|
2012-10-18 20:57:59 +00:00
|
|
|
endif
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxTmuxProperty(property)
|
|
|
|
return substitute(system("tmux display -p '#".a:property."'"), '\n$', '', '')
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxHasPane(index)
|
|
|
|
return match(system("tmux list-panes"), a:index.":")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|