From 5ac0436694ae997b440a7a768d736f536ea59254 Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Fri, 14 Mar 2014 20:35:14 +0100 Subject: [PATCH] Retry unmapping when we switched buffers. Fixes #229. --- autoload/UltiSnips/map_keys.vim | 18 ------ pythonx/UltiSnips/snippet/definition/_base.py | 3 +- pythonx/UltiSnips/snippet_manager.py | 59 +++++++++++++++---- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/autoload/UltiSnips/map_keys.vim b/autoload/UltiSnips/map_keys.vim index 3b44aea..44e4082 100644 --- a/autoload/UltiSnips/map_keys.vim +++ b/autoload/UltiSnips/map_keys.vim @@ -18,21 +18,3 @@ function! UltiSnips#map_keys#MapKeys() snoremap c snoremap c endf - -function! UltiSnips#map_keys#MapInnerKeys() - if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger - exec "inoremap " . g:UltiSnipsJumpForwardTrigger . " =UltiSnips#JumpForwards()" - exec "snoremap " . g:UltiSnipsJumpForwardTrigger . " :call UltiSnips#JumpForwards()" - endif - exec "inoremap " . g:UltiSnipsJumpBackwardTrigger . " =UltiSnips#JumpBackwards()" - exec "snoremap " . g:UltiSnipsJumpBackwardTrigger . " :call UltiSnips#JumpBackwards()" -endf - -function! UltiSnips#map_keys#RestoreInnerKeys() - if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger - exec "iunmap " . g:UltiSnipsJumpForwardTrigger - exec "sunmap " . g:UltiSnipsJumpForwardTrigger - endif - exec "iunmap " . g:UltiSnipsJumpBackwardTrigger - exec "sunmap " . g:UltiSnipsJumpBackwardTrigger -endf diff --git a/pythonx/UltiSnips/snippet/definition/_base.py b/pythonx/UltiSnips/snippet/definition/_base.py index bdb0685..833ca04 100644 --- a/pythonx/UltiSnips/snippet/definition/_base.py +++ b/pythonx/UltiSnips/snippet/definition/_base.py @@ -120,7 +120,8 @@ class SnippetDefinition(object): match = (words_suffix == self._trigger) if match and words_prefix: # Require a word boundary between prefix and suffix. - boundary_chars = escape(words_prefix[-1:] + words_suffix[:1], r'\"') + boundary_chars = escape(words_prefix[-1:] + \ + words_suffix[:1], r'\"') match = _vim.eval('"%s" =~# "\\\\v.<."' % boundary_chars) != '0' elif "i" in self._opts: match = words.endswith(self._trigger) diff --git a/pythonx/UltiSnips/snippet_manager.py b/pythonx/UltiSnips/snippet_manager.py index 44db2b9..ee4299d 100644 --- a/pythonx/UltiSnips/snippet_manager.py +++ b/pythonx/UltiSnips/snippet_manager.py @@ -76,6 +76,7 @@ class SnippetManager(object): self.expand_trigger = expand_trigger self.forward_trigger = forward_trigger self.backward_trigger = backward_trigger + self._inner_mappings_in_place = False self._supertab_keys = None self._csnippets = [] @@ -207,19 +208,19 @@ class SnippetManager(object): return False def register_snippet_source(self, name, snippet_source): - """Registers a new 'snippet_source' with the given 'name'. The given class - must be an instance of SnippetSource. This source will be queried for - snippets.""" - self._snippet_sources.append((name, snippet_source)) + """Registers a new 'snippet_source' with the given 'name'. The given + class must be an instance of SnippetSource. This source will be queried + for snippets.""" + self._snippet_sources.append((name, snippet_source)) def unregister_snippet_source(self, name): - """Unregister the source with the given 'name'. Does nothing if it is not - registered.""" - for index, (source_name, _) in enumerate(self._snippet_sources): - if name == source_name: - self._snippet_sources = \ - self._snippet_sources[:index] + self._snippet_sources[index+1:] - break + """Unregister the source with the given 'name'. Does nothing if it is + not registered.""" + for index, (source_name, _) in enumerate(self._snippet_sources): + if name == source_name: + self._snippet_sources = self._snippet_sources[:index] + \ + self._snippet_sources[index+1:] + break def reset_buffer_filetypes(self): """Reset the filetypes for the current buffer.""" @@ -244,6 +245,8 @@ class SnippetManager(object): @err_to_scratch_buffer def _cursor_moved(self): """Called whenever the cursor moved.""" + if not self._csnippets and self._inner_mappings_in_place: + self._unmap_inner_keys() self._vstate.remember_position() if _vim.eval("mode()") not in 'in': return @@ -306,6 +309,36 @@ class SnippetManager(object): self._csnippets[0].update_textobjects() self._vstate.remember_buffer(self._csnippets[0]) + def _map_inner_keys(self): + """Map keys that should only be defined when a snippet is active.""" + if self.expand_trigger != self.forward_trigger: + _vim.command("inoremap " + self.forward_trigger + + " =UltiSnips#JumpForwards()") + _vim.command("snoremap " + self.forward_trigger + + " :call UltiSnips#JumpForwards()") + _vim.command("inoremap " + self.backward_trigger + + " =UltiSnips#JumpBackwards()") + _vim.command("snoremap " + self.backward_trigger + + " :call UltiSnips#JumpBackwards()") + self._inner_mappings_in_place = True + + def _unmap_inner_keys(self): + """Unmap keys that should not be active when no snippet is active.""" + if not self._inner_mappings_in_place: + return + try: + if self.expand_trigger != self.forward_trigger: + _vim.command("iunmap %s" % self.forward_trigger) + _vim.command("sunmap %s" % self.forward_trigger) + _vim.command("iunmap %s" % self.backward_trigger) + _vim.command("sunmap %s" % self.backward_trigger) + self._inner_mappings_in_place = False + except _vim.error: + # This happens when a preview window was opened. This issues + # CursorMoved, but not BufLeave. We have no way to unmap, until we + # are back in our buffer + pass + @err_to_scratch_buffer def _save_last_visual_selection(self): """ @@ -340,7 +373,7 @@ class SnippetManager(object): """The current snippet should be terminated.""" self._csnippets.pop() if not self._csnippets: - _vim.command("call UltiSnips#map_keys#RestoreInnerKeys()") + self._unmap_inner_keys() def _jump(self, backwards=False): """Helper method that does the actual jump.""" @@ -431,7 +464,7 @@ class SnippetManager(object): def _do_snippet(self, snippet, before): """Expands the given snippet, and handles everything that needs to be done with it.""" - _vim.command("call UltiSnips#map_keys#MapInnerKeys()") + self._map_inner_keys() # Adjust before, maybe the trigger is not the complete word text_before = before