Skip to content

Commit

Permalink
term: add an option to reuse terminal windows
Browse files Browse the repository at this point in the history
Closes #2510
  • Loading branch information
bhcleek committed Aug 30, 2020
1 parent dd9b914 commit 076bef0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions autoload/go/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function! go#config#TermCloseOnExit() abort
return get(g:, 'go_term_close_on_exit', 1)
endfunction

function! go#config#TermReuse() abort
return get(g:, 'go_term_reuse', 0)
endfunction

function! go#config#SetTermCloseOnExit(value) abort
let g:go_term_close_on_exit = a:value
endfunction
Expand Down
52 changes: 51 additions & 1 deletion autoload/go/term.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
let s:cpo_save = &cpo
set cpo&vim

let s:bufnameprefix = 'goterm://'

" new creates a new terminal with the given command. Mode is set based on the
" global variable g:go_term_mode, which is by default set to :vsplit
function! go#term#new(bang, cmd, errorformat) abort
Expand All @@ -15,6 +17,10 @@ function! go#term#newmode(bang, cmd, errorformat, mode) abort
let l:mode = go#config#TermMode()
endif

if go#config#TermReuse()
call s:closeterm()
endif

let l:state = {
\ 'cmd': a:cmd,
\ 'bang' : a:bang,
Expand Down Expand Up @@ -48,6 +54,7 @@ function! go#term#newmode(bang, cmd, errorformat, mode) abort
\ }
let l:state.id = termopen(a:cmd, l:job)
let l:state.termwinid = win_getid(winnr())
let s:lasttermwinid = l:state.termwinid
call go#util#Chdir(l:dir)

" resize new term if needed.
Expand All @@ -66,18 +73,24 @@ function! go#term#newmode(bang, cmd, errorformat, mode) abort

" setup term for vim8
elseif has('terminal')
" Not great randomness, but "good enough" for our purpose here.
let l:rnd = sha256(printf('%s%s', reltimestr(reltime()), fnamemodify(bufname(''), ":p")))
let l:termname = printf("%s%s", s:bufnameprefix, l:rnd)

let l:term = {
\ 'out_cb': function('s:out_cb', [], state),
\ 'exit_cb' : function('s:exit_cb', [], state),
\ 'curwin': 1,
\ 'term_name': l:termname,
\ }

if l:mode =~ "vertical" || l:mode =~ "vsplit" || l:mode =~ "vnew"
let l:term["vertical"] = l:mode
let l:term["vertical"] = l:mode
endif

let l:state.id = term_start(a:cmd, l:term)
let l:state.termwinid = win_getid(bufwinnr(l:state.id))
let s:lasttermwinid = l:state.termwinid
call go#util#Chdir(l:dir)

" resize new term if needed.
Expand Down Expand Up @@ -217,6 +230,43 @@ function! go#term#ToggleCloseOnExit() abort
return
endfunction

function! s:closeterm()
if !exists('s:lasttermwinid')
return
endif

try
let l:termwinid = s:lasttermwinid
unlet s:lasttermwinid
let l:info = getwininfo(l:termwinid)
if empty(l:info)
return
endif

let l:info = l:info[0]

if !get(l:info, 'terminal', 0) is 1
return
endif

if has('nvim')
if 'goterm' == nvim_buf_get_option(nvim_win_get_buf(l:termwinid), 'filetype')
call nvim_win_close(l:termwinid, v:true)
endif
return
endif

if stridx(bufname(winbufnr(l:termwinid)), s:bufnameprefix, 0) == 0
let l:winid = win_getid()
call win_gotoid(l:termwinid)
close!
call win_gotoid(l:winid)
endif
catch
call go#util#EchoError(printf("vim-go: %s" v:exception))
endtry
endfunction

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
7 changes: 7 additions & 0 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,13 @@ The default command used to open a new terminal for go commands such as
Applicable to Neovim and Vim with `terminal` feature only.
>
let g:go_term_mode = "vsplit"
<
*'g:go_term_reuse'*

Reuse the terminal window when |g:go_term_enabled| is set. By default it's
disabled.
>
let g:go_term_reuse = 0
<
*'g:go_term_height'*
*'g:go_term_width'*
Expand Down

0 comments on commit 076bef0

Please sign in to comment.