2012-01-10 18:11:26 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
This file contains compatibility code to stay compatible with
|
|
|
|
as many python versions as possible.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
import vim # pylint:disable=import-error
|
2012-01-10 19:56:09 +01:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
def _vim_dec(string):
|
|
|
|
"""Decode 'string' using &encoding."""
|
2012-06-26 10:10:42 +02:00
|
|
|
try:
|
2014-02-06 21:42:07 +01:00
|
|
|
return string.decode(vim.eval("&encoding"))
|
2012-06-26 10:10:42 +02:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
# At least we tried. There might be some problems down the road now
|
2014-02-06 21:42:07 +01:00
|
|
|
return string
|
2012-06-26 10:10:42 +02:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
def _vim_enc(string):
|
|
|
|
"""Encode 'string' using &encoding."""
|
2012-06-26 10:10:42 +02:00
|
|
|
try:
|
2014-02-06 21:42:07 +01:00
|
|
|
return string.encode(vim.eval("&encoding"))
|
2012-06-26 10:10:42 +02:00
|
|
|
except UnicodeEncodeError:
|
2014-02-06 21:42:07 +01:00
|
|
|
return string
|
2012-01-10 18:11:34 +01:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
if sys.version_info >= (3, 0):
|
2014-02-23 11:19:11 +01:00
|
|
|
def open_ascii_file(filename, mode):
|
|
|
|
"""Opens a file in "r" mode."""
|
|
|
|
return open(filename, mode, encoding="utf-8")
|
|
|
|
|
2012-02-16 21:42:26 +01:00
|
|
|
def col2byte(line, col):
|
|
|
|
"""
|
|
|
|
Convert a valid column index into a byte index inside
|
|
|
|
of vims buffer.
|
|
|
|
"""
|
2012-01-10 19:56:09 +01:00
|
|
|
pre_chars = vim.current.buffer[line-1][:col]
|
2012-06-26 10:10:42 +02:00
|
|
|
return len(_vim_enc(pre_chars))
|
2012-02-16 21:42:26 +01:00
|
|
|
|
|
|
|
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]
|
2012-06-26 10:10:42 +02:00
|
|
|
raw_bytes = _vim_enc(line)[:nbyte]
|
|
|
|
return len(_vim_dec(raw_bytes))
|
2012-01-10 19:56:09 +01:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
def as_unicode(string):
|
|
|
|
"""Return 'string' as unicode instance."""
|
|
|
|
if isinstance(string, bytes):
|
|
|
|
return _vim_dec(string)
|
|
|
|
return str(string)
|
2012-01-10 18:11:34 +01:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
def as_vimencoding(string):
|
|
|
|
"""Return 'string' as Vim internal encoding."""
|
|
|
|
return string
|
2012-01-10 18:11:26 +01:00
|
|
|
else:
|
2012-02-07 21:54:03 +01:00
|
|
|
import warnings
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
2014-02-23 11:19:11 +01:00
|
|
|
def open_ascii_file(filename, mode):
|
|
|
|
"""Opens a file in "r" mode."""
|
|
|
|
return open(filename, mode)
|
2012-02-07 21:54:03 +01:00
|
|
|
|
2012-02-16 21:42:26 +01:00
|
|
|
def col2byte(line, col):
|
|
|
|
"""
|
|
|
|
Convert a valid column index into a byte index inside
|
|
|
|
of vims buffer.
|
|
|
|
"""
|
2012-06-26 10:10:42 +02:00
|
|
|
pre_chars = _vim_dec(vim.current.buffer[line-1])[:col]
|
|
|
|
return len(_vim_enc(pre_chars))
|
2012-02-16 21:42:26 +01:00
|
|
|
|
|
|
|
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
|
2012-06-26 10:10:42 +02:00
|
|
|
return len(_vim_dec(line[:nbyte]))
|
2012-01-10 19:56:09 +01:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
def as_unicode(string):
|
|
|
|
"""Return 'string' as unicode instance."""
|
|
|
|
if isinstance(string, str):
|
|
|
|
return _vim_dec(string)
|
|
|
|
return unicode(string)
|
2012-01-10 18:11:34 +01:00
|
|
|
|
2014-02-06 21:42:07 +01:00
|
|
|
def as_vimencoding(string):
|
|
|
|
"""Return 'string' as unicode instance."""
|
|
|
|
return _vim_enc(string)
|