From 43825a1f9b79d5e88ca00541995b5fa388177fbc Mon Sep 17 00:00:00 2001 From: Iulius Curt Date: Wed, 17 Oct 2012 17:35:15 +0300 Subject: [PATCH] New behaviour: Jump to latest 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 --- BackThere.py | 60 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/BackThere.py b/BackThere.py index a3cde0a..e6c8c69 100644 --- a/BackThere.py +++ b/BackThere.py @@ -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): @@ -25,11 +26,16 @@ 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() @@ -37,24 +43,52 @@ def run(self, edit): # 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])