diff --git a/TermTk/TTkGui/textcursor.py b/TermTk/TTkGui/textcursor.py index 194cde0e..b75ed10d 100644 --- a/TermTk/TTkGui/textcursor.py +++ b/TermTk/TTkGui/textcursor.py @@ -160,7 +160,7 @@ class TTkTextCursor(): def __init__(self, l=0, p=0): self.set(l,p) def copy(self): - return TTkTextCursor._CP(self.pos, self.line) + return TTkTextCursor._CP(self.line, self.pos) def set(self, l, p): self.pos = p self.line = l @@ -175,8 +175,9 @@ class TTkTextCursor(): self._properties = [TTkTextCursor._prop( TTkTextCursor._CP(), TTkTextCursor._CP())] - self._document = kwargs.get('document',TTkTextDocument()) - self._document.contentsChanged.connect(self._documentContentChanged) + if 'document' in kwargs: + self._document = kwargs.get('document') + self._document.contentsChanged.connect(self._documentContentChanged) def _documentContentChanged(self): if self._autoChanged: return True diff --git a/TermTk/TTkGui/textdocument.py b/TermTk/TTkGui/textdocument.py index 63a0e0fd..027aa323 100644 --- a/TermTk/TTkGui/textdocument.py +++ b/TermTk/TTkGui/textdocument.py @@ -26,6 +26,13 @@ from TermTk.TTkCore.log import TTkLog from TermTk.TTkCore.signal import pyTTkSignal, pyTTkSlot from TermTk.TTkCore.string import TTkString +# def ccc(c,dl): +# pp = c._properties[0].selectionStart().pos +# pl = c._properties[0].selectionStart().line +# ap = c._properties[0].selectionEnd().pos +# al = c._properties[0].selectionEnd().line +# return f"{pp=},{pl=},{ap=},{al=} -> {dl[pl].substring(pp,ap)}" + class TTkTextDocument(): class _snapDiff(): ''' @@ -69,6 +76,7 @@ class TTkTextDocument(): self._diffs = [] self._snapshots = [] self._snapshotId = -1 + self.saveSnapshot(TTkTextCursor(document=self)) def changed(self): return self._changed @@ -100,15 +108,13 @@ class TTkTextDocument(): def saveSnapshot(self, cursor): # TTkLog.debug(f"snaps: {len(self._snapshots)} id:{self._snapshotId}") # TTkLog.debug(f"Cur: {self._dataLines[0]}") - if not self._changed: - return self._changed = False self._snapshots = self._snapshots[:max(0,self._snapshotId+1)] self._snapshotId = len(self._snapshots) self._snapshots.append( TTkTextDocument._snapshot(self._dataLines.copy(), cursor)) # for i,s in enumerate(self._snapshots): - # TTkLog.debug(f"{i=} {s._lines[0]}") + # TTkLog.debug(f"{i=} {s._lines[0]}, {ccc(s._cursor, s._lines)}") def restoreSnapshot(self, inc=0): # TTkLog.debug(f"R: snaps: {len(self._snapshots)} id:{self._snapshotId}") @@ -117,6 +123,7 @@ class TTkTextDocument(): snap = self._snapshots[self._snapshotId] self._dataLines = snap._lines.copy() self.contentsChanged.emit() + # TTkLog.debug(f"R: id={self._snapshotId} {snap._lines[0]} {ccc(snap._cursor, snap._lines)}") return snap._cursor return None diff --git a/TermTk/TTkWidgets/texedit.py b/TermTk/TTkWidgets/texedit.py index bf24680c..d197e3f2 100644 --- a/TermTk/TTkWidgets/texedit.py +++ b/TermTk/TTkWidgets/texedit.py @@ -279,7 +279,11 @@ class _TTkTextEditView(TTkAbstractScrollView): ( evt.key == TTkK.Key_Delete ) or ( evt.key == TTkK.Key_Backspace ) or ( self._textDocument.changed() and evt.key == TTkK.Key_Z ) or - ( evt.mod==TTkK.ControlModifier and evt.key == TTkK.Key_V ) ) ) ): + ( evt.mod==TTkK.ControlModifier and + ( evt.key == TTkK.Key_V or + ( evt.key == TTkK.Key_Z and self._textDocument.changed() ) ) + ) ) ) ): + # TTkLog.debug(f"Saving {self._textCursor.selectedText()} {self._textCursor._properties[0].anchor.pos}") self._textDocument.saveSnapshot(self._textCursor.copy()) if evt.type == TTkK.SpecialKey: