Encapsulated the translation of the langmap including caching of results into a own object

This commit is contained in:
Holger Rapp 2010-08-18 12:52:56 +02:00
parent 1637cbef8a
commit e3d9896cf2

View File

@ -368,6 +368,44 @@ class Snippet(object):
return SnippetInstance(parent, indent, v, start, return SnippetInstance(parent, indent, v, start,
end, last_re = self._last_re, globals = self._globals) end, last_re = self._last_re, globals = self._globals)
class LangMapTranslator(object):
"""
This object cares for the vim langmap option and basically reverses
the mappings. This was the only solution to get UltiSnips to work
nicely with langmap; other stuff I tried was using inoremap movement
commands and caching and restoring the langmap option.
Note that this will not work if the langmap overwrites a character
completely, for example if 'j' is remapped, but nothing is mapped
back to 'j', then moving one line down is no longer possible and
UltiSnips will fail.
"""
_maps = {}
def _create_translation(self, langmap):
from_chars, to_chars = "", ""
for c in langmap.split(','):
if ";" in c:
a,b = c.split(';')
from_chars += a
to_chars += b
else:
from_chars += c[::2]
to_chars += c[1::2]
self._maps[langmap] = string.maketrans(to_chars, from_chars)
def translate(self, s):
langmap = vim.eval("&langmap").strip()
if langmap == "":
return s
if langmap not in self._maps:
self._create_translation(langmap)
return s.translate(self._maps[langmap])
class VimState(object): class VimState(object):
def __init__(self): def __init__(self):
self._abs_pos = None self._abs_pos = None
@ -446,30 +484,11 @@ class VimState(object):
else: else:
do_select = "%ih" % (-delta.col+1) do_select = "%ih" % (-delta.col+1)
def unmap(s): move_cmd = LangMapTranslator().translate(
from_chars, to_chars = "", "" r"\<Esc>%sv%s%s\<c-g>" % (move_one_right, move_lines, do_select)
for c in vim.eval("&langmap").split(','): )
if ";" in c:
a,b = c.split(';')
from_chars += a
to_chars += b
else:
from_chars += c[::2]
to_chars += c[1::2]
rv = "" feedkeys(move_cmd)
for c in s:
if c not in from_chars:
rv += c
continue
if c not in to_chars:
raise RuntimeError, "langmap overwrite %s" % c
rv += from_chars[to_chars.index(c)]
return rv
feedkeys(unmap(r"\<Esc>%sv%s%s\<c-g>" %
(move_one_right, move_lines, do_select)))
def buf_changed(self): def buf_changed(self):