// GLF.SR	SAME AS XLF.SR

get "BRAVO.DF";
get "CHAR.DF";
get "GINN.DF" // $$

// Incoming Procedures

external [
	hpalloca;
	stsize;
	errhlt;
	movec;
	stcopy;
	setpcsiz;
	invalidatedisplay;
	insertb;
	finsertk// **
// $$[
	cppara
	paradetails
	compactspec
// $$]
	];

// Incoming Statics

external [
	vpc;
	pctb;
	rgpctb;
	rgcp;
	rgpcd;
	];

// Outgoing Procedures

external [
	insertlf;
	setlf;
	findlfpc;
	];

// Outgoing Statics

external [
	vlf;
	dnlf;
	]; 

// Local Statics

static [
	vlf;
	dnlf;
	];

// I N S E R T L F
// catalogue no. = SPE-101
let insertlf(doc,cp,lf,id,maxsize) be
[ let ppcd = vec 2;
cp = finsertk(doc, cp, 1) // **
vlf = hpalloca(lfl);
vlf >> LF.sl = hpalloca((maxsize rshift 1)+2);
(vlf >> LF.sl) ! 0 = 1;
((vlf >> LF.sl) ! 1) = chsp lshift 8;
vlf >> LF.link = dnlf ! lf;
dnlf ! lf = vlf;
vlf >> LF.dirty = 0;
vlf >> LF.lf = lf;
vlf >> LF.id = id;
vlf >> LF.doc = doc;
vlf >> LF.maxsiz = maxsize;
ppcd >> PCD.live = 1;
ppcd >> PCD.esc = vlf;
ppcd >> PCD.lf = lf;
ppcd >> PCD.id = id;
insertb(doc,cp,ppcd,1);
vlf >> LF.pc = vpc;
] 

// S E T L F 
// catalogue no. = SPE-103
and setlf(lf,id,sb) = valof
[
let plf = dnlf ! lf;
let tcp = nil;
while plf ne 0 do
	[ if plf >> LF.id ne id then 
		[ plf = plf >> LF.link;
		loop;
		] 
	let tpc = plf >> LF.pc;
	let doc = plf >> LF.doc;
	 pctb = rgpctb ! doc;
	 rgcp = lv (pctb >> PCTB.rgcp);
	let ppcd = rgcp+(pctb >> PCTB.maxpc)+1+(tpc lshift 1);
	if (plf >> LF.pc ge pctb >> PCTB.macpc) %
		(plf >> LF.pc ls 0) %
		(not ppcd >> PCD.live) %
		(ppcd >> PCD.esc ne plf) then 
		tpc = findlfpc(doc,plf);
	if plf >> LF.maxsiz ls stsize(sb)-1 do errhlt("so");
	movec(plf >> LF.sl,(plf >> LF.sl)+((plf >> LF.maxsiz) 
		rshift 1)+1,0);
	stcopy(plf >> LF.sl,sb);
	let sizchange = setpcsiz(doc,tpc,stsize(sb)-1); // ** sizchange
// $$[
	tcp = rgcp ! tpc
	invalidatedisplay(doc,tcp,-1);
	if sizchange ls 0 then
		[
		let para = cppara(doc, tcp)
		let spec,siz,looks,changes,rcp,r=nil,nil,nil,nil,nil,nil
		paradetails(doc, para, lv spec, tcp-1)
		compactspec(spec)
		spec >> SPEC.dirty = true
		]
// $$]
	plf = plf >> LF.link;
	tcp = finsertk(doc, tcp, sizchange) // **
	] 
resultis tcp;
] 
// F I N D L F P C
// catalogue no. = SPE-108
and findlfpc(doc,plf) = valof
[  pctb = rgpctb ! doc;
 rgpcd = (lv (pctb >> PCTB.rgcp))+(pctb >> PCTB.maxpc)+1;
for i = 0 by 2 to ((pctb >> PCTB.maxpc) lshift 1) do
	[ if not ((rgpcd+i) >> PCD.live) then loop;
	if (rgpcd+i) >> PCD.esc eq plf then
		[ plf >> LF.pc = i rshift 1;
		resultis i rshift 1;
		] 
	] 
errhlt("le");
]