2012-03-23 23:58:21 +00:00
|
|
|
if exists("g:loaded_vimux") || &cp
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_vimux = 1
|
|
|
|
|
2012-02-28 02:48:42 +00:00
|
|
|
if !has("ruby")
|
|
|
|
finish
|
|
|
|
end
|
|
|
|
|
2012-02-29 04:49:05 +00:00
|
|
|
command RunLastVimTmuxCommand :call RunLastVimTmuxCommand()
|
2012-03-13 13:57:14 +00:00
|
|
|
command CloseVimTmuxPanes :call CloseVimTmuxPanes()
|
2012-02-29 04:49:05 +00:00
|
|
|
command CloseVimTmuxWindows :call CloseVimTmuxWindows()
|
2012-02-29 18:44:13 +00:00
|
|
|
command InspectVimTmuxRunner :call InspectVimTmuxRunner()
|
2012-03-01 01:19:53 +00:00
|
|
|
command InterruptVimTmuxRunner :call InterruptVimTmuxRunner()
|
2012-02-29 18:44:13 +00:00
|
|
|
command PromptVimTmuxCommand :call PromptVimTmuxCommand()
|
2012-02-29 04:49:05 +00:00
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function RunVimTmuxCommand(command)
|
2012-02-28 02:48:42 +00:00
|
|
|
let g:_VimTmuxCmd = a:command
|
|
|
|
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("g:_VimTmuxCmd"))
|
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function RunLastVimTmuxCommand()
|
2012-02-29 04:49:05 +00:00
|
|
|
if exists("g:_VimTmuxCmd")
|
|
|
|
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("g:_VimTmuxCmd"))
|
|
|
|
else
|
|
|
|
echo "No last command"
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function ClearVimTmuxWindow()
|
2012-02-29 04:49:05 +00:00
|
|
|
if exists("g:_VimTmuxRunnerPane")
|
|
|
|
unlet g:_VimTmuxRunnerPane
|
|
|
|
end
|
2012-02-28 02:48:42 +00:00
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function CloseVimTmuxWindows()
|
2012-02-28 02:48:42 +00:00
|
|
|
ruby CurrentTmuxSession.new.close_other_panes
|
2012-02-29 04:49:05 +00:00
|
|
|
call ClearVimTmuxWindow()
|
2012-03-13 13:57:14 +00:00
|
|
|
echoerr "CloseVimTmuxWindows is deprecated, use CloseVimTmuxPanes"
|
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function CloseVimTmuxPanes()
|
2012-03-13 13:57:14 +00:00
|
|
|
ruby CurrentTmuxSession.new.close_other_panes
|
|
|
|
call ClearVimTmuxWindow()
|
2012-02-28 02:48:42 +00:00
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function InterruptVimTmuxRunner()
|
2012-03-01 01:19:53 +00:00
|
|
|
ruby CurrentTmuxSession.new.interrupt_runner
|
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function InspectVimTmuxRunner()
|
2012-02-29 18:44:13 +00:00
|
|
|
ruby CurrentTmuxSession.new.inspect_runner
|
|
|
|
endfunction
|
|
|
|
|
2012-03-23 23:58:21 +00:00
|
|
|
function PromptVimTmuxCommand()
|
2012-02-29 18:44:13 +00:00
|
|
|
let l:command = input("Command? ")
|
|
|
|
call RunVimTmuxCommand(l:command)
|
|
|
|
endfunction
|
|
|
|
|
2012-02-28 02:48:42 +00:00
|
|
|
ruby << EOF
|
|
|
|
class TmuxSession
|
|
|
|
def initialize(session, window, pane)
|
|
|
|
@session = session
|
|
|
|
@window = window
|
|
|
|
@pane = pane
|
2012-02-29 04:49:05 +00:00
|
|
|
@runner_pane = vim_cached_runner_pane
|
|
|
|
end
|
|
|
|
|
|
|
|
def vim_cached_runner_pane
|
2012-02-28 02:48:42 +00:00
|
|
|
if Vim.evaluate('exists("g:_VimTmuxRunnerPane")') != 0
|
2012-02-29 04:49:05 +00:00
|
|
|
Vim.evaluate('g:_VimTmuxRunnerPane')
|
2012-02-28 02:48:42 +00:00
|
|
|
else
|
2012-02-29 04:49:05 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def vim_cached_runner_pane=(runner_pane)
|
|
|
|
Vim.command("let g:_VimTmuxRunnerPane = '#{runner_pane}'")
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_vim_cached_runner_pane
|
|
|
|
Vim.command("unlet g:_VimTmuxRunnerPane")
|
|
|
|
end
|
|
|
|
|
|
|
|
def height
|
|
|
|
if Vim.evaluate('exists("g:VimuxHeight")') != 0
|
|
|
|
Vim.evaluate('g:VimuxHeight')
|
|
|
|
else
|
|
|
|
20
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-10 01:03:54 +00:00
|
|
|
def orientation
|
|
|
|
if Vim.evaluate('exists("g:VimuxOrientation")') != 0 && ["h", "v"].include?(Vim.evaluate('g:VimuxOrientation'))
|
|
|
|
"-#{Vim.evaluate('g:VimuxOrientation')}"
|
|
|
|
else
|
|
|
|
"-v"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-29 18:44:13 +00:00
|
|
|
def inspect_runner
|
2012-03-30 18:03:53 +00:00
|
|
|
_run("select-pane -t #{target(:pane => runner_pane)}")
|
|
|
|
_run("copy-mode")
|
|
|
|
Vim.command("let g:_VimTmuxInspecting = 1")
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_inspecting_runner
|
|
|
|
if Vim.evaluate('exists("g:_VimTmuxInspecting")') != 0
|
|
|
|
_run("send-keys -t #{target(:pane => runner_pane)} q")
|
|
|
|
Vim.command("unlet g:_VimTmuxInspecting")
|
|
|
|
end
|
2012-02-29 18:44:13 +00:00
|
|
|
end
|
|
|
|
|
2012-02-28 02:48:42 +00:00
|
|
|
def current_panes
|
2012-03-30 18:03:53 +00:00
|
|
|
_run('list-panes').split("\n").map do |line|
|
2012-02-28 02:48:42 +00:00
|
|
|
line.split(':').first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_pane_id
|
2012-03-30 18:03:53 +00:00
|
|
|
_run('list-panes').split("\n").map do |line|
|
2012-02-28 02:48:42 +00:00
|
|
|
return line.split[-2] if line =~ /\(active\)/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target(args={})
|
|
|
|
"#{args.fetch(:session, @session)}:#{args.fetch(:window, @window)}.#{args.fetch(:pane, @pane)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def runner_pane
|
|
|
|
if @runner_pane.nil?
|
2012-04-10 01:03:54 +00:00
|
|
|
type = Vim.evaluate('exists("g:_VimTmuxInspecting")') != 0
|
|
|
|
_run("split-window -p #{height} #{orientation}")
|
2012-02-28 02:48:42 +00:00
|
|
|
@runner_pane = active_pane_id
|
|
|
|
Vim.command("let g:_VimTmuxRunnerPane = '#{@runner_pane}'")
|
|
|
|
end
|
|
|
|
|
2012-03-30 18:03:53 +00:00
|
|
|
_run('list-panes').split("\n").map do |line|
|
2012-02-28 02:48:42 +00:00
|
|
|
return line.split(':').first if line =~ /#{@runner_pane}/
|
|
|
|
end
|
2012-02-29 04:49:05 +00:00
|
|
|
|
|
|
|
@runner_pane = nil
|
|
|
|
clear_vim_cached_runner_pane
|
|
|
|
runner_pane
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
|
2012-03-01 01:19:53 +00:00
|
|
|
def interrupt_runner
|
2012-03-30 18:03:53 +00:00
|
|
|
stop_inspecting_runner
|
|
|
|
_run("send-keys -t #{target(:pane => runner_pane)} ^c")
|
2012-03-01 01:19:53 +00:00
|
|
|
end
|
|
|
|
|
2012-02-28 02:48:42 +00:00
|
|
|
def run_shell_command(command)
|
2012-03-30 18:03:53 +00:00
|
|
|
stop_inspecting_runner
|
|
|
|
_send_command(command, target(:pane => runner_pane))
|
|
|
|
_move_up_pane
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def close_other_panes
|
2012-03-30 18:03:53 +00:00
|
|
|
stop_inspecting_runner
|
|
|
|
if _run("list-panes").split("\n").length > 1
|
|
|
|
_run("kill-pane -a")
|
|
|
|
end
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
|
2012-03-30 18:03:53 +00:00
|
|
|
def _move_up_pane
|
|
|
|
_run("select-pane -t #{target}")
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
|
2012-03-30 18:03:53 +00:00
|
|
|
def _send_command(command, target)
|
|
|
|
_run("send-keys -t #{target} '#{command.gsub("'", "\'")}'")
|
|
|
|
_run("send-keys -t #{target} Enter")
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
|
2012-03-30 18:03:53 +00:00
|
|
|
def _run(command)
|
2012-02-28 02:48:42 +00:00
|
|
|
`tmux #{command}`
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class CurrentTmuxSession < TmuxSession
|
|
|
|
def initialize
|
2012-04-10 01:07:14 +00:00
|
|
|
session = self.get_session
|
2012-02-28 02:48:42 +00:00
|
|
|
window = self.get_property(:active, :window)
|
|
|
|
pane = self.get_property(:active, :pane)
|
|
|
|
|
|
|
|
super(session, window, pane)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_property(match, type)
|
2012-03-30 18:03:53 +00:00
|
|
|
_run("list-#{type.to_s}").split("\n").each do |line|
|
2012-02-28 02:48:42 +00:00
|
|
|
return line.split(':').first if line =~ /\(#{match.to_s}\)/
|
|
|
|
end
|
|
|
|
end
|
2012-04-10 01:07:14 +00:00
|
|
|
|
|
|
|
def get_session
|
|
|
|
_run("list-panes -F '\#\{session_name\}'").split("\n").first.strip
|
|
|
|
end
|
2012-02-28 02:48:42 +00:00
|
|
|
end
|
|
|
|
EOF
|