From 8e9c411deb73b193a9cda5834ff71f072ede4b4f Mon Sep 17 00:00:00 2001 From: Tuure Date: Mon, 16 Sep 2024 19:59:34 +0300 Subject: [PATCH] feat(#20): percent-encode paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The primary intention is to fix the case where filenames have spaces in them and this plugin doesn't encode the URL properly so it appears as if vivify can't refresh We do this whether (n)vim has python3 support or not, but with python3 support do a proper encoding with a library to handle more esoteric cases, for example curly brackets in filenames. We found that even without the library, characters such as ä, á, ñ, € already work, so it's probably a very rare case where you need any more meticulous encoding for filenames. And then even if that occurs, enabling python3 support will solve it :) --- autoload/vivify.vim | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/autoload/vivify.vim b/autoload/vivify.vim index 48f7cdb..d782ea6 100644 --- a/autoload/vivify.vim +++ b/autoload/vivify.vim @@ -21,13 +21,27 @@ else 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 +python + " 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