From 6e0bbd3d213e866f26ae7bf01e713b58c6ad3af7 Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sat, 7 Dec 2024 12:46:45 -0800 Subject: [PATCH 01/10] Check for runner existence, not just variable --- plugin/vimux.vim | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 67072a9..add2bca 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -64,7 +64,7 @@ function! VimuxRunLastCommand() abort endfunction function! VimuxRunCommand(command, ...) abort - if !exists('g:VimuxRunnerIndex') || s:hasRunner(g:VimuxRunnerIndex) ==# -1 + if ! s:hasRunner() call VimuxOpenRunner() endif let l:autoreturn = 1 @@ -87,14 +87,13 @@ function! VimuxSendText(text) abort endfunction function! VimuxSendKeys(keys) abort - 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 + call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys) endfunction function! VimuxOpenRunner() abort + if s:hasRunner() + return + endif let existingId = s:existingRunnerId() if existingId !=# '' let g:VimuxRunnerIndex = existingId @@ -112,14 +111,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' @@ -138,7 +137,7 @@ function! VimuxTogglePane() abort 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' @@ -169,14 +168,14 @@ function! VimuxInterruptRunner() abort endfunction function! VimuxClearTerminalScreen() abort - if exists('g:VimuxRunnerIndex') && s:hasRunner(g:VimuxRunnerIndex) !=# -1 + if s:hasRunner() call s:exitCopyMode() call VimuxSendKeys('C-l') endif endfunction function! VimuxClearRunnerHistory() abort - if exists('g:VimuxRunnerIndex') && s:hasRunner(g:VimuxRunnerIndex) !=# -1 + if s:hasRunner() call VimuxTmux('clear-history -t '.g:VimuxRunnerIndex) endif endfunction @@ -327,9 +326,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 ! exists('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 From a2591cf3701db33825b45f9008f3326d33c69c17 Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 11:35:19 -0700 Subject: [PATCH 02/10] Remove redundant hasRunner() check --- plugin/vimux.vim | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index add2bca..7f2e423 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -64,9 +64,7 @@ function! VimuxRunLastCommand() abort endfunction function! VimuxRunCommand(command, ...) abort - if ! s:hasRunner() - call VimuxOpenRunner() - endif + call VimuxOpenRunner() let l:autoreturn = 1 if exists('a:1') let l:autoreturn = a:1 From 512db1aced0b73e6e4ee952aa7cc062dcbb4783a Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 11:35:45 -0700 Subject: [PATCH 03/10] Fix indentation --- plugin/vimux.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 7f2e423..1779f6c 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -116,7 +116,7 @@ function! VimuxCloseRunner() abort endfunction function! VimuxTogglePane() abort - if s:hasRunner() + if s:hasRunner() if VimuxOption('VimuxRunnerType') ==# 'window' call VimuxTmux('join-pane -s '.g:VimuxRunnerIndex.' '.s:vimuxPaneOptions()) let g:VimuxRunnerType = 'pane' From eee9fee0d84ca89f370817f5a5c6315a1ef44e5a Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 12:08:22 -0700 Subject: [PATCH 04/10] Extract internal versions of sendKeys and sendText --- plugin/vimux.vim | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 1779f6c..6c045b1 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -73,19 +73,27 @@ 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(text) + else + echo 'No vimux runner pane/window. Create one with VimuxOpenRunner' + endif endfunction function! VimuxSendKeys(keys) abort - call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys) + if s:hasRunner() + call s:sendKeys(keys) + else + echo 'No vimux runner pane/window. Create one with VimuxOpenRunner' + endif endfunction function! VimuxOpenRunner() abort @@ -152,23 +160,23 @@ endfunction function! VimuxScrollUpInspect() abort call VimuxInspectRunner() call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) - call VimuxSendKeys('C-u') + call s:sendKeys('C-u') endfunction function! VimuxScrollDownInspect() abort call VimuxInspectRunner() call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) - call VimuxSendKeys('C-d') + call s:sendKeys('C-d') endfunction function! VimuxInterruptRunner() abort - call VimuxSendKeys('^c') + call s:sendKeys('^c') endfunction function! VimuxClearTerminalScreen() abort if s:hasRunner() call s:exitCopyMode() - call VimuxSendKeys('C-l') + call s:sendKeys('C-l') endif endfunction @@ -213,7 +221,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 @@ -339,3 +347,11 @@ 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 From 5bcfb6a68b4ab2ff128bb6f056ea5c5a7dfbfe0e Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 12:12:23 -0700 Subject: [PATCH 05/10] Always check hasRunner for all external functions --- plugin/vimux.vim | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 6c045b1..e02006b 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -153,24 +153,32 @@ function! VimuxZoomRunner() abort 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 + return v:false endfunction function! VimuxScrollUpInspect() abort - call VimuxInspectRunner() - call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) - call s:sendKeys('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 s:sendKeys('C-d') + if VimuxInspectRunner() + call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) + call s:sendKeys('C-d') + endif endfunction function! VimuxInterruptRunner() abort - call s:sendKeys('^c') + if s:hasRunner() + call s:sendKeys('^c') + endif endfunction function! VimuxClearTerminalScreen() abort From 7058d109f8b0e5d7081e0ab6341d4d8886beeb78 Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 12:28:14 -0700 Subject: [PATCH 06/10] Echo a helpful message if missing required runner --- plugin/vimux.vim | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index e02006b..7b4033c 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -84,7 +84,7 @@ function! VimuxSendText(text) abort if s:hasRunner() call s:sendText(text) else - echo 'No vimux runner pane/window. Create one with VimuxOpenRunner' + call s:echoNoRunner() endif endfunction @@ -92,7 +92,7 @@ function! VimuxSendKeys(keys) abort if s:hasRunner() call s:sendKeys(keys) else - echo 'No vimux runner pane/window. Create one with VimuxOpenRunner' + call s:echoNoRunner() endif endfunction @@ -139,6 +139,8 @@ function! VimuxTogglePane() abort \) let g:VimuxRunnerType = 'window' endif + else + call s:echoNoRunner() endif endfunction @@ -149,6 +151,8 @@ function! VimuxZoomRunner() abort elseif VimuxOption('VimuxRunnerType') ==# 'window' call VimuxTmux('select-window -t '.g:VimuxRunnerIndex) endif + else + call s:echoNoRunner() endif endfunction @@ -158,6 +162,7 @@ function! VimuxInspectRunner() abort call VimuxTmux('copy-mode') return v:true endif + call s:echoNoRunner() return v:false endfunction @@ -178,6 +183,8 @@ endfunction function! VimuxInterruptRunner() abort if s:hasRunner() call s:sendKeys('^c') + else + call s:echoNoRunner() endif endfunction @@ -185,12 +192,16 @@ function! VimuxClearTerminalScreen() abort if s:hasRunner() call s:exitCopyMode() call s:sendKeys('C-l') + else + call s:echoNoRunner() endif endfunction function! VimuxClearRunnerHistory() abort if s:hasRunner() call VimuxTmux('clear-history -t '.g:VimuxRunnerIndex) + else + call s:echoNoRunner() endif endfunction @@ -363,3 +374,7 @@ 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 From 2d5b13dd6bb60f761d4ab44684f29064d907f779 Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 12:35:59 -0700 Subject: [PATCH 07/10] Avoid error message if VimuxRunnerIndex undefined --- plugin/vimux.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 7b4033c..34f18cf 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -120,7 +120,7 @@ function! VimuxCloseRunner() abort if s:hasRunner() call VimuxTmux('kill-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex) endif - unlet g:VimuxRunnerIndex + unlet! g:VimuxRunnerIndex endfunction function! VimuxTogglePane() abort From e8cc82397d877c4b0e3f51f730e062f704759f5e Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 12:46:24 -0700 Subject: [PATCH 08/10] Fix indentation --- plugin/vimux.vim | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 34f18cf..e10ea66 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -98,7 +98,7 @@ endfunction function! VimuxOpenRunner() abort if s:hasRunner() - return + return endif let existingId = s:existingRunnerId() if existingId !=# '' @@ -132,11 +132,11 @@ 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 @@ -240,7 +240,7 @@ function! s:exitCopyMode() abort catch let l:versionString = s:tmuxProperty('#{version}') if str2float(l:versionString) < 3.2 - call s:sendKeys('q') + call s:sendKeys('q') endif endtry endfunction @@ -266,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 "" @@ -288,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 '' @@ -306,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 @@ -353,7 +353,7 @@ endfunction function! s:hasRunner() abort if ! exists('g:VimuxRunnerIndex') - return v:false + return v:false endif let l:runnerType = VimuxOption('VimuxRunnerType') let l:command = 'list-'.runnerType."s -F '#{".runnerType."_id}'" @@ -372,7 +372,7 @@ function! s:sendKeys(keys) abort endfunction function! s:sendText(text) abort - call s:sendKeys(shellescape(substitute(a:text, '\n$', ' ', ''))) + call s:sendKeys(shellescape(substitute(a:text, '\n$', ' ', ''))) endfunction function! s:echoNoRunner() abort From eeb2028461498b9a959ae696f5e9a0a505d4e019 Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sun, 25 May 2025 12:49:23 -0700 Subject: [PATCH 09/10] Fix undefined variable --- plugin/vimux.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index e10ea66..d697629 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -82,7 +82,7 @@ endfunction function! VimuxSendText(text) abort if s:hasRunner() - call s:sendText(text) + call s:sendText(a:text) else call s:echoNoRunner() endif @@ -90,7 +90,7 @@ endfunction function! VimuxSendKeys(keys) abort if s:hasRunner() - call s:sendKeys(keys) + call s:sendKeys(a:keys) else call s:echoNoRunner() endif From 6c66a70727b17a474c1ae25976ba310fb194629b Mon Sep 17 00:00:00 2001 From: Michael van der Kamp Date: Sat, 12 Jul 2025 13:52:34 -0700 Subject: [PATCH 10/10] Handle case where runner index is the empty string --- plugin/vimux.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/vimux.vim b/plugin/vimux.vim index d697629..4f571da 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -352,7 +352,7 @@ function! s:tmuxProperty(property) abort endfunction function! s:hasRunner() abort - if ! exists('g:VimuxRunnerIndex') + if get(g:, 'VimuxRunnerIndex', '') == '' return v:false endif let l:runnerType = VimuxOption('VimuxRunnerType')