2018-12-17 06:28:27 -05:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2015-12-16 08:53:53 -05:00
|
|
|
" 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
|
2019-05-17 10:09:13 -04:00
|
|
|
function! go#term#new(bang, cmd, errorformat) abort
|
|
|
|
return go#term#newmode(a:bang, a:cmd, a:errorformat, go#config#TermMode())
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
" go#term#newmode creates a new terminal with the given command and window mode.
|
|
|
|
function! go#term#newmode(bang, cmd, errorformat, mode) abort
|
|
|
|
let l:mode = a:mode
|
|
|
|
if empty(l:mode)
|
|
|
|
let l:mode = go#config#TermMode()
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:state = {
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'cmd': a:cmd,
|
|
|
|
\ 'bang' : a:bang,
|
|
|
|
\ 'winid': win_getid(winnr()),
|
2019-05-17 10:09:13 -04:00
|
|
|
\ 'stdout': [],
|
|
|
|
\ 'stdout_buf': '',
|
|
|
|
\ 'errorformat': a:errorformat,
|
2018-03-31 10:56:26 -04:00
|
|
|
\ }
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" execute go build in the files directory
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let l:dir = getcwd()
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
execute l:cd . fnameescape(expand("%:p:h"))
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
execute l:mode . ' __go_term__'
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
setlocal filetype=goterm
|
|
|
|
setlocal bufhidden=delete
|
|
|
|
setlocal winfixheight
|
|
|
|
setlocal noswapfile
|
|
|
|
setlocal nobuflisted
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
" explicitly bind callbacks to state so that within them, self will always
|
|
|
|
" refer to state. See :help Partial for more information.
|
|
|
|
"
|
|
|
|
" Don't set an on_stderr, because it will be passed the same data as
|
|
|
|
" on_stdout. See https://github.com/neovim/neovim/issues/2836
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:job = {
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'on_stdout': function('s:on_stdout', [], state),
|
|
|
|
\ 'on_exit' : function('s:on_exit', [], state),
|
|
|
|
\ }
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:state.id = termopen(a:cmd, l:job)
|
|
|
|
let l:state.termwinid = win_getid(winnr())
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
execute l:cd . fnameescape(l:dir)
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" resize new term if needed.
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:height = go#config#TermHeight()
|
|
|
|
let l:width = go#config#TermWidth()
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
" Adjust the window width or height depending on whether it's a vertical or
|
|
|
|
" horizontal split.
|
2019-05-17 10:09:13 -04:00
|
|
|
if l:mode =~ "vertical" || l:mode =~ "vsplit" || l:mode =~ "vnew"
|
|
|
|
exe 'vertical resize ' . l:width
|
2018-02-04 06:35:08 -05:00
|
|
|
elseif mode =~ "split" || mode =~ "new"
|
2019-05-17 10:09:13 -04:00
|
|
|
exe 'resize ' . l:height
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" we also need to resize the pty, so there you go...
|
2019-05-17 10:09:13 -04:00
|
|
|
call jobresize(l:state.id, l:width, l:height)
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
call win_gotoid(l:state.winid)
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
return l:state.id
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:on_stdout(job_id, data, event) dict abort
|
2019-05-17 10:09:13 -04:00
|
|
|
" A single empty string means EOF was reached. The first item will never be
|
|
|
|
" the empty string except for when it's the only item and is signaling that
|
|
|
|
" EOF was reached.
|
|
|
|
if len(a:data) == 1 && a:data[0] == ''
|
|
|
|
" when there's nothing buffered, return early so that an
|
|
|
|
" erroneous message will not be added.
|
|
|
|
if self.stdout_buf == ''
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let self.stdout = add(self.stdout, self.stdout_buf)
|
|
|
|
else
|
|
|
|
let l:data = copy(a:data)
|
|
|
|
let l:data[0] = self.stdout_buf . l:data[0]
|
|
|
|
|
|
|
|
" The last element may be a partial line; save it for next time.
|
|
|
|
let self.stdout_buf = l:data[-1]
|
|
|
|
let self.stdout = extend(self.stdout, l:data[:-2])
|
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:on_exit(job_id, exit_status, event) dict abort
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:winid = win_getid(winnr())
|
|
|
|
call win_gotoid(self.winid)
|
2017-11-24 08:54:40 -05:00
|
|
|
let l:listtype = go#list#Type("_term")
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
if a:exit_status == 0
|
|
|
|
call go#list#Clean(l:listtype)
|
|
|
|
call win_gotoid(l:winid)
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
call win_gotoid(self.winid)
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:title = self.cmd
|
|
|
|
if type(l:title) == v:t_list
|
|
|
|
let l:title = join(self.cmd)
|
|
|
|
endif
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:i = 0
|
|
|
|
while l:i < len(self.stdout)
|
|
|
|
let self.stdout[l:i] = substitute(self.stdout[l:i], "\r$", '', 'g')
|
|
|
|
let l:i += 1
|
|
|
|
endwhile
|
2018-03-31 10:56:26 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
call go#list#ParseFormat(l:listtype, self.errorformat, self.stdout, l:title)
|
|
|
|
let l:errors = go#list#Get(l:listtype)
|
|
|
|
call go#list#Window(l:listtype, len(l:errors))
|
2018-03-31 10:56:26 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
if empty(l:errors)
|
|
|
|
call go#util#EchoError( '[' . l:title . '] ' . "FAIL")
|
|
|
|
call win_gotoid(l:winid)
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
" close terminal; we don't need it anymore
|
|
|
|
call win_gotoid(self.termwinid)
|
|
|
|
close!
|
|
|
|
|
|
|
|
if self.bang
|
|
|
|
call win_gotoid(l:winid)
|
|
|
|
return
|
|
|
|
endif
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
call win_gotoid(self.winid)
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|