@@ -175,27 +175,34 @@ function! s:dispatch(request) abort
175175endfunction
176176
177177" }}}1
178- " :Start {{{1
178+ " :Start, :Spawn {{{1
179179
180- function ! dispatch#start_command ( bang , command ) abort
180+ function ! s: extract_title ( command ) abort
181181 let command = a: command
182- if empty (command ) && type (get (b: , ' start' , [])) == type (' ' )
183- let command = b: start
184- endif
185182 let title = matchstr (command , ' -title=\zs\%(\\.\|\S\)*' )
186183 if ! empty (title )
187184 let command = command [strlen (title ) + 8 : -1 ]
188185 endif
189186 let title = substitute (title , ' \\\(\s\)' , ' \1' , ' g' )
190- let cd = exists (' *haslocaldir' ) && haslocaldir () ? ' lcd' : ' cd'
191- let restore = ' '
187+ return [command , title ]
188+ endfunction
189+
190+ function ! dispatch#spawn_command (bang , command ) abort
191+ let [command , title ] = s: extract_title (a: command )
192+ call dispatch#spawn (command , {' background' : a: bang , ' title' : title })
193+ return ' '
194+ endfunction
195+
196+ function ! dispatch#start_command (bang , command ) abort
197+ let command = a: command
198+ if empty (command ) && type (get (b: , ' start' , [])) == type (' ' )
199+ let command = b: start
200+ endif
201+ let [command , title ] = s: extract_title (command )
192202 if command = ~# ' ^:.'
193203 unlet ! g: dispatch_last_start
194204 return substitute (command , ' \>' , get (a: 0 ? a: 1 : {}, ' background' , 0 ) ? ' !' : ' ' , ' ' )
195205 endif
196- if empty (command )
197- let command = &shell
198- endif
199206 call dispatch#start (command , {' background' : a: bang , ' title' : title })
200207 return ' '
201208endfunction
@@ -206,43 +213,52 @@ if type(get(g:, 'DISPATCH_STARTS')) != type({})
206213endif
207214
208215function ! dispatch#start (command , ... ) abort
216+ return dispatch#spawn (a: command , extend ({' manage' : 1 }, a: 0 ? a: 1 : {}))
217+ endfunction
218+
219+ function ! dispatch#spawn (command , ... ) abort
220+ let command = empty (a: command ) ? &shell : a: command
209221 let request = extend ({
210222 \ ' action' : ' start' ,
211223 \ ' background' : 0 ,
212- \ ' command' : a: command ,
224+ \ ' command' : command ,
213225 \ ' directory' : getcwd (),
214- \ ' expanded' : dispatch#expand (a: command ),
226+ \ ' expanded' : dispatch#expand (command ),
215227 \ ' title' : ' ' ,
216228 \ }, a: 0 ? a: 1 : {})
217229 let g: dispatch_last_start = request
218230 if empty (request.title )
219231 let request.title = substitute (fnamemodify (matchstr (request.command , ' \%(\\.\|\S\)\+' ), ' :t:r' ), ' \\\(\s\)' , ' \1' , ' g' )
220232 endif
221- let key = request.directory ." \t " .substitute (request.expanded, ' \s*$' , ' ' , ' ' )
222- let i = 0
223- while i < len (get (g: DISPATCH_STARTS , key , []))
224- let [handler, pid] = split (g: DISPATCH_STARTS [key ][i ], ' @' )
225- if ! s: running (pid)
226- call remove (g: DISPATCH_STARTS [key ], i )
227- continue
228- endif
229- try
230- if request.background || dispatch#{handler}#activate (pid)
231- let request.handler = handler
232- let request.pid = pid
233- return request
233+ if get (request, ' manage' )
234+ let key = request.directory ." \t " .substitute (request.expanded, ' \s*$' , ' ' , ' ' )
235+ let i = 0
236+ while i < len (get (g: DISPATCH_STARTS , key , []))
237+ let [handler, pid] = split (g: DISPATCH_STARTS [key ][i ], ' @' )
238+ if ! s: running (pid)
239+ call remove (g: DISPATCH_STARTS [key ], i )
240+ continue
234241 endif
235- catch
236- endtry
237- let i += 1
238- endwhile
242+ try
243+ if request.background || dispatch#{handler}#activate (pid)
244+ let request.handler = handler
245+ let request.pid = pid
246+ return request
247+ endif
248+ catch
249+ endtry
250+ let i += 1
251+ endwhile
252+ endif
239253 let request.file = tempname ()
240254 let s: files [request.file ] = request
241255 if s: dispatch (request)
242- if ! has_key (g: DISPATCH_STARTS , key )
243- let g: DISPATCH_STARTS [key ] = []
256+ if get (request, ' manage' )
257+ if ! has_key (g: DISPATCH_STARTS , key )
258+ let g: DISPATCH_STARTS [key ] = []
259+ endif
260+ call add (g: DISPATCH_STARTS [key ], request.handler.' @' .dispatch#pid (request))
244261 endif
245- call add (g: DISPATCH_STARTS [key ], request.handler.' @' .dispatch#pid (request))
246262 else
247263 execute ' !' . request.command
248264 endif
0 commit comments