2019-03-25 07:27:10 -04:00
|
|
|
" MIT License. Copyright (c) 2013-2019 Christian Brabandt et al.
|
2017-08-22 17:21:19 -04:00
|
|
|
" vim: et ts=2 sts=2 sw=2
|
|
|
|
|
2018-01-05 04:18:22 -05:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2017-08-22 17:21:19 -04:00
|
|
|
let s:untracked_jobs = {}
|
|
|
|
let s:mq_jobs = {}
|
|
|
|
let s:po_jobs = {}
|
2019-04-24 09:15:29 -04:00
|
|
|
let s:clean_jobs = {}
|
2017-08-22 17:21:19 -04:00
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
" Generic functions handling on exit event of the various async functions
|
2017-08-22 17:21:19 -04:00
|
|
|
function! s:untracked_output(dict, buf)
|
|
|
|
if a:buf =~? ('^'. a:dict.cfg['untracked_mark'])
|
|
|
|
let a:dict.cfg.untracked[a:dict.file] = get(g:, 'airline#extensions#branch#notexists', g:airline_symbols.notexists)
|
|
|
|
else
|
|
|
|
let a:dict.cfg.untracked[a:dict.file] = ''
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2017-09-22 15:39:17 -04:00
|
|
|
" also called from branch extension (for non-async vims)
|
2017-09-22 15:37:28 -04:00
|
|
|
function! airline#async#mq_output(buf, file)
|
2017-08-29 06:47:29 -04:00
|
|
|
let buf=a:buf
|
2017-08-22 17:21:19 -04:00
|
|
|
if !empty(a:buf)
|
2017-09-22 15:37:28 -04:00
|
|
|
if a:buf =~# 'no patches applied' ||
|
2017-09-22 15:39:17 -04:00
|
|
|
\ a:buf =~# "unknown command 'qtop'" ||
|
|
|
|
\ a:buf =~# "abort"
|
2017-08-22 17:21:19 -04:00
|
|
|
let buf = ''
|
2017-08-29 06:47:29 -04:00
|
|
|
elseif exists("b:mq") && b:mq isnot# buf
|
2017-08-22 17:21:19 -04:00
|
|
|
" make sure, statusline is updated
|
|
|
|
unlet! b:airline_head
|
|
|
|
endif
|
2017-08-29 06:47:29 -04:00
|
|
|
let b:mq = buf
|
2017-08-22 17:21:19 -04:00
|
|
|
endif
|
|
|
|
if has_key(s:mq_jobs, a:file)
|
|
|
|
call remove(s:mq_jobs, a:file)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:po_output(buf, file)
|
|
|
|
if !empty(a:buf)
|
2018-11-01 05:39:33 -04:00
|
|
|
let b:airline_po_stats = printf("%s", a:buf)
|
2017-08-22 17:21:19 -04:00
|
|
|
else
|
|
|
|
let b:airline_po_stats = ''
|
|
|
|
endif
|
|
|
|
if has_key(s:po_jobs, a:file)
|
|
|
|
call remove(s:po_jobs, a:file)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2017-08-26 02:04:44 -04:00
|
|
|
function! s:valid_dir(dir)
|
|
|
|
if empty(a:dir) || !isdirectory(a:dir)
|
|
|
|
return getcwd()
|
|
|
|
endif
|
|
|
|
return a:dir
|
|
|
|
endfunction
|
|
|
|
|
2019-04-24 07:27:25 -04:00
|
|
|
function! airline#async#vcs_untracked(config, file, vcs)
|
|
|
|
if g:airline#init#vim_async
|
|
|
|
" Vim 8 with async support
|
|
|
|
noa call airline#async#vim_vcs_untracked(a:config, a:file)
|
|
|
|
else
|
|
|
|
" nvim async or vim without job-feature
|
|
|
|
noa call airline#async#nvim_vcs_untracked(a:config, a:file, a:vcs)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2019-05-08 11:22:34 -04:00
|
|
|
function! s:set_clean_variables(file, vcs, val)
|
2019-04-25 08:43:56 -04:00
|
|
|
let var=getbufvar(fnameescape(a:file), 'buffer_vcs_config', {})
|
2019-05-24 02:13:31 -04:00
|
|
|
if has_key(var, a:vcs) && has_key(var[a:vcs], 'dirty') &&
|
|
|
|
\ type(getbufvar(fnameescape(a:file), 'buffer_vcs_config')) == type({})
|
2019-05-08 11:22:34 -04:00
|
|
|
let var[a:vcs].dirty=a:val
|
2019-06-14 04:30:05 -04:00
|
|
|
try
|
2019-06-14 03:40:30 -04:00
|
|
|
call setbufvar(fnameescape(a:file), 'buffer_vcs_config', var)
|
|
|
|
unlet! b:airline_head
|
2019-06-14 04:30:05 -04:00
|
|
|
catch
|
|
|
|
endtry
|
2019-05-01 01:45:06 -04:00
|
|
|
endif
|
2019-04-25 08:43:56 -04:00
|
|
|
endfunction
|
|
|
|
|
2019-05-02 01:57:10 -04:00
|
|
|
function! s:set_clean_jobs_variable(vcs, file, id)
|
|
|
|
if !has_key(s:clean_jobs, a:vcs)
|
|
|
|
let s:clean_jobs[a:vcs] = {}
|
|
|
|
endif
|
|
|
|
let s:clean_jobs[a:vcs][a:file]=a:id
|
|
|
|
endfunction
|
|
|
|
|
2019-04-24 09:15:29 -04:00
|
|
|
function! s:on_exit_clean(...) dict abort
|
|
|
|
let buf=self.buf
|
2019-05-08 11:22:34 -04:00
|
|
|
call s:set_clean_variables(self.file, self.vcs, !empty(buf))
|
2019-05-02 01:57:10 -04:00
|
|
|
if has_key(get(s:clean_jobs, self.vcs, {}), self.file)
|
2019-04-24 09:15:29 -04:00
|
|
|
call remove(s:clean_jobs[self.vcs], self.file)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#async#vcs_clean(cmd, file, vcs)
|
|
|
|
if g:airline#init#vim_async
|
|
|
|
" Vim 8 with async support
|
|
|
|
noa call airline#async#vim_vcs_clean(a:cmd, a:file, a:vcs)
|
2019-04-25 09:07:12 -04:00
|
|
|
elseif has("nvim")
|
|
|
|
" nvim async
|
2019-04-24 09:15:29 -04:00
|
|
|
noa call airline#async#nvim_vcs_clean(a:cmd, a:file, a:vcs)
|
2019-04-25 09:07:12 -04:00
|
|
|
else
|
|
|
|
" Vim pre 8 using system()
|
|
|
|
call airline#async#vim7_vcs_clean(a:cmd, a:file, a:vcs)
|
2019-04-24 09:15:29 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
if v:version >= 800 && has("job")
|
|
|
|
" Vim 8.0 with Job feature
|
2018-01-04 17:29:15 -05:00
|
|
|
" TODO: Check if we need the cwd option for the job_start() functions
|
|
|
|
" (only works starting with Vim 8.0.0902)
|
2017-08-22 17:21:19 -04:00
|
|
|
|
|
|
|
function! s:on_stdout(channel, msg) dict abort
|
|
|
|
let self.buf .= a:msg
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:on_exit_mq(channel) dict abort
|
2017-09-22 15:37:28 -04:00
|
|
|
call airline#async#mq_output(self.buf, self.file)
|
2017-08-22 17:21:19 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:on_exit_untracked(channel) dict abort
|
|
|
|
call s:untracked_output(self, self.buf)
|
|
|
|
if has_key(s:untracked_jobs, self.file)
|
|
|
|
call remove(s:untracked_jobs, self.file)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:on_exit_po(channel) dict abort
|
|
|
|
call s:po_output(self.buf, self.file)
|
|
|
|
call airline#extensions#po#shorten()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#async#get_mq_async(cmd, file)
|
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
|
|
|
let cmd = a:cmd
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:cmd]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let options = {'cmd': a:cmd, 'buf': '', 'file': a:file}
|
|
|
|
if has_key(s:mq_jobs, a:file)
|
|
|
|
if job_status(get(s:mq_jobs, a:file)) == 'run'
|
|
|
|
return
|
|
|
|
elseif has_key(s:mq_jobs, a:file)
|
|
|
|
call remove(s:mq_jobs, a:file)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let id = job_start(cmd, {
|
|
|
|
\ 'err_io': 'out',
|
|
|
|
\ 'out_cb': function('s:on_stdout', options),
|
|
|
|
\ 'close_cb': function('s:on_exit_mq', options)})
|
|
|
|
let s:mq_jobs[a:file] = id
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#async#get_msgfmt_stat(cmd, file)
|
|
|
|
if g:airline#init#is_windows || !executable('msgfmt')
|
|
|
|
" no msgfmt on windows?
|
|
|
|
return
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:cmd. shellescape(a:file)]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let options = {'buf': '', 'file': a:file}
|
|
|
|
if has_key(s:po_jobs, a:file)
|
|
|
|
if job_status(get(s:po_jobs, a:file)) == 'run'
|
|
|
|
return
|
|
|
|
elseif has_key(s:po_jobs, a:file)
|
|
|
|
call remove(s:po_jobs, a:file)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let id = job_start(cmd, {
|
|
|
|
\ 'err_io': 'out',
|
|
|
|
\ 'out_cb': function('s:on_stdout', options),
|
|
|
|
\ 'close_cb': function('s:on_exit_po', options)})
|
|
|
|
let s:po_jobs[a:file] = id
|
|
|
|
endfunction
|
|
|
|
|
2019-04-24 09:15:29 -04:00
|
|
|
function! airline#async#vim_vcs_clean(cmd, file, vcs)
|
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
|
|
|
let cmd = a:cmd
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:cmd]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let options = {'buf': '', 'vcs': a:vcs, 'file': a:file}
|
|
|
|
let jobs = get(s:clean_jobs, a:vcs, {})
|
|
|
|
if has_key(jobs, a:file)
|
|
|
|
if job_status(get(jobs, a:file)) == 'run'
|
|
|
|
return
|
|
|
|
elseif has_key(jobs, a:file)
|
2019-06-03 02:21:39 -04:00
|
|
|
" still running
|
|
|
|
return
|
|
|
|
" jobs dict should be cleaned on exit, so not needed here
|
|
|
|
" call remove(jobs, a:file)
|
2019-04-24 09:15:29 -04:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let id = job_start(cmd, {
|
|
|
|
\ 'err_io': 'null',
|
|
|
|
\ 'out_cb': function('s:on_stdout', options),
|
|
|
|
\ 'close_cb': function('s:on_exit_clean', options)})
|
2019-05-02 01:57:10 -04:00
|
|
|
call s:set_clean_jobs_variable(a:vcs, a:file, id)
|
2019-04-24 09:15:29 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-06-13 12:52:13 -04:00
|
|
|
function! airline#async#vim_vcs_untracked(config, file)
|
2017-08-22 17:21:19 -04:00
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
|
|
|
let cmd = a:config['cmd'] . shellescape(a:file)
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:config['cmd'] . shellescape(a:file)]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let options = {'cfg': a:config, 'buf': '', 'file': a:file}
|
|
|
|
if has_key(s:untracked_jobs, a:file)
|
|
|
|
if job_status(get(s:untracked_jobs, a:file)) == 'run'
|
|
|
|
return
|
|
|
|
elseif has_key(s:untracked_jobs, a:file)
|
|
|
|
call remove(s:untracked_jobs, a:file)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let id = job_start(cmd, {
|
|
|
|
\ 'err_io': 'out',
|
|
|
|
\ 'out_cb': function('s:on_stdout', options),
|
|
|
|
\ 'close_cb': function('s:on_exit_untracked', options)})
|
|
|
|
let s:untracked_jobs[a:file] = id
|
|
|
|
endfunction
|
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
elseif has("nvim")
|
|
|
|
" NVim specific functions
|
2017-08-22 17:21:19 -04:00
|
|
|
|
2017-08-30 07:15:55 -04:00
|
|
|
function! s:nvim_output_handler(job_id, data, event) dict
|
|
|
|
if a:event == 'stdout' || a:event == 'stderr'
|
2017-08-22 17:42:27 -04:00
|
|
|
let self.buf .= join(a:data)
|
2017-08-30 07:15:55 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:nvim_untracked_job_handler(job_id, data, event) dict
|
|
|
|
if a:event == 'exit'
|
2017-08-22 17:42:27 -04:00
|
|
|
call s:untracked_output(self, self.buf)
|
|
|
|
if has_key(s:untracked_jobs, self.file)
|
|
|
|
call remove(s:untracked_jobs, self.file)
|
|
|
|
endif
|
2017-08-22 17:21:19 -04:00
|
|
|
endif
|
2017-08-22 17:42:27 -04:00
|
|
|
endfunction
|
2017-08-22 17:21:19 -04:00
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
function! s:nvim_mq_job_handler(job_id, data, event) dict
|
2017-08-30 07:15:55 -04:00
|
|
|
if a:event == 'exit'
|
2017-09-22 15:37:28 -04:00
|
|
|
call airline#async#mq_output(self.buf, self.file)
|
2017-08-22 17:42:27 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
2017-08-22 17:21:19 -04:00
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
function! s:nvim_po_job_handler(job_id, data, event) dict
|
2017-08-30 07:15:55 -04:00
|
|
|
if a:event == 'exit'
|
2017-08-22 17:42:27 -04:00
|
|
|
call s:po_output(self.buf, self.file)
|
|
|
|
call airline#extensions#po#shorten()
|
|
|
|
endif
|
|
|
|
endfunction
|
2017-08-22 17:21:19 -04:00
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
function! airline#async#nvim_get_mq_async(cmd, file)
|
|
|
|
let config = {
|
|
|
|
\ 'buf': '',
|
|
|
|
\ 'file': a:file,
|
2017-08-29 05:53:20 -04:00
|
|
|
\ 'cwd': s:valid_dir(fnamemodify(a:file, ':p:h')),
|
2017-08-30 07:15:55 -04:00
|
|
|
\ 'on_stdout': function('s:nvim_output_handler'),
|
|
|
|
\ 'on_stderr': function('s:nvim_output_handler'),
|
2017-08-22 17:42:27 -04:00
|
|
|
\ 'on_exit': function('s:nvim_mq_job_handler')
|
|
|
|
\ }
|
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
|
|
|
let cmd = a:cmd
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:cmd]
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(s:mq_jobs, a:file)
|
|
|
|
call remove(s:mq_jobs, a:file)
|
|
|
|
endif
|
|
|
|
let id = jobstart(cmd, config)
|
|
|
|
let s:mq_jobs[a:file] = id
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#async#nvim_get_msgfmt_stat(cmd, file)
|
|
|
|
let config = {
|
|
|
|
\ 'buf': '',
|
|
|
|
\ 'file': a:file,
|
2017-08-29 05:53:20 -04:00
|
|
|
\ 'cwd': s:valid_dir(fnamemodify(a:file, ':p:h')),
|
2017-08-30 07:15:55 -04:00
|
|
|
\ 'on_stdout': function('s:nvim_output_handler'),
|
|
|
|
\ 'on_stderr': function('s:nvim_output_handler'),
|
2017-08-22 17:42:27 -04:00
|
|
|
\ 'on_exit': function('s:nvim_po_job_handler')
|
|
|
|
\ }
|
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
2017-08-22 18:00:47 -04:00
|
|
|
" no msgfmt on windows?
|
|
|
|
return
|
2017-08-22 17:42:27 -04:00
|
|
|
else
|
2017-08-22 18:00:47 -04:00
|
|
|
let cmd = ['sh', '-c', a:cmd. shellescape(a:file)]
|
2017-08-22 17:42:27 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(s:po_jobs, a:file)
|
|
|
|
call remove(s:po_jobs, a:file)
|
|
|
|
endif
|
|
|
|
let id = jobstart(cmd, config)
|
|
|
|
let s:po_jobs[a:file] = id
|
|
|
|
endfunction
|
|
|
|
|
2019-04-25 09:07:12 -04:00
|
|
|
function! airline#async#nvim_vcs_clean(cmd, file, vcs)
|
|
|
|
let config = {
|
|
|
|
\ 'buf': '',
|
|
|
|
\ 'vcs': a:vcs,
|
|
|
|
\ 'file': a:file,
|
|
|
|
\ 'cwd': s:valid_dir(fnamemodify(a:file, ':p:h')),
|
|
|
|
\ 'on_stdout': function('s:nvim_output_handler'),
|
|
|
|
\ 'on_stderr': function('s:nvim_output_handler'),
|
|
|
|
\ 'on_exit': function('s:on_exit_clean')}
|
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
|
|
|
let cmd = a:cmd
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:cmd]
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !has_key(s:clean_jobs, a:vcs)
|
|
|
|
let s:clean_jobs[a:vcs] = {}
|
|
|
|
endif
|
|
|
|
if has_key(s:clean_jobs[a:vcs], a:file)
|
2019-06-03 02:21:39 -04:00
|
|
|
" still running
|
|
|
|
return
|
|
|
|
" jobs dict should be cleaned on exit, so not needed here
|
|
|
|
" call remove(s:clean_jobs[a:vcs], a:file)
|
2019-04-25 09:07:12 -04:00
|
|
|
endif
|
|
|
|
let id = jobstart(cmd, config)
|
2019-05-02 01:57:10 -04:00
|
|
|
call s:set_clean_jobs_variable(a:vcs, a:file, id)
|
2019-04-25 09:07:12 -04:00
|
|
|
endfunction
|
|
|
|
|
2017-08-22 17:42:27 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Should work in either Vim pre 8 or Nvim
|
2017-08-22 17:21:19 -04:00
|
|
|
function! airline#async#nvim_vcs_untracked(cfg, file, vcs)
|
|
|
|
let cmd = a:cfg.cmd . shellescape(a:file)
|
2017-08-25 10:48:45 -04:00
|
|
|
let id = -1
|
2017-08-25 12:50:15 -04:00
|
|
|
let config = {
|
|
|
|
\ 'buf': '',
|
|
|
|
\ 'vcs': a:vcs,
|
|
|
|
\ 'cfg': a:cfg,
|
|
|
|
\ 'file': a:file,
|
2017-08-26 02:04:44 -04:00
|
|
|
\ 'cwd': s:valid_dir(fnamemodify(a:file, ':p:h'))
|
2017-08-25 12:50:15 -04:00
|
|
|
\ }
|
2017-08-25 10:48:45 -04:00
|
|
|
if has("nvim")
|
2017-08-25 12:50:15 -04:00
|
|
|
call extend(config, {
|
2017-08-30 07:15:55 -04:00
|
|
|
\ 'on_stdout': function('s:nvim_output_handler'),
|
2017-08-25 12:50:15 -04:00
|
|
|
\ 'on_exit': function('s:nvim_untracked_job_handler')})
|
2017-08-22 17:21:19 -04:00
|
|
|
if has_key(s:untracked_jobs, config.file)
|
|
|
|
" still running
|
|
|
|
return
|
|
|
|
endif
|
2018-03-20 09:01:46 -04:00
|
|
|
try
|
2017-08-22 17:21:19 -04:00
|
|
|
let id = jobstart(cmd, config)
|
2018-03-20 09:01:46 -04:00
|
|
|
catch
|
|
|
|
" catch-all, jobstart() failed, fall back to system()
|
|
|
|
let id=-1
|
|
|
|
endtry
|
2017-08-22 17:21:19 -04:00
|
|
|
let s:untracked_jobs[a:file] = id
|
|
|
|
endif
|
|
|
|
" vim without job feature or nvim jobstart failed
|
|
|
|
if id < 1
|
|
|
|
let output=system(cmd)
|
|
|
|
call s:untracked_output(config, output)
|
|
|
|
call airline#extensions#branch#update_untracked_config(a:file, a:vcs)
|
|
|
|
endif
|
|
|
|
endfunction
|
2019-04-25 08:43:56 -04:00
|
|
|
|
2019-04-25 09:07:12 -04:00
|
|
|
function! airline#async#vim7_vcs_clean(cmd, file, vcs)
|
|
|
|
" Vim pre 8, fallback using system()
|
2019-05-08 07:51:50 -04:00
|
|
|
" don't want to to see error messages
|
|
|
|
if g:airline#init#is_windows && &shell =~ 'cmd'
|
|
|
|
let cmd = a:cmd .' 2>nul'
|
|
|
|
else
|
|
|
|
let cmd = a:cmd .' 2>/dev/null'
|
|
|
|
endif
|
|
|
|
let output=system(cmd)
|
2019-05-08 11:22:34 -04:00
|
|
|
call s:set_clean_variables(a:file, a:vcs, !empty(output))
|
2019-04-25 08:43:56 -04:00
|
|
|
endfunction
|