Skip to content

Commit

Permalink
#183 - use opposite motions for some motions when repeating motions m…
Browse files Browse the repository at this point in the history
…ade in visual modes
  • Loading branch information
guillermooo committed Sep 8, 2013
1 parent 647e098 commit 78cf129
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions actions.py
Expand Up @@ -635,6 +635,11 @@ def run(self):
# Unreachable.
return

# Signal that we're not simply issuing an interactive command, but rather repeating one.
# This is necessary, for example, to notify _vi_k that it should become _vi_j instead
# if the former was run in visual mode.
state.settings.vi['_is_repeating'] = True

if not cmd:
return
elif cmd == 'vi_run':
Expand All @@ -659,6 +664,7 @@ def run(self):
# XXX: Needed here? Maybe enter_... type commands should be IrreversibleTextCommands so we
# must/can call them whenever we need them withouth affecting the undo stack.
self.view.run_command('vi_enter_normal_mode')
state.settings.vi['_is_repeating'] = False


class _vi_redo(IrreversibleTextCommand):
Expand Down
28 changes: 26 additions & 2 deletions transformers_visual.py
Expand Up @@ -13,6 +13,20 @@
from Vintageous.vi.text_objects import get_text_object_region


class AntonymAwarenessMixin(object):
"""
Some motions need translation when they're being repeated after having
been used in visual modes.
For example, _vi_k must become _vi_j if repeated after being used in
some visual modes.
"""
def must_run_antonym(self, mode=None):
if (VintageState(self.view).settings.vi['_is_repeating'] and
mode in (MODE_VISUAL, MODE_VISUAL_LINE)):
return True


class ExtendToMinimalWidth(sublime_plugin.TextCommand):
def run(self, edit):
def f(view, s):
Expand Down Expand Up @@ -633,7 +647,7 @@ def f(view, s):
regions_transformer(self.view, f)


class _vi_h(sublime_plugin.TextCommand):
class _vi_h(sublime_plugin.TextCommand, AntonymAwarenessMixin):
def run(self, edit, count=None, mode=None):
def f(view, s):
if mode == _MODE_INTERNAL_NORMAL:
Expand Down Expand Up @@ -664,6 +678,10 @@ def f(view, s):
# XXX: We should never reach this.
return s

if self.must_run_antonym(mode=mode):
self.view.run_command('_vi_l', {'mode': mode, 'count': count})
return

# For jagged selections (on the rhs), only those sticking out need to move leftwards.
# Example ([] denotes the selection):
#
Expand Down Expand Up @@ -817,7 +835,7 @@ def f(view, s):
regions_transformer(self.view, f)


class _vi_k(sublime_plugin.TextCommand):
class _vi_k(sublime_plugin.TextCommand, AntonymAwarenessMixin):
def previous_non_folded_pt(self, pt):
# FIXME: If we have two contiguous folds, this method will fail.
# Handle folded regions.
Expand Down Expand Up @@ -919,6 +937,12 @@ def f(view, s):

return sublime.Region(s.a, view.full_line(target_pt).a)

if self.must_run_antonym(mode=mode):
self.view.run_command('_vi_j', {'count': count,
'mode': mode,
'xpos': xpos})
return

if mode == MODE_VISUAL_BLOCK:
# Don't do anything if we have reversed selections.
if any((r.b < r.a) for r in self.view.sel()):
Expand Down

0 comments on commit 78cf129

Please sign in to comment.