Retry unmapping when we switched buffers.

Fixes #229.
This commit is contained in:
Holger Rapp 2014-03-14 20:35:14 +01:00
parent b0a3266a61
commit 5ac0436694
3 changed files with 48 additions and 32 deletions

View File

@ -18,21 +18,3 @@ function! UltiSnips#map_keys#MapKeys()
snoremap <silent> <DEL> <c-g>c snoremap <silent> <DEL> <c-g>c
snoremap <silent> <c-h> <c-g>c snoremap <silent> <c-h> <c-g>c
endf endf
function! UltiSnips#map_keys#MapInnerKeys()
if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger
exec "inoremap <buffer> <silent> " . g:UltiSnipsJumpForwardTrigger . " <C-R>=UltiSnips#JumpForwards()<cr>"
exec "snoremap <buffer> <silent> " . g:UltiSnipsJumpForwardTrigger . " <Esc>:call UltiSnips#JumpForwards()<cr>"
endif
exec "inoremap <buffer> <silent> " . g:UltiSnipsJumpBackwardTrigger . " <C-R>=UltiSnips#JumpBackwards()<cr>"
exec "snoremap <buffer> <silent> " . g:UltiSnipsJumpBackwardTrigger . " <Esc>:call UltiSnips#JumpBackwards()<cr>"
endf
function! UltiSnips#map_keys#RestoreInnerKeys()
if g:UltiSnipsExpandTrigger != g:UltiSnipsJumpForwardTrigger
exec "iunmap <buffer> " . g:UltiSnipsJumpForwardTrigger
exec "sunmap <buffer> " . g:UltiSnipsJumpForwardTrigger
endif
exec "iunmap <buffer> " . g:UltiSnipsJumpBackwardTrigger
exec "sunmap <buffer> " . g:UltiSnipsJumpBackwardTrigger
endf

View File

@ -120,7 +120,8 @@ class SnippetDefinition(object):
match = (words_suffix == self._trigger) match = (words_suffix == self._trigger)
if match and words_prefix: if match and words_prefix:
# Require a word boundary between prefix and suffix. # 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' match = _vim.eval('"%s" =~# "\\\\v.<."' % boundary_chars) != '0'
elif "i" in self._opts: elif "i" in self._opts:
match = words.endswith(self._trigger) match = words.endswith(self._trigger)

View File

@ -76,6 +76,7 @@ class SnippetManager(object):
self.expand_trigger = expand_trigger self.expand_trigger = expand_trigger
self.forward_trigger = forward_trigger self.forward_trigger = forward_trigger
self.backward_trigger = backward_trigger self.backward_trigger = backward_trigger
self._inner_mappings_in_place = False
self._supertab_keys = None self._supertab_keys = None
self._csnippets = [] self._csnippets = []
@ -207,18 +208,18 @@ class SnippetManager(object):
return False return False
def register_snippet_source(self, name, snippet_source): def register_snippet_source(self, name, snippet_source):
"""Registers a new 'snippet_source' with the given 'name'. The given class """Registers a new 'snippet_source' with the given 'name'. The given
must be an instance of SnippetSource. This source will be queried for class must be an instance of SnippetSource. This source will be queried
snippets.""" for snippets."""
self._snippet_sources.append((name, snippet_source)) self._snippet_sources.append((name, snippet_source))
def unregister_snippet_source(self, name): def unregister_snippet_source(self, name):
"""Unregister the source with the given 'name'. Does nothing if it is not """Unregister the source with the given 'name'. Does nothing if it is
registered.""" not registered."""
for index, (source_name, _) in enumerate(self._snippet_sources): for index, (source_name, _) in enumerate(self._snippet_sources):
if name == source_name: if name == source_name:
self._snippet_sources = \ self._snippet_sources = self._snippet_sources[:index] + \
self._snippet_sources[:index] + self._snippet_sources[index+1:] self._snippet_sources[index+1:]
break break
def reset_buffer_filetypes(self): def reset_buffer_filetypes(self):
@ -244,6 +245,8 @@ class SnippetManager(object):
@err_to_scratch_buffer @err_to_scratch_buffer
def _cursor_moved(self): def _cursor_moved(self):
"""Called whenever the cursor moved.""" """Called whenever the cursor moved."""
if not self._csnippets and self._inner_mappings_in_place:
self._unmap_inner_keys()
self._vstate.remember_position() self._vstate.remember_position()
if _vim.eval("mode()") not in 'in': if _vim.eval("mode()") not in 'in':
return return
@ -306,6 +309,36 @@ class SnippetManager(object):
self._csnippets[0].update_textobjects() self._csnippets[0].update_textobjects()
self._vstate.remember_buffer(self._csnippets[0]) 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 <buffer> <silent> " + self.forward_trigger +
" <C-R>=UltiSnips#JumpForwards()<cr>")
_vim.command("snoremap <buffer> <silent> " + self.forward_trigger +
" <Esc>:call UltiSnips#JumpForwards()<cr>")
_vim.command("inoremap <buffer> <silent> " + self.backward_trigger +
" <C-R>=UltiSnips#JumpBackwards()<cr>")
_vim.command("snoremap <buffer> <silent> " + self.backward_trigger +
" <Esc>:call UltiSnips#JumpBackwards()<cr>")
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 <buffer> %s" % self.forward_trigger)
_vim.command("sunmap <buffer> %s" % self.forward_trigger)
_vim.command("iunmap <buffer> %s" % self.backward_trigger)
_vim.command("sunmap <buffer> %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 @err_to_scratch_buffer
def _save_last_visual_selection(self): def _save_last_visual_selection(self):
""" """
@ -340,7 +373,7 @@ class SnippetManager(object):
"""The current snippet should be terminated.""" """The current snippet should be terminated."""
self._csnippets.pop() self._csnippets.pop()
if not self._csnippets: if not self._csnippets:
_vim.command("call UltiSnips#map_keys#RestoreInnerKeys()") self._unmap_inner_keys()
def _jump(self, backwards=False): def _jump(self, backwards=False):
"""Helper method that does the actual jump.""" """Helper method that does the actual jump."""
@ -431,7 +464,7 @@ class SnippetManager(object):
def _do_snippet(self, snippet, before): def _do_snippet(self, snippet, before):
"""Expands the given snippet, and handles everything """Expands the given snippet, and handles everything
that needs to be done with it.""" 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 # Adjust before, maybe the trigger is not the complete word
text_before = before text_before = before