// initcom.sr
// Carol Hankins
// April 8,  1976


get "bravo1.df"
get "com.df"
get "msg.df"
get "rn1.df"
get "doc.df"
get "char.df"
get "display.df"
get "st.df"
get "vm.df"
get "ALTOFILESYS.D"
get "OM.df"


// incoming procedures

external	[
	stcopy
	sbwsize
	move
	errhlta
	SiPut
	VpaGetScratchBin
	getvp
	lockbp
	quitcom
	EditCheck
	insertz
	replacez
	normalize
	deletez
	extend
	buffer
	hccom
	PageCom
	oreadwritecoms
// 	vputomfixedstor
	owritefile
	InitCom2
	unlockbp
	GetFile
	findomspace
	umax
	]


// incoming statics

external	[
	vbp
	vcfreemin
	]


// outgoing procedures

external	[
	CdInit
	CsAppend
	CiCloseCd
	InitCom
	InitVrldsys
	]


// outgoing statics

external	[
	vvpampchci
	mpchci
	vcinorepeat
	]


// local statics

static	[
	vvpampchci
	mpchci
	vcinorepeat
	]


let CdInit(cd, ricom, ristate, ripast, fokrepeat, fokundo, chundo,fselundo,fOrderedOvs, fRestoreSysWw; numargs ca) = valof
[
switchon ca into
	[
case 7: fselundo= false; 
case 8: fOrderedOvs= false;
case 9: fRestoreSysWw= false;
default: endcase;
	]
cd>>CD.ricom = ricom
cd>>CD.ristate = ristate
cd>>CD.ripast = ripast
cd>>CD.fokrepeat = fokrepeat
cd>>CD.fokundo = fokundo
cd>>CD.chundo = chundo
cd>>CD.lcdmac = lcd
cd>>CD.fselundo = fselundo
cd>>CD.fOrderedOvs = fOrderedOvs
cd>>CD.fRestoreSysWw = fRestoreSysWw
resultis cd

]


and CsAppend(cd, sbact, sbdeact, pAbProc, cwarray, rgrid, nrimac) = valof
[

let cs, asbdeact = nil, nil

cs = cd + cd>>CD.lcdmac
cs>>CS.pAbProc = pAbProc
cs>>CS.cwarray = cwarray
stcopy(lv cs>>CS.asbact, sbact)
asbdeact = lv cs>>CS.asbact + sbwsize(sbact)
stcopy(asbdeact, sbdeact)
cs>>CS.bsbdeact = asbdeact - cs
let trgrid = asbdeact + sbwsize(sbdeact)
move(rgrid, trgrid, nrimac)
cs>>CS.brgrid = trgrid - cs
cs>>CS.lcsmac = cs>>CS.brgrid + nrimac
cd>>CD.lcdmac = cd>>CD.lcdmac + cs>>CS.lcsmac
if cd>>CD.lcdmac ge lcdmax then errhlta(78)
resultis cs

]


and CiCloseCd(cd, ch) = valof
[

let ci = nil

// acsnil at the end
cd ! (cd >> CD.lcdmac) = 0
cd >> CD.lcdmac = cd >> CD.lcdmac+1

// ci = SiPut(sinil, cd, cd>>CD.lcdmac)
ci = SiPut(sinil, cd)
mpchci!ch = ci
resultis ci

]


