job_start the make command

291 views
Skip to first unread message

Ni Va

unread,
Dec 13, 2016, 1:04:53 PM12/13/16
to vim_use
Hi,

I am using a useful tool rigrep to search content through dirs and files.

Then I have applied it to makeprg and set errorformat to display found informations like this :


let &makeprg = expand("$VIM").'\vim80\rg.exe -n ' . l:args
let &errorformat = '%f:%l:%m'
make
copen

It works perfectly and found items are well displayed with copen. Now I would like to job_start the command make and retrieve same display in the copened list.

How can I do that ?
Thanks by advance

Bryan Richter

unread,
Dec 13, 2016, 5:19:00 PM12/13/16
to vim_use
There are two bits of advice I have.

First, vim is very familiar with tools for searching through dirs and
files. The most well-known is called grep (perhaps you also know it).
vim has a command specifically for doing these searches: ":grep". You
can control it with 'grepprg' just like you can control ":make" with
'makeprg'. I suggest you get comfortable using those ones!

(I also use ripgrep. Here are the relevant configurations for me on
Linux):

set grepprg=rg\ --vimgrep
set grepformat=%f:%l:%c:%m

Second, you can use the AsyncRun plugin in order to run 'grepprg'
asynchronously. The syntax is a bit cumbersome, but it looks like:

:AsyncRun -program=grep SEARCH_TERMS

That will run 'grepprg' in the background and populate the errorlist
as you desire.

https://github.com/skywind3000/asyncrun.vim

I hope someone has a better solution than this plugin. :) Maybe one
day :grep will automatically be async?
signature.asc

Ni Va

unread,
Dec 14, 2016, 5:22:00 AM12/14/16
to vim_use
Thank you Bryan for advices.

Applying that example
" From http://andrewvos.com/2016/09/14/writing-async-jobs-in-vim-8

I have done this code.
function! BackgroundCommandClose(channel)
" Read the output from the command into the quickfix window
execute "cfile! " . g:backgroundCommandOutput
" Open the quickfix window
copen
unlet g:backgroundCommandOutput
endfunction

fun! utils#Search(args) "{{{
"
"
"
let prgPath = expand("$vimruntime")."/rg.exe"
if !file_readable(prgPath)
echoerr "rigrep tool not found in ".$vimruntime
return -1
endif

let l:args = empty(a:args) ? expand("<cword>") : a:args
let cmd = prgPath . ' -n ' . l:args


" Async func
let g:backgroundCommandOutput = tempname()
let j = job_start(cmd, {'close_cb': 'BackgroundCommandClose', 'out_io': 'file', 'out_name': g:backgroundCommandOutput})
endfu
command! -bang -nargs=* -complete=file Search call utils#Search(<q-args>)


It works very well in background and copen the cfile well formatted.

Maybe you can advise me how optimizing BackgroundCommandClose func declaration in order to declare it within the utilsSearch func.

But thank for your advices.

Bram Moolenaar

unread,
Dec 14, 2016, 3:42:27 PM12/14/16
to vim...@googlegroups.com, Ni Va
This looks like a good start for a very useful plugin. One that I would
like to include in Vim.

What I would prefer is something that works just like :make, but in the
background. It would have the same arguments as :make and use the
'makeprg' option. Only that the quickfix list is set much later.
Could call it :MakeBackground.

My wishlist:
- When using it again before the previous job is finished, prompt the
user if he wants to abort the already running job. I quite often run
:make and than realize I forgot one change.
- Open the quickfix window when the --copen argument is given.
- Give a non-intrusive message when the work is done.

Anyone who would be willing to make this work and maintain it?

--
hundred-and-one symptoms of being an internet addict:
116. You are living with your boyfriend who networks your respective
computers so you can sit in separate rooms and email each other

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Justin M. Keyes

unread,
Dec 15, 2016, 12:48:21 AM12/15/16
to vim_use, Ni Va
In that case the dispatch.vim plugin should be updated to use job control:

https://github.com/tpope/vim-dispatch

It's a mature and already widely-used plugin. It would be very much
worth including, and it's backwards-compatible to older Vims.

Justin M. Keyes

Andrew Vos

unread,
Dec 15, 2016, 11:16:56 AM12/15/16
to vim_use, niva...@gmail.com
I pushed this live if you wanna give it a try:

https://github.com/AndrewVos/vim-make-background

Andrew Vos

unread,
Dec 15, 2016, 6:29:52 PM12/15/16
to vim_use, niva...@gmail.com
Bram, are you serious about possibly including something like this in Vim? If you are, then I would absolutely be interested in getting your wishlist all working and putting the time in to maintain it!

Bram Moolenaar

unread,
Dec 16, 2016, 5:34:36 AM12/16/16
to vim...@googlegroups.com, Andrew Vos, niva...@gmail.com
Yes, I think this is generally useful.

Would be good to review this, so that it fits the need of more than just
me.

--
hundred-and-one symptoms of being an internet addict:
123. You ask the car dealer to install an extra cigarette lighter
on your new car to power your notebook.

skywind3000

unread,
Dec 16, 2016, 6:33:32 AM12/16/16
to vim_use, andre...@gmail.com, niva...@gmail.com
Shameless self promotion:

https://github.com/skywind3000/asyncrun.vim

an asynchronous :Make command can be easily defined by using AsyncRun:

:command! -bang -nargs=* -complete=file Make AsyncRun -program=make @ <args>

Now, :Make works just like old :make but runs in background, errors will be matched by &errorformat and displayed to quickfix window as well.

Any shell command (not limited to &makeprg, &grepprg) can be execute like

:AsyncRun ls -la

Which is exactly the samething like old "!" command, but runs in background, errors will also be matched in quickfix window.

and many misc stuff:

1. macros like "%", "%<", ... in arguments can also be expanded.

2. autocommands about make or quickfix can be triggered before and after the job , your code in .vimrc can be triggered after before starting or after job finished (eg. you may want to connect the latest cscope file after it's been successfully generated in background).

3. global variable to indicate job status, which can be used to be displayed on your statusline.

4. Compatible from 7.4.1829 to the latest version.

5. Only one self-contained source file, asyncrun.vim.

Vincent Stone

unread,
Sep 25, 2017, 5:22:14 AM9/25/17
to vim_use
Asynchrun is too heavy to be included in the vim just for make and grep. I prefer the vim-make-background plugin. Is there any plan to include it?

--Vincent Stone

Enno

unread,
Jul 26, 2019, 6:23:54 AM7/26/19
to vim_use
Le lundi 25 septembre 2017 06:22:14 UTC-3, Vincent Stone a écrit :
> Asynchrun is too heavy to be included in the vim just for make and grep. I prefer the vim-make-background plugin. Is there any plan to include it?
>
> --Vincent Stone

In defense of AsyncRun, it worked unfailingly for the last two years. The 40 kilobytes of source code are also due to (useful) options listed in https://github.com/skywind3000/asyncrun.vim/wiki/Options

Vim-dispatch https://github.com/tpope/vim-dispatch is backwards comaptible, but bigger, and the job-feature was implemented last month: https://github.com/tpope/vim-dispatch/commit/597b338f3579ca6929c47465397974d366f4eccc
Reply all
Reply to author
Forward
0 new messages