// GEDIT.SR: more cp's to adjust and dirty doc! & getvch bumps vcp

get "BRAVO.DF";
// Incoming Procedures

external [
	hpalloca;
	move;
	hpfree;
	ult;
	binsearcha;
	ugt;
	invalidatedisplay;
	enww;
	stcopy;
	getvch;
	mapcp;
// ** GYPSY ADDED:
	cpadjustlist
	cpc
	];

// Incoming Statics

external [
	rgpctb;
	vpc;
	rgmaccp;
	vcp;
	selmain;
	selarg;
	selaux;
	rgdllast;
	rgdlfirst;
	rgcpfirst;
	rgxlast;
	rgcplast;
	rgcpfdispl;
	rgdoc;
	rgupdate;
	vdoc;
	sbpast;
	vchremain;
	vlb;
	vpw;
	vcpfirst;
	vcplast;
// ** GYPSY ADDED:
	rgdirty
	rgpara
	rgpage
	rgreadonly
	vturning
	];

// Outgoing Procedures

external [
	lengthenpctb;
	inserta;
	pcsubstring;
	makeroompc;
	deletea;
	insertb;
	setpcsiz;
	cpadjust;
	find;
// ** GYPSY ADDED:
	cpadjust2
	];

// Outgoing Statics

external [
	vcpadjusteq;
	pctb1;
	pctb2;
	rgcp1;
	rgcp2;
	vdoc1;
	vdoc2;
	deltacp2;
	pcfirst1;
	pcfirst2;
	pclast2;
	cpcnew;
	rgpcd1;
	rgpcd2;
	pctb;
	rgcp;
	rgpcd;
	pcfirst;
	pcfirstdel;
	pclastdel;
	vdeltacp;
	pclast;
	deltacp;
	cpscrt;
	]; 

// Local Statics

static [
	vcpadjusteq;
	cpadjusteq; // **
	pctb1;
	pctb2;
	rgcp1;
	rgcp2;
	vdoc1;
	vdoc2;
	deltacp2;
	pcfirst1;
	pcfirst2;
	pclast2;
	cpcnew;
	rgpcd1;
	rgpcd2;
	pctb;
	rgcp;
	rgpcd;
	pcfirst;
	pcfirstdel;
	pclastdel;
	vdeltacp;
	pclast;
	deltacp;
	cpscrt;
	];

// L E N G T H E N 
// catalogue no. = SPE-87
let lengthenpctb(doc,maxpcnew) be
[ 
let pctbn = hpalloca(maxpcnew*3+1+pctbl);
let pctbo = rgpctb ! doc;
pctbn >> PCTB.maxpc = maxpcnew;
move(pctbo+1,pctbn+1,2+(pctbo >> PCTB.macpc));
move(pctbo+3+(pctbo >> PCTB.maxpc),pctbn+3+(pctbn >> PCTB.maxpc),(pctbo >> PCTB.macpc) lshift 1);
hpfree(pctbo);
rgpctb ! doc = pctbn;
] 
// I N S E R T A 
// catalogue no. = SPE-93
and inserta(doc1,cpfirst1,doc2,cpfirst2,cplast2) be
[ if ult(cplast2,cpfirst2) then return;
pctb1 = rgpctb ! doc1;
 pctb2 = rgpctb ! doc2;
 rgcp1 = lv (pctb1 >> PCTB.rgcp);
 rgcp2 = lv (pctb2 >> PCTB.rgcp);
vdoc1 = doc1;
vdoc2 = doc2;
deltacp2 = cplast2-cpfirst2+1;
pcfirst1 = binsearcha(rgcp1,(pctb1 >> PCTB.macpc),cpfirst1);
pcfirst2 = binsearcha(rgcp2,(pctb2 >> PCTB.macpc),cpfirst2);
pclast2 = binsearcha(rgcp2,(pctb2 >> PCTB.macpc),cplast2);
cpcnew = pclast2-pcfirst2+1;
let deltapc = cpcnew;
 rgpcd1 = rgcp1+(pctb1 >> PCTB.maxpc)+1;
 rgpcd2 = rgcp2+(pctb2 >> PCTB.maxpc)+1;
test cpfirst1 eq (rgcp1 ! pcfirst1) ifnot
	[ makeroompc(doc1,pcfirst1+1,cpcnew+1,deltacp2);
	rgcp1 ! (pcfirst1+cpcnew+1) =  cpfirst1+deltacp2;
	pcsubstring(rgpcd1+((pcfirst1+cpcnew+1) lshift 1), 
			rgpcd1+(pcfirst1 lshift 1),
			cpfirst1-(rgcp1 ! pcfirst1)
			) 
	pcfirst1 = pcfirst1+1; deltapc = deltapc+1;
	] 
ifso	[ makeroompc(doc1,pcfirst1,cpcnew,deltacp2);
	] 
if doc1 eq doc2 & not ugt(cpfirst1,cpfirst2) then
	[
	cpfirst2 = cpfirst2+deltacp2;
	pcfirst2 = pcfirst2+deltapc;
	pclast2 = pclast2+deltapc;
	];
rgcp1 ! pcfirst1 = cpfirst1;
cpadjust(doc1,cpfirst1,deltacp2);
pcsubstring(rgpcd1+(pcfirst1 lshift 1), rgpcd2+(pcfirst2 lshift 1),
			cpfirst2-(rgcp2 ! pcfirst2)
			);
if pcfirst2 eq pclast2 then return;
for i = 1 to cpcnew-2 do
	[ rgcp1 ! (pcfirst1+i) = cpfirst1+(rgcp2 ! (pcfirst2+i))-cpfirst2;
	pcsubstring(rgpcd1+((pcfirst1+i) lshift 1),
			rgpcd2+((pcfirst2+i) lshift 1),
			0);
	] 
rgcp1 ! (pcfirst1+cpcnew-1) = cpfirst1+(rgcp2 ! pclast2)-cpfirst2;
pcsubstring(rgpcd1+((pcfirst1+cpcnew-1) lshift 1),
			rgpcd2+(pclast2 lshift 1),
			0
			);
] 
// P C S U B S T R I N G
// catalogue no. = SPE-91
and pcsubstring(pcd1,pcd2,pos) be
[ let trc = (pcd2 >> PCD.rc)+pos;
pcd1 >> PCD.rc = trc << PCD.rc;
pcd1 >> PCD.live = 0;
pcd1 >> PCD.vpaddr = (pcd2 >> PCD.vpaddr)+(trc << PCD.p);
] 

