// typing1.sr	User typein

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


get "BRAVO1.DF"
get "format.df"
get "CHAR.DF"
get "MSG.DF"
get "DISPLAY.DF"
get "select.DF"
get "vm.DF"
get "doc.DF"
get "rn1.df"
get "com.df"


// Incoming procedures

external	[
	select;
	selectwholeww
	bravochar;
	umin;
	ClearBuf;
	RbFromRlRi;
	RdFromRlRbRi;
	inserta;
	setsel;
	InsertK;
	replacea;
	InsertBuf;
	invalidatedisplay
	updatedisplay;
	setpcsiz;
	updateparabits;
	move;
	lockbp;
	errhlta
	getfb;
	ult;
	unlockbp;
	getbin;
	BufToRi
	SetVab
	]


// Incoming statics

external	[
	selarg;
	vesccom;
	vchterm;
	rgmaccp;
	vrldsys;
	vselcaret;
	mpWwWwd;
	mpDlDld;
	deltacp;
	cpscrt;
	mpfnof;
	vpfb;
	parstd;
	vfb;
	vbp;
	mpfrfc;
	vmacfr;
	rglook1;
	rglook2;
	rgmpbifb;
	rgmpbifc;
	dcpendofdoc
	look1std
	look2std
	]


// Outgoing procedures

external	[
	inserttx;
	endinsertk;
	estscrrun;
	getnextfbscr;
	fSwt;
	]


// Outgoing statics

external	[
	tstoflush
	tsstop;
	ccommand;
	tsbreak;
	tsmacro;
	fnts;
	vpiscrlast;
	vfbloclastpar;
	vbiofpi;
	vfcofopenpar;
	]


// Local statics

static	[
	tstoflush
	tsstop;
	ccommand;
	tsbreak;
	tsmacro;
	fnts;
	vpiscrlast;
	vfbloclastpar;
	vbiofpi;
	vfcofopenpar;
	]

 

// 
// I N S E R T T X 
//
let inserttx(txno, wwDocName, ch; numargs na) = valof
[
if na ls 2 then wwDocName = wwsys
let doctx = doctx0 + txno
selarg>>SEL.type = snone;
let tri, trb = nil, nil
let trd = vec 3			//contains cp, dcp
let tchar = chesc;
let dcp = nil;
let tpc = nil;
let tcp = nil;
if na ls 3 then
	test vesccom ifso
		ch = chesc
	ifnot	[
		test na ls 2 ifso select(selarg, 0);
		ifnot	[
			let tsel= vec sell
			selectwholeww(tsel,wwDocName)
			select(selarg,tsel)
			]
		ch = bravochar();
		vchterm = ch;
		]
let docSel = selarg>>SEL.doc
let cpMac = rgmaccp ! docSel - dcpendofdoc
let cpFirstSel = umin(selarg>>SEL.cpfirst, cpMac)
selarg>>SEL.cpfirst = cpFirstSel
let cpLastSel = umin(selarg>>SEL.cplast+1, cpMac) - 1
selarg>>SEL.cplast = cpLastSel

if docSel eq docsys & selarg>>SEL.type ne snone then
 	resultis false;
if (ch eq ctrlc) % (ch eq chdel) then 
	resultis false;
if (ch eq chesc) & (selarg>>SEL.type eq snone) then
	resultis true;

let docName = (mpWwWwd ! wwDocName)>>WWD.doc
test wwDocName eq wwsys ifso
	[
	ClearBuf(txno)
	tri = BufToRi(txno)
	trb = RbFromRlRi(vrldsys, tri)
	trd = RdFromRlRbRi(trd, vrldsys, trb, tri)
	tcp = trd>>RD.cp
	]
ifnot	[
	replacea(docName, 0, rgmaccp ! docName-dcpendofdoc, 0, 0, 0)
	tcp = 0
	]
test ch eq chesc ifnot
	[
	vselcaret>>SEL.doc = docName;
	vselcaret>>SEL.ww = wwDocName;
	setsel(vselcaret, tcp, tcp);
	vselcaret>>SEL.type = scaret;
	vselcaret>>SEL.toggle = 0;
	dcp = InsertK(wwDocName, tcp, look1std, look2std, parstd, 0, ch);
	]
ifso	[
	invalidatedisplay(docName,0,-1)
	inserta(docName, tcp, docSel, cpFirstSel, cpLastSel)
	dcp = cpLastSel - cpFirstSel + 1;
	]
if wwDocName eq wwsys then
	[
	replacea(doctx, 0, rgmaccp ! doctx, docsys, tcp, dcp)
	replacea(docsys, tcp, dcp, 0, 0, 0)
	InsertBuf(txno, doctx, 0, dcp)
	let wwdSys = mpWwWwd ! wwsys
	if wwdSys>>WWD.cpFDispl then
		[
		wwdSys>>WWD.cpFDispl = 0;
		(mpDlDld(0))>>DLD.xdLast = -1;
		wwdSys>>WWD.fUpdate = true;
		]
	]
updatedisplay();
if dcp eq 0 then resultis false;
resultis vchterm ne chdel
]

