// edit1.sr
// Carol Hankins
// February 23, 1976
// Modifications: April 6, 1976


get "BRAVO1.DF";
get "DOC.DF"
get "CHAR.DF";
get "MSG.DF";
get "NEWMSG.DF";
get "HEAP.DF";

// Incoming procedures
external
	[
	CallersFrame
	MoveBoth
	binsearcha
	ult
	ugt
	errhlta
	move
	umin
	hpalloca
	hpfree
	cpadjust
	updateparabits
	]

// Incoming statics
external
	[
	rgpctb
	vfwheel
	vcompactlock;
	]

// Outgoing procedures
external
	[
	replacea1
	replacea
	cpadjusta
	SetPcMax
	DcpReplaceA
	inserta
	deletea
	insertb
	insertd
	DcpInsertB
	SubPcd
	ParaBitsUpdate
	SetDocPtblMac
	]

//outgoing statics
external
	[
	vpcfirst
	vdocPtblMac
	vlptblMac
	]

static
	[
	vdocPtblMac
	vlptblMac
	]

manifest
	[
	piddoc1 = 0
	pidcp1 = 1
	piddoc2 = 3
	pidcp2 = 4
	piddcp2 = 5
	pidpcfirst2 = 7
	pidpcp2 = 8
	pidpcd2 = 9
	ProcNil = 0
	]


let replacea1(doc,cp,dcp,dpcinsert,DcpProc,fm,Proc1,fm1; numargs cw) be
[

let pctb,mppccp,rgpcd = nil,nil,nil
let pcfirst,pcend = nil,nil
let pcto,pcfrom = nil,nil
let macpc,maxpc,macpcnew = nil,nil,nil
let dpc = nil
let pwto,pwfrom = nil,nil
let tcp = nil
let tcpend = nil

pctb = rgpctb ! doc
mppccp = lv (pctb >> PCTB.rvmppccp)
macpc = pctb >> PCTB.macpc
if ugt(cp,mppccp!macpc) then errhlta(66)
pcfirst = binsearcha(mppccp, pctb >> PCTB.macpc,cp)
pcend = binsearcha(mppccp, pctb >> PCTB.macpc,cp+dcp)

if cp ne mppccp ! pcfirst
	do	[
		pcfirst = pcfirst+1
		let pcd = pctb+pctb>>PCTB.rgpcd+(pcfirst lshift 2)
		pcd>>PCD.fendupd = 1
		]

dpc = pcfirst - pcend + dpcinsert

maxpc = pctb>>PCTB.maxpc
macpcnew = macpc + dpc
if macpcnew gr maxpc then SetPcMax(doc,macpcnew + deltamaxpc)

pctb = rgpctb ! doc
pctb>>PCTB.macpc = macpcnew
mppccp = lv (pctb >> PCTB.rvmppccp)
pcto = pcfirst + dpcinsert
pcfrom = pcend
MoveBoth(lv mppccp ! pcto, lv mppccp ! pcfrom, macpc-pcend+1)

rgpcd = pctb >> PCTB.rgpcd + pctb
pwto = rgpcd + ( (pcfirst+dpcinsert) lshift 2 )
pwfrom = rgpcd + (pcend lshift 2)
MoveBoth(pwto,pwfrom,(macpc-pcend) lshift 2)
pcend = pcend + dpc

maxpc = pctb>>PCTB.maxpc
macpc = pctb>>PCTB.macpc
if maxpc gr macpc + 15 then SetPcMax(doc,macpc+15)
vcompactlock = true;
pctb = rgpctb ! doc
mppccp = lv (pctb >> PCTB.rvmppccp)
macpc = pctb >> PCTB.macpc
rgpcd = pctb >> PCTB.rgpcd + pctb
tcp = cp
let tpcd = lv rgpcd ! (pcfirst lshift 2)
for tpc = pcfirst to pcfirst+dpcinsert-1 do
	[
	mppccp ! tpc = tcp
	tcp = tcp+DcpProc(fm,tpcd,dpc)
	tpcd = tpcd+lpcd
	]


macpc = pctb >>PCTB.macpc
let dcp2=tcp-cp
for tpc = pcend to macpc do
	mppccp ! tpc = mppccp ! tpc + dcp2 - dcp

tcpend = mppccp ! pcend
let tdcp = nil
if tcp ne tcpend do
	[
	if pcend eq macpc % ult(mppccp!(pcend+1),tcp) 
		then errhlta(67)
	tdcp = tcp - tcpend
	mppccp ! pcend = tcp
	SubPcd(tpcd,tdcp)
	]

ParaBitsUpdate(doc,pcfirst-1)
ParaBitsUpdate(doc,pcfirst)
ParaBitsUpdate(doc,pcend-1)
ParaBitsUpdate(doc,pcend)

let bpcd = (rgpcd - pctb) + pcfirst lshift 2
if cw gr 6 & Proc1 ne ProcNil then
	for pc = pcfirst to pcend -1 do
		[
		Proc1(doc,pc,bpcd,fm1)
		bpcd = bpcd + lpcd
		]

vcompactlock = false;
cpadjusta(doc,cp,dcp,dcp2)
vpcfirst = pcfirst
]



