-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bitter Olivier
committed
Jun 13, 2024
1 parent
d6cd0ae
commit 62254e5
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function! Redir(cmd, rng, start, end) | ||
for win in range(1, winnr('$')) | ||
if getwinvar(win, 'scratch') | ||
execute win . 'windo close' | ||
endif | ||
endfor | ||
if a:cmd =~ '^!' | ||
let cmd = a:cmd =~' %' | ||
\ ? matchstr(substitute(a:cmd, ' %', ' ' . shellescape(escape(expand('%:p'), '\')), ''), '^!\zs.*') | ||
\ : matchstr(a:cmd, '^!\zs.*') | ||
if a:rng == 0 | ||
let output = systemlist(cmd) | ||
else | ||
let joined_lines = join(getline(a:start, a:end), '\n') | ||
let cleaned_lines = substitute(shellescape(joined_lines), "'\\\\''", "\\\\'", 'g') | ||
let output = systemlist(cmd . " <<< $" . cleaned_lines) | ||
endif | ||
else | ||
redir => output | ||
execute a:cmd | ||
redir END | ||
let output = split(output, "\n") | ||
endif | ||
vnew | ||
let w:scratch = 1 | ||
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile | ||
call setline(1, output) | ||
endfunction | ||
|
||
" This command definition includes -bar, so that it is possible to "chain" Vim commands. | ||
" Side effect: double quotes can't be used in external commands | ||
command! -nargs=1 -complete=command -bar -range Redir silent call Redir(<q-args>, <range>, <line1>, <line2>) | ||
|
||
" This command definition doesn't include -bar, so that it is possible to use double quotes in external commands. | ||
" Side effect: Vim commands can't be "chained". | ||
command! -nargs=1 -complete=command -range Redir silent call Redir(<q-args>, <range>, <line1>, <line2>) |