Skip to content

Commit f4bf4a8

Browse files
authored
Merge pull request #237 from mvanderkamp/catch-on-close
Prevent errors when runner is closed by something other than vimux
2 parents 7db6b2f + 6c66a70 commit f4bf4a8

File tree

1 file changed

+89
-48
lines changed

1 file changed

+89
-48
lines changed

plugin/vimux.vim

Lines changed: 89 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ function! VimuxRunLastCommand() abort
6464
endfunction
6565

6666
function! VimuxRunCommand(command, ...) abort
67-
if !exists('g:VimuxRunnerIndex') || s:hasRunner(g:VimuxRunnerIndex) ==# -1
68-
call VimuxOpenRunner()
69-
endif
67+
call VimuxOpenRunner()
7068
let l:autoreturn = 1
7169
if exists('a:1')
7270
let l:autoreturn = a:1
@@ -75,26 +73,33 @@ function! VimuxRunCommand(command, ...) abort
7573
let g:VimuxLastCommand = a:command
7674

7775
call s:exitCopyMode()
78-
call VimuxSendKeys(l:resetSequence)
79-
call VimuxSendText(a:command)
76+
call s:sendKeys(l:resetSequence)
77+
call s:sendText(a:command)
8078
if l:autoreturn ==# 1
81-
call VimuxSendKeys('Enter')
79+
call s:sendKeys('Enter')
8280
endif
8381
endfunction
8482

8583
function! VimuxSendText(text) abort
86-
call VimuxSendKeys(shellescape(substitute(a:text, '\n$', ' ', '')))
84+
if s:hasRunner()
85+
call s:sendText(a:text)
86+
else
87+
call s:echoNoRunner()
88+
endif
8789
endfunction
8890

8991
function! VimuxSendKeys(keys) abort
90-
if exists('g:VimuxRunnerIndex')
91-
call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys)
92+
if s:hasRunner()
93+
call s:sendKeys(a:keys)
9294
else
93-
echo 'No vimux runner pane/window. Create one with VimuxOpenRunner'
95+
call s:echoNoRunner()
9496
endif
9597
endfunction
9698

9799
function! VimuxOpenRunner() abort
100+
if s:hasRunner()
101+
return
102+
endif
98103
let existingId = s:existingRunnerId()
99104
if existingId !=# ''
100105
let g:VimuxRunnerIndex = existingId
@@ -112,72 +117,91 @@ function! VimuxOpenRunner() abort
112117
endfunction
113118

114119
function! VimuxCloseRunner() abort
115-
if exists('g:VimuxRunnerIndex')
120+
if s:hasRunner()
116121
call VimuxTmux('kill-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
117-
unlet g:VimuxRunnerIndex
118122
endif
123+
unlet! g:VimuxRunnerIndex
119124
endfunction
120125

121126
function! VimuxTogglePane() abort
122-
if exists('g:VimuxRunnerIndex')
127+
if s:hasRunner()
123128
if VimuxOption('VimuxRunnerType') ==# 'window'
124129
call VimuxTmux('join-pane -s '.g:VimuxRunnerIndex.' '.s:vimuxPaneOptions())
125130
let g:VimuxRunnerType = 'pane'
126131
let g:VimuxRunnerIndex = s:tmuxIndex()
127132
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
128133
elseif VimuxOption('VimuxRunnerType') ==# 'pane'
129134
let g:VimuxRunnerIndex=substitute(
130-
\ VimuxTmux('break-pane -d -s '.g:VimuxRunnerIndex." -P -F '#{window_id}'"),
131-
\ '\n',
132-
\ '',
133-
\ ''
134-
\)
135+
\ VimuxTmux('break-pane -d -s '.g:VimuxRunnerIndex." -P -F '#{window_id}'"),
136+
\ '\n',
137+
\ '',
138+
\ ''
139+
\)
135140
let g:VimuxRunnerType = 'window'
136141
endif
142+
else
143+
call s:echoNoRunner()
137144
endif
138145
endfunction
139146

140147
function! VimuxZoomRunner() abort
141-
if exists('g:VimuxRunnerIndex')
148+
if s:hasRunner()
142149
if VimuxOption('VimuxRunnerType') ==# 'pane'
143150
call VimuxTmux('resize-pane -Z -t '.g:VimuxRunnerIndex)
144151
elseif VimuxOption('VimuxRunnerType') ==# 'window'
145152
call VimuxTmux('select-window -t '.g:VimuxRunnerIndex)
146153
endif
154+
else
155+
call s:echoNoRunner()
147156
endif
148157
endfunction
149158