// M A K E R O O M P C 
// catalogue no. = SPE-92
and makeroompc(doc,pc,deltapc,deltacp) be
[  pctb = rgpctb ! doc;
if ((pctb >> PCTB.macpc)+deltapc-1) ge (pctb >> PCTB.maxpc) then
	[ lengthenpctb(doc,(pctb >> PCTB.maxpc)+deltapc+deltamaxpc);
	pctb = rgpctb ! doc;
	pctb1 = rgpctb ! vdoc1;
	pctb2 = rgpctb ! vdoc2;
	rgcp1 = lv(pctb1 >> PCTB.rgcp);
	rgcp2 = lv(pctb2 >> PCTB.rgcp);
	rgpcd1 = rgcp1+(pctb1 >> PCTB.maxpc)+1;
	rgpcd2 = rgcp2+(pctb2 >> PCTB.maxpc)+1;
	] 
 rgcp = lv (pctb >> PCTB.rgcp);
 rgpcd = rgcp+(pctb >> PCTB.maxpc)+1;
for i = (((pctb >> PCTB.macpc)-1) lshift 1) by -2 to (pc lshift 1) do 
	[ rgpcd ! (i+1+(deltapc lshift 1)) = rgpcd ! (i+1);
	rgpcd ! (i+(deltapc lshift 1)) = rgpcd ! i;
	] 
for i = (pctb >> PCTB.macpc) by -1 to pc do
	rgcp ! (i+deltapc) = (rgcp ! i)+deltacp;
pctb >> PCTB.macpc = (pctb >> PCTB.macpc)+deltapc;
] 
// D E L E T E A 
and deletea(doc,cpfirst,cplast) be
[ let tpcd = vec 2;
let deltacp = nil;
invalidatedisplay(doc,cpfirst,-1);
 pctb = rgpctb ! doc;
 rgcp = lv (pctb >> PCTB.rgcp);
 rgpcd = rgcp+(pctb >> PCTB.maxpc)+1;
pcfirst = binsearcha(rgcp,(pctb >> PCTB.macpc),cpfirst);
deltacp = cplast-cpfirst+1;
pclast = binsearcha(rgcp,(pctb >> PCTB.macpc),cplast);
pcfirstdel = pcfirst+1;
pclastdel = pclast-1;
test pcfirst eq pclast ifso
	[ test cpfirst eq rgcp ! pcfirst ifso
		[ test (cplast+1) eq rgcp ! (pclast+1) ifso
			[ pclastdel = pcfirst;
			pcfirstdel = pcfirst;
			] 
		ifnot	[ pclastdel = pcfirst-1;
			pcfirstdel = pcfirst;
			pcsubstring(rgpcd+(pcfirst lshift 1),
	rgpcd+(pcfirst lshift 1),cplast+1-(rgcp ! pcfirst));
			] 
		] 
	ifnot	[ test (cplast+1) eq rgcp ! (pclast+1) ifso
			[ pclastdel = pcfirst;
			pcfirstdel = pcfirst+1;
			] 
		ifnot	[ makeroompc(doc,pcfirst+1,1,-deltacp);
			rgcp ! (pcfirst+1) = cpfirst;
			pcsubstring( rgpcd+((pcfirst+1) lshift 1),
	rgpcd+(pcfirst lshift 1),cplast+1-(rgcp ! pclast));
			cpadjust(doc,cpfirst,-deltacp);
			return;
			] 
		] 
	] 
ifnot	[ if cpfirst eq rgcp ! pcfirst then
		pcfirstdel = pcfirst;
	test (cplast+1) eq rgcp ! (pclast+1) ifso
		pclastdel = pclast
	ifnot	pcsubstring(rgpcd+(pclast lshift 1),rgpcd+(pclast lshift 1),cplast+1-(rgcp ! pclast));
	] 
let macpc = pctb >> PCTB.macpc;
let cpcdel = pclastdel-pcfirstdel+1;
rgcp ! pcfirstdel = cpfirst;
for i = pclastdel+2 to (macpc) do
	rgcp ! (i-cpcdel) = (rgcp ! i)-(deltacp);
if (cpcdel ne 0) & (macpc ne pclastdel) then
	move(rgpcd+((pclastdel+1) lshift 1),rgpcd+(pcfirstdel lshift 1),
((macpc-pclastdel-1) lshift 1));
pctb >> PCTB.macpc = macpc-cpcdel;
cpadjust(doc,cpfirst,-deltacp);
] 
// I N S E R T B 
// catalogue no. = SPE-100
and insertb(doc,cp,ppcd,siz) be
[  pctb = rgpctb ! doc;
 rgcp = lv (pctb >> PCTB.rgcp);
 rgpcd = rgcp+(pctb >> PCTB.maxpc)+1;
let macpc = pctb >> PCTB.macpc;
test macpc eq 0 ifso 
	vpc = 0
ifnot	vpc = binsearcha(rgcp,macpc,cp);
test cp eq rgcp ! vpc ifso
	makeroompc(doc,vpc,1,siz)
ifnot	[ makeroompc(doc,vpc+1,2,siz);
	rgcp ! (vpc+2) = cp+siz;
	pcsubstring(rgpcd+((vpc+2) lshift 1),rgpcd+(vpc lshift 1),cp-rgcp ! vpc);
// set vpc to new pc
	vpc = vpc+1;
	] 
rgcp ! vpc = cp
move(ppcd,rgpcd+(vpc lshift 1),2);
cpadjust(doc,cp,siz);
] 
// F I N D 
// 
and find(dock,cpfirstk,cplastk,doc,cpfirst,cplast) = valof
[ let key = vec 30;
let p = vec 30;
let rp = 0;
let char = nil;
if ugt(cpfirst,cplast) then resultis false;
let keyl = cplastk-cpfirstk+1;
if keyl gr 29 then 
	[ stcopy(sbpast," Key too long - ")
	resultis false;
	] 
vcp = cpfirstk;
vdoc = dock;
vchremain = 0;
for i = 1 to keyl do
	key ! i = getvch( ) // **
p ! 1 = 0;
for fp = 2 to keyl do
	[ rp = rp+1;
	p ! fp = (key ! fp ne key ! rp)?rp,p ! rp;
	while (key ! fp ne key ! rp) & (rp ne 0 ) do 
		rp = p ! rp;
	] 
let i = 0;
vcp = cpfirst;
vchremain = 0;
	[ mapcp(doc,vcp);
	while vchremain do
		[ 
		test vlb ifso
			[ char = vpw >> lh
			vlb = false;
			] 
		ifnot	[ char = vpw >> rh;
			vlb = true;
			vpw = vpw+1;
			] 
		vchremain = vchremain-1;
		i = i+1;
		while (char ne key ! i) & (i ne 0) do
			i = p ! i;
		if i eq keyl then
			[ vcpfirst = vcp-keyl+1;
			vcplast = vcp;
			resultis true;
			] 
		if (vcp eq cplast) then
			[ stcopy(sbpast," Not found - ")
			resultis false;
			] 
		vcp = vcp+1;
		] 
	] repeat
] 
// S E T P C S I Z
// catalogue no. = SPE-102
and setpcsiz(doc,pc,siz) = valof // ** valof
[  pctb = rgpctb ! doc;
 rgcp = lv (pctb >> PCTB.rgcp);
let tcp = rgcp ! (pc+1);
let deltacp = siz-(tcp-rgcp ! pc);
for tpc = pc+1 to (pctb >> PCTB.macpc) do
	rgcp ! tpc = (rgcp ! tpc)+deltacp;
if deltacp ls 0 then
	tcp = tcp+deltacp;
cpadjust(doc,tcp,deltacp);
resultis deltacp // **
] 
// C P A D J U S T 
// catalogue no.
and cpadjust(doc,cp,deltacp) be
[
rgdirty ! doc = true  // **
rgmaccp ! doc = (rgmaccp ! doc)+deltacp;
vcp = cp;
vdeltacp = deltacp;
enww(cpadjust1,doc);
cpadjusteq = vcpadjusteq // **
cpadjust3(selmain,doc);
cpadjust3(selaux,doc);
cpadjusteq = 0 // **
cpadjust3(selarg,doc);
cpadjustlist(rgpara ! doc, cpadjust2, cpadjust2) ; // **
cpadjustlist(rgpage ! doc, cpadjust2, cpadjust2) ; // **
cpadjustlist(rgreadonly ! doc, cpadjust2g, cpadjust2) ; // **
] 
// C P A D J U S T 1
// catalogue no.
and cpadjust1(ww) be
[
// ** GYPSY PROCEDURIZED CPADJUSTDISPL SO TURNTO CAN HAVE IT RIGHT
if vturning then cpadjustdispl(ww) // **
for dl = rgdllast ! ww by -1 to rgdlfirst ! ww do
	[ test ugt(rgcpfirst ! dl,vcp) ifso
		[ rgcpfirst ! dl = (rgcpfirst ! dl)+vdeltacp;
		if (vdeltacp ls 0) & ult(rgcpfirst ! dl,vcp) then
			rgxlast ! dl = -1;
		rgcplast ! dl = (rgcplast ! dl)+vdeltacp;
		] 
	ifnot	[ if not ult(rgcplast ! dl,vcp) then
			rgcplast ! dl = (rgcplast ! dl)+vdeltacp;
		return;
		] 
	] 
unless vturning then cpadjustdispl(ww) // **
] 

