// newedit.sr

// Last modified October 25, 1979  6:54 PM by Taft

get "BRAVO1.DF";
get "CHAR.DF";
get "MSG.DF";
get "HEAP.DF";
get "doc.DF";
get "display.DF";
get "vm.DF";

// Incoming Procedures

external [
	hpalloca;
	move;
	hpfree;
	ult;
	binsearcha;
	ugt;
	invalidatedisplay;
	enww;
	stcopy;
	getvch;
	mapcp;
	stget;
	stsize;
	putvch;
	backdylines;
	mult;
	fcparabounds;
	mapfc;
	errhlt;
	umin;
	umax;
	mitomsg;
	cpfc;
	nocharwaiting;
	bravochar;
	estscrrun;
	findhpspace;
	mpDlDld
	];

// Incoming Statics

external [
	rgpctb;
	vpc;
	rgmaccp;
	vcp;
	selmain;
	selarg;
	selaux;
	rgcpfirst;
	vdoc;
	sbpast;
	vchremain;
	vlb;
	vpw;
	vcpfirst;
	vcplast;
	fcp;
	vbifr;
	vfcstatus;
	vfbloc;
	mppccp;
	vmapstatus;
	vfcfirst;
	vfclast;
	ppcd;
	vdlhint;
	vlook1;
	vfn;
	vfc;
	char;
	vcuripar;
	mpfnof;
	vcfreemin;
	vpzone;
	mpWwWwd;
	dcpendofdoc
	look1std
	look2std
	];

// Outgoing Procedures

external [
	inserta;
	pcsubstring;
	updateparabits;
	makeroompc;
	deletea;
	insertb;
	setpcsiz;
	cpadjust;
	PutSbScr;
	insertc;
	unsetdirty;
	insertd;
	];

// Outgoing Statics

external [
	vcpadjusteq;
	pctb1;
	pctb2;
	vdoc1;
	vdoc2;
	deltacp2;
	pcfirst1;
	pcfirst2;
	pclast2;
	cpcnew;
	mppccp1;
	mppccp2;
	rgpcd1;
	rgpcd2;
	pctb;
	rgpcd;
	pcfirst;
	pcfirstdel;
	pclastdel;
	vdeltacp;
	pclast;
	deltacp;
	cpscrt;
	vpcfirst;
	vcpc;
	vfhpsp;
	]; 

// Local Statics

static [
	vcpadjusteq;
	pctb1;
	pctb2;
	vdoc1;
	vdoc2;
	deltacp2;
	pcfirst1;
	pcfirst2;
	pclast2;
	cpcnew;
	mppccp1;
	mppccp2;
	rgpcd1;
	rgpcd2;
	pctb;
	rgpcd;
	pcfirst;
	pcfirstdel;
	pclastdel;
	vdeltacp;
	pclast;
	deltacp;
	cpscrt;
	vpcfirst;
	vcpc;
	vfhpsp;
	];

// U P D A T E P A R A B I T S
//
let updateparabits(doc,pc) be
[ pctb = rgpctb ! doc;
if pc ls 0 % pc ge pctb >> PCTB.macpc then return;
mppccp = lv (pctb >> PCTB.rvmppccp);
let cp = mppccp ! pc;
let pcd = pctb+(pctb >> PCTB.rgpcd)+(pc lshift 2);
pcd >> PCD.parabits = false;
test pc eq 0 ifso
	pcd >> PCD.parastart = true;
ifnot	[ vmapstatus = statusblind;
	mapcp(doc,cp-1);
	if vlook1 << LOOK1.trailer then
		pcd >> PCD.parastart = true;
	] 
if pcd >> PCD.live then
	[ vmapstatus = statusblind;
	mapcp(doc,cp);
	char = vlb ? vpw >> lh,vpw >> rh;
	if char eq ctrlz then
		pcd >> PCD.paraend = true;
	return;
	] 
cpfc(doc,cp);
unless (mpfnof ! vfn) >> OF.formatted then return
let len = mppccp ! (pc+1) -mppccp ! pc;
fcparabounds(vfn,vfc);
ppcd >> PCD.bifr = vbifr;
ppcd >> PCD.paraend = ugt(vfclast,vfc+len-1) ? false,true;
] 


// S E T P C S I Z
// catalogue no. = SPE-102
and setpcsiz(doc,pc,siz) be
[  pctb = rgpctb ! doc;
mppccp = lv (pctb >> PCTB.rvmppccp);
let tcp = mppccp ! (pc+1);
let deltacp = siz-(tcp-mppccp ! pc);
for tpc = pc+1 to (pctb >> PCTB.macpc) do
	mppccp ! tpc = (mppccp ! tpc)+deltacp;
if deltacp ls 0 then
	tcp = tcp+deltacp;
updateparabits(doc,pc);
updateparabits(doc,pc+1);
cpadjust(doc,tcp,deltacp);
] 