// E N D I N S E R T 
//
and endinsertk(doc, cp, pcinsertk) be
[ unless deltacp eq 0 then
	[ cpscrt = cpscrt+deltacp
	setpcsiz(doc, pcinsertk, deltacp);
	updateparabits(doc, pcinsertk);
	]
]


// E S T S C R R U N
//
and estscrrun(len, look1, look2, par;numargs n) = valof
[ let macbi = (mpfnof ! fnscr)>>OF.macbi;
let tppar1 = nil
let tppar2 = nil
test macbi eq 0 ifso
	[ getnextfbscr(0, -1)
	tppar1 = vpfb+#400-parovhd;
	move(parstd, tppar1, parovhd);
	tppar1>>PAR.par = 0;
	tppar1>>PAR.nextpar = -1;
	tppar1>>PAR.fcofpar = 0;
	tppar1>>PAR.fcofnextpar = -1;
	vfbloclastpar<<FBLOC.fb = vfb;
	vfbloclastpar<<FBLOC.bw = tppar1-vpfb;
	(lv (vpfb>>FB.rvdnpi)) ! vpiscrlast = vfbloclastpar;
	vfcofopenpar = 0;
	]
ifnot	establishbi(fnscr, macbi-1);
let bp1 = vbp;
lockbp(bp1);
let bp2 = bp1;
let bpofprevpar = bp1;
let tfc = mpfrfc ! vmacfr+len;
if look1<<LOOK1.trailer then
	[ unless (n eq 4) & (par ne 0) then
		errhlta(230);
	tppar1 = getfb(vfbloclastpar<<FBLOC.fb)+vfbloclastpar<<FBLOC.bw
	bpofprevpar = vbp;
	lockbp(bpofprevpar);
	move(par, tppar1, cwinfopar);
	tppar1>>PAR.fcofnextpar = tfc;
	tppar1>>PAR.trailerlength = len;
	]
macbi = (mpfnof ! fnscr)>>OF.macbi;
if (vmacfr+1 ge maxfrscr) % (macbi-1 ne vbiofpi) then
	[ unless (vbiofpi eq macbi) % (vbiofpi eq macbi-1) then
		errhlta(231);
	getnextfbscr(mpfrfc ! vmacfr, vfbloclastpar);
	bp2 = vbp;
	]
lockbp(bp2);
look1<<LOOK1.pi = vpiscrlast;
rglook1 ! vmacfr = look1;
rglook2 ! vmacfr = look2;
let bifr = nil;
macbi = (mpfnof ! fnscr)>>OF.macbi;
bifr<<BIFR.bi = macbi-1;
bifr<<BIFR.fr = vmacfr;
vmacfr = vmacfr+1;
vpfb>>FB.macfr = vmacfr;
mpfrfc ! vmacfr = tfc;
if look1<<LOOK1.trailer then
	[ tppar2 = (vfbloclastpar<<FBLOC.fb eq vfb) ? 
		tppar1-parovhd, vpfb+#400-parovhd;
	if vpiscrlast ge maxpi % ult(tppar2, rglook2+maxfrscr) then
		[ getnextfbscr(mpfrfc ! vmacfr, vfbloclastpar);
		bp2 = vbp;
		tppar2 = vpfb+#400-parovhd;
		]
	vpiscrlast = vpiscrlast+1;
	let tbifr = nil;
	tbifr<<BIFR.bi = (mpfnof ! fnscr)>>OF.macbi-1;
	tbifr<<BIFR.fr = vmacfr;
	tppar1>>PAR.nextpar = tbifr;
	tppar2>>PAR.par = tbifr;
	tppar2>>PAR.fcofpar = tfc;
	tppar2>>PAR.fcofnextpar = -1;
	tppar2>>PAR.nextpar = -1;
	tppar2>>PAR.label = 0;
	vfbloclastpar<<FBLOC.fb = vfb;
	vfbloclastpar<<FBLOC.bw = tppar2-vpfb;
	vfcofopenpar = tfc;
	(lv (vpfb>>FB.rvdnpi)) ! vpiscrlast = vfbloclastpar;
	]
unlockbp(bp1, true);
unlockbp(bp2, true);
unlockbp(bpofprevpar, true);
resultis bifr;
]

