From fee8a23d2daffb68f1a35f0bc1f1ceb4e2b2daf1 Mon Sep 17 00:00:00 2001 From: LFDM <1986gh@gmail.com> Date: Fri, 31 Jan 2014 16:18:14 +0100 Subject: [PATCH 1/4] Retains the unnamed register during jumps --- plugin/UltiSnips/__init__.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 575eae6..b63849e 100755 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -553,8 +553,13 @@ class SnippetManager(object): self._supertab_keys = None self._csnippets = [] + # needed to retain the unnamed register at all times + self._unnamed_reg_cached = False + self._last_placeholder = None + self.reset() + @err_to_scratch_buffer def reset(self, test_error=False): self._vstate = VimState() @@ -832,10 +837,23 @@ class SnippetManager(object): self._current_snippet_is_done() jumped = self._jump(backwards) if jumped: + self._cache_unnamed_register() self._vstate.remember_position() self._ignore_movements = True return jumped + def _cache_unnamed_register(self): + self._unnamed_reg_cached = True + unnamed_reg = _vim.eval('@"') + if self._last_placeholder != unnamed_reg: + self._unnamed_reg_cache = _vim.eval('@"') + self._last_placeholder = self._ctab._initial_text + + def restore_unnamed_register(self): + if self._unnamed_reg_cached: + _vim.command( "let @\"='%s'" % self._unnamed_reg_cache) + self._unnamed_register_cached = False + def _handle_failure(self, trigger): """ Mainly make sure that we play well with SuperTab From bf3bfdac9cf6725cee0385a3cf3bcc6abc761506 Mon Sep 17 00:00:00 2001 From: LFDM <1986gh@gmail.com> Date: Fri, 31 Jan 2014 16:20:31 +0100 Subject: [PATCH 2/4] Adds eventhandler to restore the unnamed register --- plugin/UltiSnips.vim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugin/UltiSnips.vim b/plugin/UltiSnips.vim index 67d0b64..bdbedcd 100644 --- a/plugin/UltiSnips.vim +++ b/plugin/UltiSnips.vim @@ -227,6 +227,11 @@ endf function! UltiSnips_LeavingBuffer() exec g:_uspy "UltiSnips_Manager.leaving_buffer()" endf + +function! UltiSnips_LeavingInsertMode() + exec g:_uspy "UltiSnips_Manager.restore_unnamed_register()" +endfunction + " }}} " COMPLETE FUNCTIONS {{{ function! UltiSnipsFiletypeComplete(arglead, cmdline, cursorpos) @@ -262,6 +267,7 @@ exec g:_uspy "UltiSnips_Manager.backward_trigger = vim.eval('g:UltiSnipsJumpBack au CursorMovedI * call UltiSnips_CursorMoved() au CursorMoved * call UltiSnips_CursorMoved() au BufLeave * call UltiSnips_LeavingBuffer() +au InsertLeave * call UltiSnips_LeavingInsertMode() call UltiSnips_MapKeys() From 60f1089d9dd02306aeefdc24acc3f84949f9292f Mon Sep 17 00:00:00 2001 From: LFDM <1986gh@gmail.com> Date: Fri, 31 Jan 2014 16:55:42 +0100 Subject: [PATCH 3/4] Escapes the cached string --- plugin/UltiSnips/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index b63849e..eb58fc3 100755 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -846,12 +846,13 @@ class SnippetManager(object): self._unnamed_reg_cached = True unnamed_reg = _vim.eval('@"') if self._last_placeholder != unnamed_reg: - self._unnamed_reg_cache = _vim.eval('@"') + self._unnamed_reg_cache = unnamed_reg self._last_placeholder = self._ctab._initial_text def restore_unnamed_register(self): if self._unnamed_reg_cached: - _vim.command( "let @\"='%s'" % self._unnamed_reg_cache) + escaped_cache = self._unnamed_reg_cache.replace("'", "''") + _vim.command("let @\"='%s'" % escaped_cache) self._unnamed_register_cached = False def _handle_failure(self, trigger): From f26a2ce83000948194d16da0cfd64b1d32fb9435 Mon Sep 17 00:00:00 2001 From: LFDM <1986gh@gmail.com> Date: Sat, 1 Feb 2014 01:43:43 +0100 Subject: [PATCH 4/4] Adds a test for the new feature --- test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test.py b/test.py index a1f6ed0..d7207bf 100755 --- a/test.py +++ b/test.py @@ -3041,6 +3041,13 @@ class Bug1251994(_VimTest): wanted = " world hello;blub" # End: 1251994 #}}} +# Test for Github Pull Request #134 {{{# +class RetainsTheUnnamedRegister(_VimTest): + snippets = ("test", "${1:hello} ${2:world} ${0}") + keys = "yank" + ESC + "by4lea test" + EX + "HELLO" + JF + JF + ESC + "p" + wanted = "yank HELLO world yank" +# End: Github Pull Request # 134 #}}} + class VerifyVimDict1(_VimTest): """check: correct type (4 means vim dictionary)