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()
|
2013-09-28 20:32:35 +00:00
|
|
|
if exists("g:VimuxRunnerIndex")
|
2013-04-03 18:03:03 +00:00
|
|
|
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, ...)
|
2013-09-28 20:32:35 +00:00
|
|
|
if !exists("g:VimuxRunnerIndex") || _VimuxHasRunner(g:VimuxRunnerIndex) == -1
|
|
|
|
call VimuxOpenRunner()
|
2013-04-03 18:03:03 +00:00
|
|
|
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)
|
2013-09-28 20:32:35 +00:00
|
|
|
if exists("g:VimuxRunnerIndex")
|
|
|
|
call system("tmux send-keys -t ".g:VimuxRunnerIndex." ".a:keys)
|
2012-07-17 21:18:58 +00:00
|
|
|
else
|
2013-09-28 20:32:35 +00:00
|
|
|
echo "No vimux runner pane/window. Create one with VimuxOpenRunner"
|
2012-07-17 21:18:58 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-09-28 20:32:35 +00:00
|
|
|
function! VimuxOpenRunner()
|
2013-04-03 18:03:03 +00:00
|
|
|
let height = _VimuxOption("g:VimuxHeight", 20)
|
|
|
|
let orientation = _VimuxOption("g:VimuxOrientation", "v")
|
2013-09-28 20:32:35 +00:00
|
|
|
let nearestIndex = _VimuxNearestIndex()
|
2012-07-17 21:18:58 +00:00
|
|
|
|
2013-09-28 20:32:35 +00:00
|
|
|
if _VimuxOption("g:VimuxUseNearest", 1) == 1 && nearestIndex != -1
|
|
|
|
let g:VimuxRunnerIndex = 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)
|
2013-09-28 20:32:35 +00:00
|
|
|
let g:VimuxRunnerIndex = _VimuxTmuxIndex()
|
2013-04-03 18:03:03 +00:00
|
|
|
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()
|
2013-09-28 20:32:35 +00:00
|
|
|
if exists("g:VimuxRunnerIndex")
|
|
|
|
call system("tmux kill-pane -t ".g:VimuxRunnerIndex)
|
|
|
|
unlet g:VimuxRunnerIndex
|
2013-04-03 18:03:03 +00:00
|
|
|
endif
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxInspectRunner()
|
2013-09-28 20:32:35 +00:00
|
|
|
call system("tmux select-pane -t ".g:VimuxRunnerIndex)
|
2013-04-03 18:03:03 +00:00
|
|
|
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()
|
2013-09-28 20:32:35 +00:00
|
|
|
if exists("g:VimuxRunnerIndex")
|
|
|
|
call system("tmux clear-history -t ".g:VimuxRunnerIndex)
|
2013-04-03 18:03:03 +00:00
|
|
|
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-09-28 20:32:35 +00:00
|
|
|
function! _VimuxTmuxIndex()
|
2013-04-03 18:03:03 +00:00
|
|
|
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-09-28 20:32:35 +00:00
|
|
|
function! _VimuxNearestIndex()
|
2013-04-03 18:03:03 +00:00
|
|
|
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-09-28 20:32:35 +00:00
|
|
|
function! _VimuxHasRunner(index)
|
2013-04-03 18:03:03 +00:00
|
|
|
return match(system("tmux list-panes"), a:index.":")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|