3
3
" Maintainer: skywind3000 (at) gmail.com, 2016-2022
4
4
" Homepage: https://github.com/skywind3000/asyncrun.vim
5
5
"
6
- " Last Modified: 2022/11/29 04:34
6
+ " Last Modified: 2022/11/29 05:24
7
7
"
8
8
" Run shell command in background and output to quickfix:
9
9
" :AsyncRun[!] [options] {cmd} ...
@@ -1258,6 +1258,7 @@ function! s:terminal_init(opts)
1258
1258
let pos = (pos == ' background' )? ' hide' : pos
1259
1259
let cwd = get (a: opts , ' cwd' , ' ' )
1260
1260
let cwd = (cwd != ' ' && isdirectory (cwd))? cwd : ' '
1261
+ let bid = -1
1261
1262
if get (a: opts , ' safe' , get (g: , ' asyncrun_term_safe' , 0 )) != 0
1262
1263
let command = s: ScriptWrite (command , 0 )
1263
1264
if stridx (command , ' ' ) >= 0
@@ -1352,6 +1353,7 @@ function! s:terminal_init(opts)
1352
1353
setlocal nonumber signcolumn = no norelativenumber
1353
1354
let b: asyncrun_cmd = a: opts .cmd
1354
1355
let b: asyncrun_name = get (a: opts , ' name' , ' ' )
1356
+ let b: asyncrun_bid = bid
1355
1357
if get (a: opts , ' listed' , 1 ) == 0
1356
1358
setlocal nobuflisted
1357
1359
endif
@@ -1386,7 +1388,9 @@ function! s:terminal_init(opts)
1386
1388
let info.processid = processid
1387
1389
let info.close = get (a: opts , ' close' , 0 )
1388
1390
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
1390
1394
return pid
1391
1395
endfunc
1392
1396
@@ -1489,6 +1493,27 @@ function! s:term_alive(bid)
1489
1493
endfunc
1490
1494
1491
1495
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
+
1492
1517
" ----------------------------------------------------------------------
1493
1518
" get a proper name
1494
1519
" ----------------------------------------------------------------------
@@ -1555,21 +1580,10 @@ function! s:start_in_terminal(opts)
1555
1580
let a: opts ._terminal_wipe = -1
1556
1581
for ii in range (winnr (' $' ))
1557
1582
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
1573
1587
endif
1574
1588
endfor
1575
1589
let focus = get (a: opts , ' focus' , 1 )
@@ -1584,20 +1598,9 @@ function! s:start_in_terminal(opts)
1584
1598
for i in range (tabpagenr (' $' ))
1585
1599
if tabpagewinnr (i + 1 , ' $' ) == 1
1586
1600
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
1601
1604
endif
1602
1605
endif
1603
1606
endfor
@@ -2232,7 +2235,7 @@ endfunc
2232
2235
" asyncrun - version
2233
2236
" ----------------------------------------------------------------------
2234
2237
function ! asyncrun#version ()
2235
- return ' 2.11.12 '
2238
+ return ' 2.11.13 '
2236
2239
endfunc
2237
2240
2238
2241
0 commit comments