From 807dfa5c6a3bb07c5ec2aab5640f9a2f568e19d5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 4 Nov 2013 09:55:01 +0530 Subject: [PATCH] Limit size of savepoint history --- src/calibre/gui2/tweak_book/undo.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/calibre/gui2/tweak_book/undo.py b/src/calibre/gui2/tweak_book/undo.py index 018a2df874..f9bd9ba810 100644 --- a/src/calibre/gui2/tweak_book/undo.py +++ b/src/calibre/gui2/tweak_book/undo.py @@ -8,6 +8,8 @@ __copyright__ = '2013, Kovid Goyal ' import shutil +MAX_SAVEPOINTS = 100 + def cleanup(containers): for container in containers: try: @@ -42,8 +44,17 @@ class GlobalUndoHistory(object): self.states = self.states[:self.pos+1] self.states.append(State(new_container)) self.pos += 1 + if len(self.states) > MAX_SAVEPOINTS: + num = len(self.states) - MAX_SAVEPOINTS + cleanup(self.states[:num]) + self.states = self.states[num:] def rewind_savepoint(self): + ''' Revert back to the last save point, should only be used immediately + after a call to add_savepoint. If there are intervening calls to undo + or redo, behavior is undefined. This is intended to be used in the case + where you create savepoint, perform some operation, operation fails, so + revert to state before creating savepoint. ''' if self.pos > 0 and self.pos == len(self.states) - 1: self.pos -= 1 cleanup(self.states.pop())