and SetPcMax(doc,maxpc) be
[

let pctbnew,pctbold,macpcold = nil,nil,nil
let rgpcdold,rgpcdnew = nil,nil
let lptblNew = maxpc*(lpcd + 1) + pctbovhd;

if (rgpctb ! vdocPtblMac) eq ptblNil then
	SetDocPtblMac()
if ugt(lptblNew, vlptblMac) then
	[ vdocPtblMac = doc;
	vlptblMac = lptblNew;
	];

pctbnew = hpalloca(lptblNew)
pctbold = rgpctb ! doc
macpcold = pctbold>>PCTB.macpc
move(pctbold,pctbnew,pctbovhd + macpcold)

pctbnew>>PCTB.maxpc = maxpc
pctbnew>>PCTB.macpc = macpcold

pctbnew>>PCTB.rgpcd = (offset PCTB.rvmppccp)/16 + maxpc + 1
rgpcdold = pctbold >> PCTB.rgpcd + pctbold
rgpcdnew = pctbnew >> PCTB.rgpcd + pctbnew 
move(rgpcdold,rgpcdnew,macpcold lshift 2)

hpfree(pctbold)
rgpctb ! doc = pctbnew 

]



and replacea(doc1,cp1,dcp1,doc2,cp2,dcp2,Proc,pcfirst2,pcp2,pcd2;
		numargs cw) be
[

let pctb,mppccp = nil,nil
let pclast2 = nil
let dpc = nil
let tfm = CallersFrame() + 4
let tproc = valof
		[
		test cw gr 6 ifso resultis Proc
			ifnot resultis ProcNil
		]

pcp2=-1
pcd2=0

test dcp2 ne 0
	ifso
		[
		pctb = rgpctb ! doc2
		mppccp = lv (pctb >> PCTB.rvmppccp)
		pcfirst2 = binsearcha(mppccp,pctb >> PCTB.macpc,
					cp2)
		pclast2 = binsearcha(mppccp,pctb >> PCTB.macpc,
					cp2+dcp2-1)
		dpc=pclast2-pcfirst2+1
		]
	ifnot dpc=0

replacea1(doc1,cp1,dcp1,dpc,DcpReplaceA,lv doc1,tproc,tfm)

]


