Skip to content

Commit

Permalink
Fix shellescaping for string based commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wellle committed Nov 25, 2017
1 parent e016832 commit 5f60dba
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions autoload/tmuxcomplete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,37 @@ endfunction

let s:script = expand('<sfile>:h:h') . "/sh/tmuxcomplete"

function! s:build_command(base, capture_args, splitmode)
function! s:build_command(base, capture_args, splitmode, as)
let pattern = '^' . escape(a:base, '*^$][.\') . '.'
let list_args = get(g:, 'tmuxcomplete#list_args', '-a')
let grep_args = tmuxcomplete#grepargs(a:base)

" TODO: we need to handle shellescape somewhere else now
let command = [
\ 'sh', s:script,
\ '-p', pattern,
\ '-s', a:splitmode,
\ '-l', list_args,
\ '-c', a:capture_args,
\ '-g', grep_args,
\ ]

if $TMUX_PANE !=# "" " if running inside tmux
let command = add(command, '-e') " exclude current pane
let command = s:newcommand(a:as)
let command = s:addcommand2(command, a:as, 'sh', s:script)
let command = s:addcommand2(command, a:as, '-p', pattern)
let command = s:addcommand2(command, a:as, '-s', a:splitmode)
let command = s:addcommand2(command, a:as, '-l', list_args)
let command = s:addcommand2(command, a:as, '-c', a:capture_args)
let command = s:addcommand2(command, a:as, '-g', grep_args)

if $TMUX_PANE !=# "" " if running inside tmux
let command = s:addcommand1(command, a:as, '-e') " exclude current pane
endif

return command
endfunction

function! tmuxcomplete#getcommand(base, splitmode)
return join(tmuxcomplete#getcommandlist(a:base, 0, a:splitmode))
return s:build_command(a:base, s:capture_args, a:splitmode, 'string')
endfunction

function! tmuxcomplete#getcommandlist(base, scrollback, splitmode)
let capture_args = s:capture_args . ' -S -' . a:scrollback
return s:build_command(a:base, capture_args, a:splitmode)
return s:build_command(a:base, capture_args, a:splitmode, 'list')
endfunction

function! tmuxcomplete#completions(base, capture_args, splitmode)
let command = join(s:build_command(a:base, a:capture_args, a:splitmode))
let command = s:build_command(a:base, a:capture_args, a:splitmode, 'string')

let completions = system(command) " TODO: use systemlist()?
if v:shell_error != 0
Expand Down Expand Up @@ -105,6 +103,31 @@ function! tmuxcomplete#grepargs(base)
return '-i'
endfunction

function! s:newcommand(as)
if a:as == 'list'
return []
else " string
return ''
endif
endfunction

function! s:addcommand1(command, as, value)
if a:as == 'list'
return add(a:command, a:value)
else " string
return (a:command == '' ? '' : a:command . ' ') . a:value
endif
endfunction

function! s:addcommand2(command, as, key, value)
if a:as == 'list'
call add(a:command, a:key)
return add(a:command, a:value) " no escaping here
else " string
return (a:command == '' ? '' : a:command . ' ') . a:key . ' ' . shellescape(a:value)
endif
endfunction

" for integration with completion frameworks
function! tmuxcomplete#gather_candidates()
return tmuxcomplete#complete(0, '')
Expand Down

0 comments on commit 5f60dba

Please sign in to comment.