diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 67072a9..4f571da 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -64,9 +64,7 @@ function! VimuxRunLastCommand() abort endfunction function! VimuxRunCommand(command, ...) abort - if !exists('g:VimuxRunnerIndex') || s:hasRunner(g:VimuxRunnerIndex) ==# -1 - call VimuxOpenRunner() - endif + call VimuxOpenRunner() let l:autoreturn = 1 if exists('a:1') let l:autoreturn = a:1 @@ -75,26 +73,33 @@ function! VimuxRunCommand(command, ...) abort let g:VimuxLastCommand = a:command call s:exitCopyMode() - call VimuxSendKeys(l:resetSequence) - call VimuxSendText(a:command) + call s:sendKeys(l:resetSequence) + call s:sendText(a:command) if l:autoreturn ==# 1 - call VimuxSendKeys('Enter') + call s:sendKeys('Enter') endif endfunction function! VimuxSendText(text) abort - call VimuxSendKeys(shellescape(substitute(a:text, '\n$', ' ', ''))) + if s:hasRunner() + call s:sendText(a:text) + else + call s:echoNoRunner() + endif endfunction function! VimuxSendKeys(keys) abort - if exists('g:VimuxRunnerIndex') - call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys) + if s:hasRunner() + call s:sendKeys(a:keys) else - echo 'No vimux runner pane/window. Create one with VimuxOpenRunner' + call s:echoNoRunner() endif endfunction function! VimuxOpenRunner() abort + if s:hasRunner() + return + endif let existingId = s:existingRunnerId() if existingId !=# '' let g:VimuxRunnerIndex = existingId @@ -112,14 +117,14 @@ function! VimuxOpenRunner() abort endfunction function! VimuxCloseRunner() abort - if exists('g:VimuxRunnerIndex') + if s:hasRunner() call VimuxTmux('kill-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex) - unlet g:VimuxRunnerIndex endif + unlet! g:VimuxRunnerIndex endfunction function! VimuxTogglePane() abort - if exists('g:VimuxRunnerIndex') + if s:hasRunner() if VimuxOption('VimuxRunnerType') ==# 'window' call VimuxTmux('join-pane -s '.g:VimuxRunnerIndex.' '.s:vimuxPaneOptions()) let g:VimuxRunnerType = 'pane' @@ -127,57 +132,76 @@ function! VimuxTogglePane() abort call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) elseif VimuxOption('VimuxRunnerType') ==# 'pane' let g:VimuxRunnerIndex=substitute( - \ VimuxTmux('break-pane -d -s '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), - \ '\n', - \ '', - \ '' - \) + \ VimuxTmux('break-pane -d -s '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), + \ '\n', + \ '', + \ '' + \) let g:VimuxRunnerType = 'window' endif + else + call s:echoNoRunner() endif endfunction function! VimuxZoomRunner() abort - if exists('g:VimuxRunnerIndex') + if s:hasRunner() if VimuxOption('VimuxRunnerType') ==# 'pane' call VimuxTmux('resize-pane -Z -t '.g:VimuxRunnerIndex) elseif VimuxOption('VimuxRunnerType') ==# 'window' call VimuxTmux('select-window -t '.g:VimuxRunnerIndex) endif + else + call s:echoNoRunner() endif endfunction function! VimuxInspectRunner() abort - call VimuxTmux('select-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex) - call VimuxTmux('copy-mode') + if s:hasRunner() + call VimuxTmux('select-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex) + call VimuxTmux('copy-mode') + return v:true + endif + call s:echoNoRunner() + return v:false endfunction function! VimuxScrollUpInspect() abort - call VimuxInspectRunner() - call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) - call VimuxSendKeys('C-u') + if VimuxInspectRunner() + call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) + call s:sendKeys('C-u') + endif endfunction function! VimuxScrollDownInspect() abort - call VimuxInspectRunner() - call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) - call VimuxSendKeys('C-d') + if VimuxInspectRunner() + call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) + call s:sendKeys('C-d') + endif endfunction function! VimuxInterruptRunner() abort - call VimuxSendKeys('^c') + if s:hasRunner() + call s:sendKeys('^c') + else + call s:echoNoRunner() + endif endfunction function! VimuxClearTerminalScreen() abort - if exists('g:VimuxRunnerIndex') && s:hasRunner(g:VimuxRunnerIndex) !=# -1 + if s:hasRunner() call s:exitCopyMode() - call VimuxSendKeys('C-l') + call s:sendKeys('C-l') + else + call s:echoNoRunner() endif endfunction function! VimuxClearRunnerHistory() abort - if exists('g:VimuxRunnerIndex') && s:hasRunner(g:VimuxRunnerIndex) !=# -1 + if s:hasRunner() call VimuxTmux('clear-history -t '.g:VimuxRunnerIndex) + else + call s:echoNoRunner() endif endfunction @@ -216,7 +240,7 @@ function! s:exitCopyMode() abort catch let l:versionString = s:tmuxProperty('#{version}') if str2float(l:versionString) < 3.2 - call VimuxSendKeys('q') + call s:sendKeys('q') endif endtry endfunction @@ -242,9 +266,9 @@ function! s:tmuxWindowId() abort endfunction function! s:vimuxPaneOptions() abort - let height = VimuxOption('VimuxHeight') - let orientation = VimuxOption('VimuxOrientation') - return '-l '.height.' -'.orientation + let height = VimuxOption('VimuxHeight') + let orientation = VimuxOption('VimuxOrientation') + return '-l '.height.' -'.orientation endfunction "" @@ -264,13 +288,13 @@ function! s:existingRunnerId() abort let currentId = s:tmuxIndex() let message = VimuxTmux('select-'.runnerType.' -t '.query.'') if message ==# '' - " A match was found. Make sure it isn't the current vim pane/window - " though! + " A match was found. Make sure it isn't the current vim pane/window + " though! let runnerId = s:tmuxIndex() if runnerId !=# currentId - " Success! - call VimuxTmux('last-'.runnerType) - return runnerId + " Success! + call VimuxTmux('last-'.runnerType) + return runnerId endif endif return '' @@ -282,11 +306,11 @@ function! s:nearestRunnerId() abort let runnerType = VimuxOption('VimuxRunnerType') let filter = s:getTargetFilter() let views = split( - \ VimuxTmux( - \ 'list-'.runnerType.'s' - \ ." -F '#{".runnerType.'_active}:#{'.runnerType."_id}'" - \ .filter), - \ '\n') + \ VimuxTmux( + \ 'list-'.runnerType.'s' + \ ." -F '#{".runnerType.'_active}:#{'.runnerType."_id}'" + \ .filter), + \ '\n') " '1:' is the current active pane (the one with vim). " Find the first non-active pane. for view in views @@ -327,9 +351,14 @@ function! s:tmuxProperty(property) abort return substitute(VimuxTmux("display -p '".a:property."'"), '\n$', '', '') endfunction -function! s:hasRunner(index) abort - let runnerType = VimuxOption('VimuxRunnerType') - return match(VimuxTmux('list-'.runnerType."s -F '#{".runnerType."_id}'"), a:index) +function! s:hasRunner() abort + if get(g:, 'VimuxRunnerIndex', '') == '' + return v:false + endif + let l:runnerType = VimuxOption('VimuxRunnerType') + let l:command = 'list-'.runnerType."s -F '#{".runnerType."_id}'" + let l:found = match(VimuxTmux(l:command), g:VimuxRunnerIndex) + return l:found != -1 endfunction function! s:autoclose() abort @@ -337,3 +366,15 @@ function! s:autoclose() abort call VimuxCloseRunner() endif endfunction + +function! s:sendKeys(keys) abort + call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys) +endfunction + +function! s:sendText(text) abort + call s:sendKeys(shellescape(substitute(a:text, '\n$', ' ', ''))) +endfunction + +function! s:echoNoRunner() abort + echo 'No vimux runner pane/window. Create one with VimuxOpenRunner' +endfunction