Rewrite vimux using VimL.

This commit also drops the deprecated functions and removes some rarely used
functions such as VimuxClosePanes.
1.0.0rc1
benmills 11 years ago
parent 35c54c0801
commit e729607de6

@ -3,16 +3,8 @@ if exists("g:loaded_vimux") || &cp
endif endif
let g:loaded_vimux = 1 let g:loaded_vimux = 1
if !has("ruby")
finish
end
" New style commands with 'normalized' names
command VimuxRunLastCommand :call VimuxRunLastCommand() command VimuxRunLastCommand :call VimuxRunLastCommand()
command VimuxCloseRunner :call VimuxCloseRunner() command VimuxCloseRunner :call VimuxCloseRunner()
command VimuxClosePanes :call VimuxClosePanes()
command VimuxCloseWindows :call VimuxCloseWindows()
command VimuxInspectRunner :call VimuxInspectRunner() command VimuxInspectRunner :call VimuxInspectRunner()
command VimuxScrollUpInspect :call VimuxScrollUpInspect() command VimuxScrollUpInspect :call VimuxScrollUpInspect()
command VimuxScrollDownInspect :call VimuxScrollDownInspect() command VimuxScrollDownInspect :call VimuxScrollDownInspect()
@ -20,341 +12,136 @@ command VimuxInterruptRunner :call VimuxInterruptRunner()
command VimuxPromptCommand :call VimuxPromptCommand() command VimuxPromptCommand :call VimuxPromptCommand()
command VimuxClearRunnerHistory :call VimuxClearRunnerHistory() command VimuxClearRunnerHistory :call VimuxClearRunnerHistory()
" DEPRECATED function! VimuxRunLastCommand()
command RunLastVimTmuxCommand :call VimuxRunLastCommand() if exists("g:VimuxRunnerPaneIndex")
command CloseVimTmuxRunner :call VimuxCloseRunner() call VimuxRunCommand(g:VimuxLastCommand)
command CloseVimTmuxPanes :call VimuxClosePanes() else
command CloseVimTmuxWindows :call VimuxCloseWindows() echo "No last vimux command."
command InspectVimTmuxRunner :call VimuxInspectRunner() endif
command InterruptVimTmuxRunner :call VimuxInterruptRunner() endfunction
command PromptVimTmuxCommand :call VimuxPromptCommand()
function! VimuxRunCommand(command, ...)
if !exists("g:VimuxRunnerPaneIndex") || _VimuxHasPane(g:VimuxRunnerPaneIndex) == -1
call VimuxOpenPane()
endif
" new style functions
function VimuxRunCommand(command, ...)
let l:autoreturn = 1 let l:autoreturn = 1
if exists("a:1") if exists("a:1")
let l:autoreturn = a:1 let l:autoreturn = a:1
endif endif
let s:_VimTmuxCmd = substitute(a:command, '`', '\\`', 'g') let resetSequence = _VimuxOption("g:VimuxResetSequence", "q C-u")
let s:_VimTmuxCmdAutoreturn = l:autoreturn let g:VimuxLastCommand = a:command
call VimuxSendKeys(resetSequence)
call VimuxSendText(a:command)
if l:autoreturn == 1 if l:autoreturn == 1
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("s:_VimTmuxCmd")) call VimuxSendKeys("Enter")
else
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("s:_VimTmuxCmd"), false)
endif endif
endfunction endfunction
" deprecated! function! VimuxSendText(text)
function RunVimTmuxCommand(command, ...) call VimuxSendKeys(shellescape(a:text))
" TODO replace me with the direct function call! endfunction
let l:autoreturn = 1
if exists("a:1")
let l:autoreturn = a:1
endif
let s:_VimTmuxCmd = substitute(a:command, '`', '\\`', 'g')
let s:_VimTmuxCmdAutoreturn = l:autoreturn
if l:autoreturn == 1 function! VimuxSendKeys(keys)
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("s:_VimTmuxCmd")) if exists("g:VimuxRunnerPaneIndex")
call system("tmux send-keys -t ".g:VimuxRunnerPaneIndex." ".a:keys)
else else
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("s:_VimTmuxCmd"), false) echo "No vimux runner pane. Create one with VimuxOpenPane"
endif endif
endfunction endfunction
function! VimuxOpenPane()
let height = _VimuxOption("g:VimuxHeight", 20)
let orientation = _VimuxOption("g:VimuxOrientation", "v")
let nearestIndex = _VimuxNearestPaneIndex()
function VimuxRunLastCommand() if _VimuxOption("g:VimuxUseNearestPane", 1) == 1 && nearestIndex != -1
if exists("s:_VimTmuxCmd") let g:VimuxRunnerPaneIndex = nearestIndex
if s:_VimTmuxCmdAutoreturn == 1
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("s:_VimTmuxCmd"))
else
ruby CurrentTmuxSession.new.run_shell_command(Vim.evaluate("s:_VimTmuxCmd"), false)
endif
else else
echo "No last command" call system("tmux split-window -p ".height." -".orientation)
let g:VimuxRunnerPaneIndex = _VimuxTmuxPaneIndex()
call system("tmux last-pane")
endif endif
endfunction endfunction
" deprecated! function! VimuxCloseRunner()
function RunLastVimTmuxCommand() if exists("g:VimuxRunnerPaneIndex")
call VimuxRunLastCommand() call system("tmux kill-pane -t ".g:VimuxRunnerPaneIndex)
endfunction unlet g:VimuxRunnerPaneIndex
endif
function VimuxClearWindow()
if exists("s:_VimTmuxRunnerPane")
unlet s:_VimTmuxRunnerPane
end
endfunction endfunction
" deprecated! function! VimuxInspectRunner()
function ClearVimTmuxWindow() call system("tmux select-pane -t ".g:VimuxRunnerPaneIndex)
call VimuxClearWindow() call system("tmux copy-mode")
endfunction endfunction
function! VimuxScrollUpInspect()
" deprecated! call VimuxInspectRunner()
function CloseVimTmuxWindows() call system("tmux last-pane")
call VimuxCloseWindows() call VimuxSendKeys("C-u")
endfunction endfunction
function! VimuxScrollDownInspect()
function VimuxCloseRunner() call VimuxInspectRunner()
ruby CurrentTmuxSession.new.close_runner_pane call system("tmux last-pane")
call VimuxClearWindow() call VimuxSendKeys("C-d")
endfunction endfunction
" deprecated! function! VimuxInterruptRunner()
function CloseVimTmuxRunner() call VimuxSendKeys("^c")
call VimuxCloseRunner()
endfunction endfunction
function! VimuxClearRunnerHistory()
function VimuxClosePanes() if exists("g:VimuxRunnerPaneIndex")
ruby CurrentTmuxSession.new.close_other_panes call system("tmux clear-history -t ".g:VimuxRunnerPaneIndex)
call VimuxClearWindow() endif
endfunction endfunction
" deprecated! function! VimuxPromptCommand()
function CloseVimTmuxPanes() let l:command = input(_VimuxOption("g:VimuxPromptString", "Command? "))
call VimuxClosePanes() call VimuxRunCommand(l:command)
endfunction endfunction
function! _VimuxTmuxSession()
function VimuxInterruptRunner() return _VimuxTmuxProperty("S")
ruby CurrentTmuxSession.new.interrupt_runner
endfunction endfunction
" deprecated! function! _VimuxTmuxPaneIndex()
function InterruptVimTmuxRunner() return _VimuxTmuxProperty("P")
call VimuxInterruptRunner()
endfunction endfunction
function VimuxScrollDownInspect() function! _VimuxTmuxWindowIndex()
ruby CurrentTmuxSession.new.inspect_scroll_down return _VimuxTmuxProperty("I")
endfunction endfunction
function VimuxScrollUpInspect() function! _VimuxNearestPaneIndex()
ruby CurrentTmuxSession.new.inspect_scroll_up let panes = split(system("tmux list-panes"), "\n")
endfunction
function VimuxInspectRunner() for i in panes
ruby CurrentTmuxSession.new.inspect_runner if match(panes[i], "(active)") == -1
endfunction return split(panes[i], ":")[0]
endif
endfor
" deprecated! return -1
function InspectVimTmuxRunner()
call VimuxInspectRunner()
endfunction endfunction
function! _VimuxOption(option, default)
function VimuxPromptCommand() if exists(a:option)
let l:command = input("Command? ") return eval(a:option)
if exists("g:VimuxPromptString") else
let l:command = input(g:VimuxPromptString) return a:default
endif endif
call VimuxRunCommand(l:command)
endfunction endfunction
" deprecated! function! _VimuxTmuxProperty(property)
function PromptVimTmuxCommand() return substitute(system("tmux display -p '#".a:property."'"), '\n$', '', '')
call VimuxPromptCommand()
endfunction endfunction
function! _VimuxHasPane(index)
function VimuxClearRunnerHistory() return match(system("tmux list-panes"), a:index.":")
ruby CurrentTmuxSession.new.clear_runner_history
endfunction endfunction
ruby << EOF
class TmuxSession
def initialize(session, window, pane)
@session = session
@window = window
@pane = pane
@runner_pane = vim_cached_runner_pane
end
def vim_cached_runner_pane
if Vim.evaluate('exists("s:_VimTmuxRunnerPane")') != 0
Vim.evaluate('s:_VimTmuxRunnerPane')
else
nil
end
end
def vim_cached_runner_pane=(runner_pane)
Vim.command("let s:_VimTmuxRunnerPane = '#{runner_pane}'")
end
def clear_vim_cached_runner_pane
Vim.command("unlet s:_VimTmuxRunnerPane")
end
def clear_runner_history
_run("clear-history -t #{target(:pane => runner_pane)}")
end
def height
if Vim.evaluate('exists("g:VimuxHeight")') != 0
Vim.evaluate('g:VimuxHeight')
else
20
end
end
def orientation
if Vim.evaluate('exists("g:VimuxOrientation")') != 0 && ["h", "v"].include?(Vim.evaluate('g:VimuxOrientation'))
"-#{Vim.evaluate('g:VimuxOrientation')}"
else
"-v"
end
end
def reset_sequence
if Vim.evaluate('exists("g:VimuxResetSequence")') != 0
"#{Vim.evaluate('g:VimuxResetSequence')}"
else
"q C-u"
end
end
def inspect_runner
_run("select-pane -t #{target(:pane => runner_pane)}")
_run("copy-mode")
end
def inspect_send_command(cmd)
t = target(:pane => runner_pane)
_run("select-pane -t #{t}")
_run("copy-mode")
_send_command(cmd, t, false)
_move_up_pane
end
def inspect_scroll_up
inspect_send_command("C-u")
end
def inspect_scroll_down
inspect_send_command("C-d")
end
def current_panes
_run('list-panes').split("\n").map do |line|
line.split(':').first
end
end
def active_pane_id
_run('list-panes').split("\n").map do |line|
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?
use_nearest_pane = Vim.evaluate('exists("g:VimuxUseNearestPane")') != 0
if use_nearest_pane && nearest_inactive_pane_id
_run("select-pane -t #{target(:pane => nearest_inactive_pane_id)}")
else
_run("split-window -p #{height} #{orientation}")
end
@runner_pane = active_pane_id
_send_command("cd #{`pwd`}", target(:pane => runner_pane))
Vim.command("let s:_VimTmuxRunnerPane = '#{@runner_pane}'")
end
_run('list-panes').split("\n").map do |line|
return line.split(':').first if line =~ /#{@runner_pane}/
end
@runner_pane = nil
runner_pane
end
def interrupt_runner
_run("send-keys -t #{target(:pane => runner_pane)} ^c")
end
def run_shell_command(command, auto_return = true)
reset_shell
_send_command(command, target(:pane => runner_pane), auto_return)
_move_up_pane
end
def close_runner_pane
_run("kill-pane -t #{target(:pane => runner_pane)}") unless @runner_pane.nil?
end
def close_other_panes
if _run("list-panes").split("\n").length > 1
_run("kill-pane -a")
end
end
def reset_shell
_run("send-keys -t #{target(:pane => runner_pane)} #{reset_sequence}")
end
def nearest_inactive_pane_id
panes = _run("list-pane").split("\n")
pane = panes.find { |p| p !~ /active/ }
pane ? pane.split(':').first : nil
end
def _move_up_pane
_run("select-pane -t #{target}")
end
def _send_command(command, target, auto_return = true)
_run("send-keys -t #{target} \"#{_escape_command(command)}\"")
_run("send-keys -t #{target} Enter") if auto_return
end
def _escape_command(command)
command.gsub('"', '\"').gsub('$', '\$')
end
def _run(command)
`tmux #{command}`
end
end
class CurrentTmuxSession < TmuxSession
def initialize
if tmux?
session = self.get_session
window = self.get_property(:active, :window)
pane = self.get_property(:active, :pane)
super(session, window, pane)
else
raise "You are not in a tmux session"
end
end
def get_property(match, type)
_run("list-#{type.to_s}").split("\n").each do |line|
return line.split(':').first if line =~ /\(#{match.to_s}\)/
end
end
def get_session
_run("display -p '#S'").strip
end
def tmux?
`echo $TMUX` =~ /.+/ ? true : false
end
end
EOF

Loading…
Cancel
Save