// typing2.sr	User typein


get "BRAVO1.DF"
get "CHAR.DF"
get "MSG.DF"
get "VM.DF"
get "DOC.DF"


// Incoming procedures

external	[
	endofkeystream
	errhlta
	getbin
	mapcp
	ugt
	stsize
	estscrrun
	putvch
	stget
	SiPut
	]


// Incoming statics

external	[
	tsread
	putbacks
	mpfnof
	rgbs
	vbp
	vmapstatus
	cfr
	cpc
	mpfrfc
	vfr
	vfc
	vfcfirst
	vchremain
	vchfrremain
	cpscrt
	deltacp
	]


// Outgoing procedures

external	[
	nocharwaiting;
	decrmacfrscr;
	counttrans;
	PutSbScr;
	]


// Outgoing statics

// external


// Local statics

// static


// N O C H A R W A I T I N G
//
let nocharwaiting() = valof
[
test (not tsread & endofkeystream() & not putbacks) ifso
	resultis true
ifnot	resultis false
] 


// D E C R M A C F R S C R
//
and decrmacfrscr(cfr,newmacfc; numargs n) be
[ if n eq 1 then newmacfc = -1;
let lastbi = (mpfnof ! fnscr) >> OF.macbi-1;
if lastbi eq -1 then
	test cfr eq 0 ifso
		return
	ifnot	errhlta(196);
let pfb = getbin(fnscr,lastbi);
(rgbs ! vbp) << BS.dirty = true
let tmacfr = pfb >> FB.macfr-cfr;
test tmacfr le 0 ifnot
	[ pfb >> FB.macfr = tmacfr;
	if newmacfc ne -1 then
		(pfb+pfb >> FB.mpfrfc) ! tmacfr = newmacfc;
	] 
ifso	[ pfb >> FB.macfr = 0;
	(mpfnof ! fnscr) >> OF.macbi = lastbi;
	decrmacfrscr(-tmacfr,newmacfc);
	] 
] 


// C O U N T T R A N S
//
and counttrans(doc,cpfirst,cplast) be
[ vmapstatus = statusblind;
cfr = 0;
cpc = 0;
let tcp = cpfirst;
	[ mapcp(doc,tcp);
	if cpfirst eq tcp then
		[ if mpfrfc ! vfr eq vfc then
			cfr = cfr+1;
		vfcfirst = vfc;
		] 
	tcp = tcp+vchremain;
	if ugt(tcp,cplast) then
		break;
	test vmapstatus eq endofpiece ifso
		cpc = cpc+1
	ifnot	if vchremain eq vchfrremain then
		cfr = cfr+1
	] repeat
unless cpc eq 0 then errhlta(197)
] 


// P U T   S B   S C R
//
and PutSbScr(sb, look1, look2, par, ttbl; numargs carg) = valof
[
if carg ls 4 then par = 0;
if par ne 0 then
	unless par>>PAR.fOldtab do
		par>>PAR.siTtbl = SiPut(siNil, ttbl)
let len = stsize(sb);
let bifr = estscrrun(len, look1, look2, par);
(mpfnof ! fnscr) >> OF.pos = cpscrt+deltacp;
for i = 0 to len-1 do
	[
	putvch(stget(sb, i));
	deltacp = deltacp+1;
	]
resultis bifr;
]