Skip to content

Commit 756150a

Browse files
author
skywind3000
committed
new runner: floaterm, try with :AsyncRun -mode=term -pos=floaterm ls -la
1 parent 58a0577 commit 756150a

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

autoload/asyncrun/runner/floaterm.vim

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"======================================================================
2+
"
3+
" floaterm.vim -
4+
"
5+
" Created by skywind on 2021/12/15
6+
" Last Modified: 2021/12/15 06:47:18
7+
"
8+
"======================================================================
9+
10+
" vim: set ts=4 sw=4 tw=78 noet :
11+
12+
function! s:floaterm_close() abort
13+
if &ft == 'floaterm' | return | endif
14+
for b in tabpagebuflist()
15+
if getbufvar(b, '&ft') == 'floaterm' &&
16+
\ getbufvar(b, 'floaterm_jobexists') == v:false
17+
execute b 'bwipeout!'
18+
break
19+
endif
20+
endfor
21+
autocmd! close-floaterm-runner
22+
endfunc
23+
24+
25+
"----------------------------------------------------------------------
26+
" floaterm
27+
"----------------------------------------------------------------------
28+
function! asyncrun#runner#floaterm#run(opts)
29+
if exists(':FloatermNew') != 2
30+
return asyncrun#utils#errmsg('require voldikss/vim-floaterm')
31+
endif
32+
if exists('*asyncrun#script_write') == 0
33+
return asyncrun#utils#errmsg('require asyncrun 2.7.8 or above')
34+
endif
35+
let cmd = 'FloatermNew '
36+
let cmd .= ' --wintype=float'
37+
if has_key(a:opts, 'position')
38+
let cmd .= ' --position=' . fnameescape(a:opts.position)
39+
endif
40+
if has_key(a:opts, 'width')
41+
let cmd .= ' --width=' . fnameescape(a:opts.width)
42+
endif
43+
if has_key(a:opts, 'height')
44+
let cmd .= ' --height=' . fnameescape(a:opts.height)
45+
endif
46+
if has_key(a:opts, 'title')
47+
let cmd .= ' --title=' . fnameescape(a:opts.title)
48+
endif
49+
let cmd .= ' --autoclose=0'
50+
let cmd .= ' --silent=' . get(a:opts, 'silent', 0)
51+
let cwd = (a:opts.cwd == '')? getcwd() : (a:opts.cwd)
52+
let cmd .= ' --cwd=' . fnameescape(cwd)
53+
" for precisely arguments passing and shell builtin commands
54+
" a temporary file is introduced
55+
let cmd .= ' ' . fnameescape(asyncrun#script_write(a:opts.cmd, 0))
56+
exec cmd
57+
if get(a:opts, 'focus', 1) == 0
58+
stopinsert | noa wincmd p
59+
augroup close-floaterm-runner
60+
autocmd!
61+
autocmd CursorMoved,InsertEnter * ++nested
62+
\ call timer_start(100, { -> s:floaterm_close() })
63+
augroup END
64+
endif
65+
endfunc
66+
67+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"======================================================================
2+
"
3+
" floater_reuse.vim -
4+
"
5+
" Created by skywind on 2021/12/15
6+
" Last Modified: 2021/12/15 06:48:57
7+
"
8+
"======================================================================
9+
10+
" vim: set ts=4 sw=4 tw=78 noet :
11+
12+
function! asyncrun#runner#floaterm_reuse#run(opts)
13+
let curr_bufnr = floaterm#curr()
14+
if has_key(a:opts, 'silent') && a:opts.silent == 1
15+
FloatermHide!
16+
endif
17+
let cmd = 'cd ' . shellescape(getcwd())
18+
call floaterm#terminal#send(curr_bufnr, [cmd])
19+
call floaterm#terminal#send(curr_bufnr, [a:opts.cmd])
20+
stopinsert
21+
if &filetype == 'floaterm' && g:floaterm_autoinsert
22+
call floaterm#util#startinsert()
23+
endif
24+
return 0
25+
endfunc
26+
27+

autoload/asyncrun/utils.vim

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"======================================================================
2+
"
3+
" utils.vim -
4+
"
5+
" Created by skywind on 2021/12/15
6+
" Last Modified: 2021/12/15 06:33:42
7+
"
8+
"======================================================================
9+
10+
" vim: set ts=4 sw=4 tw=78 noet :
11+
12+
13+
"----------------------------------------------------------------------
14+
" output msg
15+
"----------------------------------------------------------------------
16+
function! asyncrun#utils#errmsg(msg)
17+
redraw
18+
echohl ErrorMsg
19+
echom 'ERROR: ' . a:msg
20+
echohl NONE
21+
return 0
22+
endfunc
23+
24+
25+
"----------------------------------------------------------------------
26+
" display require message
27+
"----------------------------------------------------------------------
28+
function! asyncrun#utils#require(what)
29+
call asyncrun#utils#errmsg('require: ' . a:what . ' ')
30+
endfunc
31+
32+

plugin/runner_load.vim

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"======================================================================
2+
"
3+
" runner_load.vim -
4+
"
5+
" Created by skywind on 2021/12/15
6+
" Last Modified: 2021/12/15 05:40:55
7+
"
8+
"======================================================================
9+
10+
" vim: set ts=4 sw=4 tw=78 noet :
11+
12+
13+
"----------------------------------------------------------------------
14+
" internal
15+
"----------------------------------------------------------------------
16+
let g:asyncrun_event = get(g:, 'asyncrun_event', {})
17+
let g:asyncrun_runner = get(g:, 'asyncrun_runner', {})
18+
let s:script_time = {}
19+
20+
21+
"----------------------------------------------------------------------
22+
" find script
23+
"----------------------------------------------------------------------
24+
function! s:find_script(name)
25+
let test = 'autoload/asyncrun/runner/' . a:name . '.vim'
26+
let fn = findfile(test, &rtp)
27+
if fn == ''
28+
return ''
29+
endif
30+
let fn = fnamemodify(fn, ':p')
31+
let fn = substitute(fn, '\\', '/', 'g')
32+
return fn
33+
endfunc
34+
35+
36+
"----------------------------------------------------------------------
37+
" load runner
38+
"----------------------------------------------------------------------
39+
function! s:load_runner(name)
40+
let name = a:name
41+
let load = s:find_script(name)
42+
let test = 'asyncrun#runner#' . name . '#run'
43+
if filereadable(load) && load != ''
44+
let mtime = getftime(load)
45+
let ltime = get(s:script_time, load, 0)
46+
if mtime > ltime
47+
exec 'source ' . fnameescape(load)
48+
let s:script_time[load] = mtime
49+
endif
50+
if exists('*' . test)
51+
let g:asyncrun_runner[name] = test
52+
endif
53+
endif
54+
endfunc
55+
56+
57+
"----------------------------------------------------------------------
58+
" init runner
59+
"----------------------------------------------------------------------
60+
function! g:asyncrun_event.runner(name)
61+
call s:load_runner(a:name)
62+
endfunc
63+
64+
65+

0 commit comments

Comments
 (0)