Skip to content

Commit

Permalink
[vim] Fix argument escaping for Windows batch file
Browse files Browse the repository at this point in the history
Fix #3620
  • Loading branch information
junegunn committed May 1, 2024
1 parent a9811ad commit 835d2fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CHANGELOG
- `change-multi(0)` - disable multi-select mode
- `become` action is now supported on Windows
- Unlike in *nix, this does not use `execve(2)`. Instead it spawns a new process and waits for it to finish, so the exact behavior may differ.
- Fixed argument escaping for Windows cmd.exe
- Bug fixes and improvements

0.50.0
Expand Down
26 changes: 21 additions & 5 deletions plugin/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,28 @@ else
endfunction
endif

let s:cmd_control_chars = ['&', '|', '<', '>', '(', ')', '@', '^', '!']

function! s:shellesc_cmd(arg)
let escaped = substitute(a:arg, '[&|<>()@^]', '^&', 'g')
let escaped = substitute(escaped, '%', '%%', 'g')
let escaped = substitute(escaped, '"', '\\^&', 'g')
let escaped = substitute(escaped, '\(\\\+\)\(\\^\)', '\1\1\2', 'g')
return '^"'.substitute(escaped, '\(\\\+\)$', '\1\1', '').'^"'
let e = '"'
let slashes = 0
for c in split(a:arg, '\zs')
if c ==# '\'
let slashes += 1
elseif c ==# '"'
let e .= repeat('\', slashes + 1)
let slashes = 0
elseif c ==# '%'
let e .= '%'
elseif index(s:cmd_control_chars, c) >= 0
let e .= '^'
else
let slashes = 0
endif
let e .= c
endfor
let e .= repeat('\', slashes) .'"'
return e
endfunction

function! fzf#shellescape(arg, ...)
Expand Down

0 comments on commit 835d2fb

Please sign in to comment.