// G E T N E X T F B S C R
//
and getnextfbscr(fcfirst, dnpisub0) be
[ establishbi(fnscr, (mpfnof ! fnscr)>>OF.macbi, maxfrscr, fcfirst);
mpfrfc  ! 0 = fcfirst;
vpiscrlast = 0;
vbiofpi =  (mpfnof ! fnscr)>>OF.macbi-1;
(lv (vpfb>>FB.rvdnpi)) ! vpiscrlast = dnpisub0;
]

// E S T A B L I S H B I
//
and establishbi(fn, bi, maxfr, fcfirst;numargs n) be
[ test bi eq (mpfnof ! fn)>>OF.macbi ifso
	[ unless (n eq 4) & (fn eq fnscr) then errhlta(232);
	if bi+1 ge maxbiscr then errhlta(233);
	if ((rgmpbifb ! fn) ! bi) eq -1 then
		[ let macpos = (mpfnof ! fnscrfs)>>OF.macpos;
		if (macpos rshift 9) ge #170 then
			SetVab(abmsg,mtyAnc,227)
		(mpfnof ! fnscrfs)>>OF.macpos = macpos+#1000;
		(rgmpbifb ! fn) ! bi = macpos<<PCD.p;
		]
	(rgmpbifc ! fn) ! bi = fcfirst;
	(rgmpbifc ! fn) ! (bi+1) = -1;
	(mpfnof ! fn)>>OF.macbi = bi+1
	vpfb = getbin(fn, bi);
	vpfb>>FB.macfr = 0;
	vpfb>>FB.mpfrfc = ((offset FB.rvdnpi)/16)+maxpi+1;
	vpfb>>FB.rglook1 = vpfb>>FB.mpfrfc+maxfrscr+2;
	vpfb>>FB.rglook2 = vpfb>>FB.rglook1+maxfrscr+2;
	(vpfb+(vpfb>>FB.rglook1)) ! (-1) = 0;
	]
ifnot	if n eq 3 then errhlta(234);
vpfb = getbin(fn, bi);
vmacfr = vpfb>>FB.macfr;
mpfrfc = vpfb+vpfb>>FB.mpfrfc;
rglook1 = vpfb+vpfb>>FB.rglook1;
rglook2 = vpfb+vpfb>>FB.rglook2;
vfb = (rgmpbifb ! fn) ! bi;
]

and fSwt(fInWordLast, fInWord, fInWordNext) = valof
[ test fInWord ifso
	resultis (fInWordLast eq false)
ifnot	resultis fInWordNext
]