// C P A D J U S T 
// catalogue no.
and cpadjust(doc,cp,deltacp) be
[ rgmaccp ! doc = (rgmaccp ! doc)+deltacp;
if (doc eq vcuripar >> IPAR.doc) & (not ugt(vcuripar >> IPAR.cpfirst,cp)) & (not ult(vcuripar >> IPAR.cplast,cp)) then
	vcuripar >> IPAR.doc = -1;
fcp = cp;
vdeltacp = deltacp;
enww(cpadjust1,doc);
cpadjust3(selmain,doc);
cpadjust3(selarg,doc);
cpadjust3(selaux,doc);
// if doc eq docsys then
// 	for i = maxfieldsys-1 to 0 by -1 do
// 		[ if ult(rgcpdocsys ! i,cp) then break;
// 		rgcpdocsys ! i = rgcpdocsys ! i+deltacp;
// 		] 
] 
// C P A D J U S T 1
// catalogue no.
and cpadjust1(ww) be
[ let wwd = mpWwWwd ! ww
wwd>>WWD.fDirty = (rgmaccp!(wwd>>WWD.doc) eq dcpendofdoc) ?
	false, true
let tcplb = nil;
let fdecreasing = vdeltacp ls 0
if fdecreasing then tcplb = fcp-vdeltacp;
for dl = wwd>>WWD.dlLast by -1 to wwd>>WWD.dlFirst do
	[ let dld = mpDlDld(dl)
	if not ult(dld>>DLD.cpLast,fcp) then
		test fdecreasing & ult(dld>>DLD.cpLast,tcplb) ifso
			dld>>DLD.cpLast = fcp
		ifnot	dld>>DLD.cpLast = (dld>>DLD.cpLast)+vdeltacp;
	test ugt(rgcpfirst ! dl,fcp) ifso
		test fdecreasing & ult(rgcpfirst ! dl,tcplb) ifso
			[ dld>>DLD.xdLast = -1;
			rgcpfirst ! dl = fcp;
			] 
		ifnot	rgcpfirst ! dl = (rgcpfirst ! dl)+vdeltacp;

	ifnot	return;
	] 

//	[ test ugt(rgcpfirst ! dl,fcp) ifso
//		[ if fdecreasing & ult(rgcpfirst ! dl,tcplb) then
//			[ rgxdlast ! dl = -1;
//			vdeltacp = fcp-rgcpfirst ! dl;
//			] 
//		rgcpfirst ! dl = (rgcpfirst ! dl)+vdeltacp;
//		rgcplast ! dl = (rgcplast ! dl)+vdeltacp;
//		] 
//	ifnot	[ if not ult(rgcplast ! dl,fcp) then
//			rgcplast ! dl = (rgcplast ! dl)+vdeltacp;
//		if fdecreasing & ult(rgcplast ! dl,fcp) then
//			rgcplast ! dl = fcp;
//		return;
//		] 
//	] 


let doc = wwd >> WWD.doc
unless (rgmaccp ! doc eq 0) % ult(wwd>>WWD.cpFDispl,fcp) then
	[ wwd>>WWD.cpFDispl = wwd>>WWD.cpFDispl+vdeltacp;
	if ult(wwd>>WWD.cpFDispl,fcp) % not ult(wwd>>WWD.cpFDispl,rgmaccp ! doc) then
		[ backdylines(ww,fcp,0);
		wwd>>WWD.cpFDispl = vcp;
		wwd>>WWD.fUpdate = true;
		] 
	] 
] 
// C P A D J U S T 2 
//
and cpadjust2(pcp) be
[ if ugt(rv pcp,fcp+vcpadjusteq) then
	(rv pcp) = (rv pcp)+vdeltacp
] 
// C P A D J U S T 3
//
and cpadjust3(sel,doc) be
[
if sel >>SEL.doc eq doc then
	[
	cpadjust2(lv (sel >> SEL.cpfirst));
	cpadjust2(lv (sel >> SEL.cplast));
	sel >> SEL.xdfirst = -1;
	sel >> SEL.xdlast = -1;
	] 
] 


// I N S E R T C

and insertc(doc, cp, sb, look1, look2, par, ttbl; numargs carg) be
[
if carg ls 4 then look1 = look1std
if carg ls 5 then look2 = look2std
deltacp = 0
let bifr = carg ls 6 ? PutSbScr(sb, look1, look2),
	PutSbScr(sb, look1, look2, par, ttbl)
invalidatedisplay(doc, cp, -1);
let cch = stsize(sb);
insertd(doc, cp, cch, fnscr, cpscrt, bifr);
cpscrt = cpscrt + cch;
]


// U N S E T D I R T Y

and unsetdirty(ww) be
	(mpWwWwd!ww)>>WWD.fDirty = false