diff --git a/plugin/UltiSnips/Util.py b/plugin/UltiSnips/Util.py index 9eb3d3c..741725c 100644 --- a/plugin/UltiSnips/Util.py +++ b/plugin/UltiSnips/Util.py @@ -4,6 +4,21 @@ import vim import os +def vim_string(inp): + """ Creates a vim-friendly string from a group of + dicts, lists and strings. + """ + def conv(obj): + if isinstance(obj, list): + return u'[' + u",".join([conv(o) for o in obj]) + u']' + elif isinstance(obj, dict): + return u'{' + u','.join([ + u"%s:%s" % (conv(key), conv(value)) + for key, value in obj.iteritems()]) + u'}' + else: + return u'"%s"' % str(obj).replace(u'"', u'\\"') + return conv(inp) + class IndentUtil(object): """ Utility class for dealing properly with indentation. """ diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 0b29583..e535570 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -14,7 +14,7 @@ import vim from UltiSnips.Geometry import Position from UltiSnips.TextObjects import * from UltiSnips.Buffer import VimBuffer -from UltiSnips.Util import IndentUtil +from UltiSnips.Util import IndentUtil, vim_string from UltiSnips.Langmap import LangMapTranslator # The following lines silence DeprecationWarnings. They are raised @@ -51,17 +51,13 @@ Following is the full stack trace: _to_scratch_buffer(s) return wrapper -def _vim_quote(s): - """Quote string s as Vim literal string.""" - return "'" + s.replace("'", "''") + "'" - def feedkeys(s, mode='n'): """Wrapper around vim's feedkeys function. Mainly for convenience.""" vim.command(r'call feedkeys("%s", "%s")' % (s, mode)) def echom(mes, *args): mes = mes % args - vim.command('echom "%s"' % mes.replace('"', '\\"')) + vim.command('echom %s' % vim_string(mes)) class _SnippetDictionary(object): def __init__(self, *args, **kwargs): @@ -151,7 +147,7 @@ class _SnippetsFileParser(object): self._idx = 0 def _error(self, msg): - fn = vim.eval("""fnamemodify(%s, ":~:.")""" % _vim_quote(self._fn)) + fn = vim.eval("""fnamemodify(%s, ":~:.")""" % vim_string(self._fn)) self._sm._error("%s in %s(%d)" % (msg, fn, self._idx + 1)) def _line(self): @@ -831,7 +827,7 @@ class SnippetManager(object): # Private/Protect Functions Below # ################################### def _error(self, msg): - msg = _vim_quote("UltiSnips: " + msg) + msg = vim_string("UltiSnips: " + msg) if self._test_error: msg = msg.replace('"', r'\"') msg = msg.replace('|', r'\|')