Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

base64 encode/decode + README.markdown #17

Closed
wants to merge 6 commits into from

3 participants

@salsifis

Hello,

I added base64 support (encoding/decoding) to unimpaired and mapped it to [6 and ]6.

Also, there is a proposal for a markdown README for your plug-in.

@tpope
Owner

This is great! Only problem is that using a number for the map introduces some ambiguities: [66w should encode 6 words, but instead it will encode the line and move one word forward. I'm thinking [Y might be our best bet. There are very few choices left that can be used as an operator.

I actually had a README sitting around that I forgot to push. Whoops! Thanks anyways.

@salsifis

[Y and ]Y might be a good bet but I would like to suggest that we regroup all the encode/decode mappings behind [\ and ]\ which would seem more natural to me (mnemonic: \ is commonly used for escaping):

  • [\c and ]\c for C/C++ strings (we get rid of the [y mapping that had no obvious mnemonic)
  • [\b and ]\b for base64
  • [\u and ]\u for urls
  • [\x and ]\x for xml entities

Would you agree with this proposal, or still prefer keeping [y, [u and [x, defining a new mapping [Y .. ]Y? I let you decide before I rework on my branch.

Thank you for your feedback.

@tpope
Owner

Oooh, I really like that consistency. But I guess my concern is that 3 character (and in the case of linewise, 4 character) maps can start to get challenging for my dumb fingers to type in under a second, especially with that pinky dance in there. Let's stick to the Y maps for now. We can always change after I've had time to experiment with [\ mappings locally.

@salsifis salsifis Changed base64 encoding/decoding mapping to Y
The old mapping were [6 and ]6. The new ones are [Y and ]Y.
Documentation changed accordingly.

It is proposed that in the future the encoding and decoding mappings are
changed to:

 * [\x and ]\x : xml entities
 * [\c and ]\c : C-style string literals
 * [\u and ]\u : URL-encoding
 * [\b and ]\b : Base64
288cd3e
@salsifis

Okay, I have switched my mappings to [Y and ]Y.

@tpope
Owner

Squashed and merged. Cheers.

@tpope tpope closed this
@salsifis
@tpope
Owner

And pushed.

@tpope
Owner

I'm yanking this, and instead making the API for defining your own transformations public. It's been bugging me that this is inconsistent with the other commands in that it encodes rather than escapes. Implications of this include that it's not clear what to do when decoding targets unencoded content, and the problem with nulls is brought front and center.

I'm also unclear on how this is superior just filtering through the base64 command (except maybe Windows support). I should have brought this up earlier.

@tpope tpope referenced this pull request from a commit
@tpope Remove base64 encoding
References #17
44e06d4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 6, 2012
  1. @salsifis

    Added README.markdown

    salsifis authored
  2. @salsifis
Commits on Feb 20, 2012
  1. @salsifis

    Added base64 encoding/decoding capabilities.

    salsifis authored
    The bracket mappings `[6` and `]6` map to base64 encoding and
    decoding.
Commits on Feb 27, 2012
  1. @markus1189
Commits on Mar 7, 2012
  1. @salsifis
  2. @salsifis

    Changed base64 encoding/decoding mapping to Y

    salsifis authored
    The old mapping were [6 and ]6. The new ones are [Y and ]Y.
    Documentation changed accordingly.
    
    It is proposed that in the future the encoding and decoding mappings are
    changed to:
    
     * [\x and ]\x : xml entities
     * [\c and ]\c : C-style string literals
     * [\u and ]\u : URL-encoding
     * [\b and ]\b : Base64
This page is out of date. Refresh to see the latest.
Showing with 72 additions and 1 deletion.
  1. +1 −1  README.markdown
  2. +10 −0 doc/unimpaired.txt
  3. +61 −0 plugin/unimpaired.vim
View
2  README.markdown
@@ -16,7 +16,7 @@ line with the one above or below it.
There are mappings for encoding and decoding. `[x` and `]x` encode and
decode XML (and HTML). `[u` and `]u` encode and decode URLs. `[y` and
-`]y` do C String style escaping.
+`]y` do C String style escaping. `[Y` and `]Y` encode and decode Base64.
And in the miscellaneous category, there's `[o` and `]o` to go to the
next/previous file in the directory, and `[n` and `]n` to jump between
View
10 doc/unimpaired.txt
@@ -104,6 +104,16 @@ Mnenomic: encoding always comes before decoding; "[" always comes before "]".
]yy
{Visual}]y
+ *[Y* *[YY* *v_[Y*
+[Y{motion} Base64 encode.
+[YY foo bar => Zm9vIGJhcg==
+{Visual}[Y
+
+ *]Y* *]YY* *v_]Y*
+]Y{motion} Base64 decode
+]YY Input length must be a multiple of 4.
+{Visual}]Y
+
TODO *unimpaired-todo*
Avoid munging null characters when encoding and decoding.
View
61 plugin/unimpaired.vim
@@ -186,6 +186,65 @@ function! s:UrlDecode(str)
return substitute(str,'%\(\x\x\)','\=nr2char("0x".submatch(1))','g')
endfunction
+let g:unimpaired_base64_chars = map(range(char2nr('A'),char2nr('Z')),'nr2char(v:val)')
+ \ + map(range(char2nr('a'),char2nr('z')),'nr2char(v:val)')
+ \ + map(range(char2nr('0'),char2nr('9')),'nr2char(v:val)')
+ \ + ['+','/']
+
+let g:unimpaired_base64_filler = '='
+let g:unimpaired_base64_reverse_map = {}
+let s:pos = 0
+for s:char in g:unimpaired_base64_chars
+ let g:unimpaired_base64_reverse_map[s:char] = s:pos
+ let s:pos = s:pos + 1
+endfor
+unlet s:pos
+
+function! s:Base64Encode(str)
+ " Respect current file encoding
+ let to_be_encoded=a:str
+ let encoded = ''
+ while len(to_be_encoded) > 2
+ let encoded .= g:unimpaired_base64_chars[char2nr(to_be_encoded[0])/4]
+ \ . g:unimpaired_base64_chars[16*(char2nr(to_be_encoded[0])%4 )+char2nr(to_be_encoded[1])/16]
+ \ . g:unimpaired_base64_chars[4 *(char2nr(to_be_encoded[1])%16)+char2nr(to_be_encoded[2])/64]
+ \ . g:unimpaired_base64_chars[char2nr(to_be_encoded[2])%64]
+ let to_be_encoded = to_be_encoded[3:]
+ endwhile
+ if len(to_be_encoded) == 2
+ let encoded .= g:unimpaired_base64_chars[char2nr(to_be_encoded[0])/4]
+ \ . g:unimpaired_base64_chars[16*(char2nr(to_be_encoded[0])%4 )+char2nr(to_be_encoded[1])/16]
+ \ . g:unimpaired_base64_chars[4 *(char2nr(to_be_encoded[1])%16)]
+ \ . g:unimpaired_base64_filler
+ elseif len(to_be_encoded) == 1
+ let encoded .= g:unimpaired_base64_chars[char2nr(to_be_encoded[0])/4]
+ \ . g:unimpaired_base64_chars[16*(char2nr(to_be_encoded[0])%4 )]
+ \ . g:unimpaired_base64_filler
+ \ . g:unimpaired_base64_filler
+ endif
+ return encoded
+endfunction
+
+function! s:Base64Decode(str)
+ let to_be_decoded=a:str
+ let decoded=''
+ while len(to_be_decoded) % 4 == 0
+ let decoded .= nr2char( 4 * (g:unimpaired_base64_reverse_map[to_be_decoded[0]] )
+ \ + (g:unimpaired_base64_reverse_map[to_be_decoded[1]] / 16 )
+ \)
+ if to_be_decoded[2] !=# g:unimpaired_base64_filler
+ let decoded .= nr2char(16 * (g:unimpaired_base64_reverse_map[to_be_decoded[1]] % 16) + (g:unimpaired_base64_reverse_map[to_be_decoded[2]]/4))
+ if to_be_decoded[3] !=# g:unimpaired_base64_filler
+ let decoded .= nr2char(64 * (g:unimpaired_base64_reverse_map[to_be_decoded[2]] % 4) + g:unimpaired_base64_reverse_map[to_be_decoded[3]])
+ endif
+ endif
+ let to_be_decoded = to_be_decoded[4:]
+ if empty(to_be_decoded)
+ return decoded
+ endif
+ endwhile
+endfunction
+
" HTML entities {{{2
let g:unimpaired_html_entities = {
@@ -331,6 +390,8 @@ call s:MapTransform('UrlEncode','[u')
call s:MapTransform('UrlDecode',']u')
call s:MapTransform('XmlEncode','[x')
call s:MapTransform('XmlDecode',']x')
+call s:MapTransform('Base64Encode','[Y')
+call s:MapTransform('Base64Decode',']Y')
" }}}1
Something went wrong with that request. Please try again.