Skip to content

Commit

Permalink
Add MakeJobStop
Browse files Browse the repository at this point in the history
Command is used to cancel a running job.

Without an argument, the command will cancel a running MakeJob in one of
two ways:
    1. If the current buffer spawned a MakeJob, that job will be
       stopped.
    2. If the current buffer is displaying MakeJob output, that job will
       stopped.

MakeJobStop also accepts as an argument the name of the MakeJob to stop
(e.g., ":MakeJobStop make"). Command completion can be used to make
finding the name easier.

Additionally do the following refactorings:
1. Add MakeJobStop to help file
2. Update README for MakeJobStop
3. Roll version on script
4. Rename CreateMakeJobWindow to CreateMakeJobBuffer
5. Use <sid> in command definitions according to best practices
  • Loading branch information
djmoch committed Apr 30, 2017
1 parent bd891d3 commit e70bc58
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ be parsed into the Quickfix or LocationList window when the job
completes. Bask in your newfound freedom to do as you please in Vim
while _MakeJob_ runs.

If _MakeJob_ reports findings, use `:copen` to view the QuickFix window
If _MakeJob_ reports findings, use `:copen` to view the Quickfix window
(in the case of `:MakeJob`), and likewise `:lopen` to open the LocationList
for `:LmakeJob`.
for `:LmakeJob`. There's also `:MakeJobStop` to stop a running MakeJob.

Speaking of `:LmakeJob`, all of the LocationList complements to the
Quickfix commands are there with _MakeJob_, bringing the full list of
commands to:

- `:MakeJob`
- `:MakeJobStop`
- `:LmakeJob`
- `:GrepJob`
- `:LgrepJob`
Expand Down
15 changes: 15 additions & 0 deletions doc/makejob.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ GrepaddJob[!] {filename} Same as ":GrepJob", but add findings to the
LgrepaddJob[!] {filename} Same as ":GrepaddJob", but add findings to the
location list without clearing it first.

*makejob-MakeJobStop* *:MakeJobStop*
MakeJobStop [{MakeJob}] Use this command to cancel a running MakeJob.

Without an argument, the command will cancel a
running MakeJob in one of two ways:
1. If the current buffer spawned a MakeJob,
that job will be stopped.
2. If the current buffer is displaying MakeJob
output, that job will stopped.

