From 380e2c81c8c612442fec33520b881240450a1def Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Sun, 27 Sep 2020 00:44:24 +0100 Subject: [PATCH] VIM: Adds mappings for url and base64 encoding / decoding Defines functions for each as autoload functions. I need to look into weather it would be better to have this as a local function in a plugin. The function for url encoding was taken from vim-unimpared: https://github.com/tpope/vim-unimpaired I don't need all the functionality it provides The function for base64 encoding was taken from vim-base64: https://github.com/christianrondeau/vim-base64 although I use it with mappings in the vim-unimpared style [b and ]b base64 encode and decode respectively [u url encodes characters that are normally encodede in a url [U url encodes all characters ]u and ]U both urldecode all encodede characters --- nvim/.config/nvim/autoload/mine/encoding.vim | 50 ++++++++++++++++++++ nvim/.config/nvim/plugin/mappings.vim | 7 +++ 2 files changed, 57 insertions(+) create mode 100644 nvim/.config/nvim/autoload/mine/encoding.vim diff --git a/nvim/.config/nvim/autoload/mine/encoding.vim b/nvim/.config/nvim/autoload/mine/encoding.vim new file mode 100644 index 00000000..742e0b25 --- /dev/null +++ b/nvim/.config/nvim/autoload/mine/encoding.vim @@ -0,0 +1,50 @@ +" base64 encodes text +" It relies on the executable base64 which should be installed on most unix-y +" systems. +function! mine#encoding#base64Encode(text) abort + return substitute(system('base64 --wrap=0', a:text), '\n$', '', 'g') +endfunction + +" base64 decodes text +" It relies on the executable base64 which should be installed on most unix-y +" systems. +function! mine#encoding#base64Decode(text) abort + return substitute(system('base64 --decode --ignore-garbage --wrap=0', a:text), '\n$', '', 'g') +endfunction + +" Url encodes characters that are normally encoded in urls +" Taken from https://github.com/tpope/vim-unimpaired/blob/master/plugin/unimpaired.vim +function! mine#encoding#urlEncode(text) abort + " iconv trick to convert utf-8 bytes to 8bits indiviual char. + return substitute(iconv(a:text, 'latin1', 'utf-8'),'[^A-Za-z0-9_.~-]','\="%".printf("%02X",char2nr(submatch(0)))','g') +endfunction + +" Url decodes text +" Taken from https://github.com/tpope/vim-unimpaired/blob/master/plugin/unimpaired.vim +function! mine#encoding#urlDecode(text) abort + let text = substitute(substitute(substitute(a:text,'%0[Aa]\n$','%0A',''),'%0[Aa]','\n','g'),'+',' ','g') + return iconv(substitute(text,'%\(\x\x\)','\=nr2char("0x".submatch(1))','g'), 'utf-8', 'latin1') +endfunction + +" Url encodes all characters +function! mine#encoding#urlEncodeAll(text) abort + " iconv trick to convert utf-8 bytes to 8bits indiviual char. + return substitute(iconv(a:text, 'latin1', 'utf-8'),'.','\="%".printf("%02X",char2nr(submatch(0)))','g') +endfunction + +function! mine#encoding#wrapper(fn) abort + " keep track of what paste was set to + let l:paste = &paste + " make sure it is enabled + set paste + + " make sure the selection is selected + normal! gv + + " transform text + " adapted from: https://github.com/christianrondeau/vim-base64/blob/master/autoload/base64.vim#L36 + execute "normal! c\=mine#encoding#" . a:fn . "(@\")\\" + + " reset paste to whatever it was before + let &paste = l:paste +endfunction diff --git a/nvim/.config/nvim/plugin/mappings.vim b/nvim/.config/nvim/plugin/mappings.vim index 958e2994..4dd7267a 100644 --- a/nvim/.config/nvim/plugin/mappings.vim +++ b/nvim/.config/nvim/plugin/mappings.vim @@ -43,3 +43,10 @@ inoremap u[s1z=`]au inoremap :silent s/^/#!\/usr\/bin\/env / filetype detect:nohlsearcho " alternative: inoremap :silent exe ".!which " s/^/#!/ filetype detectYpDi +" Mappings for my encoding functions +vnoremap [b :call mine#encoding#wrapper('base64Encode') +vnoremap ]b :call mine#encoding#wrapper('base64Decode') +vnoremap [u :call mine#encoding#wrapper('urlEncode') +vnoremap ]u :call mine#encoding#wrapper('urlDecode') +vnoremap [U :call mine#encoding#wrapper('urlEncodeAll') +vnoremap ]U :call mine#encoding#wrapper('urlDecode')