@@ -188,7 +188,11 @@ function! s:extract_opts(command) abort
188188 else
189189 let val = 1
190190 endif
191- let opts[opt ] = substitute (val, ' \\\(\s\)' , ' \1' , ' g' )
191+ if opt == # ' dir' || opt == # ' directory'
192+ let opts.directory = fnamemodify (expand (val), ' :p' )
193+ else
194+ let opts[opt ] = substitute (val, ' \\\(\s\)' , ' \1' , ' g' )
195+ endif
192196 let command = substitute (command , ' ^-\w\+\%(=\%(\\.\|\S\)*\)\=\s*' , ' ' , ' ' )
193197 endwhile
194198 return [command , opts]
@@ -261,16 +265,27 @@ function! dispatch#spawn(command, ...) abort
261265 endif
262266 let request.file = tempname ()
263267 let s: files [request.file ] = request
264- if s: dispatch (request)
265- if get (request, ' manage' )
266- if ! has_key (g: DISPATCH_STARTS , key )
267- let g: DISPATCH_STARTS [key ] = []
268+ let cd = exists (' *haslocaldir' ) && haslocaldir () ? ' lcd' : ' cd'
269+ try
270+ if request.directory !=# getcwd ()
271+ let cwd = getcwd ()
272+ execute cd fnameescape (request.directory )
273+ endif
274+ if s: dispatch (request)
275+ if get (request, ' manage' )
276+ if ! has_key (g: DISPATCH_STARTS , key )
277+ let g: DISPATCH_STARTS [key ] = []
278+ endif
279+ call add (g: DISPATCH_STARTS [key ], request.handler.' @' .dispatch#pid (request))
268280 endif
269- call add (g: DISPATCH_STARTS [key ], request.handler.' @' .dispatch#pid (request))
281+ else
282+ execute ' !' . request.command
270283 endif
271- else
272- execute ' !' . request.command
273- endif
284+ finally
285+ if exists (' cwd' )
286+ execute cd fnameescape (cwd)
287+ endif
288+ endtry
274289 return request
275290endfunction
276291
@@ -444,7 +459,6 @@ function! dispatch#compile_command(bang, args, count) abort
444459 call extend (request, {
445460 \ ' action' : ' make' ,
446461 \ ' background' : a: bang ,
447- \ ' file' : tempname (),
448462 \ ' format' : ' %+I%.%#'
449463 \ }, ' keep' )
450464
@@ -483,21 +497,27 @@ function! dispatch#compile_command(bang, args, count) abort
483497 silent ! wall
484498 endif
485499 cclose
500+ let request.file = tempname ()
486501 let &errorfile = request.file
487502
488503 let efm = &l: efm
489504 let makeprg = &l: makeprg
490505 let compiler = get (b: , ' current_compiler' , ' ' )
491506 let modelines = &modelines
492507 let after = ' '
508+ let cd = exists (' *haslocaldir' ) && haslocaldir () ? ' lcd' : ' cd'
493509 try
494510 let &modelines = 0
495511 call s: set_current_compiler (get (request, ' compiler' , ' ' ))
496512 let &l: efm = request.format
497513 let &l: makeprg = request.command
498514 silent doautocmd QuickFixCmdPre dispatch- make
499- let request.directory = getcwd ()
500- let request.expanded = dispatch#expand (request.command )
515+ let request.directory = get (request, ' directory' , getcwd ())
516+ if request.directory !=# getcwd ()
517+ let cwd = getcwd ()
518+ execute cd fnameescape (request.directory )
519+ endif
520+ let request.expanded = get (request, ' expanded' , dispatch#expand (request.command ))
501521 call extend (s: makes , [request])
502522 let request.id = len (s: makes )
503523 let s: files [request.file ] = request
@@ -514,6 +534,9 @@ function! dispatch#compile_command(bang, args, count) abort
514534 let &l: efm = efm
515535 let &l: makeprg = makeprg
516536 call s: set_current_compiler (compiler )
537+ if exists (' cwd' )
538+ execute cd fnameescape (cwd)
539+ endif
517540 endtry
518541 execute after
519542 return ' '
0 commit comments