Commits

Nicholas Dudfield committed 4902d52

Keep scroll position upon revert as it's useful for rewind/copy/revert/paste workflow

Comments (0)

Files changed (2)

automatic-backups.sublime-commands

+[
+    { "caption" : "Automatic Backups: Merge",
+      "command": "navigate_backups",
+      "args" : {"cmd":  "merge"}
+     }
+]

automaticbackups.py

 from subprocess import Popen
 
 import backups
+# import difflib
 
-WINMERGE = '"D:\portable_sync\PortableApps\WinMergePortable\WinMergePortable.exe"'
+WINMERGE = '"C:\Program Files\WinMerge\WinMergeU.exe"'
 KEY      = "AutoBackups"
 
 class NavigateBackupsStateManager(sublime_plugin.EventListener):
 
     def on_post_save(self, view):
         NavigateBackups(view).teardown()
-        
+
     def on_modified(self, view):
         state = NavigateBackups(view)
         if NavigateBackups(view).index is not None:
     def teardown(self):
         self.default_state()
         self.view.erase_status(KEY)
+        self.view.erase_regions('diff_changes')
 
     def __new__(cls, view):
         "There's only ever one instance per view"
         Popen(CMD)
 
     def buffer(self, view, edit):
+        navigate_to = self.navigate_to(view)
+
+        with file(self.backup_full_path) as old_file:
+            old_buffer = unicode(old_file.read(), 'utf8')
+            view.erase(edit, sublime.Region(0, view.size()))
+            view.insert(edit, 0, old_buffer)
+            view.set_status('AutoBackups', "%s [%s of %s]" %\
+                (self.backup, self.index+1, len(self.found_backup_files)-1))
+
+            sublime.set_timeout(navigate_to, 0)
+
+    def navigated_to_end_of_backups(self):
+        return self.index == len(self.found_backup_files)-1
+
+    def navigate_to(self, view):
         line, _  = view.rowcol(view.visible_region().begin())
 
         region = view.sel()[0] if view.sel() else None
         if region is not None: text = view.substr(region)
         view.sel().clear()
 
-        with file(self.backup_full_path) as old_file:
-            view.erase(edit, sublime.Region(0, view.size()))
-            view.insert(edit, 0, unicode(old_file.read(), 'utf8'))
-            view.set_status('AutoBackups', "%s [%s of %s]" %\
-                (self.backup, self.index+1, len(self.found_backup_files)-1))
+        def inner():
+            # These 3 lines should force sublime to complete `loading`
+            # basically synchronisation.
+            for pt in range(view.size()): view.scope_name(pt)
+            view.layout_extent()
+            view.viewport_extent()
 
-            def inner(t=0):
-                # These 3 lines should force sublime to complete `loading`
-                # basically synchronisation.
-                for pt in range(view.size()): view.scope_name(pt)
-                view.layout_extent()
-                view.viewport_extent()
+            pt = view.text_point(line, 0)
+            y  = view.text_to_layout(pt)[1]
+            view.set_viewport_position((0, y))
 
-                pt = view.text_point(line, 0)
-                y  = view.text_to_layout(pt)[1]
-                view.set_viewport_position((0, y))
+            if region:
+                found = view.find(text, view.visible_region().begin(),
+                                        sublime.LITERAL)
+                if found:
+                    view.sel().add(found)
+                    view.show_at_center(found)
 
-                if region:
-                    found = view.find(text, view.visible_region().begin(),
-                                            sublime.LITERAL)
-                    if found:
-                        view.sel().add(found)
-                        view.show_at_center(found)
-
-            sublime.set_timeout(inner, 0)
-
-    def navigated_to_end_of_backups(self):
-        return self.index == len(self.found_backup_files)-1
+        return inner
 
     def revert(self, view):
+        navigate_to = self.navigate_to(view)
+
         def inner():
             view.run_command('revert')
             self.teardown()
             self.just_reverted = True
+            sublime.set_timeout(navigate_to, 200)
 
-        sublime.set_timeout(inner, 50)
+        sublime.set_timeout(inner, 5)
 
     def is_enabled(self, **args):
         return self.view.file_name()
     """Creates an automatic backup of every file you save. This
     gives you a rudimentary mechanism for making sure you don't lose
     information while working."""
-    
-    
+
+
     def on_pre_save(self, view):
       """When a file is saved, and there is no backup of it yet, create a
       backup of the original file before saving the new, modified version."""
 
       filepart, extensionpart = os.path.splitext (
                                             os.path.split(buffer_file_name)[1])
-      newname = os.path.join( backup_dir, 
+      newname = os.path.join( backup_dir,
                               filepart + "-0000-00-00-00-00-00" + extensionpart)
-      
+
       if os.path.isfile(newname):
         # Backup of original file already exists
         return
 
       backups.log("backing up original version to " + newname)
       shutil.copy(buffer_file_name, newname)
-    
+
     def on_post_save(self, view):
         """When a file is saved, put a copy of the file into the
         backup directory"""