jpayne@68: import string jpayne@68: jpayne@68: from idlelib.delegator import Delegator jpayne@68: jpayne@68: # tkinter import not needed because module does not create widgets, jpayne@68: # although many methods operate on text widget arguments. jpayne@68: jpayne@68: #$ event <> jpayne@68: #$ win jpayne@68: #$ unix jpayne@68: jpayne@68: #$ event <> jpayne@68: #$ win jpayne@68: #$ unix jpayne@68: jpayne@68: #$ event <> jpayne@68: #$ win jpayne@68: #$ unix jpayne@68: jpayne@68: jpayne@68: class UndoDelegator(Delegator): jpayne@68: jpayne@68: max_undo = 1000 jpayne@68: jpayne@68: def __init__(self): jpayne@68: Delegator.__init__(self) jpayne@68: self.reset_undo() jpayne@68: jpayne@68: def setdelegate(self, delegate): jpayne@68: if self.delegate is not None: jpayne@68: self.unbind("<>") jpayne@68: self.unbind("<>") jpayne@68: self.unbind("<>") jpayne@68: Delegator.setdelegate(self, delegate) jpayne@68: if delegate is not None: jpayne@68: self.bind("<>", self.undo_event) jpayne@68: self.bind("<>", self.redo_event) jpayne@68: self.bind("<>", self.dump_event) jpayne@68: jpayne@68: def dump_event(self, event): jpayne@68: from pprint import pprint jpayne@68: pprint(self.undolist[:self.pointer]) jpayne@68: print("pointer:", self.pointer, end=' ') jpayne@68: print("saved:", self.saved, end=' ') jpayne@68: print("can_merge:", self.can_merge, end=' ') jpayne@68: print("get_saved():", self.get_saved()) jpayne@68: pprint(self.undolist[self.pointer:]) jpayne@68: return "break" jpayne@68: jpayne@68: def reset_undo(self): jpayne@68: self.was_saved = -1 jpayne@68: self.pointer = 0 jpayne@68: self.undolist = [] jpayne@68: self.undoblock = 0 # or a CommandSequence instance jpayne@68: self.set_saved(1) jpayne@68: jpayne@68: def set_saved(self, flag): jpayne@68: if flag: jpayne@68: self.saved = self.pointer jpayne@68: else: jpayne@68: self.saved = -1 jpayne@68: self.can_merge = False jpayne@68: self.check_saved() jpayne@68: jpayne@68: def get_saved(self): jpayne@68: return self.saved == self.pointer jpayne@68: jpayne@68: saved_change_hook = None jpayne@68: jpayne@68: def set_saved_change_hook(self, hook): jpayne@68: self.saved_change_hook = hook jpayne@68: jpayne@68: was_saved = -1 jpayne@68: jpayne@68: def check_saved(self): jpayne@68: is_saved = self.get_saved() jpayne@68: if is_saved != self.was_saved: jpayne@68: self.was_saved = is_saved jpayne@68: if self.saved_change_hook: jpayne@68: self.saved_change_hook() jpayne@68: jpayne@68: def insert(self, index, chars, tags=None): jpayne@68: self.addcmd(InsertCommand(index, chars, tags)) jpayne@68: jpayne@68: def delete(self, index1, index2=None): jpayne@68: self.addcmd(DeleteCommand(index1, index2)) jpayne@68: jpayne@68: # Clients should call undo_block_start() and undo_block_stop() jpayne@68: # around a sequence of editing cmds to be treated as a unit by jpayne@68: # undo & redo. Nested matching calls are OK, and the inner calls jpayne@68: # then act like nops. OK too if no editing cmds, or only one jpayne@68: # editing cmd, is issued in between: if no cmds, the whole jpayne@68: # sequence has no effect; and if only one cmd, that cmd is entered jpayne@68: # directly into the undo list, as if undo_block_xxx hadn't been jpayne@68: # called. The intent of all that is to make this scheme easy jpayne@68: # to use: all the client has to worry about is making sure each jpayne@68: # _start() call is matched by a _stop() call. jpayne@68: jpayne@68: def undo_block_start(self): jpayne@68: if self.undoblock == 0: jpayne@68: self.undoblock = CommandSequence() jpayne@68: self.undoblock.bump_depth() jpayne@68: jpayne@68: def undo_block_stop(self): jpayne@68: if self.undoblock.bump_depth(-1) == 0: jpayne@68: cmd = self.undoblock jpayne@68: self.undoblock = 0 jpayne@68: if len(cmd) > 0: jpayne@68: if len(cmd) == 1: jpayne@68: # no need to wrap a single cmd jpayne@68: cmd = cmd.getcmd(0) jpayne@68: # this blk of cmds, or single cmd, has already jpayne@68: # been done, so don't execute it again jpayne@68: self.addcmd(cmd, 0) jpayne@68: jpayne@68: def addcmd(self, cmd, execute=True): jpayne@68: if execute: jpayne@68: cmd.do(self.delegate) jpayne@68: if self.undoblock != 0: jpayne@68: self.undoblock.append(cmd) jpayne@68: return jpayne@68: if self.can_merge and self.pointer > 0: jpayne@68: lastcmd = self.undolist[self.pointer-1] jpayne@68: if lastcmd.merge(cmd): jpayne@68: return jpayne@68: self.undolist[self.pointer:] = [cmd] jpayne@68: if self.saved > self.pointer: jpayne@68: self.saved = -1 jpayne@68: self.pointer = self.pointer + 1 jpayne@68: if len(self.undolist) > self.max_undo: jpayne@68: ##print "truncating undo list" jpayne@68: del self.undolist[0] jpayne@68: self.pointer = self.pointer - 1 jpayne@68: if self.saved >= 0: jpayne@68: self.saved = self.saved - 1 jpayne@68: self.can_merge = True jpayne@68: self.check_saved() jpayne@68: jpayne@68: def undo_event(self, event): jpayne@68: if self.pointer == 0: jpayne@68: self.bell() jpayne@68: return "break" jpayne@68: cmd = self.undolist[self.pointer - 1] jpayne@68: cmd.undo(self.delegate) jpayne@68: self.pointer = self.pointer - 1 jpayne@68: self.can_merge = False jpayne@68: self.check_saved() jpayne@68: return "break" jpayne@68: jpayne@68: def redo_event(self, event): jpayne@68: if self.pointer >= len(self.undolist): jpayne@68: self.bell() jpayne@68: return "break" jpayne@68: cmd = self.undolist[self.pointer] jpayne@68: cmd.redo(self.delegate) jpayne@68: self.pointer = self.pointer + 1 jpayne@68: self.can_merge = False jpayne@68: self.check_saved() jpayne@68: return "break" jpayne@68: jpayne@68: jpayne@68: class Command: jpayne@68: # Base class for Undoable commands jpayne@68: jpayne@68: tags = None jpayne@68: jpayne@68: def __init__(self, index1, index2, chars, tags=None): jpayne@68: self.marks_before = {} jpayne@68: self.marks_after = {} jpayne@68: self.index1 = index1 jpayne@68: self.index2 = index2 jpayne@68: self.chars = chars jpayne@68: if tags: jpayne@68: self.tags = tags jpayne@68: jpayne@68: def __repr__(self): jpayne@68: s = self.__class__.__name__ jpayne@68: t = (self.index1, self.index2, self.chars, self.tags) jpayne@68: if self.tags is None: jpayne@68: t = t[:-1] jpayne@68: return s + repr(t) jpayne@68: jpayne@68: def do(self, text): jpayne@68: pass jpayne@68: jpayne@68: def redo(self, text): jpayne@68: pass jpayne@68: jpayne@68: def undo(self, text): jpayne@68: pass jpayne@68: jpayne@68: def merge(self, cmd): jpayne@68: return 0 jpayne@68: jpayne@68: def save_marks(self, text): jpayne@68: marks = {} jpayne@68: for name in text.mark_names(): jpayne@68: if name != "insert" and name != "current": jpayne@68: marks[name] = text.index(name) jpayne@68: return marks jpayne@68: jpayne@68: def set_marks(self, text, marks): jpayne@68: for name, index in marks.items(): jpayne@68: text.mark_set(name, index) jpayne@68: jpayne@68: jpayne@68: class InsertCommand(Command): jpayne@68: # Undoable insert command jpayne@68: jpayne@68: def __init__(self, index1, chars, tags=None): jpayne@68: Command.__init__(self, index1, None, chars, tags) jpayne@68: jpayne@68: def do(self, text): jpayne@68: self.marks_before = self.save_marks(text) jpayne@68: self.index1 = text.index(self.index1) jpayne@68: if text.compare(self.index1, ">", "end-1c"): jpayne@68: # Insert before the final newline jpayne@68: self.index1 = text.index("end-1c") jpayne@68: text.insert(self.index1, self.chars, self.tags) jpayne@68: self.index2 = text.index("%s+%dc" % (self.index1, len(self.chars))) jpayne@68: self.marks_after = self.save_marks(text) jpayne@68: ##sys.__stderr__.write("do: %s\n" % self) jpayne@68: jpayne@68: def redo(self, text): jpayne@68: text.mark_set('insert', self.index1) jpayne@68: text.insert(self.index1, self.chars, self.tags) jpayne@68: self.set_marks(text, self.marks_after) jpayne@68: text.see('insert') jpayne@68: ##sys.__stderr__.write("redo: %s\n" % self) jpayne@68: jpayne@68: def undo(self, text): jpayne@68: text.mark_set('insert', self.index1) jpayne@68: text.delete(self.index1, self.index2) jpayne@68: self.set_marks(text, self.marks_before) jpayne@68: text.see('insert') jpayne@68: ##sys.__stderr__.write("undo: %s\n" % self) jpayne@68: jpayne@68: def merge(self, cmd): jpayne@68: if self.__class__ is not cmd.__class__: jpayne@68: return False jpayne@68: if self.index2 != cmd.index1: jpayne@68: return False jpayne@68: if self.tags != cmd.tags: jpayne@68: return False jpayne@68: if len(cmd.chars) != 1: jpayne@68: return False jpayne@68: if self.chars and \ jpayne@68: self.classify(self.chars[-1]) != self.classify(cmd.chars): jpayne@68: return False jpayne@68: self.index2 = cmd.index2 jpayne@68: self.chars = self.chars + cmd.chars jpayne@68: return True jpayne@68: jpayne@68: alphanumeric = string.ascii_letters + string.digits + "_" jpayne@68: jpayne@68: def classify(self, c): jpayne@68: if c in self.alphanumeric: jpayne@68: return "alphanumeric" jpayne@68: if c == "\n": jpayne@68: return "newline" jpayne@68: return "punctuation" jpayne@68: jpayne@68: jpayne@68: class DeleteCommand(Command): jpayne@68: # Undoable delete command jpayne@68: jpayne@68: def __init__(self, index1, index2=None): jpayne@68: Command.__init__(self, index1, index2, None, None) jpayne@68: jpayne@68: def do(self, text): jpayne@68: self.marks_before = self.save_marks(text) jpayne@68: self.index1 = text.index(self.index1) jpayne@68: if self.index2: jpayne@68: self.index2 = text.index(self.index2) jpayne@68: else: jpayne@68: self.index2 = text.index(self.index1 + " +1c") jpayne@68: if text.compare(self.index2, ">", "end-1c"): jpayne@68: # Don't delete the final newline jpayne@68: self.index2 = text.index("end-1c") jpayne@68: self.chars = text.get(self.index1, self.index2) jpayne@68: text.delete(self.index1, self.index2) jpayne@68: self.marks_after = self.save_marks(text) jpayne@68: ##sys.__stderr__.write("do: %s\n" % self) jpayne@68: jpayne@68: def redo(self, text): jpayne@68: text.mark_set('insert', self.index1) jpayne@68: text.delete(self.index1, self.index2) jpayne@68: self.set_marks(text, self.marks_after) jpayne@68: text.see('insert') jpayne@68: ##sys.__stderr__.write("redo: %s\n" % self) jpayne@68: jpayne@68: def undo(self, text): jpayne@68: text.mark_set('insert', self.index1) jpayne@68: text.insert(self.index1, self.chars) jpayne@68: self.set_marks(text, self.marks_before) jpayne@68: text.see('insert') jpayne@68: ##sys.__stderr__.write("undo: %s\n" % self) jpayne@68: jpayne@68: jpayne@68: class CommandSequence(Command): jpayne@68: # Wrapper for a sequence of undoable cmds to be undone/redone jpayne@68: # as a unit jpayne@68: jpayne@68: def __init__(self): jpayne@68: self.cmds = [] jpayne@68: self.depth = 0 jpayne@68: jpayne@68: def __repr__(self): jpayne@68: s = self.__class__.__name__ jpayne@68: strs = [] jpayne@68: for cmd in self.cmds: jpayne@68: strs.append(" %r" % (cmd,)) jpayne@68: return s + "(\n" + ",\n".join(strs) + "\n)" jpayne@68: jpayne@68: def __len__(self): jpayne@68: return len(self.cmds) jpayne@68: jpayne@68: def append(self, cmd): jpayne@68: self.cmds.append(cmd) jpayne@68: jpayne@68: def getcmd(self, i): jpayne@68: return self.cmds[i] jpayne@68: jpayne@68: def redo(self, text): jpayne@68: for cmd in self.cmds: jpayne@68: cmd.redo(text) jpayne@68: jpayne@68: def undo(self, text): jpayne@68: cmds = self.cmds[:] jpayne@68: cmds.reverse() jpayne@68: for cmd in cmds: jpayne@68: cmd.undo(text) jpayne@68: jpayne@68: def bump_depth(self, incr=1): jpayne@68: self.depth = self.depth + incr jpayne@68: return self.depth jpayne@68: jpayne@68: jpayne@68: def _undo_delegator(parent): # htest # jpayne@68: from tkinter import Toplevel, Text, Button jpayne@68: from idlelib.percolator import Percolator jpayne@68: undowin = Toplevel(parent) jpayne@68: undowin.title("Test UndoDelegator") jpayne@68: x, y = map(int, parent.geometry().split('+')[1:]) jpayne@68: undowin.geometry("+%d+%d" % (x, y + 175)) jpayne@68: jpayne@68: text = Text(undowin, height=10) jpayne@68: text.pack() jpayne@68: text.focus_set() jpayne@68: p = Percolator(text) jpayne@68: d = UndoDelegator() jpayne@68: p.insertfilter(d) jpayne@68: jpayne@68: undo = Button(undowin, text="Undo", command=lambda:d.undo_event(None)) jpayne@68: undo.pack(side='left') jpayne@68: redo = Button(undowin, text="Redo", command=lambda:d.redo_event(None)) jpayne@68: redo.pack(side='left') jpayne@68: dump = Button(undowin, text="Dump", command=lambda:d.dump_event(None)) jpayne@68: dump.pack(side='left') jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: from unittest import main jpayne@68: main('idlelib.idle_test.test_undo', verbosity=2, exit=False) jpayne@68: jpayne@68: from idlelib.idle_test.htest import run jpayne@68: run(_undo_delegator)