// rn1.sr
// Carol Hankins
// March 24, 1976

get "bravo1.df"
get "rn1.df"
get "newmsg.df"

//incoming procedures
external
	[
	ParaBitsUpdate
	Zero
	getvp
	replacea
	replacea1
	lockbp
	unlockbp
	errhlta
	binsearcha
	invalidatedisplay
	]

//incoming statics
external
	[
	rgvpa
	dnbp
	rgrl
	vbp
	vrldsys
	]

//outgoing procedures
external
	[
	MapRi
	RbFromRlRi
	RdFromRlRbRi
	SetRegionA
	DcpSetRegionA
	SetRegionB
	SetRegionSize
	SetRegionSys
	VpaGetOne
	VpaGetMany
	]

//outgoing statics
//local statics

//local manifests
manifest			//for SetRegionA's frame
	[
	pidimac = 2
	pidrgrid = 3
	pidi = 5
	piddcp = 6
	]


let RbFromRlRi(rl,ri) = valof
[

if rl >> RL.fRes then resultis rl >> RL.rb
let tvpa = nil

tvpa = rl>>RL.VpaGet(rl,ri)
test tvpa eq rgvpa ! rl>>RL.bphint
	ifso vbp = rl>>RL.bphint
	ifnot	[
		getvp(tvpa)
		rl>>RL.bphint = vbp
		]

resultis dnbp ! vbp
]


and RdFromRlRbRi(rd,rl,rb,ri) = valof
[
let fcFirst = (rl >> RL.fRes) ? 0,(rl>>RL.mpbifcfirst) ! (rb>>RX.bi)
rd>>RD.cp = (rb + rb>>RX.brgdcpfirst) ! (ri - rb>>RX.rifirst) +
		fcFirst
rd>>RD.dcp = (rb + rb>>RX.brgdcp) ! (ri - rb>>RX.rifirst)
if rl>>RL.krl eq krlw then rd>>RD.dfcmax = 
		(rb + rb>>RX.brgdfcmax) ! (ri - rb>>RX.rifirst)

resultis rd
]


and MapRi(rlf,ri,lvfn,lvfc,lvdfc) be
[

let trd = vec lrd

let trb = RbFromRlRi(rlf,ri)
trd = RdFromRlRbRi(trd,rlf,trb,ri)
@lvfc = trd>>RD.fc
@lvdfc = trd>>RD.dfc
@lvfn = rlf>>RL.fn

]


and SetRegionA(rld,ri,imac,rgrid,fappend,i,dcp; numargs na) be
[

let trb = nil
let tcp,tdcp = nil,nil
let rd = vec lrd

if na eq 4 then fappend = false
i = 0
dcp = 0
trb = RbFromRlRi(rld,ri)

RdFromRlRbRi(rd,rld,trb,ri)
tcp = fappend ? rd>>RD.cp+rd>>RD.dcp, rd>>RD.cp
tdcp = fappend ? 0, rd>>RD.dcp
replacea1(rld>>RL.doc,tcp,tdcp,imac,DcpSetRegionA,lv rld,
		SetRegionA1,lv rld)

tdcp = fappend ? rd>>RD.dcp+dcp, dcp
SetRegionSize(rld,trb,ri,tdcp)
invalidatedisplay(rld>>RL.doc,rd>>RD.cp,-1)

]


and DcpSetRegionA(fm,pcd,dpc) = valof
[

let tri = nil
let tnrl = nil
let trl = nil
let trb = nil
let rd = vec lrd

tri =( (fm!pidrgrid)!(fm!pidi) ) <<RID.ri
tnrl =( (fm!pidrgrid)!(fm!pidi) ) << RID.nrl
trl = rgrl ! tnrl

RdFromRlRbRi(rd,trl,RbFromRlRi(trl,tri),tri)

Zero(pcd,lpcd)

pcd>>PCD.rc = (rd>>RD.fc)<<PCD.rc
(lv (pcd>>PCD.vpa) ) >> VPA.fp = (rd>>RD.fc)<<PCD.p
(lv (pcd>>PCD.vpa) ) >> VPA.fn = trl>>RL.fn
pcd>>PCD.visibuf = false
pcd>>PCD.fop = fopNil
pcd>>PCD.bifr = -1

fm!pidi = fm!pidi + 1
fm!piddcp = fm!piddcp + rd>>RD.dfc

resultis rd>>RD.dfc

]


and SetRegionA1(doc,pc,bpcd,fm) be
[
if fm!pidi ne 0 & fm!pidi ne ( (fm!pidimac)-1) do ParaBitsUpdate(doc,pc)
]



and SetRegionB(rld,ri,doc,cp,dcp) be
[

let trb = nil
let tbp = nil
let rd = vec lrd

trb = RbFromRlRi(rld,ri)
// tbp = vbp

// lockbp(tbp)

RdFromRlRbRi(rd,rld,trb,ri)

replacea(rld>>RL.doc,rd>>RD.cp,rd>>RD.dcp,doc,cp,dcp)

SetRegionSize(rld,trb,ri,dcp)
invalidatedisplay(rld>>RL.doc,rd>>RD.cp,-1)
// unlockbp(tbp,true)

]



and SetRegionSize(rl,rb,ri,dcp) be
[

let dcpupdate = nil
let tdri = nil
let tbi = nil
let drioffset = nil

if ri ge rl>>RL.rimac then errhlta(71)

drioffset = ri - rb>>RX.rifirst
dcpupdate = dcp - ( (rb + rb>>RX.brgdcp) ! drioffset)
(rb + rb>>RX.brgdcp) ! drioffset = dcp

for tdri = drioffset+1 to rb>>RX.dri-1 do
	(rb+rb>>RX.brgdcpfirst)!tdri =
 		(rb+rb>>RX.brgdcpfirst)!tdri+dcpupdate

// for tbi = rb>>RX.bi+1 to rl>>RL.bimac-1 do
// 	(rl>>RL.mpbicpfirst)!tbi = (rl>>RL.mpbicpfirst)!tbi + dcpupdate

]


and VpaGetOne(rl,ri) = valof
[
resultis rl>>RL.mpbivpa ! 0
]


and VpaGetMany(rl,ri) = valof
[
resultis (rl>>RL.mpbivpa ! (binsearcha(rl>>RL.mpbirifirst,rl>>RL.bimac,ri) ) )
]


and SetRegionSys(ri,rid0,rid1,rid2,rid3,rid4,rid5,rid6; numargs cw) be
[
SetRegionA(vrldsys,ri,cw-1,lv rid0)
]