Skip to content

Commit 364f854

Browse files
author
skywind3000
committed
terminal: simplify terminal reusable check
1 parent e0ad3b1 commit 364f854

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

plugin/asyncrun.vim

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Maintainer: skywind3000 (at) gmail.com, 2016-2022
44
" Homepage: https://github.com/skywind3000/asyncrun.vim
55
"
6-
" Last Modified: 2022/11/29 04:34
6+
" Last Modified: 2022/11/29 05:24
77
"
88
" Run shell command in background and output to quickfix:
99
" :AsyncRun[!] [options] {cmd} ...
@@ -1258,6 +1258,7 @@ function! s:terminal_init(opts)
12581258
let pos = (pos == 'background')? 'hide' : pos
12591259
let cwd = get(a:opts, 'cwd', '')
12601260
let cwd = (cwd != '' && isdirectory(cwd))? cwd : ''
1261+
let bid = -1
12611262
if get(a:opts, 'safe', get(g:, 'asyncrun_term_safe', 0)) != 0
12621263
let command = s:ScriptWrite(command, 0)
12631264
if stridx(command, ' ') >= 0
@@ -1352,6 +1353,7 @@ function! s:terminal_init(opts)
13521353
setlocal nonumber signcolumn=no norelativenumber
13531354
let b:asyncrun_cmd = a:opts.cmd
13541355
let b:asyncrun_name = get(a:opts, 'name', '')
1356+
let b:asyncrun_bid = bid
13551357
if get(a:opts, 'listed', 1) == 0
13561358
setlocal nobuflisted
13571359
endif
@@ -1386,7 +1388,9 @@ function! s:terminal_init(opts)
13861388
let info.processid = processid
13871389
let info.close = get(a:opts, 'close', 0)
13881390
let s:async_term[pid] = info
1389-
call setbufvar(bid, 'asyncrun_term_pid', pid)
1391+
if bid >= 0
1392+
call setbufvar(bid, 'asyncrun_pid', pid)
1393+
endif
13901394
return pid
13911395
endfunc
13921396

@@ -1489,6 +1493,27 @@ function! s:term_alive(bid)
14891493
endfunc
14901494

14911495

1496+
"----------------------------------------------------------------------
1497+
" check terminal is reusable
1498+
"----------------------------------------------------------------------
1499+
function! s:term_reusable(bid)
1500+
if getbufvar(a:bid, '&buftype') != 'terminal'
1501+
return 0
1502+
endif
1503+
if getbufvar(a:bid, 'asyncrun_bid', -1) < 0
1504+
return 0
1505+
endif
1506+
if has('nvim') == 0
1507+
return (term_getstatus(a:bid) == 'finished')? 1 : 0
1508+
else
1509+
let ch = getbufvar(a:bid, '&channel')
1510+
let status = (jobwait([ch], 0)[0] == -1)? 1 : 0
1511+
return (status == 0)? 1 : 0
1512+
endif
1513+
return 0
1514+
endfunc
1515+
1516+
14921517
"----------------------------------------------------------------------
14931518
" get a proper name
14941519
"----------------------------------------------------------------------
@@ -1555,21 +1580,10 @@ function! s:start_in_terminal(opts)
15551580
let a:opts._terminal_wipe = -1
15561581
for ii in range(winnr('$'))
15571582
let wid = ii + 1
1558-
if getwinvar(wid, '&bt') == 'terminal'
1559-
if has('nvim') == 0
1560-
let bid = winbufnr(wid)
1561-
if term_getstatus(bid) == 'finished'
1562-
let avail = wid
1563-
break
1564-
endif
1565-
else
1566-
let ch = getwinvar(wid, '&channel')
1567-
let status = (jobwait([ch], 0)[0] == -1)? 1 : 0
1568-
if status == 0
1569-
let avail = wid
1570-
break
1571-
endif
1572-
endif
1583+
let bid = winbufnr(wid)
1584+
if s:term_reusable(bid)
1585+
let avail = wid
1586+
break
15731587
endif
15741588
endfor
15751589
let focus = get(a:opts, 'focus', 1)
@@ -1584,20 +1598,9 @@ function! s:start_in_terminal(opts)
15841598
for i in range(tabpagenr('$'))
15851599
if tabpagewinnr(i + 1, '$') == 1
15861600
let bid = tabpagebuflist(i + 1)[0]
1587-
if getbufvar(bid, '&bt', '') == 'terminal'
1588-
if has('nvim') == 0
1589-
if term_getstatus(bid) == 'finished'
1590-
let avail = i + 1
1591-
break
1592-
endif
1593-
else
1594-
let ch = getbufvar(bid, '&channel')
1595-
let status = (jobwait([ch], 0)[0] == -1)? 1 : 0
1596-
if status == 0
1597-
let avail = i + 1
1598-
break
1599-
endif
1600-
endif
1601+
if s:term_reusable(bid)
1602+
let avail = i + 1
1603+
break
16011604
endif
16021605
endif
16031606
endfor
@@ -2232,7 +2235,7 @@ endfunc
22322235
" asyncrun - version
22332236
"----------------------------------------------------------------------
22342237
function! asyncrun#version()
2235-
return '2.11.12'
2238+
return '2.11.13'
22362239
endfunc
22372240

22382241

0 commit comments

Comments
 (0)