Skip to content

Commit

Permalink
New behaviour: Jump to latest
Browse files Browse the repository at this point in the history
When GoBackThere command is called, but no position was saved in the
current view (buffer/file), it will try to jump to the latest used
position (used either by SaveBackThere or GoBackThere command) if
at least one position was saved in the current session.

This is, in a way, made in response to feature-request #5.

Signed-off-by: Iulius Curt <iulius.curt@gmail.com>
  • Loading branch information
iuliux committed Oct 17, 2012
1 parent f006c62 commit 43825a1
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions BackThere.py
Expand Up @@ -9,6 +9,7 @@
# Jumps to the latest saved position


# TODO: This class is mostly futile and should be replaced
class BackThereMemory:
""" Utility class to remember position """
def __init__(self):
Expand All @@ -25,36 +26,69 @@ def get(self):

# One memory bank for each buffer
bt_memory = {}
# Keep track of the latest saved position: (position, view)
bt_latest = None


class SaveBackThereCommand(sublime_plugin.TextCommand):
""" Puts the current location of the cursor in memory """
def run(self, edit):
global bt_memory
global bt_latest

buff_id = self.view.buffer_id()
sels = self.view.sel()

if not buff_id in bt_memory:
# First time in current buffer
bt_memory[buff_id] = BackThereMemory()
bt_memory[buff_id].put(sels[0].begin())
bt_latest = (sels[0].begin(), self.view)


class GoBackThereCommand(sublime_plugin.TextCommand):
""" Moves the cursor to the position in memory """
def run(self, edit):
global bt_memory
global bt_latest

buff_id = self.view.buffer_id()
switch_needed = False
if buff_id in bt_memory and bt_memory[buff_id].valid:
saved_region = sublime.Region(bt_memory[buff_id].get())

# Check if the saved position is still inside the buffer limits
buff_end = self.view.size()
if saved_region.begin() > buff_end:
saved_region = sublime.Region(buff_end, buff_end)

# Move the cursor _back there_
self.view.sel().clear()
self.view.sel().add(saved_region)

# Center the new cursor position in the viewport
# (only if cursor is out of viewport)
self.view.show(saved_region)
current_view = self.view
# Update the latest
bt_latest = (bt_memory[buff_id].get(), current_view)
elif bt_latest:
# This is the case where no position is saved in the current view,
# but there exists at least one position saved in some other view,
# so, that one will be used
saved_region = sublime.Region(bt_latest[0])
current_view = bt_latest[1]
switch_needed = True
else:
return

# Check if the saved position is still inside the buffer limits
buff_end = current_view.size()
if saved_region.begin() > buff_end:
saved_region = sublime.Region(buff_end, buff_end)

# Move the cursor _back there_
current_view.sel().clear()
current_view.sel().add(saved_region)

# Center the new cursor position in the viewport
# (only if cursor is out of viewport)
current_view.show(saved_region)

# If it's the case, it will call switch_back_window to jump to the
# wanted view
if switch_needed:
self.view.window().run_command('switch_back_window')


class SwitchBackWindowCommand(sublime_plugin.WindowCommand):
""" Switches the view to where the last position was saved """
def run(self):
self.window.focus_view(bt_latest[1])

0 comments on commit 43825a1

Please sign in to comment.