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