Skip to content

Commit

Permalink
Merge pull request #21 from tuurep/issue/20-percent-encode-paths
Browse files Browse the repository at this point in the history
Percent encode paths
  • Loading branch information
tuurep authored Sep 16, 2024
2 parents 822b527 + 8e274ff commit 18ba8b4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions autoload/vivify.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ let s:viv_url = 'http://localhost:' . ($VIV_PORT == '' ? '31622' : $VIV_PORT)

" Note: nvim's jobstart isn't exactly a drop-in replacement for vim's job_start
" See here: https://stackoverflow.com/questions/74999614/difference-between-vims-job-start-function-and-neovims-jobstart-functi
if has("nvim")
let s:job_start = function("jobstart")
if has('nvim')
let s:job_start = function('jobstart')

" job is job_id as returned from jobstart()
function! s:stdin_send_and_close(job, data)
Expand All @@ -18,16 +18,30 @@ else
call ch_close_in(l:channel)
endfunction

let s:job_start = function("job_start")
let s:job_start = function('job_start')
endif

function! s:percent_encode(path)
if has('python3')
" With python3 support, do proper percent encoding of url
" Vim needs to be compiled with +python3 (check `vim --version`)
" Neovim needs pynvim installed
py3 from urllib.parse import quote
return py3eval('quote("' . a:path . '")')
else
" If no python3 support, still handle the biggest problem
" which is spaces in filenames
return substitute(a:path, ' ', '%20', 'g')
endif
endfunction

function! s:post(data)
let l:job = s:job_start([
\ 'curl',
\ '-X', 'POST',
\ '-H', 'Content-type: application/json',
\ '--data', '@-',
\ s:viv_url . '/viewer' . expand('%:p')
\ s:viv_url . '/viewer' . s:percent_encode(expand('%:p'))
\])
call s:stdin_send_and_close(l:job, json_encode(a:data))
endfunction
Expand All @@ -44,6 +58,6 @@ function! vivify#open()
" Note: nvim's jobstart doesn't use these opt keys
call s:job_start(
\ ['viv', expand('%:p')->substitute(':', '\\:', 'g') . ':' . getpos('.')[1]],
\ {"in_io": "null", "out_io": "null", "err_io": "null"}
\ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'}
\)
endfunction

0 comments on commit 18ba8b4

Please sign in to comment.