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 charactersmaster
parent
33777548d5
commit
c94fb3a761
2 changed files with 57 additions and 0 deletions
@ -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\<c-r>=mine#encoding#" . a:fn . "(@\")\<cr>\<esc>" |
||||
|
||||
" reset paste to whatever it was before |
||||
let &paste = l:paste |
||||
endfunction |
Loading…
Reference in new issue