Implement clipboard

This commit is contained in:
Jeremy Penner 2020-08-02 13:09:05 -04:00
parent 43a66bebeb
commit d4a7f6fbc9
3 changed files with 38 additions and 16 deletions

View file

@ -61,6 +61,9 @@ K_END = 261
K_PGUP = 262 K_PGUP = 262
K_PGDN = 263 K_PGDN = 263
def K_CTRL(ch):
return chr(ord(ch.upper()) - ord('@'))
def StrKey(ch): def StrKey(ch):
if type(ch) == str: if type(ch) == str:
return ord(ch) return ord(ch)

View file

@ -189,7 +189,7 @@ class Synt(Typeable):
return cls() return cls()
def WithParent(self, syntParent): def WithParent(self, syntParent):
assert self.syntParent == None assert self.syntParent == None or syntParent == None
self.syntParent = syntParent self.syntParent = syntParent
return self return self
@ -344,7 +344,7 @@ class SyntBlock(Synt):
with pcur.Indent(2 if pcur.pwHoriz != None else 0): with pcur.Indent(2 if pcur.pwHoriz != None else 0):
pcur.EndLine() pcur.EndLine()
def OnInsertNewLine(syntAfter, psel): def OnInsertNewLine(syntAfter, psel):
self.InsertLineAfter(syntAfter) self.InsertLineAt(0)
psel.Inc(pcur.Reset().Project().pwVert) psel.Inc(pcur.Reset().Project().pwVert)
PwButtonHidden(pcur.pwVert, "[insert new line]", None, OnInsertNewLine, pcur.dxindent) PwButtonHidden(pcur.pwVert, "[insert new line]", None, OnInsertNewLine, pcur.dxindent)
for syntLine in self.rgsynt: for syntLine in self.rgsynt:
@ -355,29 +355,34 @@ class SyntBlock(Synt):
def StypeForChild(self, syntChild): def StypeForChild(self, syntChild):
return StypeStmt() return StypeStmt()
def InsertLineAfter(self, syntAfter): def InsertLineAt(self, isynt, syntStmt = None):
syntStmt = SyntHole() if syntStmt == None:
if syntAfter == None: syntStmt = SyntHole()
self.rgsynt.insert(0, syntStmt) syntStmt = syntStmt.WithParent(self)
else: self.rgsynt.insert(isynt, syntStmt)
self.rgsynt.insert(self.rgsynt.index(syntAfter) + 1, syntStmt)
syntStmt.syntParent = self
return syntStmt return syntStmt
def RemoveLine(self, synt): def RemoveLine(self, synt, psel):
psel.syntClipboard = synt.WithParent(None)
self.rgsynt.remove(synt) self.rgsynt.remove(synt)
def HandleKey(self, pwKey, pov, psel, key): def HandleKey(self, pwKey, pov, psel, key):
if ansi.FEnter(key): if ansi.FEnter(key):
self.InsertLineAfter(pwKey.pwChild.Value()) isyntInsert = self.rgsynt.index(pwKey.pwChild.Value()) + 1
self.InsertLineAt(isyntInsert)
clevelVert = psel.CLevelChild(pwKey.PwParent()) clevelVert = psel.CLevelChild(pwKey.PwParent())
pwVert = psel.PwSelected(pov.PwProjected(), clevelVert - 1) pwVert = psel.PwSelected(pov.PwProjected(), clevelVert - 1)
psel.Inc(pwVert) psel.Inc(pwVert)
return True elif (key == ansi.K_DEL or key == ansi.K_CTRL('x')) and pwKey.pwChild.RgpwChild()[0] == psel.PwSelected(pwKey):
elif key == ansi.K_DEL and pwKey.pwChild.RgpwChild()[0] == psel.PwSelected(pwKey): self.RemoveLine(pwKey.pwChild.Value(), psel)
self.RemoveLine(pwKey.pwChild.Value()) elif key == ansi.K_CTRL('c') and pwKey.pwChild.RgpwChild()[0] == psel.PwSelected(pwKey):
return True psel.syntClipboard = synt.DeepClone().WithParent(None)
return False elif key == ansi.K_CTRL('v') and psel.syntClipboard:
isyntInsert = self.rgsynt.index(pwKey.pwChild.Value())
self.InsertLineAt(isyntInsert, psel.syntClipboard.DeepClone())
else:
return False
return True
def Eval(self, env): def Eval(self, env):
for synt in self.rgsynt: for synt in self.rgsynt:
@ -860,6 +865,7 @@ class Psel(TPrs):
assert len(pw.RgpwChild()) == 0 #leaf assert len(pw.RgpwChild()) == 0 #leaf
self.rgo_ipw = self.Rgo_ipwFromPw(pw) self.rgo_ipw = self.Rgo_ipwFromPw(pw)
self.ksel = ksel self.ksel = ksel
self.syntClipboard = None
def Rgo_ipwFromPw(self, pw): def Rgo_ipwFromPw(self, pw):
rgo_ipw = [] rgo_ipw = []

View file

@ -84,6 +84,9 @@ class TPrs(object):
def UpgradeFrom(self, versionOld): def UpgradeFrom(self, versionOld):
pass pass
def DeepClone(self):
return Odb.DeepClone(self)
def __getattr__(self, key): def __getattr__(self, key):
stPersistent = "no persistent yet" stPersistent = "no persistent yet"
if key != "_persistent": if key != "_persistent":
@ -162,6 +165,16 @@ class Odb(object):
cls.rgtprsToUpgrade = None cls.rgtprsToUpgrade = None
cls.rgtprsToInit = None cls.rgtprsToInit = None
return tprs return tprs
@classmethod
def DeepClone(cls, tprs):
cls.rgtprsToInit = []
clone = cPickle.loads(cPickle.dumps(tprs))
for tprsToInit in cls.rgtprsToInit:
with tprsToInit.SetTransiently():
tprsToInit.InitTransient()
cls.rgtprsToInit = None
return clone
clsList = list clsList = list
clsDict = dict clsDict = dict