Skip to content

Commit

Permalink
Hitting the shortcut multiple times will now swap the search direction.
Browse files Browse the repository at this point in the history
  • Loading branch information
xavi- committed Aug 25, 2013
1 parent 9d09e52 commit 6c72a67
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -13,6 +13,8 @@ Fill a void in your Sublime Text multiple selection capabilities! This plugin al
- `-/regex/`: backwards regex.
- `-{character count}`: select backwards a certain number of characters (`{-count}` works too).

- <kbd>ctrl/alt</kbd> + <kbd>shift</kbd> + <kbd>s</kbd> a second time: reverses the search direction.

- <kbd>ctrl/alt</kbd> + <kbd>shift</kbd> + <kbd>r</kbd>: reverse all selections (so if the insertion point is at the end of the selection, it is moved to the beginning, and vice versa).

On Windows and Linux the default shortcuts use <kbd>alt</kbd>. On OSX <kbd>ctrl</kbd> is used.
Expand Down
44 changes: 42 additions & 2 deletions select-until.py
Expand Up @@ -15,6 +15,9 @@ def clean_up(view):
view.erase_regions("select-until")
view.erase_regions("select-until-originals")

SelectUntilCommand.input_panel = None
SelectUntilCommand.first_opened = True

def on_done(view, extend):
if extend:
newSels = view.get_regions("select-until-extended")
Expand Down Expand Up @@ -59,6 +62,22 @@ def find_matching_point(view, sel, selector):
return region.begin()
return -1

def negate_selector(selector):
if selector == "": return selector

result = rSelector.search(selector)

if result is None: return True, "-[" + selector + "]"

groups = result.groups()
makeReverse = (groups[0] != "-")
num, chars, regex = groups[1:]

negateChar = "-" if makeReverse else ""
if num is not None: return makeReverse, negateChar + "{" + num + "}"
elif chars is not None: return makeReverse, negateChar + "[" + chars + "]"
elif regex is not None: return makeReverse, negateChar + "/" + regex + "/"

def on_change(view, oriSels, selector, extend):
SelectUntilCommand.temp = selector
extendedSels = []
Expand Down Expand Up @@ -92,6 +111,8 @@ def on_cancel(view, oriSels):
class SelectUntilCommand(sublime_plugin.TextCommand):
temp = ""
prevSelector = ""
input_panel = None
first_opened = True

def run(self, edit, extend):
view = self.view
Expand All @@ -104,8 +125,27 @@ def run(self, edit, extend):
lambda selector: on_change(view, oriSels, selector, extend),
lambda : on_cancel(view, oriSels)
)
v.sel().clear()
v.sel().add(Region(0, v.size()))

# view.window().show_input_panel returns None when input_panel is already shown in subl v2
if v is not None:

This comment has been minimized.

Copy link
@adzenith

adzenith Aug 27, 2013

Contributor

This doesn't work in ST3 either. v is never None.

This comment has been minimized.

Copy link
@xavi-

xavi- Aug 27, 2013

Author Owner

If v is never None that should be okay, I think. I added this check because I was seeing a number of error in Sublime 2.

v.sel().clear()
v.sel().add(Region(0, v.size()))
SelectUntilCommand.input_panel = v

input_panel = SelectUntilCommand.input_panel
if input_panel and not SelectUntilCommand.first_opened:
fullRegion = Region(0, input_panel.size())

isReverse, negSelector = negate_selector(input_panel.substr(fullRegion))
highlight = Region(1 + (1 if isReverse else 0), len(negSelector) - 1)

edit = input_panel.begin_edit()

This comment has been minimized.

Copy link
@adzenith

adzenith Aug 27, 2013

Contributor

This line doesn't work in Sublime Text 3, because of the changes to the way that edits work.

This comment has been minimized.

Copy link
@xavi-

xavi- Aug 27, 2013

Author Owner

Damn, I need to upgrade. Thanks for the heads up. I'll try and fix when I get home.

This comment has been minimized.

Copy link
@adzenith

This comment has been minimized.

Copy link
@xavi-

xavi- Aug 27, 2013

Author Owner

Made a commit that hopefully fixed sublime3 compatibility: 8e95f2e

Let me know if it's working for you.

input_panel.replace(edit, fullRegion, negSelector)
input_panel.sel().clear()
input_panel.sel().add(highlight)
input_panel.end_edit(edit)

SelectUntilCommand.first_opened = False;

class ReverseSelectCommand(sublime_plugin.TextCommand):

Expand Down

0 comments on commit 6c72a67

Please sign in to comment.