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/19 01:07
6
+ " Last Modified: 2022/11/20 22:01
7
7
"
8
8
" Run shell command in background and output to quickfix:
9
9
" :AsyncRun[!] [options] {cmd} ...
@@ -1300,6 +1300,9 @@ function! s:terminal_init(opts)
1300
1300
let opts.cwd = cwd
1301
1301
endif
1302
1302
endif
1303
+ if has (' patch-8.1.1630' )
1304
+ let opts.term_name = s: term_new_name (a: opts )
1305
+ endif
1303
1306
try
1304
1307
let bid = term_start (command , opts)
1305
1308
catch /^.*/
@@ -1400,6 +1403,14 @@ function! s:terminal_open(opts)
1400
1403
if a: opts .cwd != ' '
1401
1404
silent ! call s: chdir (previous )
1402
1405
endif
1406
+ if get (a: opts , ' reuse' , 0 )
1407
+ let bid = get (a: opts , ' _terminal_wipe' , -1 )
1408
+ if bid > 0
1409
+ if bufexists (bid)
1410
+ silent ! exec ' bw ' . bid
1411
+ endif
1412
+ endif
1413
+ endif
1403
1414
return hr
1404
1415
endfunc
1405
1416
@@ -1447,6 +1458,65 @@ function! s:terminal_exit(...)
1447
1458
endfunc
1448
1459
1449
1460
1461
+ " ----------------------------------------------------------------------
1462
+ " check terminal is still running
1463
+ " ----------------------------------------------------------------------
1464
+ function ! s: term_alive (bid)
1465
+ if getbufvar (a: bid , ' &buftype' ) != ' terminal'
1466
+ return 0
1467
+ endif
1468
+ if has (' nvim' ) == 0
1469
+ return (term_getstatus (a: bid ) == ' finished' )? 0 : 1
1470
+ else
1471
+ let ch = getbufvar (a: bid , ' &channel' )
1472
+ let status = (jobwait ([ch ], 0 )[0 ] == -1 )? 1 : 0
1473
+ return (status == 0 )? 0 : 1
1474
+ endif
1475
+ return 0
1476
+ endfunc
1477
+
1478
+
1479
+ " ----------------------------------------------------------------------
1480
+ " get a proper name
1481
+ " ----------------------------------------------------------------------
1482
+ function ! s: term_new_name (opts)
1483
+ let command = a: opts .cmd
1484
+ let name = ' !' . command
1485
+ let wipe = get (a: opts , ' _terminal_wipe' , -1 )
1486
+ if ! bufexists (name)
1487
+ return name
1488
+ elseif get (a: opts , ' reuse' , 0 )
1489
+ let bid = bufnr (name)
1490
+ if bid == bufnr (' %' )
1491
+ unsilent echom ' match'
1492
+ if ! s: term_alive (bid)
1493
+ if wipe == bid
1494
+ return name
1495
+ endif
1496
+ endif
1497
+ endif
1498
+ endif
1499
+ let index = 1
1500
+ while 1
1501
+ let test = printf (' %s (%d)' , name, index )
1502
+ if bufnr (test) < 0
1503
+ return test
1504
+ elseif get (a: opts , ' reuse' , 0 )
1505
+ let bid = bufnr (test)
1506
+ if bid == bufnr (' %' )
1507
+ if ! s: term_alive (bid)
1508
+ if wipe == bid
1509
+ return test
1510
+ endif
1511
+ endif
1512
+ endif
1513
+ endif
1514
+ let index += 1
1515
+ endwhile
1516
+ return name
1517
+ endfunc
1518
+
1519
+
1450
1520
" ----------------------------------------------------------------------
1451
1521
" run in a terminal
1452
1522
" ----------------------------------------------------------------------
@@ -1457,6 +1527,7 @@ function! s:start_in_terminal(opts)
1457
1527
return -1
1458
1528
endif
1459
1529
let avail = -1
1530
+ let a: opts ._terminal_wipe = -1
1460
1531
for ii in range (winnr (' $' ))
1461
1532
let wid = ii + 1
1462
1533
if getwinvar (wid, ' &bt' ) == ' terminal'
@@ -1512,6 +1583,7 @@ function! s:start_in_terminal(opts)
1512
1583
endif
1513
1584
else
1514
1585
exec ' tabn ' . avail
1586
+ let a: opts ._terminal_wipe = bufnr (' %' )
1515
1587
endif
1516
1588
endif
1517
1589
let hr = s: terminal_open (a: opts )
@@ -1554,6 +1626,7 @@ function! s:start_in_terminal(opts)
1554
1626
endif
1555
1627
if avail > 0
1556
1628
exec " normal! " . avail . " \<c-w>\<c-w> "
1629
+ let a: opts ._terminal_wipe = bufnr (' %' )
1557
1630
endif
1558
1631
let uid = win_getid ()
1559
1632
keepalt noautocmd call win_gotoid (origin)
@@ -2124,7 +2197,7 @@ endfunc
2124
2197
" asyncrun - version
2125
2198
" ----------------------------------------------------------------------
2126
2199
function ! asyncrun#version ()
2127
- return ' 2.11.3 '
2200
+ return ' 2.11.4 '
2128
2201
endfunc
2129
2202
2130
2203
0 commit comments