and DcpReplaceA(fm,pcd,dpc) = valof
[
let pctb,mppccp,rgpcd = nil,nil,nil
let tcpmax,tcpmin=nil,nil

if fm!pidpcp2 eq pnil then
	[
	if fm!piddoc1 eq fm!piddoc2  &  (not ult(fm!pidcp2,fm!pidcp1))
		 then fm!pidpcfirst2=fm!pidpcfirst2+dpc
	pctb = rgpctb  ! (fm!piddoc2)
	mppccp = lv (pctb >> PCTB.rvmppccp)
	fm!pidpcp2 = lv (mppccp ! (fm!pidpcfirst2) )
	rgpcd = pctb >> PCTB.rgpcd + pctb
	fm!pidpcd2 = rgpcd + (fm!pidpcfirst2) lshift 2
	]

move(fm!pidpcd2,pcd,lpcd)

test ult(rv (fm!pidpcp2),fm!pidcp2) 
	 ifso 	 [ 
		SubPcd(pcd,fm!pidcp2-(rv (fm!pidpcp2) ) )
		tcpmax = fm!pidcp2
		]
	ifnot	tcpmax = rv (fm!pidpcp2)

fm!pidpcp2 = fm!pidpcp2 + 1
fm!pidpcd2 = fm!pidpcd2 + lpcd

test ult(rv (fm!pidpcp2), fm!pidcp2+fm!piddcp2)
	ifso	tcpmin = rv (fm!pidpcp2)
	ifnot	[
		pcd>>PCD.fendupd = 1
		tcpmin = fm!pidcp2 + fm!piddcp2
		]

resultis (tcpmin - tcpmax)
]


and inserta(doc1,cpfirst1,doc2,cpfirst2,cplast2) be
 	replacea(doc1,cpfirst1,0,doc2,cpfirst2,cplast2-cpfirst2+1)


and deletea(doc,cpfirst,cplast) be
 	replacea(doc,cpfirst,cplast-cpfirst+1,0,0,0)


and insertb(doc,cp,pcd,dcp) = valof
[ replacea1(doc,cp,0,1,DcpInsertB,lv doc)
resultis vpcfirst
] 

and insertd(doc,cp,siz,fn,fc,bifr) = valof
[ let tpcd = vec 4;
tpcd >> PCD.live = false;
tpcd >> PCD.fop = fopNil;
tpcd >> PCD.parabits = true;
tpcd >> PCD.visibuf = false;
tpcd >> PCD.rc = fc << PCD.rc;
let vpa = nil;
vpa << VPA.fn = fn;
vpa << VPA.fp = fc << PCD.p;
tpcd >> PCD.vpa = vpa;
tpcd >> PCD.bifr = bifr;
resultis insertb(doc,cp,tpcd,siz);
] 

and DcpInsertB(fm,pcd,dpc) = valof
[
let pidpcd,piddcp = fm!2,fm!3

move(pidpcd, pcd,lpcd)
resultis (piddcp)
]


and SubPcd(pcd,dcp) be
[
let tfc = nil
let tpcdp = nil
let tpcdrc = nil

tfc = pcd >> PCD.rc + dcp
tpcdrc = tfc << PCD.rc
tpcdp = tfc << PCD.p

pcd >> PCD.rc = tpcdrc
pcd >> PCD.vpa = pcd >> PCD.vpa + tpcdp
pcd >> PCD.fstartupd = true
]


and cpadjusta(doc,cp,dcp1,dcp2) be
	cpadjust(doc,cp,dcp2-dcp1)


and ParaBitsUpdate(doc,pc) be
[

let pctb = rgpctb ! doc
let macpc = pctb>>PCTB.macpc

if (pc ls 0) % (pc ge macpc) then return
let rgpcd = pctb + pctb>>PCTB.rgpcd
let pcd = rgpcd + (pc lshift 2)

if true % pcd>>PCD.fstartupd % pcd>>PCD.fendupd then
	[
	pcd>>PCD.fstartupd = 0
	pcd>>PCD.fendupd = 0
	updateparabits(doc,pc)
	]

]

and SetDocPtblMac() be
[ vlptblMac = 0;
for tdoc = 0 to maxdoc-1 do
	[ let tptbl = rgpctb ! tdoc;
	if tptbl eq ptblNil then loop;
	let tlptblMac = (tptbl-((offset HP.use)/16))>>HP.siz
	if ugt(tlptblMac, vlptblMac) then
		[ vdocPtblMac = tdoc;
		vlptblMac = tlptblMac;
		]
	];
]