From be07baf2a8b2a25c8cde20fbdefda208e173590d Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Wed, 16 Feb 2011 13:38:51 +0100 Subject: [PATCH] Handle the case when vim was not compiled with the langmap option --- plugin/UltiSnips/Langmap.py | 61 ++++++++++++++++++++++++++++++++++++ plugin/UltiSnips/__init__.py | 38 +--------------------- 2 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 plugin/UltiSnips/Langmap.py diff --git a/plugin/UltiSnips/Langmap.py b/plugin/UltiSnips/Langmap.py new file mode 100644 index 0000000..5c09b1c --- /dev/null +++ b/plugin/UltiSnips/Langmap.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" +This file contains object to care 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. +""" + +import string + +import vim + +class Real_LangMapTranslator(object): + """ + This is the object to deal with langmaps if this option is compiled + into vim. + """ + _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 Dummy_LangMapTranslator(object): + """ + If vim hasn't got the langmap compiled in, we never have to do anything. + Then this class is used. + """ + translate = lambda self, s: s + +LangMapTranslator = Real_LangMapTranslator +if not int(vim.eval('has("langmap")')): + LangMapTranslator = Dummy_LangMapTranslator + + + diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 17e8f8c..9b98c27 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -11,6 +11,7 @@ from UltiSnips.Geometry import Position from UltiSnips.TextObjects import * from UltiSnips.Buffer import VimBuffer from UltiSnips.Util import IndentUtil +from UltiSnips.Langmap import LangMapTranslator # The following lines silence DeprecationWarnings. They are raised # by python2.6 for vim.error (which is a string that is used as an exception, @@ -375,43 +376,6 @@ class Snippet(object): return SnippetInstance(parent, indent, v, start, 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): def __init__(self): self._abs_pos = None