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
master
Jonathan Hodgson 4 years ago
parent 33777548d5
commit c94fb3a761
  1. 50
      nvim/.config/nvim/autoload/mine/encoding.vim
  2. 7
      nvim/.config/nvim/plugin/mappings.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\<c-r>=mine#encoding#" . a:fn . "(@\")\<cr>\<esc>"
" reset paste to whatever it was before
let &paste = l:paste
endfunction

@ -43,3 +43,10 @@ inoremap <C-l> <c-g>u<Esc>[s1z=`]a<c-g>u
inoremap <C-y> <Esc>:silent s/^/#!\/usr\/bin\/env / <bar> filetype detect<cr>:nohlsearch<cr>o inoremap <C-y> <Esc>:silent s/^/#!\/usr\/bin\/env / <bar> filetype detect<cr>:nohlsearch<cr>o
" alternative: inoremap <C-y> <Esc>:silent exe ".!which <cWORD>" <bar> s/^/#!/ <bar> filetype detect<cr>YpDi " alternative: inoremap <C-y> <Esc>:silent exe ".!which <cWORD>" <bar> s/^/#!/ <bar> filetype detect<cr>YpDi
" Mappings for my encoding functions
vnoremap [b :call mine#encoding#wrapper('base64Encode')<cr>
vnoremap ]b :call mine#encoding#wrapper('base64Decode')<cr>
vnoremap [u :call mine#encoding#wrapper('urlEncode')<cr>
vnoremap ]u :call mine#encoding#wrapper('urlDecode')<cr>
vnoremap [U :call mine#encoding#wrapper('urlEncodeAll')<cr>
vnoremap ]U :call mine#encoding#wrapper('urlDecode')<cr>

Loading…
Cancel
Save