@@ -64,9 +64,7 @@ function! VimuxRunLastCommand() abort
64
64
endfunction
65
65
66
66
function ! VimuxRunCommand (command , ... ) abort
67
- if ! exists (' g:VimuxRunnerIndex' ) || s: hasRunner (g: VimuxRunnerIndex ) == # -1
68
- call VimuxOpenRunner ()
69
- endif
67
+ call VimuxOpenRunner ()
70
68
let l: autoreturn = 1
71
69
if exists (' a:1' )
72
70
let l: autoreturn = a: 1
@@ -75,26 +73,33 @@ function! VimuxRunCommand(command, ...) abort
75
73
let g: VimuxLastCommand = a: command
76
74
77
75
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 )
80
78
if l: autoreturn == # 1
81
- call VimuxSendKeys (' Enter' )
79
+ call s: sendKeys (' Enter' )
82
80
endif
83
81
endfunction
84
82
85
83
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
87
89
endfunction
88
90
89
91
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 )
92
94
else
93
- echo ' No vimux runner pane/window. Create one with VimuxOpenRunner '
95
+ call s: echoNoRunner ()
94
96
endif
95
97
endfunction
96
98
97
99
function ! VimuxOpenRunner () abort
100
+ if s: hasRunner ()
101
+ return
102
+ endif
98
103
let existingId = s: existingRunnerId ()
99
104
if existingId !=# ' '
100
105
let g: VimuxRunnerIndex = existingId
@@ -112,72 +117,91 @@ function! VimuxOpenRunner() abort
112
117
endfunction
113
118
114
119
function ! VimuxCloseRunner () abort
115
- if exists ( ' g:VimuxRunnerIndex ' )
120
+ if s: hasRunner ( )
116
121
call VimuxTmux (' kill-' .VimuxOption (' VimuxRunnerType' ).' -t ' .g: VimuxRunnerIndex )
117
- unlet g: VimuxRunnerIndex
118
122
endif
123
+ unlet ! g: VimuxRunnerIndex
119
124
endfunction
120
125
121
126
function ! VimuxTogglePane () abort
122
- if exists ( ' g:VimuxRunnerIndex ' )
127
+ if s: hasRunner ( )
123
128
if VimuxOption (' VimuxRunnerType' ) == # ' window'
124
129
call VimuxTmux (' join-pane -s ' .g: VimuxRunnerIndex .' ' .s: vimuxPaneOptions ())
125
130
let g: VimuxRunnerType = ' pane'
126
131
let g: VimuxRunnerIndex = s: tmuxIndex ()
127
132
call VimuxTmux (' last-' .VimuxOption (' VimuxRunnerType' ))
128
133
elseif VimuxOption (' VimuxRunnerType' ) == # ' pane'
129
134
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
+ \)
135
140
let g: VimuxRunnerType = ' window'
136
141
endif
142
+ else
143
+ call s: echoNoRunner ()
137
144
endif
138
145
endfunction
139
146
140
147
function ! VimuxZoomRunner () abort
141
- if exists ( ' g:VimuxRunnerIndex ' )
148
+ if s: hasRunner ( )
142
149
if VimuxOption (' VimuxRunnerType' ) == # ' pane'
143
150
call VimuxTmux (' resize-pane -Z -t ' .g: VimuxRunnerIndex )
144
151
elseif VimuxOption (' VimuxRunnerType' ) == # ' window'
145
152
call VimuxTmux (' select-window -t ' .g: VimuxRunnerIndex )
146
153
endif
154
+ else
155
+ call s: echoNoRunner ()
147
156
endif
148
157
endfunction
149
158
150
159
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
153
167
endfunction
154
168
155
169
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
159
174
endfunction
160
175
161
176
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
165
181
endfunction
166
182
167
183
function ! VimuxInterruptRunner () abort
168
- call VimuxSendKeys (' ^c' )
184
+ if s: hasRunner ()
185
+ call s: sendKeys (' ^c' )
186
+ else
187
+ call s: echoNoRunner ()
188
+ endif
169
189
endfunction
170
190
171
191
function ! VimuxClearTerminalScreen () abort
172
- if exists ( ' g:VimuxRunnerIndex ' ) && s: hasRunner (g: VimuxRunnerIndex ) !=# -1
192
+ if s: hasRunner ()
173
193
call s: exitCopyMode ()
174
- call VimuxSendKeys (' C-l' )
194
+ call s: sendKeys (' C-l' )
195
+ else
196
+ call s: echoNoRunner ()
175
197
endif
176
198
endfunction
177
199
178
200
function ! VimuxClearRunnerHistory () abort
179
- if exists ( ' g:VimuxRunnerIndex ' ) && s: hasRunner (g: VimuxRunnerIndex ) !=# -1
201
+ if s: hasRunner ()
180
202
call VimuxTmux (' clear-history -t ' .g: VimuxRunnerIndex )
203
+ else
204
+ call s: echoNoRunner ()
181
205
endif
182
206
endfunction
183
207
@@ -216,7 +240,7 @@ function! s:exitCopyMode() abort
216
240
catch
217
241
let l: versionString = s: tmuxProperty (' #{version}' )
218
242
if str2float (l: versionString ) < 3.2
219
- call VimuxSendKeys (' q' )
243
+ call s: sendKeys (' q' )
220
244
endif
221
245
endtry
222
246
endfunction
@@ -242,9 +266,9 @@ function! s:tmuxWindowId() abort
242
266
endfunction
243
267
244
268
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
248
272
endfunction
249
273
250
274
" "
@@ -264,13 +288,13 @@ function! s:existingRunnerId() abort
264
288
let currentId = s: tmuxIndex ()
265
289
let message = VimuxTmux (' select-' .runnerType.' -t ' .query.' ' )
266
290
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!
269
293
let runnerId = s: tmuxIndex ()
270
294
if runnerId !=# currentId
271
- " Success!
272
- call VimuxTmux (' last-' .runnerType)
273
- return runnerId
295
+ " Success!
296
+ call VimuxTmux (' last-' .runnerType)
297
+ return runnerId
274
298
endif
275
299
endif
276
300
return ' '
@@ -282,11 +306,11 @@ function! s:nearestRunnerId() abort
282
306
let runnerType = VimuxOption (' VimuxRunnerType' )
283
307
let filter = s: getTargetFilter ()
284
308
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' )
290
314
" '1:' is the current active pane (the one with vim).
291
315
" Find the first non-active pane.
292
316
for view in views
@@ -327,13 +351,30 @@ function! s:tmuxProperty(property) abort
327
351
return substitute (VimuxTmux (" display -p '" .a: property ." '" ), ' \n$' , ' ' , ' ' )
328
352
endfunction
329
353
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
333
362
endfunction
334
363
335
364
function ! s: autoclose () abort
336
365
if VimuxOption (' VimuxCloseOnExit' )
337
366
call VimuxCloseRunner ()
338
367
endif
339
368
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