-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathvimux.vim
343 lines (307 loc) · 10.3 KB
/
vimux.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
if exists('g:loaded_vimux') || &compatible
finish
endif
let g:loaded_vimux = 1
" Set up all global options with defaults right away, in one place
let g:VimuxDebug = get(g:, 'VimuxDebug', v:false)
let g:VimuxHeight = get(g:, 'VimuxHeight', '20%')
let g:VimuxOpenExtraArgs = get(g:, 'VimuxOpenExtraArgs', '')
let g:VimuxOrientation = get(g:, 'VimuxOrientation', 'v')
let g:VimuxPromptString = get(g:, 'VimuxPromptString', 'Command? ')
let g:VimuxResetSequence = get(g:, 'VimuxResetSequence', 'C-u')
let g:VimuxRunnerName = get(g:, 'VimuxRunnerName', '')
let g:VimuxRunnerType = get(g:, 'VimuxRunnerType', 'pane')
let g:VimuxRunnerQuery = get(g:, 'VimuxRunnerQuery', {})
let g:VimuxTmuxCommand = get(g:, 'VimuxTmuxCommand', 'tmux')
let g:VimuxUseNearest = get(g:, 'VimuxUseNearest', v:true)
let g:VimuxExpandCommand = get(g:, 'VimuxExpandCommand', v:false)
let g:VimuxCloseOnExit = get(g:, 'VimuxCloseOnExit', v:false)
let g:VimuxCommandShell = get(g:, 'VimuxCommandShell', v:true)
function! VimuxOption(name) abort
return get(b:, a:name, get(g:, a:name))
endfunction
if !executable(VimuxOption('VimuxTmuxCommand'))
echohl ErrorMsg | echomsg 'Failed to find executable '.VimuxOption('VimuxTmuxCommand') | echohl None
finish
endif
command -nargs=* VimuxRunCommand :call VimuxRunCommand(<args>)
command -bar VimuxRunLastCommand :call VimuxRunLastCommand()
command -bar VimuxOpenRunner :call VimuxOpenRunner()
command -bar VimuxCloseRunner :call VimuxCloseRunner()
command -bar VimuxZoomRunner :call VimuxZoomRunner()
command -bar VimuxInspectRunner :call VimuxInspectRunner()
command -bar VimuxScrollUpInspect :call VimuxScrollUpInspect()
command -bar VimuxScrollDownInspect :call VimuxScrollDownInspect()
command -bar VimuxInterruptRunner :call VimuxInterruptRunner()
command -nargs=? VimuxPromptCommand :call VimuxPromptCommand(<args>)
command -bar VimuxClearTerminalScreen :call VimuxClearTerminalScreen()
command -bar VimuxClearRunnerHistory :call VimuxClearRunnerHistory()
command -bar VimuxTogglePane :call VimuxTogglePane()
augroup VimuxAutocmds
au!
autocmd VimLeave * call s:autoclose()
augroup END
function! VimuxRunCommandInDir(command, useFile) abort
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() abort
if exists('g:VimuxLastCommand')
call VimuxRunCommand(g:VimuxLastCommand)
else
echo 'No last vimux command.'
endif
endfunction
function! VimuxRunCommand(command, ...) abort
if ! s:hasRunner()
call VimuxOpenRunner()
endif
let l:autoreturn = 1
if exists('a:1')
let l:autoreturn = a:1
endif
let l:resetSequence = VimuxOption('VimuxResetSequence')
let g:VimuxLastCommand = a:command
call s:exitCopyMode()
call VimuxSendKeys(l:resetSequence)
call VimuxSendText(a:command)
if l:autoreturn ==# 1
call VimuxSendKeys('Enter')
endif
endfunction
function! VimuxSendText(text) abort
call VimuxSendKeys(shellescape(substitute(a:text, '\n$', ' ', '')))
endfunction
function! VimuxSendKeys(keys) abort
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
else
let extraArguments = VimuxOption('VimuxOpenExtraArgs')
if VimuxOption('VimuxRunnerType') ==# 'pane'
call VimuxTmux('split-window '.s:vimuxPaneOptions().' '.extraArguments)
elseif VimuxOption('VimuxRunnerType') ==# 'window'
call VimuxTmux('new-window '.extraArguments)
endif
let g:VimuxRunnerIndex = s:tmuxIndex()
call s:setRunnerName()
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
endif
endfunction
function! VimuxCloseRunner() abort
if s:hasRunner()
call VimuxTmux('kill-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
endif
unlet g:VimuxRunnerIndex
endfunction
function! VimuxTogglePane() abort
if s:hasRunner()
if VimuxOption('VimuxRunnerType') ==# 'window'
call VimuxTmux('join-pane -s '.g:VimuxRunnerIndex.' '.s:vimuxPaneOptions())
let g:VimuxRunnerType = 'pane'
let g:VimuxRunnerIndex = s:tmuxIndex()
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',
\ '',
\ ''
\)
let g:VimuxRunnerType = 'window'
endif
endif
endfunction
function! VimuxZoomRunner() abort
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
endif
endfunction
function! VimuxInspectRunner() abort
call VimuxTmux('select-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
call VimuxTmux('copy-mode')
endfunction
function! VimuxScrollUpInspect() abort
call VimuxInspectRunner()
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
call VimuxSendKeys('C-u')
endfunction
function! VimuxScrollDownInspect() abort
call VimuxInspectRunner()
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
call VimuxSendKeys('C-d')
endfunction
function! VimuxInterruptRunner() abort
call VimuxSendKeys('^c')
endfunction
function! VimuxClearTerminalScreen() abort
if s:hasRunner()
call s:exitCopyMode()
call VimuxSendKeys('C-l')
endif
endfunction
function! VimuxClearRunnerHistory() abort
if s:hasRunner()
call VimuxTmux('clear-history -t '.g:VimuxRunnerIndex)
endif
endfunction
function! VimuxPromptCommand(...) abort
let command = a:0 ==# 1 ? a:1 : ''
if VimuxOption('VimuxCommandShell')
let l:command = input(VimuxOption('VimuxPromptString'), command, 'shellcmd')
else
let l:command = input(VimuxOption('VimuxPromptString'), command)
endif
if VimuxOption('VimuxExpandCommand')
let l:command = join(map(split(l:command, ' '), 'expand(v:val)'), ' ')
endif
call VimuxRunCommand(l:command)
endfunction
function! VimuxTmux(arguments) abort
let l:tmuxCommand = VimuxOption('VimuxTmuxCommand').' '.a:arguments
if VimuxOption('VimuxDebug')
echom l:tmuxCommand
endif
if has_key(environ(), 'TMUX')
let l:output = system(l:tmuxCommand)
if v:shell_error
throw 'Tmux command failed with message:' . l:output
endif
return l:output
else
throw 'Aborting, because not inside tmux session.'
endif
endfunction
function! s:exitCopyMode() abort
try
call VimuxTmux('copy-mode -q -t '.g:VimuxRunnerIndex)
catch
let l:versionString = s:tmuxProperty('#{version}')
if str2float(l:versionString) < 3.2
call VimuxSendKeys('q')
endif
endtry
endfunction
function! s:tmuxSession() abort
return s:tmuxProperty('#S')
endfunction
function! s:tmuxIndex() abort
if VimuxOption('VimuxRunnerType') ==# 'pane'
return s:tmuxPaneId()
else
return s:tmuxWindowId()
end
endfunction
function! s:tmuxPaneId() abort
return s:tmuxProperty('#{pane_id}')
endfunction
function! s:tmuxWindowId() abort
return s:tmuxProperty('#{window_id}')
endfunction
function! s:vimuxPaneOptions() abort
let height = VimuxOption('VimuxHeight')
let orientation = VimuxOption('VimuxOrientation')
return '-l '.height.' -'.orientation
endfunction
""
" @return a string of the form '%4', the ID of the pane or window to use,
" or '' if no nearest pane or window is found.
function! s:existingRunnerId() abort
let runnerType = VimuxOption('VimuxRunnerType')
let query = get(VimuxOption('VimuxRunnerQuery'), runnerType, '')
if empty(query)
if empty(VimuxOption('VimuxUseNearest'))
return ''
else
return s:nearestRunnerId()
endif
endif
" Try finding the runner using the provided query
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!
let runnerId = s:tmuxIndex()
if runnerId !=# currentId
" Success!
call VimuxTmux('last-'.runnerType)
return runnerId
endif
endif
return ''
endfunction
function! s:nearestRunnerId() abort
" Try finding the runner in the current window/session, optionally using a
" name/title filter
let runnerType = VimuxOption('VimuxRunnerType')
let filter = s:getTargetFilter()
let views = split(
\ 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
if match(view, '1:') ==# -1
return split(view, ':')[1]
endif
endfor
return ''
endfunction
function! s:getTargetFilter() abort
let targetName = VimuxOption('VimuxRunnerName')
if targetName ==# ''
return ''
endif
let runnerType = VimuxOption('VimuxRunnerType')
if runnerType ==# 'window'
return " -f '#{==:#{window_name},".targetName."}'"
elseif runnerType ==# 'pane'
return " -f '#{==:#{pane_title},".targetName."}'"
endif
endfunction
function! s:setRunnerName() abort
let targetName = VimuxOption('VimuxRunnerName')
if targetName ==# ''
return
endif
let runnerType = VimuxOption('VimuxRunnerType')
if runnerType ==# 'window'
call VimuxTmux('rename-window '.targetName)
elseif runnerType ==# 'pane'
call VimuxTmux('select-pane -T '.targetName)
endif
endfunction
function! s:tmuxProperty(property) abort
return substitute(VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
endfunction
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
if VimuxOption('VimuxCloseOnExit')
call VimuxCloseRunner()
endif
endfunction