Skip to content

Commit 18a0d80

Browse files
author
skywind3000
committed
new: parameter -hidden when using -mode=term to set bufhidden to hidden
improve: keep alternative window when splitting an terminal window improve: more safe to start a terminal in a new tab
1 parent 3cb6226 commit 18a0d80

File tree

2 files changed

+61
-41
lines changed

2 files changed

+61
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ See: [Cooperate with famous plugins](https://github.com/skywind3000/asyncrun.vim
288288

289289
## History
290290

291+
- 2.2.6 (2020-02-06): new: parameter `-hidden` when using `-mode=term` to set `bufhidden` to `hidden`.
291292
- 2.2.5 (2020-02-05): more safe to start a terminal.
292293
- 2.2.4 (2020-02-05): exit when starting terminal failed in current window with `-pos=curwin`.
293294
- 2.2.3 (2020-02-05): new `-program=wsl` to run command in wsl (windows 10 only).

plugin/asyncrun.vim

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Maintainer: skywind3000 (at) gmail.com, 2016, 2017, 2018, 2019, 2020
44
" Homepage: http://www.vim.org/scripts/script.php?script_id=5431
55
"
6-
" Last Modified: 2020/02/05 06:36
6+
" Last Modified: 2020/02/06 14:43
77
"
88
" Run shell command in background and output to quickfix:
99
" :AsyncRun[!] [options] {cmd} ...
@@ -44,8 +44,10 @@
4444
" $VIM_COLUMNS - How many columns in vim's screen
4545
" $VIM_LINES - How many lines in vim's screen
4646
"
47-
" parameters also accept these environment variables wrapped by
48-
" "$(...)", and "$(VIM_FILEDIR)" will be expanded as file directory
47+
" Parameters also accept these environment variables wrapped by
48+
" "$(...)", and "$(VIM_FILEDIR)" will be expanded as file directory.
49+
"
50+
" It is safe to use "$(...)" than "%:xx" when filenames contain spaces.
4951
"
5052
" There can be some options before [cmd]:
5153
" -mode=0/1/2 - start mode: 0(async, default), 1(makeprg), 2(!)
@@ -83,15 +85,25 @@
8385
"
8486
" Requirements:
8587
" vim 7.4.1829 is minimal version to support async mode
88+
" vim 8.1.1 is minial version to use "-mode=term"
8689
"
8790
" Examples:
8891
" :AsyncRun gcc % -o %<
8992
" :AsyncRun make
90-
" :AsyncRun -raw python $(VIM_FILEPATH)
93+
" :AsyncRun -raw -cwd=$(VIM_FILEDIR) python "$(VIM_FILEPATH)"
9194
" :AsyncRun -cwd=<root> make
92-
" :AsyncRun! grep -R <cword> .
95+
" :AsyncRun! grep -n -R <cword> .
9396
" :noremap <F7> :AsyncRun gcc % -o %< <cr>
9497
"
98+
" Run in the internal terminal:
99+
" :AsyncRun -mode=term bash
100+
" :AsyncRun -mode=term -pos=tab bash
101+
" :AsyncRun -mode=term -pos=curwin bash
102+
" :AsyncRun -mode=term -pos=top -rows=15 bash
103+
" :AsyncRun -mode=term -pos=bottom -rows=15 bash
104+
" :AsyncRun -mode=term -pos=left -cols=40 bash
105+
" :AsyncRun -mode=term -pos=right -cols=40 bash
106+
"
95107
" Additional:
96108
" AsyncRun uses quickfix window to show job outputs, in order to
97109
" see the outputs in realtime, you need open quickfix window at
@@ -1061,6 +1073,7 @@ endfunc
10611073
function! s:start_in_terminal(opts)
10621074
let command = a:opts.command
10631075
let pos = get(a:opts, 'pos', 'bottom')
1076+
let hidden = get(a:opts, 'hidden', 0)
10641077
if has('patch-8.1.1') == 0 && has('nvim-0.3') == 0
10651078
call s:ErrorMsg("Terminal is not available in this vim")
10661079
return -1
@@ -1083,47 +1096,50 @@ function! s:start_in_terminal(opts)
10831096
endif
10841097
endfor
10851098
if pos == 'tab'
1099+
exec "tab split"
10861100
if has('nvim') == 0
1087-
let cmd = 'tab term ++noclose ++norestore'
1101+
let cmd = 'tab term ++noclose ++norestore ++curwin'
10881102
if has('patch-8.1.2255') || v:version >= 802
1089-
exec cmd . ' ++shell ' . command
1103+
exec cmd . ' ++shell ++kill=term ' . command
10901104
else
1091-
exec cmd . ' ' . command
1092-
endif
1093-
if &bt == 'terminal'
1094-
setlocal nonumber signcolumn=no norelativenumber
1105+
exec cmd . ' ++kill=term ' . command
10951106
endif
10961107
else
1097-
exec 'tabe term://'. fnameescape(command)
1098-
if &bt == 'terminal'
1099-
setlocal nonumber signcolumn=no norelativenumber
1100-
startinsert
1108+
exec 'term '. command
1109+
endif
1110+
if &bt == 'terminal'
1111+
setlocal nonumber signcolumn=no norelativenumber
1112+
let b:asyncrun_cmd = command
1113+
exec has('nvim')? 'startinsert' : ''
1114+
if has_key(a:opts, 'hidden')
1115+
exec 'setlocal bufhidden=' .. (hidden? 'hide' : '')
11011116
endif
11021117
endif
11031118
return 0
11041119
elseif pos == 'cur' || pos == 'curwin' || pos == 'current'
11051120
if has('nvim') == 0
11061121
let cmd = 'term ++noclose ++norestore ++curwin'
11071122
if has('patch-8.1.2255') || v:version >= 802
1108-
exec cmd . ' ++shell ' . command
1123+
exec cmd . ' ++shell ++kill=term ' . command
11091124
else
1110-
exec cmd . ' ' . command
1111-
endif
1112-
if &bt == 'terminal'
1113-
setlocal nonumber signcolumn=no norelativenumber
1125+
exec cmd . ' ++kill=term ' . command
11141126
endif
11151127
else
11161128
exec 'term '. command
1117-
if &bt == 'terminal'
1118-
setlocal nonumber signcolumn=no norelativenumber
1119-
startinsert
1129+
endif
1130+
if &bt == 'terminal'
1131+
setlocal nonumber signcolumn=no norelativenumber
1132+
let b:asyncrun_cmd = command
1133+
exec has('nvim')? 'startinsert' : ''
1134+
if has_key(a:opts, 'hidden')
1135+
exec 'setlocal bufhidden=' .. (hidden? 'hide' : '')
11201136
endif
11211137
endif
11221138
return 0
11231139
endif
11241140
let uid = win_getid()
1125-
noautocmd windo call s:save_restore_view(0)
1126-
noautocmd call win_gotoid(uid)
1141+
keepalt noautocmd windo call s:save_restore_view(0)
1142+
keepalt noautocmd call win_gotoid(uid)
11271143
let focus = get(a:opts, 'focus', 1)
11281144
let origin = win_getid()
11291145
if avail < 0
@@ -1145,7 +1161,9 @@ function! s:start_in_terminal(opts)
11451161
exec "normal! ". avail . "\<c-w>\<c-w>"
11461162
endif
11471163
let uid = win_getid()
1148-
noautocmd windo call s:save_restore_view(1)
1164+
keepalt noautocmd call win_gotoid(origin)
1165+
keepalt noautocmd windo call s:save_restore_view(1)
1166+
keepalt noautocmd call win_gotoid(origin)
11491167
noautocmd call win_gotoid(uid)
11501168
if has('nvim') == 0
11511169
let cmd = 'term ++noclose ++norestore ++curwin '
@@ -1154,14 +1172,15 @@ function! s:start_in_terminal(opts)
11541172
else
11551173
exec cmd . ' ++kill=term ' . command
11561174
endif
1157-
if &bt == 'terminal'
1158-
setlocal nonumber signcolumn=no norelativenumber
1159-
endif
11601175
else
11611176
exec 'term '. command
1162-
if &bt == 'terminal'
1163-
setlocal nonumber signcolumn=no norelativenumber
1164-
startinsert
1177+
endif
1178+
if &bt == 'terminal'
1179+
setlocal nonumber signcolumn=no norelativenumber
1180+
let b:asyncrun_cmd = command
1181+
exec has('nvim')? 'startinsert' : ''
1182+
if has_key(a:opts, 'hidden')
1183+
exec 'setlocal bufhidden=' .. (hidden? 'hide' : '')
11651184
endif
11661185
endif
11671186
if focus == 0 && &bt == 'terminal'
@@ -1570,7 +1589,7 @@ endfunc
15701589
" asyncrun -version
15711590
"----------------------------------------------------------------------
15721591
function! asyncrun#version()
1573-
return '2.2.5'
1592+
return '2.2.6'
15741593
endfunc
15751594

15761595

@@ -1605,27 +1624,27 @@ function! asyncrun#quickfix_toggle(size, ...)
16051624
endfunc
16061625
let s:quickfix_open = 0
16071626
let l:winnr = winnr()
1608-
noautocmd windo call s:WindowCheck(0)
1609-
noautocmd silent! exec ''.l:winnr.'wincmd w'
1627+
keepalt noautocmd windo call s:WindowCheck(0)
1628+
keepalt noautocmd silent! exec ''.l:winnr.'wincmd w'
16101629
if l:mode == 0
16111630
if s:quickfix_open != 0
16121631
silent! cclose
16131632
endif
16141633
elseif l:mode == 1
16151634
if s:quickfix_open == 0
1616-
exec 'botright copen '. ((a:size > 0)? a:size : ' ')
1617-
wincmd k
1635+
keepalt exec 'botright copen '. ((a:size > 0)? a:size : ' ')
1636+
keepalt wincmd k
16181637
endif
16191638
elseif l:mode == 2
16201639
if s:quickfix_open == 0
1621-
exec 'botright copen '. ((a:size > 0)? a:size : ' ')
1622-
wincmd k
1640+
keepalt exec 'botright copen '. ((a:size > 0)? a:size : ' ')
1641+
keepalt wincmd k
16231642
else
16241643
silent! cclose
16251644
endif
16261645
endif
1627-
noautocmd windo call s:WindowCheck(1)
1628-
noautocmd silent! exec ''.l:winnr.'wincmd w'
1646+
keepalt noautocmd windo call s:WindowCheck(1)
1647+
keepalt noautocmd silent! exec ''.l:winnr.'wincmd w'
16291648
endfunc
16301649

16311650

0 commit comments

Comments
 (0)