Skip to content

Commit 4caf963

Browse files
committed
Provide :Spawn
1 parent c736899 commit 4caf963

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

autoload/dispatch.vim

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,34 @@ function! s:dispatch(request) abort
175175
endfunction
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 ''
201208
endfunction
@@ -206,43 +213,52 @@ if type(get(g:, 'DISPATCH_STARTS')) != type({})
206213
endif
207214

208215
function! 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

doc/dispatch.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ COMMANDS *dispatch-commands*
9999
Start a process in a window titled {title}. This is
100100
not normally used interactively.
101101

102+
*dispatch-:Spawn*
103+
:Spawn[!] [command] Like |:Start|, but always spawn a new process rather
104+
than focusing an existing one. The default is always
105+
to spawn a new 'shell'.
106+
102107
STRATEGIES *dispatch-strategies*
103108

104109
Strategies are listed in order of precedence. The first available one is

plugin/dispatch.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ command! -bang -nargs=* -complete=customlist,dispatch#command_complete FocusDisp
1616
command! -bang -nargs=* -complete=customlist,dispatch#make_complete Make
1717
\ Dispatch<bang> _ <args>
1818

19+
command! -bang -nargs=* -complete=customlist,dispatch#command_complete Spawn
20+
\ execute dispatch#spawn_command(<bang>0, <q-args>)
21+
1922
command! -bang -nargs=* -complete=customlist,dispatch#command_complete Start
2023
\ execute dispatch#start_command(<bang>0, <q-args>)
2124

0 commit comments

Comments
 (0)