Skip to content

Commit 8820e7a

Browse files
author
skywind3000
committed
terminal: initialize a proper terminal name for vim-8.2 and above
terminal: wipe previous finished terminal buffer when '-reuse' is presented
1 parent 6d5e981 commit 8820e7a

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

plugin/asyncrun.vim

Lines changed: 75 additions & 2 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/19 01:07
6+
" Last Modified: 2022/11/20 22:01
77
"
88
" Run shell command in background and output to quickfix:
99
" :AsyncRun[!] [options] {cmd} ...
@@ -1300,6 +1300,9 @@ function! s:terminal_init(opts)
13001300
let opts.cwd = cwd
13011301
endif
13021302
endif
1303+
if has('patch-8.1.1630')
1304+
let opts.term_name = s:term_new_name(a:opts)
1305+
endif
13031306
try
13041307
let bid = term_start(command, opts)
13051308
catch /^.*/
@@ -1400,6 +1403,14 @@ function! s:terminal_open(opts)
14001403
if a:opts.cwd != ''
14011404
silent! call s:chdir(previous)
14021405
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
14031414
return hr
14041415
endfunc
14051416

@@ -1447,6 +1458,65 @@ function! s:terminal_exit(...)
14471458
endfunc
14481459

14491460

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+
14501520
"----------------------------------------------------------------------
14511521
" run in a terminal
14521522
"----------------------------------------------------------------------
@@ -1457,6 +1527,7 @@ function! s:start_in_terminal(opts)
14571527
return -1
14581528
endif
14591529
let avail = -1
1530+
let a:opts._terminal_wipe = -1
14601531
for ii in range(winnr('$'))
14611532
let wid = ii + 1
14621533
if getwinvar(wid, '&bt') == 'terminal'
@@ -1512,6 +1583,7 @@ function! s:start_in_terminal(opts)
15121583
endif
15131584
else
15141585
exec 'tabn ' . avail
1586+
let a:opts._terminal_wipe = bufnr('%')
15151587
endif
15161588
endif
15171589
let hr = s:terminal_open(a:opts)
@@ -1554,6 +1626,7 @@ function! s:start_in_terminal(opts)
15541626
endif
15551627
if avail > 0
15561628
exec "normal! ". avail . "\<c-w>\<c-w>"
1629+
let a:opts._terminal_wipe = bufnr('%')
15571630
endif
15581631
let uid = win_getid()
15591632
keepalt noautocmd call win_gotoid(origin)
@@ -2124,7 +2197,7 @@ endfunc
21242197
" asyncrun - version
21252198
"----------------------------------------------------------------------
21262199
function! asyncrun#version()
2127-
return '2.11.3'
2200+
return '2.11.4'
21282201
endfunc
21292202

21302203

0 commit comments

Comments
 (0)