Skip to content

Commit c4ab24a

Browse files
committed
Async support added
1 parent 6cb5653 commit c4ab24a

File tree

4 files changed

+94
-51
lines changed

4 files changed

+94
-51
lines changed

autoload/executor.vim

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,35 @@
33
" or selection and redirects output to a new buffer
44
" Author: Alexander Skachko <[email protected]>
55
" Homepage: https://github.com/lucerion/vim-executor
6-
" Version: 0.1
6+
" Version: 0.2
77
" Licence: BSD-3-Clause
88
" ===========================================================================
99

1010
let s:previous_buffer = ''
1111

12-
func! executor#exec(start_line, end_line, count, ...)
12+
func! executor#exec(start_line, end_line, ...)
1313
if !exists('g:loaded_buffr')
1414
call s:show_error('Please, install vim-buffr plugin first') | return
1515
endif
1616

17-
call s:execute(a:start_line, a:end_line, a:count, a:000)
18-
endfunc
19-
20-
func! s:execute(start_line, end_line, count, command)
21-
let l:selection = s:selection(a:start_line, a:end_line)
22-
let l:command = s:command(a:command)
23-
let l:result = s:result(l:command, l:selection, a:count)
24-
let l:buffer_name = s:buffer_name(l:command)
25-
26-
call s:open_result(l:result, l:buffer_name)
27-
endfunc
28-
29-
func! s:selection(start_line, end_line)
30-
return getline(a:start_line, a:end_line)
31-
endfunc
17+
let l:command = join(a:000)
18+
let l:selection = getline(a:start_line, a:end_line)
3219

33-
func! s:command(command)
34-
let l:command = join(a:command)
35-
if len(substitute(l:command, ' ', '', 'g'))
36-
return l:command
20+
if g:executor_exec_async && (v:version >= 800)
21+
call executor#async#exec(l:command, l:selection)
22+
else
23+
call executor#default#exec(l:command, l:selection)
3724
end
38-
39-
return g:executor_default_command
4025
endfunc
4126

42-
func! s:result(command, selection, count)
43-
if len(a:selection) == 1
44-
let l:command = a:command . ' ' . a:selection[0]
45-
return system(l:command)
46-
endif
47-
48-
if a:count
49-
let l:selection = join(a:selection, "\n") . "\n"
50-
return system(a:command, l:selection)
51-
endif
52-
53-
return system(a:command)
27+
func! executor#open_result(result, command)
28+
let l:buffer_name = s:buffer_name(a:command)
29+
call s:open_buffer(l:buffer_name)
30+
call append(0, a:result)
31+
silent normal! Gddgg
5432
endfunc
5533

56-
func! s:open_result(result, buffer_name)
34+
func! s:open_buffer(buffer_name)
5735
let l:buffer_name = a:buffer_name
5836

5937
if g:executor_reuse_buffer
@@ -64,25 +42,23 @@ func! s:open_result(result, buffer_name)
6442
endif
6543

6644
call buffr#open_or_create_buffer({
67-
\ 'position': g:executor_position,
45+
\ 'position': 'bottom',
6846
\ 'name': l:buffer_name
6947
\ })
7048
call s:set_buffer_defaults()
7149

7250
if g:executor_reuse_buffer
7351
silent exec 'edit ' . escape(s:previous_buffer, ' ')
7452
endif
75-
76-
call append(0, split(a:result, "\n"))
77-
silent exec 'normal! Gdd'
7853
endfunc
7954

8055
func! s:buffer_name(command)
81-
let l:buffer_name = g:executor_buffer_name
82-
let l:buffer_name = substitute(l:buffer_name, '{command}', a:command, 'g')
83-
let l:buffer_name = substitute(l:buffer_name, '{filename}', expand('%:t'), 'g')
56+
let l:name = g:executor_buffer_name
57+
let l:name = substitute(l:name, '{command}', a:command, 'g')
58+
let l:name = substitute(l:name, '{filename}', expand('%:t'), 'g')
59+
let l:name = escape(l:name, '|')
8460