and cpadjustdispl(ww) be // **
if ugt(rgcpfdispl ! ww,vcp) then
	[ rgcpfdispl ! ww = (rgcpfdispl ! ww)+vdeltacp;
	if ult(rgcpfdispl ! ww,vcp) % not ult(rgcpfdispl ! ww,rgmaccp ! (rgdoc ! ww)) then
		[
		rgcpfdispl ! ww = vcp;
		rgupdate ! ww = true;
		];
	] 

// C P A D J U S T 2 
//
and cpadjust2(pcp) be
[ if ugt(rv pcp,vcp) then // **:
	rv pcp = vdeltacp gr 0 % ugt(rv pcp, vcp-vdeltacp)?
			rv pcp + vdeltacp, vcp
] 
and cpadjust2g(pcp, adj) be // ** new
[ if cpc(rv pcp+adj,vcp) gr 0 then // **:
	rv pcp = vdeltacp gr 0 % cpc(rv pcp, vcp-vdeltacp) gr 0?
			rv pcp + vdeltacp, vcp
] 
// C P A D J U S T 3
//
and cpadjust3(sel,doc) be
[
if sel >>SEL.doc eq doc then
	[
	cpadjustg(lv sel >> SEL.cpfirst, lv sel >> SEL.cplast)
	sel >> SEL.xfirst = -1;
	sel >> SEL.xlast = -1;
	] 
if sel >>SEL.uldocoff eq doc then // **
	cpadjustg(lv sel >> SEL.ulcpfirstoff, lv sel >> SEL.ulcplastoff)
] 

and cpadjustg(pcpfirst, pcplast) be // **
[
let carrot = @pcplast eq @pcpfirst - 1
cpadjust2g(pcpfirst, cpadjusteq);
test carrot
ifso @pcplast = @pcpfirst - 1
ifnot cpadjust2g(pcplast, cpadjusteq);
]