1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#!/usr/bin/env python
# encoding: utf-8
"""This file contains compatibility code to stay compatible with as many python
versions as possible."""
import sys
import vim # pylint:disable=import-error
if sys.version_info >= (3, 0):
def _vim_dec(string):
"""Decode 'string' using &encoding."""
# We don't have the luxury here of failing, everything
# falls apart if we don't return a bytearray from the
# passed in string
return string.decode(vim.eval("&encoding"), "replace")
def _vim_enc(bytearray):
"""Encode 'string' using &encoding."""
# We don't have the luxury here of failing, everything
# falls apart if we don't return a string from the passed
# in bytearray
return bytearray.encode(vim.eval("&encoding"), "replace")
def open_ascii_file(filename, mode):
"""Opens a file in "r" mode."""
return open(filename, mode, encoding="utf-8")
def col2byte(line, col):
"""Convert a valid column index into a byte index inside of vims
buffer."""
# We pad the line so that selecting the +1 st column still works.
pre_chars = (vim.current.buffer[line - 1] + " ")[:col]
return len(_vim_enc(pre_chars))
def byte2col(line, nbyte):
"""Convert a column into a byteidx suitable for a mark or cursor
position inside of vim."""
line = vim.current.buffer[line - 1]
raw_bytes = _vim_enc(line)[:nbyte]
return len(_vim_dec(raw_bytes))
def as_unicode(string):
"""Return 'string' as unicode instance."""
if isinstance(string, bytes):
return _vim_dec(string)
return str(string)
def as_vimencoding(string):
"""Return 'string' as Vim internal encoding."""
return string
else:
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
def _vim_dec(string):
"""Decode 'string' using &encoding."""
try:
return string.decode(vim.eval("&encoding"))
except UnicodeDecodeError:
# At least we tried. There might be some problems down the road now
return string
def _vim_enc(string):
"""Encode 'string' using &encoding."""
try:
return string.encode(vim.eval("&encoding"))
except UnicodeDecodeError:
return string
except UnicodeEncodeError:
return string
def open_ascii_file(filename, mode):
"""Opens a file in "r" mode."""
return open(filename, mode)
def col2byte(line, col):
"""Convert a valid column index into a byte index inside of vims
buffer."""
# We pad the line so that selecting the +1 st column still works.
pre_chars = _vim_dec(vim.current.buffer[line - 1] + " ")[:col]
return len(_vim_enc(pre_chars))
def byte2col(line, nbyte):
"""Convert a column into a byteidx suitable for a mark or cursor
position inside of vim."""
line = vim.current.buffer[line - 1]
if nbyte >= len(line): # This is beyond end of line
return nbyte
return len(_vim_dec(line[:nbyte]))
def as_unicode(string):
"""Return 'string' as unicode instance."""
if isinstance(string, str):
return _vim_dec(string)
return unicode(string)
def as_vimencoding(string):
"""Return 'string' as unicode instance."""
return _vim_enc(string)
|