<s> import vim <EOL> import translate <EOL> import urllib2 <EOL> import string <EOL> def __do_visual ( buff , cur , res = '<STR_LIT>' ) : <EOL> cursor = cur . window . cursor <EOL> if cursor [ <NUM_LIT:0> ] == buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] : <EOL> if buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] == buff . mark ( '<STR_LIT:>>' ) [ <NUM_LIT:0> ] : <EOL> return buff [ <EOL> buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] - <NUM_LIT:1> ] [ <EOL> buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:1> ] : buff . mark ( '<STR_LIT:>>' ) [ <NUM_LIT:1> ] ] <EOL> else : <EOL> res = buff [ buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] - <NUM_LIT:1> ] [ buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:1> ] : ] <EOL> for x in xrange ( buff . mark ( '<STR_LIT:>>' ) [ <NUM_LIT:0> ] - buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] - <NUM_LIT:1> ) : <EOL> res = '<STR_LIT:U+0020>' . join ( ( res , buff [ buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] + x ] ) ) <EOL> return '<STR_LIT:U+0020>' . join ( ( <EOL> res , <EOL> buff [ buff . mark ( '<STR_LIT:>>' ) [ <NUM_LIT:0> ] - <NUM_LIT:1> ] [ : buff . mark ( '<STR_LIT:>>' ) [ <NUM_LIT:1> ] ] ) ) <EOL> def __do_cursor ( cur , res = '<STR_LIT>' ) : <EOL> cursor = cur . window . cursor <EOL> line = cur . buffer [ cursor [ <NUM_LIT:0> ] - <NUM_LIT:1> ] <EOL> unwordable = '<STR_LIT>' . join ( ( string . punctuation , string . whitespace ) ) <EOL> x = line [ cursor [ <NUM_LIT:1> ] ] <EOL> i = <NUM_LIT:0> <EOL> while x not in unwordable and i + cursor [ <NUM_LIT:1> ] >= <NUM_LIT:0> : <EOL> res = '<STR_LIT>' . join ( ( x , res ) ) <EOL> i -= <NUM_LIT:1> <EOL> x = line [ cursor [ <NUM_LIT:1> ] + i ] <EOL> x = line [ cursor [ <NUM_LIT:1> ] + <NUM_LIT:1> ] <EOL> i = <NUM_LIT:1> <EOL> while x not in unwordable and i + cursor [ <NUM_LIT:1> ] <= len ( line ) : <EOL> res = "<STR_LIT>" . join ( ( res , x ) ) <EOL> i += <NUM_LIT:1> <EOL> x = line [ cursor [ <NUM_LIT:1> ] + i ] <EOL> return res <EOL> def vim_gtranslate ( ) : <EOL> to_lang = vim . eval ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> from_lang = vim . eval ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> trans = translate . Translator ( to_lang = to_lang , from_lang = from_lang ) <EOL> encoding = vim . eval ( '<STR_LIT>' ) or '<STR_LIT:utf-8>' <EOL> buff = vim . current . buffer <EOL> cur = vim . current <EOL> cursor = cur . window . cursor <EOL> res = '<STR_LIT>' <EOL> if buff . mark ( '<STR_LIT:<>' ) and buff . mark ( '<STR_LIT:>>' ) and cursor [ <NUM_LIT:1> ] == <NUM_LIT:0> and buff . mark ( '<STR_LIT:<>' ) [ <NUM_LIT:0> ] <= cursor [ <NUM_LIT:0> ] <= buff . mark ( '<STR_LIT:>>' ) [ <NUM_LIT:0> ] : <EOL> res = __do_visual ( buff , cur ) <EOL> else : <EOL> res = __do_cursor ( cur ) <EOL> translated = '<STR_LIT>' <EOL> try : <EOL> translated = trans . translate ( res if res else '<STR_LIT>' ) <EOL> except urllib2 . HTTPError : <EOL> pass <EOL> print translated . encode ( encoding ) </s>
