1
0
mirror of https://github.com/dschmenk/PLASMA.git synced 2024-10-18 22:24:28 +00:00

working module based text editor

This commit is contained in:
David Schmenk 2017-12-19 18:29:25 -08:00
parent e6fbdd5e68
commit 00de6686d3

View File

@ -50,16 +50,12 @@ const keyctrlx = $98
const keyctrlz = $9A
const keydelete = $FF
//
// Input buffer
//
const getbuff = $01FF
//
// Data and text buffer constants
//
const MAXLINES = 1500
const MAXLINESSIZE = MAXLINES+24
const MAXLNLEN = 79
const MAXSTRPLSIZE = $8000 // $7000
const MAXSTRPLSIZE = $8000
//const STRPLMAPSIZE = 224 // $E0 = 28K is memory@16 bytes per bit map, 128 bytes per 8 bit map, 1K bytes per 8 byte map
const pgjmp = 16
const changed = 1
@ -76,16 +72,16 @@ word = $0450,$04D0,$0550,$05D0,$0650,$06D0,$0750,$07D0
//
// Editor variables
//
byte nullstr = ""
byte[64] txtfile = "UNTITLED"
byte exit = FALSE
byte flags = 0
byte flash = 0
word numlines = 0
word cutbuf = 0
word strplsize = MAXSTRPLSIZE
word strplmapsize
word strlinbuf, strpoolmap, strpool
byte nullstr = ""
byte[64] filename = "UNTITLED"
byte exit = FALSE
byte flags = 0
byte flash = 0
word numlines = 0
word cutbuf = 0
word arg
word strplsize = MAXSTRPLSIZE
word strpool, strplmapsize, strlinbuf, strpoolmap
byte cursx, cursy, scrnleft, curscol, underchr, curschr
word keyin, cursrow, scrntop, cursptr
//
@ -257,18 +253,20 @@ end
def inittxtbuf#0
word i
strlinbuf = heapalloc(MAXLINESSIZE*2)
memset(strlinbuf, MAXLINESSIZE*2, @nullstr)
while isult(heapavail, strplsize)
strplsize = strplsize - 4096
loop
if isult(heapavail - strplsize, 4096) // Keep at least 4096 free
strplsize = strplsize - 4096
if not strpool
strlinbuf = heapalloc(MAXLINESSIZE*2)
while isult(heapavail, strplsize)
strplsize = strplsize - 4096
loop
if isult(heapavail - strplsize, 4096) // Keep at least 4096 free
strplsize = strplsize - 4096
fin
strplmapsize = strplsize / 128
strpoolmap = heapalloc(strplmapsize)
strpool = heapalloc(strplsize)
fin
strplmapsize = strplsize / 128
strpoolmap = heapalloc(strplmapsize)
strpool = heapalloc(strplsize)
memset(strpoolmap, strplmapsize, 0)
memset(strlinbuf, @nullstr, MAXLINESSIZE*2)
memset(strpoolmap, 0, strplmapsize)
numlines = 1
cursrow = 0
curscol = 0
@ -314,7 +312,7 @@ def txtupper#0
flags = flags | uppercase
for i = numlines - 1 downto 0
strupper(strlinbuf:[i])
strupper(strlinbuf=>[i])
next
end
def txtlower#0
@ -322,34 +320,9 @@ def txtlower#0
flags = flags & ~uppercase
for i = numlines - 1 downto 0
strlower(strlinbuf:[i])
strlower(strlinbuf=>[i])
next
end
def print(i)#0
byte numstr[7]
byte place, sign
place = 6
if i < 0
sign = 1
i = -i
else
sign = 0
fin
while i >= 10
numstr[place] = i % 10 + '0'
i = i / 10
place--
loop
numstr[place] = i + '0'
place--
if sign
numstr[place] = '-'
place--
fin
numstr[place] = 6 - place
puts(@numstr[place])
end
def nametostr(namestr, len, strptr)#0
^strptr = len
memcpy(strptr + 1, namestr, len)
@ -360,15 +333,15 @@ end
def readtxt(filename)#0
byte txtbuf[81], refnum, i, j
refnum = fileio:open(filename)
if refnum
newline(refnum, $7F, $0D)
refnum = fileio:open(filename)
fileio:newline(refnum, $7F, $0D)
repeat
txtbuf = fileio:read(refnum, @txtbuf + 1, MAXLNLEN)
if txtbuf
sethibit(@txtbuf)
if flags & uppercase; strupper(@txtbuf); fin
strlinbuf:[numlines] = newstr(@txtbuf)
strlinbuf=>[numlines] = newstr(@txtbuf)
numlines++
fin
if !(numlines & $0F); putc('.'); fin
@ -377,8 +350,8 @@ def readtxt(filename)#0
//
// Make sure there is a blank line at the end of the buffer
//
if numlines < MAXLINES and strlinbuf:[numlines - 1] <> @nullstr
strlinbuf:[numlines] = @nullstr
if numlines < MAXLINES and strlinbuf=>[numlines - 1] <> @nullstr
strlinbuf=>[numlines] = @nullstr
numlines++
fin
fin
@ -397,12 +370,12 @@ def writetxt(filename)#0
//
// Remove blank lines at end of text.
//
while numlines > 1 and strlinbuf:[numlines - 1] == @nullstr; numlines = numlines - 1; loop
while numlines > 1 and strlinbuf=>[numlines - 1] == @nullstr; numlines = numlines - 1; loop
//
// Write all the text line to the file.
//
for i = 0 to numlines - 1
cpyln(strlinbuf:[i], @txtbuf)
cpyln(strlinbuf=>[i], @txtbuf)
txtbuf = txtbuf + 1
txtbuf[txtbuf] = $0D
fileio:write(refnum, @txtbuf + 1, txtbuf)
@ -429,17 +402,16 @@ def drawrow(row, ofst, strptr)#0
if numchars >= 40
numchars = 40
else
memset(scrnptr + numchars, 40 - numchars, $A0A0)
memset(scrnptr + numchars, $A0A0, 40 - numchars)
fin
memcpy(scrnptr, strptr + ofst + 1, numchars)
end
def drawscrn(toprow, ofst)#0
byte row, numchars
word strptr, scrnptr
if ofst
for row = 0 to 23
strptr = strlinbuf:[toprow + row]
strptr = strlinbuf=>[toprow + row]
scrnptr = txtscrn[row]
if ofst >= ^strptr
numchars = 0
@ -449,19 +421,19 @@ def drawscrn(toprow, ofst)#0
if numchars >= 40
numchars = 40
else
memset(scrnptr + numchars, 40 - numchars, $A0A0)
memset(scrnptr + numchars, $A0A0, 40 - numchars)
fin
memcpy(scrnptr, strptr + ofst + 1, numchars)
next
else
for row = 0 to 23
strptr = strlinbuf:[toprow + row]
strptr = strlinbuf=>[toprow + row]
scrnptr = txtscrn[row]
numchars = ^strptr
if numchars >= 40
numchars = 40
else
memset(scrnptr + numchars, 40 - numchars, $A0A0)
memset(scrnptr + numchars, $A0A0, 40 - numchars)
fin
memcpy(scrnptr, strptr + 1, numchars)
next
@ -608,10 +580,27 @@ end
// Keyboard routines
//
def keyin2e
byte key
repeat
cursflash
until ^keyboard >= 128
return ^keystrobe
key = ^keyboard
until key >= 128
^keystrobe
if ^pushbttn2 & 128 // Closed Apple pressed
when key
is keyarrowleft
key = keyctrla; break
is keyarrowright
key = keyctrls; break
is keyarrowup
key = keyctrlw; break
is keyarrowdown
key = keyctrlz; break
is keyenter
key = keyctrlf; break
wend
fin
return key
end
def keyin2
byte key
@ -661,7 +650,7 @@ def printtxt(slot)#0
scrncsw = *csw
*csw = $C000 | (slot << 8)
for i = 0 to numlines - 1
cpyln(strlinbuf:[i], @txtbuf)
cpyln(strlinbuf=>[i], @txtbuf)
puts(@txtbuf)
putln
next
@ -669,8 +658,8 @@ def printtxt(slot)#0
end
def openline(row)
if numlines < MAXLINES
memcpy(@strlinbuf:[row + 1], @strlinbuf:[row], (numlines - row) * 2)
strlinbuf:[row] = @nullstr
memcpy(@strlinbuf=>[row + 1], @strlinbuf=>[row], (numlines - row) * 2)
strlinbuf=>[row] = @nullstr
numlines++
flags = flags | changed
return TRUE
@ -680,8 +669,8 @@ def openline(row)
end
def cutline#0
delstr(cutbuf)
cutbuf = strlinbuf:[cursrow]
memcpy(@strlinbuf:[cursrow], @strlinbuf:[cursrow + 1], (numlines - cursrow) * 2)
cutbuf = strlinbuf=>[cursrow]
memcpy(@strlinbuf=>[cursrow], @strlinbuf=>[cursrow + 1], (numlines - cursrow) * 2)
if numlines > 1
numlines--
fin
@ -693,8 +682,8 @@ def cutline#0
end
def pasteline#0
if cutbuf and numlines < MAXLINES
memcpy(@strlinbuf:[cursrow + 1], @strlinbuf:[cursrow], (numlines - cursrow) * 2)
strlinbuf:[cursrow] = newstr(cutbuf)
memcpy(@strlinbuf=>[cursrow + 1], @strlinbuf=>[cursrow], (numlines - cursrow) * 2)
strlinbuf=>[cursrow] = newstr(cutbuf)
numlines++
flags = flags | changed
redraw
@ -706,16 +695,16 @@ def joinline#0
byte joinstr[80], joinlen
if cursrow < numlines - 1
strcpy(@joinstr, strlinbuf:[cursrow])
joinlen = joinstr + ^(strlinbuf:[cursrow + 1])
strcpy(@joinstr, strlinbuf=>[cursrow])
joinlen = joinstr + ^(strlinbuf=>[cursrow + 1])
if joinlen < 80
memcpy(@joinstr + joinstr + 1, strlinbuf:[cursrow + 1] + 1, ^(strlinbuf:[cursrow + 1]))
memcpy(@joinstr + joinstr + 1, strlinbuf=>[cursrow + 1] + 1, ^(strlinbuf=>[cursrow + 1]))
joinstr = joinlen
delstr(strlinbuf:[cursrow])
strlinbuf:[cursrow] = newstr(@joinstr)
delstr(strlinbuf:[cursrow + 1])
delstr(strlinbuf=>[cursrow])
strlinbuf=>[cursrow] = newstr(@joinstr)
delstr(strlinbuf=>[cursrow + 1])
numlines--
memcpy(@strlinbuf:[cursrow + 1], @strlinbuf:[cursrow + 2], (numlines - cursrow) * 2)
memcpy(@strlinbuf=>[cursrow + 1], @strlinbuf=>[cursrow + 2], (numlines - cursrow) * 2)
flags = flags | changed
redraw
else
@ -728,19 +717,19 @@ def splitline#0
if openline(cursrow + 1)
if curscol
splitlen = ^(strlinbuf:[cursrow])
splitlen = ^(strlinbuf=>[cursrow])
if curscol < splitlen - 1
memcpy(@splitstr + 1, strlinbuf:[cursrow] + curscol + 1, splitlen - curscol)
memcpy(@splitstr + 1, strlinbuf=>[cursrow] + curscol + 1, splitlen - curscol)
splitstr = splitlen - curscol
strlinbuf:[cursrow + 1] = newstr(@splitstr)
memcpy(@splitstr + 1, strlinbuf:[cursrow] + 1, curscol)
strlinbuf=>[cursrow + 1] = newstr(@splitstr)
memcpy(@splitstr + 1, strlinbuf=>[cursrow] + 1, curscol)
splitstr = curscol
delstr(strlinbuf:[cursrow])
strlinbuf:[cursrow] = newstr(@splitstr)
delstr(strlinbuf=>[cursrow])
strlinbuf=>[cursrow] = newstr(@splitstr)
fin
else
strlinbuf:[cursrow + 1] = strlinbuf:[cursrow]
strlinbuf:[cursrow] = @nullstr
strlinbuf=>[cursrow + 1] = strlinbuf=>[cursrow]
strlinbuf=>[cursrow] = @nullstr
fin
curscol = 0
cursx = 0
@ -767,10 +756,10 @@ def editline(key)
if (editkey(key))
flags = flags | changed
memset(@editstr, 80, $A0A0)
strcpy(@editstr, strlinbuf:[cursrow])
undoline = strlinbuf:[cursrow]
strlinbuf:[cursrow] = @editstr
memset(@editstr, $A0A0, 80)
strcpy(@editstr, strlinbuf=>[cursrow])
undoline = strlinbuf=>[cursrow]
strlinbuf=>[cursrow] = @editstr
repeat
if key >= keyspace
if key == keydelete
@ -843,9 +832,9 @@ def editline(key)
key = keyin()
until not editkey(key)
if editstr
strlinbuf:[cursrow] = newstr(@editstr)
strlinbuf=>[cursrow] = newstr(@editstr)
else
strlinbuf:[cursrow] = @nullstr
strlinbuf=>[cursrow] = @nullstr
fin
delstr(undoline)
fin
@ -880,7 +869,7 @@ def editmode#0
pasteline; break
is keyctrlf
if numlines < MAXLINES and cursrow == numlines - 1
strlinbuf:[numlines] = @nullstr
strlinbuf=>[numlines] = @nullstr
numlines++
fin
cursdown
@ -919,7 +908,9 @@ def editmode#0
is keyescape
cursoff
cmdmode
redraw
if not exit
redraw
fin
break
wend
until exit
@ -1023,7 +1014,7 @@ def cmdmode#0
clrscrn
puts("PLASMA ][ EDITOR VERSION 0.99\n")
while not exit
puts(@txtfile)
puts(@filename)
cmdptr = gets($BA)
when toupper(parsecmd(cmdptr))
is 'A'
@ -1034,17 +1025,17 @@ def cmdmode#0
if chkchng
inittxtbuf
numlines = 0
strcpy(@txtfile, cmdptr)
readtxt(@txtfile)
strcpy(@filename, cmdptr)
readtxt(@filename)
if numlines == 0; numlines = 1; fin
flags = flags & ~changed
fin
break
is 'W'
if ^cmdptr
strcpy(@txtfile, cmdptr)
strcpy(@filename, cmdptr)
fin
writetxt(@txtfile)
writetxt(@filename)
//if flags & changed; fin
flags = flags & ~changed
break
@ -1068,7 +1059,7 @@ def cmdmode#0
is 'N'
if chkchng
inittxtbuf
strcpy(@txtfile, "UNTITLED")
strcpy(@filename, "UNTITLED")
fin
break
otherwise
@ -1088,21 +1079,22 @@ end
//
// Init editor
//
if !(^MACHID & $80)
if !(MACHID & $80)
flags = uppercase | shiftlock
keyin = @keyin2
else
keyin = @keyin2e
fin
inittxtbuf
if ^argNext(argFirst)
strcpy(@txtfile, cmdline)
puts(@txtfile)
arg = argNext(argFirst)
if arg
strcpy(@filename, arg)
puts(@filename)
numlines = 0
readtxt(@txtfile)
readtxt(@filename)
fin
curschr = '+'
flags = flags | insmode
flags = flags | insmode
drawscrn(scrntop, scrnleft)
curson
editmode