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()
|
2013-08-31 18:14:39 +00:00
|
|
|
command VimuxZoomRunner :call VimuxZoomRunner()
|
2012-07-17 21:18:58 +00:00
|
|
|
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()
|
|
|
|
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-09-28 21:26:59 +00:00
|
|
|
if _VimuxRunnerType() == "pane"
|
|
|
|
let height = _VimuxOption("g:VimuxHeight", 20)
|
|
|
|
let orientation = _VimuxOption("g:VimuxOrientation", "v")
|
|
|
|
call system("tmux split-window -p ".height." -".orientation)
|
|
|
|
elseif _VimuxRunnerType() == "window"
|
|
|
|
call system("tmux new-window")
|
|
|
|
endif
|
|
|
|
|
2013-09-28 20:32:35 +00:00
|
|
|
let g:VimuxRunnerIndex = _VimuxTmuxIndex()
|
2013-09-28 21:14:41 +00:00
|
|
|
call system("tmux last-"._VimuxRunnerType())
|
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")
|
2013-09-28 21:14:41 +00:00
|
|
|
call system("tmux kill-"._VimuxRunnerType()." -t ".g:VimuxRunnerIndex)
|
2013-09-28 20:32:35 +00:00
|
|
|
unlet g:VimuxRunnerIndex
|
2013-04-03 18:03:03 +00:00
|
|
|
endif
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-08-31 18:14:39 +00:00
|
|
|
function! VimuxZoomRunner()
|
|
|
|
if exists("g:VimuxRunnerPaneIndex")
|
|
|
|
call system("tmux resize-pane -Z -t ".g:VimuxRunnerPaneIndex)
|
|
|
|
unlet g:VimuxRunnerPaneIndex
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! VimuxInspectRunner()
|
2013-09-28 21:14:41 +00:00
|
|
|
call system("tmux select-"._VimuxRunnerType()." -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()
|
2013-09-28 21:14:41 +00:00
|
|
|
call system("tmux last-"._VimuxRunnerType())
|
2013-04-03 18:03:03 +00:00
|
|
|
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()
|
2013-09-28 21:14:41 +00:00
|
|
|
call system("tmux last-"._VimuxRunnerType())
|
2013-04-03 18:03:03 +00:00
|
|
|
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-12-02 21:47:07 +00:00
|
|
|
if _VimuxRunnerType() == "pane"
|
2013-09-28 21:26:59 +00:00
|
|
|
return _VimuxTmuxPaneIndex()
|
|
|
|
else
|
|
|
|
return _VimuxTmuxWindowIndex()
|
|
|
|
end
|
|
|
|
endfunction
|
|
|
|
|
2013-04-03 18:03:03 +00:00
|
|
|
function! _VimuxTmuxPaneIndex()
|
2013-09-28 21:26:59 +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-09-28 21:14:41 +00:00
|
|
|
let panes = split(system("tmux list-"._VimuxRunnerType()."s"), "\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-09-28 21:14:41 +00:00
|
|
|
function! _VimuxRunnerType()
|
|
|
|
return _VimuxOption("g:VimuxRunnerType", "pane")
|
|
|
|
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-09-28 21:14:41 +00:00
|
|
|
return match(system("tmux list-"._VimuxRunnerType()."s"), a:index.":")
|
2012-07-17 21:18:58 +00:00
|
|
|
endfunction
|