150159
function! VimuxInspectRunner() abort
151-
call VimuxTmux('select-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
152-
call VimuxTmux('copy-mode')
160+
if s:hasRunner()
161+
call VimuxTmux('select-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
162+
call VimuxTmux('copy-mode')
163+
return v:true
164+
endif
165+
call s:echoNoRunner()
166+
return v:false
153167
endfunction
154168

155169
function! VimuxScrollUpInspect() abort
156-
call VimuxInspectRunner()
157-
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
158-
call VimuxSendKeys('C-u')
170+
if VimuxInspectRunner()
171+
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
172+
call s:sendKeys('C-u')
173+
endif
159174
endfunction
160175

161176
function! VimuxScrollDownInspect() abort
162-
call VimuxInspectRunner()
163-
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
164-
call VimuxSendKeys('C-d')
177+
if VimuxInspectRunner()
178+
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
179+
call s:sendKeys('C-d')
180+
endif
165181
endfunction
166182

167183
function! VimuxInterruptRunner() abort
168-
call VimuxSendKeys('^c')
184+
if s:hasRunner()
185+
call s:sendKeys('^c')
186+
else
187+
call s:echoNoRunner()
188+
endif
169189
endfunction
170190

171191
function! VimuxClearTerminalScreen() abort
172-
if exists('g:VimuxRunnerIndex') && s:hasRunner(g:VimuxRunnerIndex) !=# -1
192+
if s:hasRunner()
173193
call s:exitCopyMode()
174-
call VimuxSendKeys('C-l')
194+
call s:sendKeys('C-l')
195+
else
196+
call s:echoNoRunner()
175197
endif
176198
endfunction
177199

178200
function! VimuxClearRunnerHistory() abort
179-
if exists('g:VimuxRunnerIndex') && s:hasRunner(g:VimuxRunnerIndex) !=# -1
201+
if s:hasRunner()
180202
call VimuxTmux('clear-history -t '.g:VimuxRunnerIndex)
203+
else
204+
call s:echoNoRunner()
181205
endif
182206
endfunction
183207

@@ -216,7 +240,7 @@ function! s:exitCopyMode() abort
216240
catch
217241
let l:versionString = s:tmuxProperty('#{version}')
218242
if str2float(l:versionString) < 3.2
219-
call VimuxSendKeys('q')
243+
call s:sendKeys('q')
220244
endif
221245
endtry
222246
endfunction
@@ -242,9 +266,9 @@ function! s:tmuxWindowId() abort
242266
endfunction
243267

244268
function! s:vimuxPaneOptions() abort
245-
let height = VimuxOption('VimuxHeight')
246-
let orientation = VimuxOption('VimuxOrientation')
247-
return '-l '.height.' -'.orientation
269+
let height = VimuxOption('VimuxHeight')
270+
let orientation = VimuxOption('VimuxOrientation')
271+
return '-l '.height.' -'.orientation
248272
endfunction
249273

250274
""
@@ -264,13 +288,13 @@ function! s:existingRunnerId() abort
264288
let currentId = s:tmuxIndex()
265289
let message = VimuxTmux('select-'.runnerType.' -t '.query.'')
266290
if message ==# ''
267-
" A match was found. Make sure it isn't the current vim pane/window
268-
" though!
291+
" A match was found. Make sure it isn't the current vim pane/window
292+
" though!
269293
let runnerId = s:tmuxIndex()
270294
if runnerId !=# currentId
271-
" Success!
272-
call VimuxTmux('last-'.runnerType)
273-
return runnerId
295+
" Success!
296+
call VimuxTmux('last-'.runnerType)
297+
return runnerId
274298
endif
275299
endif
276300
return ''
@@ -282,11 +306,11 @@ function! s:nearestRunnerId() abort
282306
let runnerType = VimuxOption('VimuxRunnerType')
283307
let filter = s:getTargetFilter()
284308
let views = split(
285-
\ VimuxTmux(
286-
\ 'list-'.runnerType.'s'
287-
\ ." -F '#{".runnerType.'_active}:#{'.runnerType."_id}'"
288-
\ .filter),
289-
\ '\n')
309+
\ VimuxTmux(
310+
\ 'list-'.runnerType.'s'
311+
\ ." -F '#{".runnerType.'_active}:#{'.runnerType."_id}'"
312+
\ .filter),
313+
\ '\n')
290314
" '1:' is the current active pane (the one with vim).
291315
" Find the first non-active pane.
292316
for view in views
@@ -327,13 +351,30 @@ function! s:tmuxProperty(property) abort
327351
return substitute(VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
328352
endfunction
329353

330-
function! s:hasRunner(index) abort
331-
let runnerType = VimuxOption('VimuxRunnerType')
332-
return match(VimuxTmux('list-'.runnerType."s -F '#{".runnerType."_id}'"), a:index)
354+
function! s:hasRunner() abort
355+
if get(g:, 'VimuxRunnerIndex', '') == ''
356+
return v:false
357+
endif
358+
let l:runnerType = VimuxOption('VimuxRunnerType')
359+
let l:command = 'list-'.runnerType."s -F '#{".runnerType."_id}'"
360+
let l:found = match(VimuxTmux(l:command), g:VimuxRunnerIndex)
361+
return l:found != -1
333362
endfunction
334363

335364
function! s:autoclose() abort
336365
if VimuxOption('VimuxCloseOnExit')
337366
call VimuxCloseRunner()
338367
endif
339368
endfunction
369+
370+
function! s:sendKeys(keys) abort
371+
call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys)
372+
endfunction
373+
374+
function! s:sendText(text) abort
375+
call s:sendKeys(shellescape(substitute(a:text, '\n$', ' ', '')))
376+
endfunction
377+
378+
function! s:echoNoRunner() abort
379+
echo 'No vimux runner pane/window. Create one with VimuxOpenRunner'
380+
endfunction

0 commit comments

Comments
 (0)