// GCPDL.SR 	 ** GYPSY changes

get "BRAVO.DF"
get "CHAR.DF"
get "GINN.DF" // **

// Incoming procedures

external
	[
	ult
	ugt
	binsearcha
	invalidatedisplay
	updatedisplay
	mapcp
	move
	format
	mapcpb;
	errhlt
// ** GYPSY ADDED:
	paracp
	cppara
	umin
	vrgcc
	]

// Incoming statics

external
	[
	rgdlfirst
	rgdllast
	rgcpfirst
	rgcplast
	rgdoc
	rgcpfdispl
	vcp
	rgmaccp
	pctb
	rgpctb
	rgcp
	vpc
	vchremain
	vlb
	vpw
	vcplast
	vchremainb
// ** GYPSY ADDED:
	vww
	rgupdate
	rgpara
	vturning
	]

// Outgoing procedures

external
	[
	finddl;
	cpvisible;
	backnlines;
	visible;
	backscan
	forwardscan
// ** GYPSY ADDED:
	cpseeable;
	]

// Outgoing statics

external
	[
	rgcpfirstinl
	cpfirstinl;
	]

// Local statics

static
	[
	rgcpfirstinl
	cpfirstinl;
	]


// F I N D D L
// SPE catalogue no.

let finddl(ww,cp) = valof
[
let dlfirst = rgdlfirst ! ww;
if (rgdllast ! ww ls dlfirst) % ult(cp,rgcpfirst ! dlfirst) % ugt(
cp,rgcplast ! (rgdllast ! ww))
then resultis -1;
resultis dlfirst+binsearcha(lv(rgcpfirst ! dlfirst),
	 ((rgdllast ! ww)-dlfirst),cp);
] // end finddl

// C P V I S I B L E 
// 
and cpvisible(ww,cp) be 
[ let tdoc = rgdoc ! ww;
invalidatedisplay(tdoc,rgcpfdispl ! ww,-1);
backnlines(ww,cp,0);
invalidatedisplay(tdoc,vcp,-1);
rgcpfdispl ! ww = vcp;
vturning = true
updatedisplay( );
] 

 // B A C K N L I N E S
//catalogue no. = 116
and backnlines(ww,cp,n) = valof
[ 
let tcp = 0;
let char = nil;
let tl = nil;
let k = nil;
let doc = rgdoc ! ww;
let chfixpoint = chcr;
n = umin(n,maxl-1);
if cp eq rgmaccp ! doc & cp ne 0 then
	cp = cp-1;
vcp = 0;
unless cp eq 0 do
	[ tcp = cp-1;
	if backscan(doc,tcp,-1,chfixpoint) then
		if n do
			[ vww = ww;
			tcp = vcp;
			if vcp & (chfixpoint eq chcr) then
				vcp = vcp+1;
			format(doc,vcp);
			vcp = tcp;
			unless ult(vcplast,cp) do
				backscan(doc,tcp-1,-1,chfixpoint);
			] 
	] 
if vcp & (chfixpoint eq chcr) then
	vcp = vcp+1;
vww = ww;
let l = 0;
	[ 
	test l ls maxl ifso
		[ rgcpfirstinl ! l = vcp;
		] 
	ifnot	[
		move(rgcpfirstinl+1,rgcpfirstinl,maxl-1)
		rgcpfirstinl ! (maxl-1) = vcp;
		] 
	format(doc,vcp);
	if not ult(vcplast,cp) then
		[ k = umin(l,n);
		tl = umin(maxl-1,l);
		vcp = rgcpfirstinl ! (tl-k);
		resultis k;
		] 
	vcp = vcplast+1;
	l = l+1;
	] repeat 
] 

// B A C K S C A N
//
and backscan(doc,cp,mode,arg1,arg2; numargs N) = valof
[
let tchar = nil;
let twoargs = true;
if N ls 5 then
	[ arg2 = -1;
	twoargs = false;
	] 
pctb = rgpctb ! doc;
rgcp = lv (pctb >> PCTB.rgcp);
vcp = cp;
let foundnonbreakchar = false;
	[ 
	mapcpb(doc,vcp);
		[ test vlb ifso
			[ tchar = vpw >> lh;
			vpw = vpw-1;
			vlb = false;
			] 
		ifnot	[ tchar = vpw >> rh;
			vlb = true;
			] 
		unless foundnonbreakchar % (vrgcc+tchar) >> CC.breakchar do
			foundnonbreakchar = false;
		test mode ifso
			test twoargs ifnot
				if tchar eq arg1 then
					resultis true
			ifso	if (tchar eq arg1) % (tchar eq arg2) then
					resultis true
		ifnot	test ult(vcp,arg1) ifso
			test tchar eq chcr ifso
				resultis false
			ifnot	resultis true
		ifnot	if foundnonbreakchar & (vrgcc+tchar) >> CC.breakchar then
			resultis false;
		if vcp eq 0 then
			resultis false;
		vchremainb = vchremainb-1;
		vcp = vcp-1;
		unless vchremainb then break;
		] repeat 
	] repeat 
]

// F O R W A R D S C A N
//
and forwardscan(doc,cp,mode,arg1,arg2; numargs N) = valof
[
let tchar = nil;
let twoargs = true;
let maccp = rgmaccp ! doc;
if N ls 5 then
	[ arg2 = -1;
	twoargs = false;
	] 
unless mode do errhlt("m0");
pctb = rgpctb ! doc;
rgcp = lv (pctb >> PCTB.rgcp);
vcp = cp;
	[ 
	mapcp(doc,vcp);
	while vchremain do
		[ test vlb ifso
			[ tchar = vpw >> lh;
			vlb = false;
			] 
		ifnot	[ tchar = vpw >> rh;
			vlb = true;
			vpw = vpw+1;
			] 
		vchremain = vchremain-1;
		test mode ifso
			test twoargs ifnot
				if tchar eq arg1 then
					resultis true
			ifso	if (tchar eq arg1) % (tchar eq arg2) then
					resultis true
		ifnot	errhlt("m0");
		vcp = vcp+1;
		if vcp eq maccp then
			resultis false;
		] 
	] repeat 
]

// V I S I B L E
//
and visible(ww,cp) = valof
[ test rgdllast ! ww ls rgdlfirst ! ww % // ** new condition
		ult(cp,rgcpfdispl ! ww) %
		ugt(cp,(rgcplast ! (rgdllast ! ww))) // ** was +1) 
ifso	resultis false
ifnot	resultis true;
] 

// C P S E E A B L E // **

and cpseeable(ww,cp,orcp; numargs N) = valof
	[	// ** marks must be off **
	if N eq 3 & visible(ww, orcp) then resultis false
	test ult(cp,rgcpfdispl ! ww)
	ifso	test ult(cp, rgcpfdispl ! ww - farcp)
		ifso cpvisible(ww, cp)
		ifnot	[
			let n = 2;
			vcp = rgcpfdispl ! ww;
				[  
				n = n-backnlines(ww,vcp,n);
				] repeatuntil (vcp eq 0) % (n eq 0)
			rgcpfdispl ! ww = vcp;
			rgupdate ! ww = true;
			vturning = true
			updatedisplay( );
			resultis true;
			] 
	ifnot if not ult(cp,rgcplast ! (rgdllast ! ww)) then
		test ugt(cp, rgcplast ! (rgdllast ! ww)+farcp)
		ifso cpvisible(ww, cp)
		ifnot	[
			rgcpfdispl ! ww=rgcplast ! (rgdlfirst ! ww)+1;
			rgupdate ! ww = true;
			vturning = true
			updatedisplay( );
			resultis true;
			]
	resultis false
	]