85-
return escape(l:buffer_name, '|')
61+
return l:name
8662
endfunc
8763

8864
func! s:set_buffer_defaults()

autoload/executor/async.vim

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
" ===========================================================================
2+
" Description: Vim plugin that executes shell command with a buffer contents
3+
" or selection and redirects output to a new buffer
4+
" Author: Alexander Skachko <[email protected]>
5+
" Homepage: https://github.com/lucerion/vim-executor
6+
" Version: 0.2
7+
" Licence: BSD-3-Clause
8+
" ===========================================================================
9+
10+
func! executor#async#exec(command, selection)
11+
let s:command = a:command
12+
let s:result = []
13+
call s:execute(a:command, a:selection)
14+
endfunc
15+
16+
func! s:execute(command, selection)
17+
let l:command = a:command
18+
19+
if len(a:selection) == 1 && !empty(a:selection[0])
20+
let l:command .= ' '
21+
let l:command .= a:selection[0]
22+
endif
23+
24+
if len(a:selection) > 1
25+
let l:command = 'echo "' . join(a:selection, "\n") . '" | ' . l:command
26+
endif
27+
28+
call job_start([&shell, &shellcmdflag, l:command], {
29+
\ 'out_cb': function('s:out_callback'),
30+
\ 'exit_cb': function('s:exit_callback')
31+
\ })
32+
endfunc
33+
34+
func! s:out_callback(channel, data)
35+
call add(s:result, a:data)
36+
endfunc
37+
38+
func! s:exit_callback(job, status)
39+
call executor#open_result(s:result, s:command)
40+
endfunc

autoload/executor/default.vim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
" ===========================================================================
2+
" Description: Vim plugin that executes shell command with a buffer contents
3+
" or selection and redirects output to a new buffer
4+
" Author: Alexander Skachko <[email protected]>
5+
" Homepage: https://github.com/lucerion/vim-executor
6+
" Version: 0.2
7+
" Licence: BSD-3-Clause
8+
" ===========================================================================
9+
10+
func! executor#default#exec(command, selection)
11+
let l:result = s:execute(a:command, a:selection)
12+
call executor#open_result(split(l:result, "\n"), a:command)
13+
endfunc
14+
15+
func! s:execute(command, selection)
16+
if !len(a:selection) || (len(a:selection) == 1 && empty(a:selection[0]))
17+
return system(a:command)
18+
endif
19+
20+
if len(a:selection) == 1
21+
let l:command = a:command . ' ' . a:selection[0]
22+
return system(l:command)
23+
endif
24+
25+
let l:selection = join(a:selection, "\n") . "\n"
26+
return system(a:command, l:selection)
27+
endfunc

plugin/executor.vim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" or selection and redirects output to a new buffer
44
" Author: Alexander Skachko <[email protected]>
55
" Homepage: https://github.com/lucerion/vim-executor
6-
" Version: 0.1
6+
" Version: 0.2
77
" Licence: BSD-3-Clause
88
" ===========================================================================
99

@@ -15,10 +15,6 @@ if !exists('g:executor_position')
1515
let g:executor_position = 'bottom'
1616
endif
1717

18-
if !exists('g:executor_default_command')
19-
let g:executor_default_command = 'sh -e'
20-
endif
21-
2218
if !exists('g:executor_buffer_name')
2319
let g:executor_buffer_name = '{command}'
2420
endif
@@ -27,7 +23,11 @@ if !exists('g:executor_reuse_buffer')
2723
let g:executor_reuse_buffer = 0
2824
endif
2925

30-
comm! -nargs=* -range=0 -complete=shellcmd Exec
31-
\ call executor#exec(<line1>, <line2>, <count>, <q-args>)
26+
if !exists('g:executor_exec_async')
27+
let g:executor_exec_async = 1
28+
endif
29+
30+
comm! -nargs=+ -range=0 -complete=shellcmd Exec
31+
\ call executor#exec(<line1>, <line2>, <q-args>)
3232

3333
let g:loaded_executor = 1

0 commit comments

Comments
 (0)