-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathfloaterm.vim
More file actions
309 lines (282 loc) · 9.66 KB
/
Copy pathfloaterm.vim
File metadata and controls
309 lines (282 loc) · 9.66 KB
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
" vim:sw=2:
" ============================================================================
" FileName: floaterm.vim
" Author: voldikss <dyzplus@gmail.com>
" GitHub: https://github.com/voldikss
" ============================================================================
" ----------------------------------------------------------------------------
" wrapper function for `floaterm#new()` and `floaterm#update()` since they
" share the same argument: `config`
" ----------------------------------------------------------------------------
function! floaterm#run(action, bang, rangeargs, cmdargs) abort
let [cmd, config] = floaterm#cmdline#parse(a:cmdargs)
if a:action == 'new'
let [visualmode, range, line1, line2] = a:rangeargs
if range > 0
let lines = floaterm#util#get_selected_text(visualmode, range, line1, line2)
endif
let bufnr = floaterm#new(a:bang, cmd, {}, config)
if range > 0 && !empty(lines)
call floaterm#terminal#send(bufnr, lines)
endif
elseif a:action == 'update'
call floaterm#update(config)
endif
endfunction
" create a floaterm. return bufnr of the terminal
" argument `jobopts` is passed by user in the case using this function as API
function! floaterm#new(bang, cmd, jobopts, config) abort
let env = floaterm#util#setenv()
let vim_version = floaterm#util#vim_version()
if vim_version[0] == 'nvim' && vim_version[1] <= '0.4.4'
for [name, value] in items(env)
call setenv(name, value)
endfor
else
call floaterm#util#deep_extend(a:jobopts, {'env': env})
endif
if !empty(a:cmd)
let wrappers_path = globpath(&runtimepath, 'autoload/floaterm/wrapper/*vim', 0, 1)
let wrappers = map(wrappers_path, "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')")
let maybe_wrapper = split(a:cmd, '\s')[0]
if index(wrappers, maybe_wrapper) >= 0
try
let [shell, shellslash, shellcmdflag, shellxquote] = floaterm#util#use_sh_or_cmd()
let WrapFunc = function(printf('floaterm#wrapper#%s#', maybe_wrapper))
" NOTE: a:jobopts and a:config can be changed in WrapFunc
let [send2shell, newcmd] = WrapFunc(a:cmd, a:jobopts, a:config)
if send2shell
let bufnr = floaterm#terminal#open(-1, g:floaterm_shell, a:jobopts, a:config)
call floaterm#terminal#send(bufnr, [newcmd])
else
let bufnr = floaterm#terminal#open(-1, newcmd, a:jobopts, a:config)
endif
finally
let [&shell, &shellslash, &shellcmdflag, &shellxquote] = [shell, shellslash, shellcmdflag, shellxquote]
endtry
elseif a:bang
let bufnr = floaterm#terminal#open(-1, g:floaterm_shell, a:jobopts, a:config)
call floaterm#terminal#send(bufnr, [a:cmd])
else
let bufnr = floaterm#terminal#open(-1, a:cmd, a:jobopts, a:config)
endif
else
let bufnr = floaterm#terminal#open(-1, g:floaterm_shell, a:jobopts, a:config)
endif
return bufnr
endfunction
" ----------------------------------------------------------------------------
" toggle on/off the floaterm named `name`
" ----------------------------------------------------------------------------
function! floaterm#toggle(bang, bufnr, name) abort
if a:bang
let found_winnr = floaterm#window#find()
if found_winnr > 0
call floaterm#hide(1, 0, '')
else
let buffers = floaterm#buflist#gather()
if empty(buffers)
let curr_bufnr = floaterm#new(v:true, '', {}, {})
else
call floaterm#show(1, 0, '')
endif
endif
return
endif
let bufnr = a:bufnr
if bufnr == 0 && !empty(a:name)
let bufnr = floaterm#terminal#get_bufnr(a:name)
endif
if bufnr == -1
call floaterm#new(a:bang, '', {}, {'name': a:name})
elseif bufnr == 0
if &filetype == 'floaterm'
call floaterm#window#hide(bufnr('%'))
else
let found_winnr = floaterm#window#find()
if found_winnr > 0
execute found_winnr . 'wincmd w'
else
call floaterm#curr()
endif
endif
elseif getbufvar(bufnr, 'floaterm_winid', -1) != -1
if bufnr == bufnr('%')
call floaterm#window#hide(bufnr)
elseif bufwinnr(bufnr) > -1
execute bufwinnr(bufnr) . 'wincmd w'
else
call floaterm#terminal#open_existing(bufnr)
endif
else
call floaterm#util#show_msg('No floaterms with the bufnr or name', 'error')
endif
endfunction
" ----------------------------------------------------------------------------
" toggle fullscreen on/off the floaterm
" ----------------------------------------------------------------------------
function! floaterm#toggle_fullscreen() abort
let bufnr = bufnr('%')
call floaterm#update({'fullscreen': !getbufvar(bufnr, 'floaterm_fullscreen', v:false)})
endfunction
" ----------------------------------------------------------------------------
" toggle border on/off the floaterm
" ----------------------------------------------------------------------------
function! floaterm#toggle_border() abort
let bufnr = bufnr('%')
call floaterm#update({'show_border': !getbufvar(bufnr, 'floaterm_show_border', v:false)})
endfunction
" ----------------------------------------------------------------------------
" toggle title on/off the floaterm
" ----------------------------------------------------------------------------
function! floaterm#toggle_title() abort
let bufnr = bufnr('%')
call floaterm#update({'show_title': !getbufvar(bufnr, 'floaterm_show_title', v:false)})
endfunction
" ----------------------------------------------------------------------------
" update the attributes of a floaterm
" ----------------------------------------------------------------------------
function! floaterm#update(config) abort
if &filetype !=# 'floaterm'
call floaterm#util#show_msg('You have to be in a floaterm window to change window config.', 'error')
return
endif
let bufnr = bufnr('%')
call floaterm#window#hide(bufnr)
call floaterm#config#set_all(bufnr, a:config)
call floaterm#terminal#open_existing(bufnr)
endfunction
function! floaterm#next() abort
let next_bufnr = floaterm#buflist#next()
if next_bufnr == -1
let msg = 'No more floaterms'
call floaterm#util#show_msg(msg, 'warning')
else
call floaterm#terminal#open_existing(next_bufnr)
endif
endfunction
function! floaterm#prev() abort
let prev_bufnr = floaterm#buflist#prev()
if prev_bufnr == -1
let msg = 'No more floaterms'
call floaterm#util#show_msg(msg, 'warning')
else
call floaterm#terminal#open_existing(prev_bufnr)
endif
endfunction
function! floaterm#curr() abort
let curr_bufnr = floaterm#buflist#curr()
if curr_bufnr == -1
let curr_bufnr = floaterm#new(v:true, '', {}, {})
else
call floaterm#terminal#open_existing(curr_bufnr)
endif
return curr_bufnr
endfunction
function! floaterm#first() abort
let first_bufnr = floaterm#buflist#first()
if first_bufnr == -1
call floaterm#util#show_msg('No more floaterms', 'warning')
else
call floaterm#terminal#open_existing(first_bufnr)
endif
endfunction
function! floaterm#last() abort
let last_bufnr = floaterm#buflist#last()
if last_bufnr == -1
call floaterm#util#show_msg('No more floaterms', 'warning')
else
call floaterm#terminal#open_existing(last_bufnr)
endif
endfunction
function! floaterm#kill(bang, bufnr, name) abort
if a:bang
for bufnr in floaterm#buflist#gather()
call floaterm#terminal#kill(bufnr)
endfor
return
endif
let bufnr = a:bufnr
if bufnr == 0 && !empty(a:name)
let bufnr = floaterm#terminal#get_bufnr(a:name)
endif
if bufnr == 0 || bufnr == -1
let bufnr = floaterm#buflist#curr()
endif
if bufnr > 0
call floaterm#terminal#kill(bufnr)
else
call floaterm#util#show_msg('No floaterms with the bufnr or name', 'error')
endif
endfunction
function! floaterm#show(bang, bufnr, name) abort
if a:bang
call floaterm#hide(1, 0, '')
for bufnr in floaterm#buflist#gather()
call floaterm#terminal#open_existing(bufnr)
endfor
return
endif
let bufnr = a:bufnr
if bufnr == 0 && !empty(a:name)
let bufnr = floaterm#terminal#get_bufnr(a:name)
endif
if bufnr == 0 || bufnr == -1
let bufnr = floaterm#buflist#curr()
endif
if bufnr > 0
call floaterm#terminal#open_existing(bufnr)
else
call floaterm#util#show_msg('No floaterms with the bufnr or name', 'error')
endif
endfunction
function! floaterm#hide(bang, bufnr, name) abort
if a:bang
for bufnr in floaterm#buflist#gather()
call floaterm#window#hide(bufnr)
endfor
return
endif
let bufnr = a:bufnr
if bufnr == 0 && !empty(a:name)
let bufnr = floaterm#terminal#get_bufnr(a:name)
endif
if bufnr == 0 || bufnr == -1
let bufnr = bufnr('%')
endif
if bufnr > 0
call floaterm#window#hide(bufnr)
else
call floaterm#util#show_msg('No floaterms with the bufnr or name', 'error')
endif
endfunction
function! floaterm#send(bang, visualmode, range, line1, line2, argstr) abort
let [cmd, config] = floaterm#cmdline#parse(a:argstr)
let termname = get(config, 'name', '')
if !empty(termname)
let bufnr = floaterm#terminal#get_bufnr(termname)
if bufnr == -1
call floaterm#util#show_msg('No floaterm found with name: ' . termname, 'error')
return
endif
else
let bufnr = floaterm#buflist#curr()
if bufnr == -1
call floaterm#util#show_msg('No more floaterms', 'warning')
return
endif
endif
if !empty(cmd)
call floaterm#terminal#send(bufnr, [cmd])
return
endif
let lines = floaterm#util#get_selected_text(a:visualmode, a:range, a:line1, a:line2)
if empty(lines)
call floaterm#util#show_msg('No lines were selected', 'error')
return
endif
if a:bang
let lines = floaterm#util#leftalign_lines(lines)
endif
call floaterm#terminal#send(bufnr, lines)
endfunction