You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
vim-vimux/plugin/vimux.vim

223 lines
6.1 KiB
VimL

if exists("g:loaded_vimux") || &cp
finish
endif
let g:loaded_vimux = 1
function! _VimuxOption(option, default)
if exists(a:option)
return eval(a:option)
else
return a:default
endif
endfunction
function! _VimuxTmuxCmd()
return _VimuxOption("g:VimuxTmuxCommand", "tmux")
endfunction
if !executable(_VimuxTmuxCmd())
echohl ErrorMsg | echomsg "Failed to find executable "._VimuxTmuxCmd() | echohl None
finish
endif
command -nargs=* VimuxRunCommand :call VimuxRunCommand(<args>)
command VimuxRunLastCommand :call VimuxRunLastCommand()
command VimuxOpenRunner :call VimuxOpenRunner()
command VimuxCloseRunner :call VimuxCloseRunner()
command VimuxZoomRunner :call VimuxZoomRunner()
command VimuxInspectRunner :call VimuxInspectRunner()
command VimuxScrollUpInspect :call VimuxScrollUpInspect()
command VimuxScrollDownInspect :call VimuxScrollDownInspect()
command VimuxInterruptRunner :call VimuxInterruptRunner()
command -nargs=? VimuxPromptCommand :call VimuxPromptCommand(<args>)
command VimuxClearTerminalScreen :call VimuxClearTerminalScreen()
command VimuxClearRunnerHistory :call VimuxClearRunnerHistory()
command VimuxTogglePane :call VimuxTogglePane()
function! VimuxRunCommandInDir(command, useFile)
let l:file = ""
if a:useFile ==# 1
let l:file = shellescape(expand('%:t'), 1)
endif
call VimuxRunCommand("(cd ".shellescape(expand('%:p:h'), 1)." && ".a:command." ".l:file.")")
endfunction
function! VimuxRunLastCommand()
if exists("g:VimuxRunnerIndex")
call VimuxRunCommand(g:VimuxLastCommand)
else
echo "No last vimux command."
endif
endfunction
function! VimuxRunCommand(command, ...)
if !exists("g:VimuxRunnerIndex") || _VimuxHasRunner(g:VimuxRunnerIndex) == -1
call VimuxOpenRunner()
endif
let l:autoreturn = 1
if exists("a:1")
let l:autoreturn = a:1
endif
let resetSequence = _VimuxOption("g:VimuxResetSequence", "q C-u")
let g:VimuxLastCommand = a:command
call VimuxSendKeys(resetSequence)
call VimuxSendText(a:command)
if l:autoreturn == 1
call VimuxSendKeys("Enter")
endif
endfunction
function! VimuxSendText(text)
call VimuxSendKeys(shellescape(substitute(a:text, "\n$", " ", "")))
endfunction
function! VimuxSendKeys(keys)
if exists("g:VimuxRunnerIndex")
call _VimuxTmux("send-keys -t ".g:VimuxRunnerIndex." ".a:keys)
else
echo "No vimux runner pane/window. Create one with VimuxOpenRunner"
endif
endfunction
function! VimuxOpenRunner()
let nearestIndex = _VimuxNearestIndex()
if _VimuxOption("g:VimuxUseNearest", 1) == 1 && nearestIndex != -1
let g:VimuxRunnerIndex = nearestIndex
else
let extraArguments = _VimuxOption("g:VimuxOpenExtraArgs", "")
if _VimuxRunnerType() == "pane"
let height = _VimuxOption("g:VimuxHeight", 20)
let orientation = _VimuxOption("g:VimuxOrientation", "v")
call _VimuxTmux("split-window -p ".height." -".orientation." ".extraArguments)
elseif _VimuxRunnerType() == "window"
call _VimuxTmux("new-window ".extraArguments)
endif
let g:VimuxRunnerIndex = _VimuxTmuxIndex()
call _VimuxTmux("last-"._VimuxRunnerType())
endif
endfunction
function! VimuxCloseRunner()
if exists("g:VimuxRunnerIndex")
call _VimuxTmux("kill-"._VimuxRunnerType()." -t ".g:VimuxRunnerIndex)
unlet g:VimuxRunnerIndex
endif
endfunction
function! VimuxTogglePane()
if exists("g:VimuxRunnerIndex")
if _VimuxRunnerType() == "window"
call _VimuxTmux("join-pane -d -s ".g:VimuxRunnerIndex." -p "._VimuxOption("g:VimuxHeight", 20))
let g:VimuxRunnerType = "pane"
elseif _VimuxRunnerType() == "pane"
let g:VimuxRunnerIndex=substitute(_VimuxTmux("break-pane -d -t ".g:VimuxRunnerIndex." -P -F '#{window_id}'"), "\n", "", "")
let g:VimuxRunnerType = "window"
endif
endif
endfunction
function! VimuxZoomRunner()
if exists("g:VimuxRunnerIndex")
if _VimuxRunnerType() == "pane"
call _VimuxTmux("resize-pane -Z -t ".g:VimuxRunnerIndex)
elseif _VimuxRunnerType() == "window"
call _VimuxTmux("select-window -t ".g:VimuxRunnerIndex)
endif
endif
endfunction
function! VimuxInspectRunner()
call _VimuxTmux("select-"._VimuxRunnerType()." -t ".g:VimuxRunnerIndex)
call _VimuxTmux("copy-mode")
endfunction
function! VimuxScrollUpInspect()
call VimuxInspectRunner()
call _VimuxTmux("last-"._VimuxRunnerType())
call VimuxSendKeys("C-u")
endfunction
function! VimuxScrollDownInspect()
call VimuxInspectRunner()
call _VimuxTmux("last-"._VimuxRunnerType())
call VimuxSendKeys("C-d")
endfunction
function! VimuxInterruptRunner()
call VimuxSendKeys("^c")
endfunction
function! VimuxClearTerminalScreen()
if exists("g:VimuxRunnerIndex")
call VimuxSendKeys("C-l")
endif
endfunction
function! VimuxClearRunnerHistory()
if exists("g:VimuxRunnerIndex")
call _VimuxTmux("clear-history -t ".g:VimuxRunnerIndex)
endif
endfunction
function! VimuxPromptCommand(...)
let command = a:0 == 1 ? a:1 : ""
let l:command = input(_VimuxOption("g:VimuxPromptString", "Command? "), command, 'shellcmd')
call VimuxRunCommand(l:command)
endfunction
function! _VimuxTmux(arguments)
return system(_VimuxTmuxCmd()." ".a:arguments)
endfunction
function! _VimuxTmuxSession()
return _VimuxTmuxProperty("#S")
endfunction
function! _VimuxTmuxIndex()
if _VimuxRunnerType() == "pane"
return _VimuxTmuxPaneId()
else
return _VimuxTmuxWindowId()
end
endfunction
function! _VimuxTmuxPaneId()
return _VimuxTmuxProperty("#{pane_id}")
endfunction
function! _VimuxTmuxWindowId()
return _VimuxTmuxProperty("#{window_id}")
endfunction
function! _VimuxNearestIndex()
let t = _VimuxRunnerType()
let views = split(_VimuxTmux("list-".t."s -F '#{".t."_active}:#{".t."_id}'"), "\n")
for view in views
if match(view, "1:") == -1
return split(view, ":")[1]
endif
endfor
return -1
endfunction
function! _VimuxRunnerType()
return _VimuxOption("g:VimuxRunnerType", "pane")
endfunction
function! _VimuxTmuxProperty(property)
return substitute(_VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
endfunction
function! _VimuxHasRunner(index)
let t = _VimuxRunnerType()
return match(_VimuxTmux("list-".t."s -F '#{".t."_id}'"), a:index)
endfunction