MakeJobStop also accepts as an argument the name
of the MakeJob to stop (e.g., ":MakeJobStop
make"). Command completion can be used to make
finding the name easier.

=============================================================================
2. CONFIGURATION *makejob-configuration*

Expand Down
72 changes: 56 additions & 16 deletions plugin/makejob.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"
" TITLE: VIM-MAKEJOB
" AUTHOR: Daniel Moch <[email protected]>
" VERSION: 1.2
" VERSION: 1.3-dev
"
if exists('g:loaded_makejob') || &cp
finish
Expand All @@ -28,6 +28,31 @@ function! s:InitAutocmd(lmake, grep, cmd)
return l:returnval
endfunction

function! s:JobStop(...) abort
if a:0
if bufexists(a:1)
execute bufwinnr(a:1).'wincmd w'
if exists('b:makejob')
if !job_stop(b:makejob)
echoerr 'Failed to stop current MakeJob'
end
else
echoerr 'Provided buffer is not a MakeJob'
end
wincmd p
else
echoerr 'Provided MakeJob does not exist'
endif
elseif exists('b:makejob')
let l:job = s:jobinfo[split(job_getchannel(b:makejob))[1]]
if !job_stop(b:makejob)
echoerr 'Failed to stop '.l:job['prog']
endif
else
echoerr 'Not in a MakeJob buffer, and none specified'
endif
endfunction

function! s:JobHandler(channel) abort
let l:job = remove(s:jobinfo, split(a:channel)[1])

Expand Down Expand Up @@ -76,9 +101,9 @@ function! s:JobHandler(channel) abort
echomsg l:job['prog']." ended with ".l:makeoutput." findings"
endfunction

function! s:CreateMakeJobWindow(prog)
function! s:CreateMakeJobBuffer(prog)
silent execute 'belowright 10split '.a:prog
setlocal bufhidden=hide buftype=nofile nobuflisted nolist
setlocal bufhidden=hide buftype=nofile buflisted nolist
setlocal noswapfile nowrap nomodifiable
let l:bufnum = winbufnr(0)
if g:makejob_hide_preview_window
Expand All @@ -103,7 +128,7 @@ function! s:Expand(input)
return join(l:expanded_input)
endfunction

function! s:MakeJob(grep, lmake, grepadd, bang, ...)
function! s:MakeJob(grep, lmake, grepadd, bang, ...) abort
let l:make = a:grep ? s:Expand(&grepprg) : s:Expand(&makeprg)
let l:prog = split(l:make)[0]
let l:internal_grep = l:make ==# 'internal' ? 1 : 0
Expand Down Expand Up @@ -148,40 +173,55 @@ function! s:MakeJob(grep, lmake, grepadd, bang, ...)

silent execute s:InitAutocmd(a:lmake, a:grep, 'Pre')

if &autowrite && !a:grep
if &autowrite && !empty(bufname('%')) && !a:grep
silent write
endif

if l:internal_grep
execute l:make
return
else
let l:outbufnr = s:CreateMakeJobWindow(prog)
let l:outbufnr = s:CreateMakeJobBuffer(prog)

let l:job = job_start(l:make, l:opts)
let s:jobinfo[split(job_getchannel(l:job))[1]] =
let l:makejob = job_start(l:make, l:opts)
let b:makejob = l:makejob
let s:jobinfo[split(job_getchannel(b:makejob))[1]] =
\ { 'prog': l:prog,'lmake': a:lmake,
\ 'outbufnr': l:outbufnr,
\ 'srcbufnr': winbufnr(0),
\ 'cfirst': !a:bang, 'grep': a:grep,
\ 'grepadd': a:grepadd,
\ 'grepadd': a:grepadd, 'job': b:makejob,
\ 'outbufhidden': g:makejob_hide_preview_window }
echomsg s:jobinfo[split(job_getchannel(l:job))[1]]['prog']
echomsg s:jobinfo[split(job_getchannel(b:makejob))[1]]['prog']
\ .' started'

execute bufwinnr(l:outbufnr).'wincmd w'
let b:makejob = l:makejob
wincmd p
end
endfunction

function! s:MakeJobCompletion(arglead, cmdline, cursorpos)
let l:return = []
for l:key in keys(s:jobinfo)
let l:return += [s:jobinfo[l:key]['prog']]
endfor
return l:return
endfunction

command! -bang -nargs=* -complete=file MakeJob
\ call s:MakeJob(0,0,0,<bang>0,<q-args>)
\ call <sid>MakeJob(0,0,0,<bang>0,<q-args>)
command! -bang -nargs=* -complete=file LmakeJob
\ call s:MakeJob(0,1,0,<bang>0,<q-args>)
\ call <sid>MakeJob(0,1,0,<bang>0,<q-args>)
command! -bang -nargs=+ -complete=file GrepJob
\ call s:MakeJob(1,0,0,<bang>0,<q-args>)
\ call <sid>MakeJob(1,0,0,<bang>0,<q-args>)
command! -bang -nargs=+ -complete=file LgrepJob
\ call s:MakeJob(1,1,0,<bang>0,<q-args>)
\ call <sid>MakeJob(1,1,0,<bang>0,<q-args>)
command! -bang -nargs=+ -complete=file GrepaddJob
\ call s:MakeJob(1,0,1,<bang>0,<q-args>)
\ call <sid>MakeJob(1,0,1,<bang>0,<q-args>)
command! -bang -nargs=+ -complete=file LgrepaddJob
\ call s:MakeJob(1,1,1,<bang>0,<q-args>)
\ call <sid>MakeJob(1,1,1,<bang>0,<q-args>)
command! -nargs=? -complete=customlist,<sid>MakeJobCompletion
\ MakeJobStop call <sid>JobStop(<f-args>)
let &cpo = s:save_cpo
unlet s:save_cpo

0 comments on commit e70bc58

Please sign in to comment.