and InitCom() be 
[

let cs, tbp = nil, nil
let cd = vec lcdmax
let rgrid = vec 25

vvpampchci = VpaGetScratchBin()
mpchci = getvp(vvpampchci)
tbp = vbp
lockbp(tbp)
for tch = 0 to #377 do mpchci ! tch = cinil

// format for this region
// CdInit(cd, ricom, ristate, ripast, fokrepeat, fokundo, chundo)
// CsAppend(cd, sbact, sbdeact, pAbProc, cwarray, rgrid, nrimac)
//

// Quit
cd = CdInit(cd, 19, 74, rinil, false, false, 0, false, false, true)
rgrid ! 0 = 19;	rgrid ! 1 = 50
rgrid ! 2 = 65;	rgrid ! 3 = 50
rgrid ! 4 = rinil
CsAppend(cd, "BCDKLJQT", "BCDKLJQT", lv quitcom, 2500, rgrid, 5)
CiCloseCd(cd, $q)

// Insert
cd = CdInit(cd, 0, 12, rinil, true, true, $d )
CsAppend(cd, "", "", lv EditCheck, 0, 0, 0)
CsAppend(cd, "RM", "", lv insertz, 0, 0, 0)
CiCloseCd(cd, $i)

// Append
cd = CdInit(cd, 1, 12, rinil, true, true, $d )
CsAppend(cd, "", "", lv EditCheck, 0, 0, 0)
CsAppend(cd, "RM", "", lv insertz, 0, 0, 0)
CiCloseCd(cd, $a)

// Replace
cd = CdInit(cd, 2, 12, rinil, true, true, $r )
CsAppend(cd, "", "", lv EditCheck, 0, 0, 0)
CsAppend(cd, "RM", "", lv replacez, 0, 0, 0)
CiCloseCd(cd, $r)

// Delete
cd = CdInit(cd, 18, rinil, rinil, true, true, $i )
CsAppend(cd, "", "", lv deletez, 0, 0, 0)
CiCloseCd(cd, $d)

// Normalize
cd = CdInit(cd, 17, rinil, rinil, true, false, 0 )
CsAppend(cd, "", "", lv normalize, 0, 0, 0)
CiCloseCd(cd, $n)

// Extend
cd = CdInit(cd, 16, rinil, rinil, false, false, 0)
CsAppend(cd, "", "", lv extend, 0, 0, 0)
CiCloseCd(cd, $e)

// Buffer commands
cd = CdInit(cd, 9, 173, rinil, true, false, 0 )
rgrid ! 0 = 206;	rgrid ! 1 = 50
CsAppend(cd, "M", "M", lv buffer, 0, rgrid, 2)
CiCloseCd(cd, $b)

// Hardcopy
cd = CdInit(cd, 13, rinil, rinil, true, false, 0,false,true)
rgrid ! 0 = 126;	rgrid ! 1 = 50
rgrid ! 2 = 99;	rgrid ! 3 = 50
rgrid ! 4 = 100;	rgrid ! 5 = 50
rgrid ! 6 = 156;	rgrid ! 7 = 50
rgrid ! 8 = 157;	rgrid ! 9 = 50
rgrid ! 10 = 13;	rgrid ! 11 = 50
rgrid ! 12 = 160;	rgrid ! 13 = 50
rgrid ! 14 = 169;	rgrid ! 15 = 50
CsAppend(cd, "BU↑Ma*140", "BU", lv hccom, 3400, rgrid, 16)
CiCloseCd(cd, $h)

// Page Boundary
cd = CdInit(cd, rinil, rinil, rinil, true, false, 0)
CsAppend(cd, "ca*140", "ca*140", lv PageCom, 0, 0, 0)
CiCloseCd(cd, chlf)

let tputomfixedstor = 2210+(lnovhdr+5);
// Get
cd = CdInit(cd, 4, 67, rinil, false, true,  $d, true )
rgrid ! 0 = 94;	rgrid ! 1 = 50
rgrid ! 2 = 39;	rgrid ! 3 = 50
rgrid ! 4 = 99;	rgrid ! 5 = 50
rgrid ! 6 = 100;	rgrid ! 7 = 50
rgrid ! 8 = 97;	rgrid ! 9 = 50
rgrid ! 10 = 66;	rgrid ! 11 = 50
rgrid ! 12 = 75;	rgrid ! 13 = 50
CsAppend(cd, "MG", "MG", lv oreadwritecoms, 0, rgrid, 14)
CsAppend(cd, "BHJQT", "BHJQT", lv GetFile, tputomfixedstor, 
	rgrid, 14)
CiCloseCd(cd, $g)

// CtrlZ
cd = CdInit(cd, 5, 67, rinil, false, true,  $d )
rgrid ! 0 = 94;	rgrid ! 1 = 50
rgrid ! 2 = 39;	rgrid ! 3 = 50
rgrid ! 4 = 99;	rgrid ! 5 = 50
rgrid ! 6 = 100;	rgrid ! 7 = 50
rgrid ! 8 = 97;	rgrid ! 9 = 50
CsAppend(cd, "MG", "MG", lv oreadwritecoms, 0, 
	rgrid, 10)
CsAppend(cd, "BHJQT", "BHJQT", lv GetFile, tputomfixedstor, 
	rgrid, 10)
CiCloseCd(cd, ctrlz)

// Put
cd = CdInit(cd, 3, 67, rinil, false, false, 0)
rgrid ! 0 = 94;	rgrid ! 1 = 50
rgrid ! 2 = 39;	rgrid ! 3 = 50
rgrid ! 4 = 3;	rgrid ! 5 = 50
rgrid ! 6 = 93;	rgrid ! 7 = 50
CsAppend(cd, "BMG", "BM", lv oreadwritecoms, tputomfixedstor, 
	rgrid, 8)
rgrid ! 0 = 99;	rgrid ! 1 = 50
rgrid ! 2 = 100;	rgrid ! 3 = 50
rgrid ! 4 = 102
CsAppend(cd, "BGIJQTLK", "BGIJQTLK", lv owritefile, tputomfixedstor, 
	rgrid, 5)
CiCloseCd(cd, $p)

let tcspput1 = findomspace("MG")
let tcspput2 = findomspace("BGIJQTLK");
vcfreemin = tputomfixedstor + umax(tcspput1,tcspput2);
InitCom2()

unlockbp(tbp, true)

]