2015-12-16 08:53:53 -05:00
|
|
|
if has('nvim') && !exists("g:go_term_mode")
|
2016-06-26 07:12:36 -04:00
|
|
|
let g:go_term_mode = 'vsplit'
|
2015-12-16 08:53:53 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
" 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
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#term#new(bang, cmd) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
return go#term#newmode(a:bang, a:cmd, g:go_term_mode)
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" new creates a new terminal with the given command and window mode.
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#term#newmode(bang, cmd, mode) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let mode = a:mode
|
|
|
|
if empty(mode)
|
|
|
|
let mode = g:go_term_mode
|
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let state = {
|
|
|
|
\ 'cmd': a:cmd,
|
|
|
|
\ 'bang' : a:bang,
|
|
|
|
\ 'winid': win_getid(winnr()),
|
|
|
|
\ 'stdout': []
|
|
|
|
\ }
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" execute go build in the files directory
|
|
|
|
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
|
|
|
|
let dir = getcwd()
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
execute cd . fnameescape(expand("%:p:h"))
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
execute 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
|
2016-12-27 09:46:49 -05:00
|
|
|
let 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
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let state.id = termopen(a:cmd, job)
|
|
|
|
let state.termwinid = win_getid(winnr())
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
execute cd . fnameescape(dir)
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" resize new term if needed.
|
|
|
|
let height = get(g:, 'go_term_height', winheight(0))
|
|
|
|
let width = get(g:, 'go_term_width', winwidth(0))
|
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.
|
2018-02-04 06:35:08 -05:00
|
|
|
if mode =~ "vertical" || mode =~ "vsplit" || mode =~ "vnew"
|
2016-06-26 07:12:36 -04:00
|
|
|
exe 'vertical resize ' . width
|
2018-02-04 06:35:08 -05:00
|
|
|
elseif mode =~ "split" || mode =~ "new"
|
|
|
|
exe 'resize ' . 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...
|
2018-03-31 10:56:26 -04:00
|
|
|
call jobresize(state.id, width, height)
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
call win_gotoid(state.winid)
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
return 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
|
2018-03-31 10:56:26 -04:00
|
|
|
call extend(self.stdout, a:data)
|
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
|
2017-11-24 08:54:40 -05:00
|
|
|
let l:listtype = go#list#Type("_term")
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" usually there is always output so never branch into this clause
|
2018-03-31 10:56:26 -04:00
|
|
|
if empty(self.stdout)
|
|
|
|
call s:cleanlist(self.winid, l:listtype)
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let errors = go#tool#ParseErrors(self.stdout)
|
2016-06-26 07:12:36 -04:00
|
|
|
let errors = go#tool#FilterValids(errors)
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if !empty(errors)
|
2018-03-31 10:56:26 -04:00
|
|
|
" close terminal; we don't need it anymore
|
|
|
|
call win_gotoid(self.termwinid)
|
2016-12-27 09:46:49 -05:00
|
|
|
close
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
call win_gotoid(self.winid)
|
|
|
|
|
|
|
|
call go#list#Populate(l:listtype, errors, self.cmd)
|
2016-06-26 07:12:36 -04:00
|
|
|
call go#list#Window(l:listtype, len(errors))
|
|
|
|
if !self.bang
|
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2015-12-16 08:53:53 -05:00
|
|
|
endif
|
2018-03-31 10:56:26 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return
|
|
|
|
endif
|
2015-12-16 08:53:53 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
call s:cleanlist(self.winid, l:listtype)
|
|
|
|
endfunction
|
2016-04-12 04:31:09 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:cleanlist(winid, listtype) abort
|
|
|
|
" There are no errors. Clean and close the list. Jump to the window to which
|
|
|
|
" the location list is attached, close the list, and then jump back to the
|
|
|
|
" current window.
|
|
|
|
let winid = win_getid(winnr())
|
|
|
|
call win_gotoid(a:winid)
|
|
|
|
call go#list#Clean(a:listtype)
|
|
|
|
call win_gotoid(l:winid)
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|